import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { actTruthTravel as act, advanceTruthTravel as advance, createTruthTravelFixture as fixture, settleTruthTravel as settle, truthTravelClean as clean, truthTravelInventory as inventory, truthTravelPlayer as player } from './fixture-truth-travel'; import { nextStrongholdFactionsNativeStep as nextNative } 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 action must not partially mutate the source game.'); } function deal(game: Game, id: string) { const found = game.deals?.find(row => row.id === id); assert.ok(found); return found; } function offer(game: Game, proposer: string, target: string, payer: string, recipient: string, territory: string, amount: number, dueTurn: number, travelTurn?: number) { const next = act(game, proposer, {type:'dealOffer', target, terms:'Pay the agreed amount and make the agreed shipment.', payment:{kind:'deferredBribe', payer, recipient, amount, dueTurn}, travel:{kind:'ship', territory, minimum:1, answer:true, ...(travelTurn === undefined ? {} : {dueTurn:travelTurn})}}); const offered = next.deals?.at(-1); assert.ok(offered); return {game:next, id:offered.id}; } function accept(game: Game, target: string, id: string) { return act(game, target, {type:'dealRespond', deal:id, accept:true}); } function status(game: Game, viewer: string, id: string) { return viewGame(game, viewer).dealTravelStatus[id]; } /** Genuine public Basic two-seat setup and native phase-five window; the * fixture stages only unopened cards/Spice. Every offer, vote, payment and * shipment below is executed through public player actions. */ for (const payerRole of ['traveler','proposer'] as const) for (const order of ['pay-first','travel-first'] as const) { void test(`current-turn deferred payment and shipment coexist with ${payerRole} paying (${order})`, () => { const f = fixture(), before = f.game, payer = payerRole === 'traveler' ? f.target : f.asker; const recipient = payerRole === 'traveler' ? f.asker : f.target; const cash = player(before,payer).spice, travelCash = player(before,f.target).spice; const escrow = player(before,recipient).bribes; assert.ok(cash >= (payerRole === 'traveler' ? f.shipmentCost + 1 : 1)); const offered = offer(before,f.asker,f.target,payer,recipient,f.shipmentTerritory,1,before.turn); assert.equal(deal(offered.game,offered.id).status,'offered'); assert.equal(player(offered.game,payer).spice,cash); assert.equal(offered.game.travelPromises?.some(row => row.sourceDeal === offered.id) ?? false,false); let game = accept(offered.game,f.target,offered.id); assert.equal(deal(game,offered.id).status,'active'); assert.equal(deal(game,offered.id).paymentStatus,'pending'); assert.equal(status(game,f.target,offered.id),'pending'); assert.equal(player(game,payer).spice,cash); assert.equal(game.travelPromises?.filter(row => row.sourceDeal === offered.id).length,1); reject(game,f.asker,{type:'dealRespond',deal:offered.id,accept:true}); if (order === 'pay-first') { game = act(game,payer,{type:'dealPay',deal:offered.id}); assert.equal(deal(game,offered.id).paymentStatus,'paid'); assert.equal(player(game,recipient).bribes,escrow+1); game = settle(act(game,f.target,f.shipmentAction)); } else { game = settle(act(game,f.target,f.shipmentAction)); assert.equal(deal(game,offered.id).paymentStatus,'pending'); game = act(game,payer,{type:'dealPay',deal:offered.id}); } assert.equal(deal(game,offered.id).paymentStatus,'paid'); assert.equal(status(game,f.target,offered.id),'fulfilled'); assert.equal(player(game,f.target).forces['polar_sink:0'],1); assert.equal(player(game,payer).spice,cash - 1 - (payerRole === 'traveler' ? f.shipmentCost : 0)); if (payerRole === 'proposer') assert.equal(player(game,f.target).spice,travelCash-f.shipmentCost); assert.equal(player(game,recipient).bribes,escrow+1); reject(game,payer,{type:'dealPay',deal:offered.id}); assert.deepEqual(inventory(game),f.originalCards); }); } void test('current-turn joint consent rejects an unaffordable payer-traveler without creating either obligation', () => { const f = fixture(), cash = player(f.game,f.target).spice; assert.ok(cash > 0 && f.shipmentCost > 0); const offered = offer(f.game,f.asker,f.target,f.target,f.asker,f.shipmentTerritory,cash,f.game.turn); const before = structuredClone(offered.game); reject(offered.game,f.target,{type:'dealRespond',deal:offered.id,accept:true}); assert.deepEqual(offered.game,before); assert.equal(deal(offered.game,offered.id).status,'offered'); assert.equal(offered.game.travelPromises?.some(row => row.sourceDeal === offered.id) ?? false,false); assert.equal(player(offered.game,f.target).spice,cash); }); void test('both participants must release the unpaid payment and matching travel together', () => { const f = fixture(); const offered = offer(f.game,f.asker,f.target,f.target,f.asker,f.shipmentTerritory,1,f.game.turn); let game = accept(offered.game,f.target,offered.id); game = act(game,f.asker,{type:'dealRelease',deal:offered.id,agree:true}); assert.equal(status(game,f.target,offered.id),'pending'); assert.equal(deal(game,offered.id).paymentStatus,'pending'); reject(game,f.target,f.currentEnd); game = act(game,f.target,{type:'dealRelease',deal:offered.id,agree:true}); assert.equal(status(game,f.target,offered.id),'released'); assert.equal(deal(game,offered.id).paymentStatus,'released'); reject(game,f.target,{type:'dealPay',deal:offered.id}); game = settle(act(game,f.target,f.currentEnd)); assert.equal(status(game,f.target,offered.id),'released'); assert.deepEqual(inventory(game),f.originalCards); }); for (const payerRole of ['traveler','proposer'] as const) void test( `future-due deferred payment and shipment survive rollover with ${payerRole} paying`, () => { const f = fixture(), dueTurn = f.game.turn+1, payer = payerRole === 'traveler' ? f.target : f.asker; const recipient = payerRole === 'traveler' ? f.asker : f.target; const offered = offer(f.game,f.asker,f.target,payer,recipient,f.shipmentTerritory,1,dueTurn,dueTurn); let game = accept(offered.game,f.target,offered.id); assert.equal(status(game,f.target,offered.id),'scheduled'); assert.equal(deal(game,offered.id).paymentStatus,'pending'); game = settle(act(game,f.target,f.currentEnd)); game = advance(game,state => state.turn === dueTurn && state.phase === 2 && clean(state)); assert.equal(deal(game,offered.id).paymentStatus,'pending'); game = act(game,payer,{type:'dealPay',deal:offered.id}); assert.equal(deal(game,offered.id).paymentStatus,'paid'); game = advance(game,state => state.turn === dueTurn && state.phase === 5 && state.active === f.target && clean(state)); game = settle(act(game,f.target,f.shipmentAction)); assert.equal(status(game,f.target,offered.id),'fulfilled'); assert.equal(deal(game,offered.id).paymentStatus,'paid'); assert.equal(player(game,f.target).forces['polar_sink:0'],1); assert.deepEqual(inventory(game),f.originalCards); }, ); const groupProposer = 'atreides', groupTraveler = 'emperor', groupPayer = 'harkonnen', groupOther = 'fremen'; const groupShipment: Action = {type:'ship',territory:'polar_sink',sector:0,amount:1,elite:0,allyPayment:0}; function groupReach(game: Game, until: (state: Game) => boolean): Game { for (let count = 0; count < 1600; count++) { if (until(game)) return game; assert.notEqual(game.status,'finished'); const step = nextNative(game); assert.ok(step, `Native continuation needed at ${game.turn}/${game.phase}`); game = act(game,step.actor,step.action); } throw Error('The original group-deal programme did not reach its requested boundary.'); } /** Four original human Basic seats. Only unopened starting cards and two land * Spice cards are ordered; no resource, vote, promise, or force is fabricated. */ function groupFixture(): Game { let game = createGame('GROUPDEFERTRAVEL',newPlayer(groupProposer,'Atreides','atreides'),false,[]); for (const [id,faction] of [[groupTraveler,'emperor'],[groupPayer,'harkonnen'],[groupOther,'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,groupProposer,{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 = groupReach(game,state => state.status === 'playing'); for (let position = 0; position < 2; position++) { const from = game.spiceDeck.findIndex((card,index) => index >= position && 'territory' in card && !card.discovery); assert.ok(from >= position); game.spiceDeck.splice(position,0,game.spiceDeck.splice(from,1)[0]); } game = groupReach(game,state => state.turn === 1 && state.phase === 5 && state.active === groupTraveler && clean(state)); assert.ok(player(game,groupTraveler).spice >= 2); return game; } function groupOffer(game: Game, payer: string, amount: number, dueTurn: number, travelTurn?: number) { const next = act(game,groupProposer,{type:'dealOffer',targets:[groupPayer,groupTraveler], terms:'Deferred payment and a shared journey.', payment:{kind:'deferredBribe',payer,recipient:groupProposer,amount,dueTurn}, travel:{actor:groupTraveler,kind:'ship',territory:'polar_sink',minimum:1,answer:true, ...(travelTurn === undefined ? {} : {dueTurn:travelTurn})}}); const offered = next.deals?.at(-1); assert.ok(offered); return {game:next,id:offered.id}; } for (const payer of [groupTraveler,groupPayer]) void test( `three-party deferred travel requires the traveler last and retains ${payer}'s pending payment`, () => { const initial = groupFixture(), cards = inventory(initial), cash = player(initial,payer).spice; assert.ok(cash >= (payer === groupTraveler ? 3 : 1)); const offered = groupOffer(initial,payer,1,initial.turn); let game = offered.game; reject(game,groupTraveler,{type:'dealRespond',deal:offered.id,accept:true}); game = accept(game,groupPayer,offered.id); assert.equal(deal(game,offered.id).status,'offered'); assert.equal(game.travelPromises?.some(row => row.sourceDeal === offered.id) ?? false,false); assert.equal(player(game,payer).spice,cash); game = accept(game,groupTraveler,offered.id); assert.equal(deal(game,offered.id).status,'active'); assert.equal(deal(game,offered.id).paymentStatus,'pending'); assert.equal(status(game,groupTraveler,offered.id),'pending'); assert.equal(player(game,payer).spice,cash); game = act(game,payer,{type:'dealPay',deal:offered.id}); assert.equal(deal(game,offered.id).paymentStatus,'paid'); game = groupReach(act(game,groupTraveler,groupShipment),state => clean(state) && !state.pendingShipment); assert.equal(status(game,groupTraveler,offered.id),'fulfilled'); assert.equal(player(game,groupTraveler).forces['polar_sink:0'],1); assert.equal(player(game,payer).spice,cash - 1 - (payer === groupTraveler ? 2 : 0)); assert.deepEqual(inventory(game),cards); }, ); void test('final group consent rolls back when the traveling payer cannot fund both obligations', () => { const initial = groupFixture(), cash = player(initial,groupTraveler).spice; const offered = groupOffer(initial,groupTraveler,cash,initial.turn); const waiting = accept(offered.game,groupPayer,offered.id); reject(waiting,groupTraveler,{type:'dealRespond',deal:offered.id,accept:true}); assert.equal(deal(waiting,offered.id).status,'offered'); assert.equal(player(waiting,groupTraveler).spice,cash); assert.equal(waiting.travelPromises?.some(row => row.sourceDeal === offered.id) ?? false,false); }); void test('a third-party deferred payer and named traveler fulfill their actual next-turn group obligations', () => { const initial = groupFixture(), cards = inventory(initial), dueTurn = initial.turn+1; const offered = groupOffer(initial,groupPayer,1,dueTurn,dueTurn); let game = accept(offered.game,groupPayer,offered.id); game = accept(game,groupTraveler,offered.id); assert.equal(status(game,groupTraveler,offered.id),'scheduled'); assert.equal(deal(game,offered.id).paymentStatus,'pending'); game = groupReach(act(game,groupTraveler,{type:'endMovement'}),state => state.turn === dueTurn && clean(state)); game = act(game,groupPayer,{type:'dealPay',deal:offered.id}); assert.equal(deal(game,offered.id).paymentStatus,'paid'); game = groupReach(game,state => state.turn === dueTurn && state.phase === 5 && state.active === groupTraveler && clean(state)); game = groupReach(act(game,groupTraveler,groupShipment),state => clean(state) && !state.pendingShipment); assert.equal(status(game,groupTraveler,offered.id),'fulfilled'); assert.equal(deal(game,offered.id).paymentStatus,'paid'); assert.deepEqual(inventory(game),cards); });