Commit 93140e61 authored by Clement Brizard's avatar Clement Brizard
Browse files

fix: compute hardware damages

parent 40763b77
......@@ -247,7 +247,7 @@ class Hardware extends Component {
and add it to composite hardware damage */
Object.values(this.components).forEach(component => {
component.computeDamage({ meetingDuration, bound })
damage.add(component.damage)
damage = damage.add(component.damage)
})
} else {
const operatingVisio = this.computeTypedDamage(hardwareDamageTypes.OPERATING_VISIO, meetingDuration, bound)
......@@ -368,7 +368,7 @@ class Hardware extends Component {
}
const weightEmbodied = Object.assign({}, this.embodied)
Object.keys(weightEmbodied).map(category => {
Object.keys(weightEmbodied).forEach(category => {
weightEmbodied[category] *= boundSpecificWeight
})
......
......@@ -307,6 +307,8 @@ describe('Hardware class', () => {
)
Object.values(expected).forEach(categoryDamageValue => {
assert.isNotNaN(categoryDamageValue)
assert.isNotNull(categoryDamageValue)
assert.notEqual(0, categoryDamageValue)
})
})
})
......@@ -429,10 +431,11 @@ describe('Hardware class', () => {
)
}).forEach(json => {
const component = new Hardware({ name: json.name })
const damage = component.computeTypedDamage(damageType, meetingDuration)
// Compute expected damage
const expected = new Damage({ component })
assert.deepStrictEqual(expected, component.computeTypedDamage(damageType, meetingDuration))
Object.keys(damage).forEach(category => {
assert.strictEqual(damage[category], 0)
})
})
})
})
......@@ -468,7 +471,13 @@ describe('Hardware class', () => {
})
}
assert.deepStrictEqual(expected, component.computeTypedDamage(damageType, meetingDuration))
const damage = component.computeTypedDamage(damageType, meetingDuration)
Object.keys(damage).forEach(category => {
assert.strictEqual(damage[category], expected[category])
assert.isNotNaN(damage[category])
assert.isNotNull(damage[category])
assert.notEqual(0, damage[category])
})
})
})
})
......@@ -504,34 +513,16 @@ describe('Hardware class', () => {
})
}
assert.deepStrictEqual(expected, component.computeTypedDamage(damageType, meetingDuration))
const damage = component.computeTypedDamage(damageType, meetingDuration)
Object.keys(damage).forEach(category => {
assert.strictEqual(damage[category], expected[category])
assert.isNotNaN(damage[category])
assert.isNotNull(damage[category])
assert.notEqual(0, damage[category])
})
})
})
})
it('should compute assimilated embodied damages', () => {
Object.values(hardwareDatabase)
.filter(json => json.embodiedAssimilatedTo)
.forEach(json => {
const instance = new Hardware({ name: json.name })
const weight = (json.weight.upper && json.weight.lower)
? json.weight.upper
: json.weight
const expected = hardwareDatabase[json.embodiedAssimilatedTo].embodied
Object.keys(expected).map(category => {
expected[category] *= weight
})
assert.deepStrictEqual(
instance.getTypedDamage(hardwareDamageTypes.EMBODIED_VISIO),
expected
)
Object.values(expected).forEach(categoryDamageValue => {
assert.isNotNaN(categoryDamageValue)
})
})
})
it('should compute the damage of a composite hardware', () => {
Object.values(hardwareDamageTypes).forEach(damageType => {
Object.values(hardwareDatabase)
......@@ -589,6 +580,9 @@ describe('Hardware class', () => {
Object.keys(double).forEach(category => {
assert.strictEqual(double[category], simple[category] * 2)
assert.isNotNaN(double[category])
assert.isNotNull(double[category])
assert.notEqual(0, double[category])
})
})
it('the lower bound damage should be lower than the default (upper) damage', () => {
......@@ -605,25 +599,26 @@ describe('Hardware class', () => {
it('should compute the damage of a composite hardware with x instances of the same component (x > 1)', () => {
const json = hardwareDatabase.LOGITECH_KIT
const instance = new Hardware({ name: json.name })
// Compute expected damage
const expected = new Damage()
let expected = new Damage()
for (const [name, quantity] of Object.entries(json.components)) {
const component = new Hardware({ name, quantity: 1 })
component.computeDamage(meetingDuration)
const damage = component.damage
component.computeDamage({ meetingDuration })
component.damage.mutate(category => {
component.damage[category] *= quantity
})
expected.add(damage.mutate(category => {
return damage[category] * quantity
}))
expected = expected.add(component.damage)
}
instance.computeDamage(meetingDuration)
instance.computeDamage({ meetingDuration })
const actual = instance.damage
assert.deepStrictEqual(actual, expected)
Object.values(actual).forEach(categoryDamageValue => {
assert.isNotNaN(categoryDamageValue)
assert.isNotNull(categoryDamageValue)
assert.notEqual(0, categoryDamageValue)
})
})
it('should compute the damage of each composite hardware', () => {
......@@ -636,19 +631,21 @@ describe('Hardware class', () => {
const instance = new Hardware({ name: json.name })
// Compute expected damage
const expected = new Damage()
let expected = new Damage()
for (const [name, quantity] of Object.entries(json.components)) {
const component = new Hardware({ name, quantity })
component.computeDamage(meetingDuration)
expected.add(component.damage)
component.computeDamage({ meetingDuration })
expected = expected.add(component.damage)
}
instance.computeDamage(meetingDuration)
instance.computeDamage({ meetingDuration })
const actual = instance.damage
assert.deepStrictEqual(actual, expected)
Object.values(actual).forEach(categoryDamageValue => {
assert.isNotNaN(categoryDamageValue)
assert.isNotNull(categoryDamageValue)
assert.notEqual(0, categoryDamageValue)
})
})
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment