import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Game } from '../game/engine'; import type { TruthQuestion } from '../game/truthtrance'; import { actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance, createTruthtranceRichesePromisesFixture as fixture, nextTruthtranceRichesePromisesStep as next, passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player, } from './fixture-truthtrance-richese-promises'; type Fixture = ReturnType; const options = {position:'last' as const, method:'silent' as const}; /** Genuine Advanced setup and auctions; stage only the second original Truth in * Atreides' hand, conserving the physical deck. */ function source() { const f = fixture({advanced:true}); let game = structuredClone(f.earlySource); const index = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(index >= 0); const [second] = game.deck.splice(index,1); player(game,f.asker).hand.push(second); assert.deepEqual(inventory(game),f.originalCards); game = advance(game,state => clean(state) && !!state.auction,[],options); assert.equal(game.auction?.active,f.richese); game = act(game,f.richese,{type:'bid',amount:1,allyPayment:0}); // Reach the real target's ordinary turn without fabricating a current bid. for (let n = 0; game.auction?.active !== f.emperor && n < game.players.length; n++) { const actor = game.auction?.active; assert.ok(actor); game = act(game,actor,{type:'passBid'}); } assert.equal(game.auction?.active,f.emperor); return {f,game,second:second.id}; } function ask(game:Game, f:Fixture, card:string, question:TruthQuestion, answer:'yes'|'no') { game = act(game,f.asker,{type:'card',card}); game = advance(game,state => state.truthtrance?.stage === 'ask',[],options); game = act(game,f.asker,{type:'truthAsk',question}); const view = viewGame(game,f.emperor); const choices = question.kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers : view.truthBidAnswers; assert.ok(choices?.includes(answer),`Must offer ${question.kind} ${answer}; offered ${JSON.stringify(choices)}`); return passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer})); } function bind(local:'yes'|'no') { const {f,second,...rest} = source(); let game = rest.game; const lot = game.auction!.index+1; game = ask(game,f,f.truth,{kind:'phaseAnyBid',target:f.emperor,turn:game.turn,minimum:6},'yes'); game = ask(game,f,second,{kind:'bid',target:f.emperor,turn:game.turn,lot,minimum:local === 'yes' ? 2 : 4},local); return {f,game}; } /** Continue through native actions, deliberately passing later ordinary lots * so the all-auction promise must be fulfilled in the real final cache. */ function reachCache(game:Game) { for (let n = 0; n < 1000; n++) { if (clean(game) && game.richeseAuction?.source === 'cache' && !game.richeseAuction.outcome) return game; if (clean(game) && game.auction?.active) { game = act(game,game.auction.active,{type:'passBid'}); continue; } const step = next(game,options); assert.ok(step,'Native path must reach the last cache'); game = act(game,step.actor,step.action); } throw Error('The real final cache did not open'); } function bidAtCache(game:Game, f:Fixture) { const lot = game.richeseAuction; assert.ok(lot); assert.equal(lot.source,'cache'); const guide = viewGame(game,f.emperor).phaseAnyBidGuide; assert.ok(guide); assert.equal(guide.action.type,'richeseBid'); assert.equal(guide.action.amount,6); const purse = player(game,f.emperor).spice; game = act(game,f.emperor,guide.action); assert.equal(player(game,f.emperor).spice,purse,'A declaration is not an invoice'); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.emperor && row.fulfilled && !row.released)); assert.deepEqual(inventory(game),f.originalCards); game = advance(game,state => player(state,f.emperor).hand.some(card => card.id === lot.cardId),[],options); assert.equal(player(game,f.emperor).spice,purse-6); assert.deepEqual(inventory(game),f.originalCards); } void test('ordinary No and all-auction Yes reach and fulfill the native last cache',() => { const {f,...bound} = bind('no'); let game = bound.game; const rejected = structuredClone(game); assert.throws(() => act(game,f.emperor,{type:'bid',amount:4,allyPayment:0})); assert.deepEqual(game,rejected); game = act(game,f.emperor,{type:'passBid'}); const promise = game.phaseAnyBidPromises?.find(row => row.player === f.emperor); assert.ok(promise); assert.equal(!!promise.fulfilled,false); assert.equal(!!promise.released,false); game = reachCache(game); bidAtCache(game,f); }); void test('ordinary lower Yes purchases its real card while reserving funding for the last cache Yes',() => { const {f,...bound} = bind('yes'); let game = bound.game; const lotCard = game.auction!.cards[game.auction!.index].id; const purse = player(game,f.emperor).spice; game = act(game,f.emperor,{type:'bid',amount:2,allyPayment:0}); assert.ok(game.bidPromises?.some(row => row.player === f.emperor && row.fulfilled)); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.emperor && !row.fulfilled && !row.released)); assert.equal(player(game,f.emperor).spice,purse); const reserved = structuredClone(game); assert.throws(() => act(game,f.emperor,{type:'bribe',target:f.asker,amount:3}), 'The pending ordinary invoice and six-spice cache bid must both remain funded'); assert.deepEqual(game,reserved); assert.doesNotThrow(() => act(game,f.emperor,{type:'bribe',target:f.asker,amount:2})); game = reachCache(game); assert.ok(player(game,f.emperor).hand.some(card => card.id === lotCard)); assert.equal(player(game,f.emperor).spice,purse-2); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.emperor && !row.fulfilled && !row.released)); bidAtCache(game,f); });