import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early'; import { truthMovementInventory as inventory } from './fixture-truth-movement'; const player = (game: Game, id: string) => { const seat = game.players.find(row => row.id === id); assert.ok(seat); return seat; }; const quiet = (game: Game) => !game.response && !game.decision && !game.phaseOpening && !game.truthtrance && !game.pendingTreacheryDiscard; function advance(game: Game, until: (state: Game) => boolean): Game { for (let count = 0; count < 1800; count++) { if (until(game)) return game; assert.notEqual(game.status, 'finished'); const step = game.phase === 5 && game.active === 'emperor' && quiet(game) ? {actor:'emperor', action:{type:'endMovement'} satisfies Action} : nextTruthMovementNativeEarlyStep(game, {position:'first', method:'onceAround', direction:'clockwise', keepUnbid:false}); assert.ok(step, `A native continuation is needed at ${game.turn}/${game.phase}/${game.decision?.kind}`); game = act(game,step.actor,step.action); } throw Error('The native game did not reach the requested boundary.'); } /** Real selected-deck Basic setup, paid No-Field shipment, and two separately * consented deals. Only original unopened starting cards and Spice positions * are selected; no marker, offer, payment, balance or response is staged. */ function source() { let game = createGame('REVEALDEAL',newPlayer('richese','Richese','richese'),false,['choam','ix']); for (const id of ['choam','emperor'] as const) joinGame(game,newPlayer(id,id,id)); for (const seat of game.players) game = act(game,seat.id,{type:'ready'}); game = act(game,game.host,{type:'start'}); const ordinary = game.deck.filter(card => ['projectile','poison','shield','snooper'].includes(card.kind)).slice(0,2); const karama = game.deck.find(card => card.effect === 'karama'); assert.equal(ordinary.length,2); assert.ok(karama); for (const [position,card] of [...ordinary,karama].entries()) { const from = game.deck.findIndex(candidate => candidate.id === card.id); assert.ok(from >= position); [game.deck[position],game.deck[from]] = [game.deck[from],game.deck[position]]; } game = advance(game,state => state.status === 'playing'); for (const position of [0,1]) { const from = game.spiceDeck.findIndex((card,index) => index >= position && 'territory' in card && !card.discovery); assert.ok(from >= position); [game.spiceDeck[position],game.spiceDeck[from]] = [game.spiceDeck[from],game.spiceDeck[position]]; } game = advance(game,state => state.turn === 1 && state.phase === 5 && state.active === 'richese' && quiet(state)); const token = player(game,'richese').noField!.tokens.find(row => row.value === 3); assert.ok(token); const event = player(game,'richese').noFieldEvent; assert.ok(event); game = act(game,'richese',{type:'ship',noField:token.id,event,territory:'polar_sink',sector:0,allyPayment:0}); game = advance(game,quiet); game = act(game,'richese',{type:'dealOffer',target:'choam',terms:'Public agreement.'}); const written = game.deals?.at(-1); assert.ok(written); game = act(game,'choam',{type:'dealRespond',deal:written.id,accept:true}); const payment = {kind:'deferredBribe',payer:'richese',recipient:'choam',amount:1,dueTurn:2}; game = act(game,'richese',{type:'dealOffer',target:'choam',terms:'One spice on turn two.',payment}); const offer = game.deals?.at(-1); assert.ok(offer); game = act(game,'choam',{type:'dealRespond',deal:offer.id,accept:true}); assert.equal(game.deals?.find(row => row.id === offer.id)?.paymentStatus,'pending'); game = act(game,'richese',{type:'endMovement'}); game = advance(game,state => state.turn === 2 && state.phase === 2 && state.response?.kind === 'choamCharity'); const revealEvent = player(game,'richese').noFieldEvent; assert.ok(revealEvent); return {game,token:token.id,event:revealEvent,written:written.id,payment:offer.id}; } let cached: ReturnType | undefined; const fixture = () => cached ??= source(); void test('a revealed No-Field preserves both active public deals and the original Charity response', () => { const {token,event,written,payment} = fixture(); let before = structuredClone(fixture().game); before = act(before,'richese',{type:'passResponse'}); const response = structuredClone(before.response), deals = structuredClone(before.deals), cards = inventory(before); const reserves = player(before,'richese').reserves, forces = player(before,'richese').forces['polar_sink:0'] ?? 0; assert.equal(viewGame(before,'richese').richeseNoField?.canReveal,true); const after = act(before,'richese',{type:'revealNoField',token,event}); assert.deepEqual(after.response,response); assert.deepEqual(after.deals,deals); assert.equal(player(after,'richese').noField?.deployed,null); assert.equal(player(after,'richese').reserves,reserves - 3); assert.equal(player(after,'richese').forces['polar_sink:0'],forces + 3); assert.deepEqual(inventory(after),cards); const continued = advance(after,state => quiet(state) && state.response?.kind !== 'choamCharity'); assert.equal(continued.deals?.find(row => row.id === written)?.status,'active'); assert.equal(continued.deals?.find(row => row.id === payment)?.paymentStatus,'pending'); const payer = player(continued,'richese').spice, recipientBribes = player(continued,'choam').bribes; const paid = act(continued,'richese',{type:'dealPay',deal:payment}); assert.equal(paid.deals?.find(row => row.id === payment)?.paymentStatus,'paid'); assert.equal(player(paid,'richese').spice,payer - 1); assert.equal(player(paid,'choam').bribes,recipientBribes + 1); assert.equal(player(paid,'richese').forces['polar_sink:0'],forces + 3); assert.deepEqual(inventory(paid),cards); });