154 lines
8.8 KiB
TypeScript
154 lines
8.8 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createGame, joinGame, newPlayer, type Action, type Game } from '../game/engine';
|
|
import { actTruthTravel as act, createTruthTravelFixture as fixture, settleTruthTravel as settle,
|
|
truthTravelClean as clean, truthTravelInventory as inventory, truthTravelPlayer as player } from './fixture-truth-travel';
|
|
import { nextStrongholdFactionsNativeStep as next } from './fixture-stronghold-factions';
|
|
|
|
function reject(game: Game, actor: string, action: Action) {
|
|
const before = structuredClone(game);
|
|
assert.throws(() => act(game, actor, action));
|
|
assert.deepEqual(game, before, 'Rejected consent preserves every vote, commitment and resource.');
|
|
}
|
|
function receipts(game: Game, id: string) {
|
|
return (game.travelPromises ?? []).filter(row => row.sourceDeal === id);
|
|
}
|
|
function answer(game: Game, actor: string, id: string) {
|
|
return act(game, actor, {type:'dealRespond',deal:id,accept:true});
|
|
}
|
|
|
|
/** The original shared fixture reaches a real Basic shipment turn through
|
|
* public setup and native phase actions. No force, purse, promise or vote is
|
|
* assigned by these tests. */
|
|
void test('one deal binds and fulfills both a real shipment and a real move', () => {
|
|
const f = fixture(), originalCards = inventory(f.game), cash = player(f.game,f.target).spice;
|
|
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Ship, then move.',
|
|
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;
|
|
assert.equal(receipts(game,id).length,0);
|
|
game = answer(game,f.target,id);
|
|
const bound = receipts(game,id);
|
|
assert.equal(bound.length,2);
|
|
assert.equal(bound[0].sourceDealIndex,undefined);
|
|
assert.equal(bound[1].sourceDealIndex,0);
|
|
assert.ok(bound.every(row => row.player === f.target && !row.fulfilled && !row.released));
|
|
reject(game,f.target,f.currentEnd);
|
|
game = settle(act(game,f.target,f.shipmentAction));
|
|
assert.equal(receipts(game,id)[0].fulfilled,true);
|
|
assert.equal(receipts(game,id)[1].fulfilled,false);
|
|
reject(game,f.target,f.currentEnd);
|
|
game = settle(act(game,f.target,f.firstMove));
|
|
assert.ok(receipts(game,id).every(row => row.fulfilled));
|
|
assert.equal(player(game,f.target).spice,cash-f.shipmentCost);
|
|
assert.deepEqual(inventory(game),originalCards);
|
|
});
|
|
|
|
void test('a conflicting same-turn required and forbidden shipment rejects final consent', () => {
|
|
const f = fixture();
|
|
const offered = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Inconsistent shipment terms.',
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
|
|
additionalTravel:[{actor:f.target,kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:false}]});
|
|
const id = offered.deals!.at(-1)!.id;
|
|
reject(offered,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
assert.equal(offered.deals!.at(-1)!.status,'offered');
|
|
assert.equal(receipts(offered,id).length,0);
|
|
});
|
|
|
|
void test('shipment, movement and a due payment cannot spend the same purse twice', () => {
|
|
const f = fixture(), cash = player(f.game,f.target).spice;
|
|
assert.ok(cash >= f.shipmentCost);
|
|
const offered = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Pay and travel.',
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
|
|
additionalTravel:[{actor:f.target,kind:'move',territory:f.movementTerritory,minimum:1,answer:true}],
|
|
payment:{kind:'deferredBribe',payer:f.target,recipient:f.asker,amount:cash,dueTurn:f.game.turn}});
|
|
const id = offered.deals!.at(-1)!.id;
|
|
reject(offered,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
assert.equal(offered.deals!.at(-1)!.status,'offered');
|
|
assert.equal(receipts(offered,id).length,0);
|
|
assert.equal(player(offered,f.target).spice,cash);
|
|
});
|
|
|
|
void test('future multi-travel contradictions reject atomically at final acceptance', () => {
|
|
const f = fixture(), dueTurn = f.game.turn+1;
|
|
const offered = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Contradictory future shipment.',
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true,dueTurn},
|
|
additionalTravel:[{actor:f.target,kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:false,dueTurn}]});
|
|
const id = offered.deals!.at(-1)!.id;
|
|
reject(offered,f.target,{type:'dealRespond',deal:id,accept:true});
|
|
assert.equal(receipts(offered,id).length,0);
|
|
});
|
|
|
|
void test('unanimous release cancels every unfinished term, including future travel for another participant', () => {
|
|
const f = fixture(), dueTurn = f.game.turn+1;
|
|
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Both of us will ship next turn.',
|
|
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true,dueTurn},
|
|
additionalTravel:[{actor:f.asker,kind:'ship',territory:'polar_sink',minimum:1,answer:true,dueTurn}]});
|
|
const id = game.deals!.at(-1)!.id;
|
|
game = answer(game,f.target,id);
|
|
assert.deepEqual(receipts(game,id).map(row => row.player),[f.target,f.asker]);
|
|
assert.ok(receipts(game,id).every(row => row.turn === dueTurn));
|
|
game = act(game,f.asker,{type:'dealRelease',deal:id,agree:true});
|
|
assert.ok(receipts(game,id).every(row => !row.released));
|
|
game = act(game,f.target,{type:'dealRelease',deal:id,agree:true});
|
|
assert.ok(receipts(game,id).every(row => row.released && row.releasedByDeal));
|
|
assert.equal(game.deals!.at(-1)!.status,'released');
|
|
});
|
|
|
|
|
|
const proposer = 'atreides', firstTraveler = 'emperor', secondTraveler = 'harkonnen', other = 'fremen';
|
|
function reach(game: Game, until: (state: Game) => boolean): Game {
|
|
for (let count = 0; count < 1200; count++) {
|
|
if (until(game)) return game;
|
|
assert.notEqual(game.status,'finished');
|
|
const step = next(game); assert.ok(step, `Native continuation needed at ${game.turn}/${game.phase}.`);
|
|
game = act(game,step.actor,step.action);
|
|
}
|
|
throw Error('The original group game did not reach the movement opportunity.');
|
|
}
|
|
/** Four actual human Basic seats, with only unopened cards and spice ordering
|
|
* selected, continued through native setup to the Emperor's real turn. */
|
|
function groupFixture(): Game {
|
|
let game = createGame('MULTITRAVEL',newPlayer(proposer,'Atreides','atreides'),false,[]);
|
|
for (const [id,faction] of [[firstTraveler,'emperor'],[secondTraveler,'harkonnen'],[other,'fremen']] as const)
|
|
joinGame(game,newPlayer(id,id,faction));
|
|
for (const seat of game.players) game = act(game,seat.id,{type:'ready'});
|
|
game = act(game,proposer,{type:'start'});
|
|
const cards = game.deck.filter(card => ['projectile','poison','shield','snooper'].includes(card.kind)).slice(0,4);
|
|
assert.equal(cards.length,4);
|
|
for (const [position,card] of cards.entries()) {
|
|
const from = game.deck.findIndex(row => row.id === card.id); assert.ok(from >= position);
|
|
[game.deck[position],game.deck[from]] = [game.deck[from],game.deck[position]];
|
|
}
|
|
game = reach(game,state => state.status === 'playing');
|
|
const from = game.spiceDeck.findIndex(card => 'territory' in card && !card.discovery); assert.ok(from >= 0);
|
|
game.spiceDeck.unshift(game.spiceDeck.splice(from,1)[0]);
|
|
return reach(game,state => state.turn === 1 && state.phase === 5 && state.active === firstTraveler && clean(state));
|
|
}
|
|
let source: Game | undefined;
|
|
const group = () => structuredClone(source ??= groupFixture());
|
|
for (const voters of [[firstTraveler,secondTraveler],[secondTraveler,firstTraveler]]) void test(
|
|
`ordinary invitees consent before both travelers, who may accept in either order (${voters.join(' then ')})`, () => {
|
|
const initial = group(), dueTurn = initial.turn+1, cards = inventory(initial);
|
|
let game = act(initial,proposer,{type:'dealOffer',targets:[firstTraveler,secondTraveler,other],
|
|
terms:'Two players will ship on different turns.',
|
|
travel:{actor:firstTraveler,kind:'ship',territory:'polar_sink',minimum:1,answer:true},
|
|
additionalTravel:[{actor:secondTraveler,kind:'ship',territory:'polar_sink',minimum:1,answer:true,dueTurn}]});
|
|
const id = game.deals!.at(-1)!.id;
|
|
reject(game,voters[0],{type:'dealRespond',deal:id,accept:true});
|
|
game = answer(game,other,id);
|
|
game = answer(game,voters[0],id);
|
|
assert.equal(game.deals!.at(-1)!.status,'offered');
|
|
assert.equal(receipts(game,id).length,0);
|
|
game = answer(game,voters[1],id);
|
|
assert.equal(game.deals!.at(-1)!.status,'active');
|
|
assert.deepEqual(receipts(game,id).map(row => [row.player,row.turn]),
|
|
[[firstTraveler,initial.turn],[secondTraveler,dueTurn]]);
|
|
game = reach(act(game,firstTraveler,{type:'ship',territory:'polar_sink',sector:0,amount:1,elite:0,allyPayment:0}),
|
|
state => clean(state) && !state.pendingShipment);
|
|
assert.equal(receipts(game,id)[0].fulfilled,true);
|
|
assert.equal(receipts(game,id)[1].fulfilled,false);
|
|
assert.deepEqual(inventory(game),cards);
|
|
},
|
|
);
|