Added tests for applying adjustments

This commit is contained in:
ThaumRystra
2024-09-02 22:10:19 +02:00
parent f4d9806f6f
commit b1e7ac1161
4 changed files with 170 additions and 6 deletions

View File

@@ -0,0 +1,164 @@
import { assert } from 'chai';
import {
allMutations,
createTestCreature,
getRandomIds,
removeAllCreaturesAndProps,
runActionById
} from '/imports/api/engine/action/functions/actionEngineTest.testFn';
const [
creatureId, targetCreatureId, targetCreature2Id, adjustmentToTargetId, adjustmentToSelfId, targetCreatureStrengthId, targetCreature2StrengthId, selfDexterityId
] = getRandomIds(100);
const actionTestCreature = {
_id: creatureId,
props: [
{
_id: adjustmentToTargetId,
type: 'adjustment',
target: 'target',
stat: 'strength',
operation: 'increment',
amount: { calculation: '2' }
},
{
_id: adjustmentToSelfId,
type: 'adjustment',
target: 'self',
stat: 'dexterity',
operation: 'set',
amount: { calculation: '11' }
},
{
_id: selfDexterityId,
type: 'attribute',
name: 'Dexterity',
attributeType: 'ability',
variableName: 'dexterity',
baseValue: { calculation: '13' },
},
],
}
const actionTargetCreature = {
_id: targetCreatureId,
props: [
{
_id: targetCreatureStrengthId,
type: 'attribute',
attributeType: 'ability',
variableName: 'strength',
baseValue: { calculation: '12' },
}
]
}
const actionTargetCreature2 = {
_id: targetCreature2Id,
props: [
{
_id: targetCreature2StrengthId,
type: 'attribute',
attributeType: 'ability',
variableName: 'strength',
baseValue: { calculation: '18' },
}
]
}
describe('Apply Adjustment Properties', function () {
// Increase timeout
this.timeout(8000);
before(async function () {
await removeAllCreaturesAndProps();
await createTestCreature(actionTestCreature);
await createTestCreature(actionTargetCreature);
await createTestCreature(actionTargetCreature2);
});
it('Adjusts the attributes of self', async function () {
const action = await runActionById(adjustmentToSelfId);
assert.exists(action);
assert.deepEqual(allMutations(action), [{
contents: [
{
inline: true,
name: 'Attribute damage',
value: 'Dexterity set to 11',
}
],
targetIds: [creatureId],
updates: [
{
propId: selfDexterityId,
type: 'attribute',
set: { damage: 2, value: 11 },
},
],
}]);
});
it('Adjusts the attributes of a single target', async function () {
const action = await runActionById(adjustmentToTargetId, [targetCreatureId]);
assert.exists(action);
assert.deepEqual(allMutations(action), [{
contents: [
{
inline: true,
name: 'Attribute damaged',
value: '2 Attribute',
}
],
targetIds: [targetCreatureId],
updates: [
{
propId: targetCreatureStrengthId,
type: 'attribute',
inc: { damage: 2, value: -2 },
},
],
}]);
});
it('Adjusts the attributes of multiple targets', async function () {
const action = await runActionById(adjustmentToTargetId, [
targetCreatureId, targetCreature2Id
]);
assert.exists(action);
assert.deepEqual(allMutations(action), [{
contents: [
{
inline: true,
name: 'Attribute damaged',
value: '2 Attribute',
}
],
targetIds: [targetCreatureId],
updates: [
{
propId: targetCreatureStrengthId,
type: 'attribute',
inc: { damage: 2, value: -2 },
},
],
}, {
contents: [
{
inline: true,
name: 'Attribute damaged',
value: '2 Attribute',
}
],
targetIds: [targetCreature2Id],
updates: [
{
propId: targetCreature2StrengthId,
type: 'attribute',
inc: { damage: 2, value: -2 },
},
],
}]);
});
});

View File

@@ -48,8 +48,7 @@ export default async function applyAdjustmentProperty(
}, damageTargetIds);
return;
}
applyTask(action, {
await applyTask(action, {
prop,
targetIds: damageTargetIds,
subtaskFn: 'damageProp',

View File

@@ -63,7 +63,8 @@ export default async function applyDamagePropTask(
} else {
value = scope['~set']?.value;
}
const targetPropId = scope['~attributeDamaged']?._propId;
const targetPropId = scope['~attributeDamaged']?._propId ??
scope['~attributeDamaged']?._id;
// If there are no targets, just log the result that would apply and end
if (!task.targetIds?.length) {
@@ -108,7 +109,7 @@ export default async function applyDamagePropTask(
...prop.silent && { silenced: true },
}]
});
setScope(result, targetProp, newValue, damage);
if (targetId === action.creatureId) setScope(result, targetProp, newValue, damage);
} else if (operation === 'increment') {
const currentValue = targetProp.value || 0;
const currentDamage = targetProp.damage || 0;
@@ -134,7 +135,7 @@ export default async function applyDamagePropTask(
...prop.silent && { silenced: true },
}]
});
setScope(result, targetProp, newValue, damage);
if (targetId === action.creatureId) setScope(result, targetProp, newValue, damage);
}
await applyTriggers(action, targetProp, [targetId], 'damageTriggerIds.after', userInput);
await applyTriggers(action, targetProp, [targetId], 'damageTriggerIds.afterChildren', userInput);

View File

@@ -7,7 +7,7 @@ import computeSlotQuantityFilled from './buildComputation/tests/computeSlotQuant
import computeToggleDependencies from './buildComputation/tests/computeToggleDependencies.testFn';
import linkCalculationDependencies from './buildComputation/tests/linkCalculationDependencies.testFn';
import linkInventory from './buildComputation/tests/linkInventory.testFn';
import linkTypeDependencies from './buildComputation/tests/linkTypeDependencies.testFn';
import linkTypeDependencies from '/imports/api/engine/computation/buildComputation/tests/linkTypeDependencies.testfn';
describe('buildComputation', function () {
it('Builds something at all', function () {