78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
'use strict';
|
|||
|
|
|
||
|
|
// rerollAllInitiatives: bulk d20+initMod for every participant.
|
||
|
|
// Single commit, single log, undo restores old values + order.
|
||
|
|
|
||
|
|
const {
|
||
|
|
makeParticipant,
|
||
|
|
startEncounter,
|
||
|
|
rerollAllInitiatives,
|
||
|
|
} = require('@ttrpg/shared');
|
||
|
|
const { mockCtx, undoLast } = require('./_helpers');
|
||
|
|
|
||
|
|
function part(id, init, initMod, extra = {}) {
|
||
|
|
return makeParticipant({
|
||
|
|
id, name: id, type: initMod !== undefined ? 'monster' : 'character',
|
||
|
|
initiative: init, initMod, maxHp: 20, currentHp: 20, ...extra,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function enc(participants, extra = {}) {
|
||
|
|
return {
|
||
|
|
id: 'e1', name: 'reroll-all-test', participants,
|
||
|
|
isStarted: false, isPaused: false, round: 0,
|
||
|
|
currentTurnParticipantId: null, turnOrderIds: [],
|
||
|
|
...extra,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const find = (e, id) => e.participants.find(p => p.id === id);
|
||
|
|
|
||
|
|
describe('rerollAllInitiatives', () => {
|
||
|
|
test('rolls d20+initMod for every participant; single write', async () => {
|
||
|
|
const { ctx, storage } = mockCtx();
|
||
|
|
const e = enc([part('a', 10, 5), part('b', 8, 2), part('c', 15, 0)]);
|
||
|
|
|
||
|
|
const out = await rerollAllInitiatives(e, ctx);
|
||
|
|
|
||
|
|
for (const id of ['a', 'b', 'c']) {
|
||
|
|
const p = find(out, id);
|
||
|
|
const mod = id === 'a' ? 5 : id === 'b' ? 2 : 0;
|
||
|
|
expect(p.initiative).toBeGreaterThanOrEqual(mod + 1);
|
||
|
|
expect(p.initiative).toBeLessThanOrEqual(mod + 20);
|
||
|
|
}
|
||
|
|
// one write for the whole operation
|
||
|
|
expect(storage.calls.filter(c => c.fn === 'updateDoc' && c.path === 'encounters/e1')).toHaveLength(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('undo restores exact old initiatives and order', async () => {
|
||
|
|
const { ctx } = mockCtx();
|
||
|
|
const e = enc([part('a', 10, 5), part('b', 8, 2)]);
|
||
|
|
const orderBefore = [...e.turnOrderIds];
|
||
|
|
|
||
|
|
const out = await rerollAllInitiatives(e, ctx);
|
||
|
|
expect(find(out, 'a').initiative).not.toBe(10);
|
||
|
|
|
||
|
|
const undone = await undoLast(ctx, out);
|
||
|
|
expect(find(undone, 'a').initiative).toBe(10);
|
||
|
|
expect(find(undone, 'b').initiative).toBe(8);
|
||
|
|
expect(undone.turnOrderIds).toEqual(orderBefore);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('inactive participants rerolled too (they stay inactive)', async () => {
|
||
|
|
const { ctx } = mockCtx();
|
||
|
|
const e = enc([part('a', 10, 1), part('b', 9, 1, { isActive: false })]);
|
||
|
|
|
||
|
|
const out = await rerollAllInitiatives(e, ctx);
|
||
|
|
expect(find(out, 'b').isActive).toBe(false);
|
||
|
|
expect(find(out, 'b').initiative).toBeGreaterThanOrEqual(2);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('throws during active combat (not paused)', async () => {
|
||
|
|
const { ctx } = mockCtx();
|
||
|
|
let e = enc([part('a', 10, 1)]);
|
||
|
|
e = await startEncounter(e, ctx);
|
||
|
|
|
||
|
|
await expect(rerollAllInitiatives({ ...e, isPaused: false }, ctx)).rejects.toThrow(/paused/i);
|
||
|
|
});
|
||
|
|
});
|