From b1e7ac1161f76556ed833f8e7c41b3a4cb703804 Mon Sep 17 00:00:00 2001 From: ThaumRystra Date: Mon, 2 Sep 2024 22:10:19 +0200 Subject: [PATCH] Added tests for applying adjustments --- .../applyAdjustmentProperty.test.ts | 164 ++++++++++++++++++ .../applyAdjustmentProperty.ts | 3 +- .../action/tasks/applyDamagePropTask.ts | 7 +- .../buildCreatureComputation.test.js | 2 +- 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.test.ts diff --git a/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.test.ts new file mode 100644 index 00000000..ab60949a --- /dev/null +++ b/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.test.ts @@ -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 }, + }, + ], + }]); + }); +}); diff --git a/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.ts b/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.ts index 057147f2..80e97f15 100644 --- a/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.ts +++ b/app/imports/api/engine/action/applyProperties/applyAdjustmentProperty.ts @@ -48,8 +48,7 @@ export default async function applyAdjustmentProperty( }, damageTargetIds); return; } - - applyTask(action, { + await applyTask(action, { prop, targetIds: damageTargetIds, subtaskFn: 'damageProp', diff --git a/app/imports/api/engine/action/tasks/applyDamagePropTask.ts b/app/imports/api/engine/action/tasks/applyDamagePropTask.ts index e7dada77..6423b97c 100644 --- a/app/imports/api/engine/action/tasks/applyDamagePropTask.ts +++ b/app/imports/api/engine/action/tasks/applyDamagePropTask.ts @@ -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); diff --git a/app/imports/api/engine/computation/buildCreatureComputation.test.js b/app/imports/api/engine/computation/buildCreatureComputation.test.js index 2a1485ef..08d18107 100644 --- a/app/imports/api/engine/computation/buildCreatureComputation.test.js +++ b/app/imports/api/engine/computation/buildCreatureComputation.test.js @@ -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 () {