96 lines
5.4 KiB
TypeScript
96 lines
5.4 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { viewGame, type Game } from '../game/engine';
|
|
import { actTruthTravel as act, createTruthTravelFixture as fixture, settleTruthTravel as settle } from './fixture-truth-travel';
|
|
|
|
/** Native public Basic setup and phase-five shipment; only unopened component
|
|
* order is selected by the shared fixture. All terms and actions are real. */
|
|
function accepted() {
|
|
const f = fixture(), dueTurn = f.game.turn+1;
|
|
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Ship, move and ship next turn.',
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
|
|
additionalTravel:[
|
|
{actor:f.target,kind:'move',territory:f.movementTerritory,minimum:1,answer:true},
|
|
{actor:f.target,kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true,dueTurn},
|
|
]});
|
|
const id = game.deals!.at(-1)!.id;
|
|
game = act(game,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
return {f,game,id};
|
|
}
|
|
function linked(game: Game, id: string) {
|
|
return (game.travelPromises ?? []).filter(row => row.sourceDeal === id);
|
|
}
|
|
|
|
void test('primary and indexed additional terms project independent pending, scheduled, fulfilled and released statuses', () => {
|
|
const {f,id,game:initial} = accepted();
|
|
let game = initial;
|
|
assert.equal(viewGame(game,f.target).dealTravelStatus[id],'pending');
|
|
assert.deepEqual(viewGame(game,f.target).dealAdditionalTravelStatus[id],['pending','scheduled']);
|
|
game = settle(act(game,f.target,f.shipmentAction));
|
|
assert.equal(viewGame(game,f.asker).dealTravelStatus[id],'fulfilled');
|
|
assert.deepEqual(viewGame(game,f.asker).dealAdditionalTravelStatus[id],['pending','scheduled']);
|
|
game = settle(act(game,f.target,f.firstMove));
|
|
assert.deepEqual(viewGame(game,f.target).dealAdditionalTravelStatus[id],['fulfilled','scheduled']);
|
|
game = act(game,f.asker,{type:'dealRelease',deal:id,agree:true});
|
|
game = act(game,f.target,{type:'dealRelease',deal:id,agree:true});
|
|
assert.equal(viewGame(game,f.target).dealTravelStatus[id],'fulfilled');
|
|
assert.deepEqual(viewGame(game,f.target).dealAdditionalTravelStatus[id],['fulfilled','released']);
|
|
const rows = linked(game,id);
|
|
assert.equal(rows[0].fulfilled,true);
|
|
assert.equal(rows[1].fulfilled,true);
|
|
assert.equal(rows[2].releasedByDeal,true);
|
|
});
|
|
|
|
void test('missing, duplicated, misindexed and mismatched deal travel receipts are rejected without mutation', () => {
|
|
const {f,id,game} = accepted();
|
|
const variants: Array<(draft: Game) => void> = [
|
|
draft => { draft.travelPromises = draft.travelPromises!.filter(row => !(row.sourceDeal === id && row.sourceDealIndex === 0)); },
|
|
draft => { const row = linked(draft,id)[1]; draft.travelPromises!.push(structuredClone(row)); },
|
|
draft => { linked(draft,id)[1].sourceDealIndex = 1; },
|
|
draft => { linked(draft,id)[1].sourceDealIndex = 12; },
|
|
draft => { linked(draft,id)[1].player = f.asker; },
|
|
draft => { linked(draft,id)[1].turn += 1; },
|
|
draft => { linked(draft,id)[1].answer = false; },
|
|
];
|
|
for (const corrupt of variants) {
|
|
const draft = structuredClone(game); corrupt(draft);
|
|
const before = structuredClone(draft);
|
|
assert.throws(() => viewGame(draft,f.target));
|
|
assert.deepEqual(draft,before,'Rejecting a damaged source receipt does not mutate the game.');
|
|
}
|
|
});
|
|
|
|
void test('legacy immediate-settlement signatures stay identical when there are no additional travel terms', () => {
|
|
const f = fixture();
|
|
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Two immediate payments.',
|
|
payment:{kind:'immediateBribe',payer:f.target,recipient:f.asker,amount:1},
|
|
additionalImmediatePayments:[{kind:'immediateBribe',payer:f.target,recipient:f.asker,amount:1}]});
|
|
const id = game.deals!.at(-1)!.id;
|
|
game = act(game,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
const deal = game.deals!.find(row => row.id === id)!;
|
|
assert.equal(deal.additionalTravel,undefined);
|
|
const expected = JSON.stringify([deal.id,deal.turn,deal.from,deal.to,deal.participants,deal.terms,
|
|
deal.payment,deal.additionalImmediatePayments,deal.additionalPayments?.map(term => term.payment),deal.travel]);
|
|
const receipt = game.publicDealSettlementHistory?.find(row => row.deal === id);
|
|
assert.equal(receipt?.signature,expected);
|
|
const restored = JSON.parse(JSON.stringify(game)) as Game;
|
|
assert.doesNotThrow(() => viewGame(restored,f.target));
|
|
});
|
|
|
|
void test('additional travel is included in new immediate-settlement signatures', () => {
|
|
const f = fixture();
|
|
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Pay and travel twice.',
|
|
payment:{kind:'immediateBribe',payer:f.target,recipient:f.asker,amount:1},
|
|
additionalImmediatePayments:[{kind:'immediateBribe',payer:f.target,recipient:f.asker,amount:1}],
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
|
|
additionalTravel:[{actor:f.target,kind:'move',territory:f.movementTerritory,minimum:1,answer:true}]});
|
|
const id = game.deals!.at(-1)!.id;
|
|
game = act(game,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
assert.ok(game.publicDealSettlementHistory?.some(row => row.deal === id));
|
|
const changed = structuredClone(game), term = changed.deals!.find(row => row.id === id)!.additionalTravel![0];
|
|
term.minimum += 1;
|
|
const before = structuredClone(changed);
|
|
assert.throws(() => viewGame(changed,f.target));
|
|
assert.deepEqual(changed,before);
|
|
});
|