import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Game } from '../game/engine'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance, askTruthtranceRichesePromises as ask, createTruthtranceRichesePromisesFixture as fixture, passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player, } from './fixture-truthtrance-richese-promises'; /** Public Basic Richese/Atreides/Emperor start. The original unopened Karama, * second Truthtrance and cached Semuta are moved to named hands, conserving * the full physical inventory. No wallet, promise or auction is installed. */ function source(earlyAnswer: 'yes' | 'no', joint: boolean, position: 'first' | 'last') { const f = fixture({advanced:false}); let game = structuredClone(f.earlySource); const take = (effect: string) => { const index = game.deck.findIndex(card => card.effect === effect && card.id !== f.truth); assert.ok(index >= 0, `The original ${effect} remains in the deck`); return game.deck.splice(index,1)[0]; }; const selfKarama = take('karama'), otherKarama = take('karama'); player(game,f.emperor).hand.push(selfKarama); player(game,f.asker).hand.push(otherKarama); const secondTruth = joint ? take('truthtrance') : null; if (secondTruth) player(game,f.asker).hand.push(secondTruth); const semutaIndex = game.richeseCache!.findIndex(card => card.id === SEMUTA_DRUG_ID); assert.ok(semutaIndex >= 0); const [semuta] = game.richeseCache!.splice(semutaIndex,1); player(game,f.richese).hand.push(semuta); assert.deepEqual(inventory(game),f.originalCards); game = ask(game,f,{kind:'earlyRicheseBid',target:f.emperor,turn:game.turn,minimum:4}); assert.ok(viewGame(game,f.emperor).truthEarlyRicheseBidAnswers?.includes(earlyAnswer)); game = passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer:earlyAnswer})); const early = game.earlyRicheseBidPromises?.find(row => row.player === f.emperor); assert.ok(early); if (secondTruth) { game = act(game,f.asker,{type:'card',card:secondTruth.id}); game = advance(game,state => state.truthtrance?.stage === 'ask'); game = act(game,f.asker,{type:'truthAsk',question:{ kind:'phaseAnyBid',target:f.emperor,turn:game.turn,minimum:6, }}); assert.ok(viewGame(game,f.emperor).truthPhaseAnyBidAnswers?.includes('yes')); game = passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer:'yes'})); } const phase = game.phaseAnyBidPromises?.find(row => row.player === f.emperor); game = advance(game,state => state.decision?.kind === 'richeseDeclaration'); const ordinaryBefore = game.richeseBidding!.normalCount!; game = act(game,f.richese,{type:'decision',event:game.richeseBidding!.event,position}); assert.equal(game.response?.kind,'richeseAuction'); assert.ok(!game.richeseBidding?.cacheCanceled); assert.deepEqual(inventory(game),f.originalCards); return {f,game,early,phase,selfKarama,otherKarama,ordinaryBefore}; } function settleDiscard(state: Game, owner: string, claim: boolean): Game { let game = state; const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction?.stage === 'offer', 'The actual cancellation opens Semuta'); const event = pending.batch.event; if (!claim) return passTruthRicheseFreshDiscard(game); assert.ok(viewGame(game,owner).semutaReaction?.canCommit); game = act(game,owner,{type:'semutaCommit',event}); if (game.pendingTreacheryDiscard?.reaction?.stage === 'select') { const card = pending.batch.entries[0].card.id; game = act(game,owner,{type:'semutaSelect',event,card}); } assert.notEqual(game.pendingTreacheryDiscard?.batch.event,event); return game; } for (const position of ['first','last'] as const) for (const joint of [false,true]) void test( `Basic ${position} self-cancellation cannot evade an early cache Yes${joint ? ' with phase Yes' : ''}`, () => { const f = source('yes',joint,position), before = structuredClone(f.game); assert.ok(!viewGame(f.game,f.f.emperor).responseControls?.cancelCards.includes(f.selfKarama.id), 'The private cancellation controls explain the unsupported voluntary action'); assert.match(viewGame(f.game,f.f.emperor).responseControls?.blocked ?? '', /Truthtrance Yes/); assert.throws(() => act(f.game,f.f.emperor,{type:'card',card:f.selfKarama.id,mode:'cancel'})); assert.deepEqual(f.game,before, 'Rejected action does not spend Karama or alter the cache response'); assert.ok(player(f.game,f.f.emperor).hand.some(card => card.id === f.selfKarama.id)); assert.ok(f.game.earlyRicheseBidPromises?.some(row => row.id === f.early.id && !row.released && !row.boundEvent)); if (f.phase) assert.ok(f.game.phaseAnyBidPromises?.some(row => row.event === f.phase!.event && !row.released)); assert.deepEqual(inventory(f.game),f.f.originalCards); }, ); for (const claim of [false,true]) void test( `Another player's cache cancellation releases the Yes; Semuta ${claim ? 'claims' : 'passes'}`, () => { const f = source('yes',true,'first'); let game = act(f.game,f.f.asker,{type:'card',card:f.otherKarama.id,mode:'cancel'}); game = settleDiscard(game,f.f.richese,claim); assert.equal(game.richeseBidding?.cacheCanceled,true); assert.equal(game.richeseBidding?.normalCount,f.ordinaryBefore+1); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === f.phase!.event && !row.fulfilled && !row.released)); assert.equal(player(game,f.f.richese).hand.some(card => card.id === f.otherKarama.id),claim); assert.equal(player(game,f.f.richese).hand.some(card => card.id === SEMUTA_DRUG_ID),!claim); game = advance(game,state => clean(state) && !!state.auction && state.auction.active === f.f.emperor); assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === f.early.id && row.released && !row.boundEvent)); const guide = viewGame(game,f.f.emperor).phaseAnyBidGuide?.action; assert.ok(guide); assert.equal(guide.type,'bid'); assert.equal(guide.amount,6); game = act(game,f.f.emperor,guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === f.phase!.event && row.fulfilled)); assert.deepEqual(inventory(game),f.f.originalCards); }, ); void test('a player may cancel the cache after answering No to the early bid question', () => { const f = source('no',false,'first'); let game = act(f.game,f.f.emperor,{type:'card',card:f.selfKarama.id,mode:'cancel'}); game = settleDiscard(game,f.f.richese,false); assert.equal(game.richeseBidding?.cacheCanceled,true); assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === f.early.id && row.fulfilled && !row.released)); assert.deepEqual(inventory(game),f.f.originalCards); }); void test('self-cancellation is allowed after an external shortfall already made the Yes impossible', () => { const f = source('yes',false,'first'); // Disclosed external-state supplement: this completed loss is staged while // the cache response is open, so the old Yes has not been reconciled yet. // No physical cards, bids, responses or promise receipts are fabricated. const game = structuredClone(f.game); player(game,f.f.emperor).spice = 0; assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === f.early.id && !row.released)); assert.ok(viewGame(game,f.f.emperor).responseControls?.cancelCards.includes(f.selfKarama.id)); let resolved = act(game,f.f.emperor,{type:'card',card:f.selfKarama.id,mode:'cancel'}); resolved = settleDiscard(resolved,f.f.richese,false); assert.equal(resolved.richeseBidding?.cacheCanceled,true); assert.ok(resolved.earlyRicheseBidPromises?.some(row => row.id === f.early.id && row.released)); assert.deepEqual(inventory(resolved),f.f.originalCards); });