75 lines
4.7 KiB
TypeScript
75 lines
4.7 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { compileMixedActionExpression, type MixedActionExpression } from '../game/mixed-action-question';
|
|
import {
|
|
TurnActionError, closeTurnActionBattleChoice, finishTurnActionTurn, parseTurnActionLeaf,
|
|
recordTurnActionBattleChoice, turnActionBranches, turnActionValue, validateTurnActionPromises,
|
|
type TurnActionLeaf, type TurnActionPromise,
|
|
} from '../game/turn-action-promises';
|
|
|
|
const source = 'battleOrder:[2,null,0]';
|
|
const leaf = (territory = 'carthag', target = 'guild'): TurnActionLeaf =>
|
|
({kind:'battleChoice',source,territory,target});
|
|
const action = (value: TurnActionLeaf): MixedActionExpression<TurnActionLeaf> => ({kind:'action',action:value});
|
|
function promise(mixed: MixedActionExpression<TurnActionLeaf>, answer = true): TurnActionPromise {
|
|
return {turn:2,event:'answer-1',player:'atreides',asker:'emperor',answer,mixed,
|
|
compiled:compileMixedActionExpression(mixed,() => 'yes',answer),
|
|
shipment:{pending:false,event:null},progress:[],payments:[],transportClosed:true,paymentClosed:true,
|
|
battleChoice:{source,closed:false,selected:null},fulfilled:false,released:false};
|
|
}
|
|
const selected = {territory:'carthag',target:'guild'};
|
|
const seats = ['atreides','emperor','guild','fremen'];
|
|
|
|
void test('battle choice accepts only an exact canonical source and original public fields', () => {
|
|
assert.deepEqual(parseTurnActionLeaf(leaf()),leaf());
|
|
for (const value of [
|
|
{...leaf(), source:'other'}, {...leaf(), source:'battleOrder:[2,null,-1]'},
|
|
{...leaf(), source:'battleOrder:[ 2,null,0]'}, {...leaf(), source:'battleOrder:[0,null,0]'},
|
|
{...leaf(), target:''}, {...leaf(), territory:''}, {...leaf(), extra:true},
|
|
]) assert.throws(() => parseTurnActionLeaf(value),TurnActionError);
|
|
const p = promise(action(leaf())); validateTurnActionPromises([p],seats);
|
|
assert.throws(() => validateTurnActionPromises([{...p,battleChoice:{...p.battleChoice!,source:'battleOrder:[3,null,0]'}}],seats));
|
|
assert.throws(() => validateTurnActionPromises([{...p,battleChoice:{...p.battleChoice!,selected}}],seats));
|
|
});
|
|
|
|
void test('an accepted native choice settles the exact Yes; No rejects the same selection atomically', () => {
|
|
const yes = promise(action(leaf())), before = structuredClone(yes);
|
|
const [recorded] = recordTurnActionBattleChoice([yes],'atreides',2,source,selected);
|
|
assert.deepEqual(yes,before);
|
|
assert.equal(recorded.fulfilled,true); assert.equal(turnActionValue(recorded),true);
|
|
assert.deepEqual(recorded.battleChoice?.selected,selected);
|
|
validateTurnActionPromises([recorded],seats);
|
|
assert.equal(recordTurnActionBattleChoice([recorded],'atreides',2,source,selected)[0],recorded);
|
|
const no = promise(action(leaf()),false), snapshot = structuredClone(no);
|
|
assert.throws(() => recordTurnActionBattleChoice([no],'atreides',2,source,selected),TurnActionError);
|
|
assert.deepEqual(no,snapshot);
|
|
const [different] = recordTurnActionBattleChoice([no],'atreides',2,source,{territory:'arrakeen',target:'fremen'});
|
|
assert.equal(different.fulfilled,true); assert.equal(turnActionValue(different),false);
|
|
validateTurnActionPromises([different],seats);
|
|
});
|
|
|
|
void test('one source can satisfy OR alternatives, but never two different positive choices', () => {
|
|
const first = action(leaf()), second = action(leaf('arrakeen','fremen'));
|
|
const and = promise({kind:'and',terms:[first,second]});
|
|
const or = promise({kind:'or',terms:[first,second]});
|
|
assert.deepEqual([...turnActionBranches(and)],[]);
|
|
assert.equal([...turnActionBranches(or)].length,2);
|
|
const [chosen] = recordTurnActionBattleChoice([or],'atreides',2,source,selected);
|
|
assert.equal(chosen.fulfilled,true); validateTurnActionPromises([chosen],seats);
|
|
const noOr = promise({kind:'or',terms:[first,second]},false);
|
|
assert.equal([...turnActionBranches(noOr)][0].length,2);
|
|
assert.throws(() => recordTurnActionBattleChoice([noOr],'atreides',2,source,selected));
|
|
});
|
|
|
|
void test('closing the original source records no fabricated battle and turn expiry releases an unfulfilled Yes', () => {
|
|
const yes = promise(action(leaf()));
|
|
const [closed] = closeTurnActionBattleChoice([yes],'atreides',2,source);
|
|
assert.equal(closed.battleChoice?.closed,true); assert.equal(closed.battleChoice?.selected,null);
|
|
assert.equal(closed.fulfilled,false);
|
|
const [expired] = finishTurnActionTurn([closed],'atreides',2);
|
|
assert.equal(expired.released,true); validateTurnActionPromises([expired],seats);
|
|
const [notChosen] = closeTurnActionBattleChoice([promise(action(leaf()),false)],'atreides',2,source);
|
|
assert.equal(notChosen.fulfilled,true); validateTurnActionPromises([notChosen],seats);
|
|
assert.throws(() => recordTurnActionBattleChoice([yes],'atreides',3,source,selected));
|
|
});
|