Files
ttrpg-initiative-tracker/shared/tests/turn.effects.test.js
T
david raistrick 295c7e06b4 feat: add round-based tracked effects
- add generic encounter effects with round countdown, damage expiry, logs, and undo/redo
- add 5e Turned and Spiritual Weapon presets inside existing Conditions panel
- support custom participant or encounter effects with owner, notes, duration, and player visibility
- convert 5e minute durations to rounds while keeping generic rules round-only
- show prominent or compact effects on player and DM participant cards
- attach owner-linked encounter effects to owner card with global fallback for ownerless effects
- add direct turns-left editing plus step controls
2026-10-20 18:09:56 -04:00

116 lines
4.4 KiB
JavaScript

const shared = require('@ttrpg/shared');
const { mockCtx, readyEnc, undoLast } = require('./_helpers');
const {
makeEffect, addEffect, updateEffect, removeEffect,
nextTurn, applyHpChange, updateParticipant,
} = shared;
function effect(overrides = {}) {
return makeEffect({
name: 'Turned', presetId: 'turned', scope: 'participant',
targetParticipantId: 'p2', remainingRounds: 10,
expiresOnDamage: true, playerDisplay: 'prominent',
...overrides,
});
}
function runningEnc(overrides = {}) {
return {
...readyEnc(),
isStarted: true,
round: 2,
currentTurnParticipantId: 'p1',
turnOrderIds: ['p1', 'p2'],
effects: [],
...overrides,
};
}
describe('tracked effects', () => {
test('makeEffect normalizes generic effect fields', () => {
const e = effect({ id: 'fx1', remainingRounds: 3.9 });
expect(e).toEqual(expect.objectContaining({
id: 'fx1', name: 'Turned', presetId: 'turned', scope: 'participant',
targetParticipantId: 'p2', remainingRounds: 3,
expiresOnDamage: true, playerDisplay: 'prominent',
}));
});
test('add, update, remove effect are persisted and undoable', async () => {
const { ctx } = mockCtx();
let enc = readyEnc({ participants: readyEnc().participants });
enc.effects = [];
const fx = effect({ id: 'fx1' });
enc = await addEffect(enc, fx, ctx);
expect(enc.effects).toEqual([fx]);
enc = await updateEffect(enc, 'fx1', { remainingRounds: 6 }, ctx);
expect(enc.effects[0].remainingRounds).toBe(6);
const afterRemove = await removeEffect(enc, 'fx1', ctx);
expect(afterRemove.effects).toEqual([]);
const undone = await undoLast(ctx, afterRemove);
expect(undone.effects[0]).toEqual(expect.objectContaining({ id: 'fx1', remainingRounds: 6 }));
});
test('round wrap decrements timed effects and removes expired effects', async () => {
const { ctx } = mockCtx();
const enc = runningEnc({
currentTurnParticipantId: 'p2',
effects: [effect({ id: 'keep', remainingRounds: 2 }), effect({ id: 'expire', remainingRounds: 1 })],
});
const out = await nextTurn(enc, ctx);
expect(out.round).toBe(3);
expect(out.effects).toHaveLength(1);
expect(out.effects[0]).toEqual(expect.objectContaining({ id: 'keep', remainingRounds: 1 }));
const undone = await undoLast(ctx, out);
expect(undone.round).toBe(2);
expect(undone.effects.map(e => [e.id, e.remainingRounds])).toEqual([['keep', 2], ['expire', 1]]);
});
test('same-round turn advance does not decrement effects', async () => {
const { ctx } = mockCtx();
const enc = runningEnc({ effects: [effect({ id: 'fx', remainingRounds: 10 })] });
const out = await nextTurn(enc, ctx);
expect(out.round).toBe(2);
expect(out.effects[0].remainingRounds).toBe(10);
});
test('damage expires targeted effect even when temp HP absorbs all damage; undo restores it', async () => {
const { ctx } = mockCtx();
const participants = readyEnc().participants.map(p => p.id === 'p2' ? { ...p, tempHp: 5 } : p);
const enc = runningEnc({ participants, effects: [effect({ id: 'fx' })] });
const out = await applyHpChange(enc, 'p2', 'damage', 2, ctx);
expect(out.participants.find(p => p.id === 'p2').currentHp).toBe(10);
expect(out.effects).toEqual([]);
const undone = await undoLast(ctx, out);
expect(undone.effects).toHaveLength(1);
expect(undone.effects[0].id).toBe('fx');
});
test('direct HP reduction expires targeted effect; increase does not', async () => {
const { ctx } = mockCtx();
let enc = runningEnc({ effects: [effect({ id: 'fx' })] });
enc = await updateParticipant(enc, 'p2', { currentHp: 8 }, ctx);
expect(enc.effects).toEqual([]);
enc = { ...enc, effects: [effect({ id: 'fx2' })] };
enc = await updateParticipant(enc, 'p2', { currentHp: 9 }, ctx);
expect(enc.effects).toHaveLength(1);
});
test('damage leaves encounter-wide and non-expiring effects active', async () => {
const { ctx } = mockCtx();
const enc = runningEnc({ effects: [
effect({ id: 'weapon', presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter', targetParticipantId: null, expiresOnDamage: false }),
effect({ id: 'bless', name: 'Bless', expiresOnDamage: false }),
] });
const out = await applyHpChange(enc, 'p2', 'damage', 1, ctx);
expect(out.effects.map(e => e.id)).toEqual(['weapon', 'bless']);
});
});