80 lines
3.9 KiB
TypeScript
80 lines
3.9 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import {test} from 'node:test';
|
|
import {
|
|
initialSaphoGreatMakerOrder, reorderSaphoGreatMaker, saphoGreatMakerEvent,
|
|
SaphoGreatMakerError, validateSaphoGreatMaker, type SaphoGreatMakerContext,
|
|
} from '../game/sapho-great-maker';
|
|
|
|
function source(cursor = 0): SaphoGreatMakerContext {
|
|
return {encounter:'great-maker-1',turn:2,stormOrder:['a','b','c','d'],
|
|
queue:['a','b','c','d'],cursor};
|
|
}
|
|
const use = (context: SaphoGreatMakerContext, player: string, mode: 'first' | 'last') =>
|
|
reorderSaphoGreatMaker(context, {event:saphoGreatMakerEvent(context),player,mode});
|
|
|
|
void test('legacy votes retain physical order without any added history', () => {
|
|
const context = source(2), before = structuredClone(context);
|
|
validateSaphoGreatMaker(context);
|
|
assert.equal(context.order, undefined);
|
|
assert.deepEqual(context, before);
|
|
assert.deepEqual(initialSaphoGreatMakerOrder(context.stormOrder),
|
|
{version:1,original:['a','b','c','d'],uses:[]});
|
|
assert.throws(() => validateSaphoGreatMaker({...context,queue:['a','c','b','d']}), SaphoGreatMakerError);
|
|
});
|
|
|
|
void test('first and last change only unanswered voters, preserving their completed prefix', () => {
|
|
const first = source(1), firstBefore = structuredClone(first);
|
|
const changed = use(first, 'd', 'first');
|
|
assert.deepEqual(changed.queue, ['a','d','b','c']);
|
|
assert.deepEqual(changed.order.original, first.stormOrder);
|
|
assert.equal(changed.order.uses[0].cursor, 1);
|
|
assert.deepEqual(first, firstBefore);
|
|
const later = {...first, queue:changed.queue, order:changed.order, cursor:2};
|
|
validateSaphoGreatMaker(later);
|
|
const last = use(later, 'b', 'last');
|
|
assert.deepEqual(last.queue, ['a','d','c','b']);
|
|
assert.deepEqual(last.queue.slice(0,2), ['a','d']);
|
|
validateSaphoGreatMaker({...later,queue:last.queue,order:last.order});
|
|
});
|
|
|
|
void test('recovered Sapho can reorder again with a new event and retained earlier use', () => {
|
|
const initial = source(), first = use(initial, 'd', 'first');
|
|
const replay = {...initial,queue:first.queue,order:first.order};
|
|
assert.notEqual(saphoGreatMakerEvent(replay), saphoGreatMakerEvent(initial));
|
|
const second = use(replay, 'd', 'last');
|
|
assert.deepEqual(second.queue, ['a','b','c','d']);
|
|
assert.equal(second.order.uses.length, 2);
|
|
validateSaphoGreatMaker({...replay,queue:second.queue,order:second.order});
|
|
});
|
|
|
|
void test('stale events, completed voters, no-op choices and malformed scopes reject unchanged', () => {
|
|
const context = source(1), before = structuredClone(context), event = saphoGreatMakerEvent(context);
|
|
for (const request of [
|
|
{event:'stale',player:'d',mode:'first'},
|
|
{event,player:'a',mode:'last'},
|
|
{event,player:'outsider',mode:'first'},
|
|
{event,player:'b',mode:'first'},
|
|
{event,player:'d',mode:'last'},
|
|
] as const) assert.throws(() => reorderSaphoGreatMaker(context,request), SaphoGreatMakerError);
|
|
assert.deepEqual(context, before);
|
|
assert.throws(() => validateSaphoGreatMaker({...context,cursor:5}), SaphoGreatMakerError);
|
|
assert.throws(() => validateSaphoGreatMaker({...context,stormOrder:['a','a','c','d']}), SaphoGreatMakerError);
|
|
assert.throws(() => initialSaphoGreatMakerOrder(['a']), SaphoGreatMakerError);
|
|
});
|
|
|
|
void test('history binds original encounter, revision, cursor and actual resulting queue', () => {
|
|
const initial = source(), first = use(initial,'d','first');
|
|
const next = {...initial,queue:first.queue,order:first.order,cursor:1};
|
|
for (const mutate of [
|
|
(c: SaphoGreatMakerContext) => {c.encounter='another';},
|
|
(c: SaphoGreatMakerContext) => {c.turn++;},
|
|
(c: SaphoGreatMakerContext) => {c.queue=['d','b','a','c'];},
|
|
(c: SaphoGreatMakerContext) => {c.order!.uses[0].cursor=1;},
|
|
(c: SaphoGreatMakerContext) => {c.order!.uses[0].player='a';},
|
|
(c: SaphoGreatMakerContext) => {c.order!.original.reverse();},
|
|
]) {
|
|
const broken = structuredClone(next); mutate(broken);
|
|
assert.throws(() => validateSaphoGreatMaker(broken), SaphoGreatMakerError);
|
|
}
|
|
});
|