import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Action, type Game } from '../game/engine'; import { actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance, createTruthtranceRichesePromisesFixture as fixture, passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player, type TruthtranceRichesePromisesFixture, } from './fixture-truthtrance-richese-promises'; type Method = 'silent' | 'onceAround'; type Kind = 'richeseBid' | 'mixedRicheseBid' | 'constantMixed'; /** Public Advanced R/A/E start with original unopened setup/Spice order. Move * the second original Truth to Atreides, the original Semuta from the cache * to Richese, and the original Nullentropy Box from the * cache to Atreides. The actual Semuta reclaims the first used Truth; the * actual two-spice Box search recovers Semuta for Atreides, who can claim * Richese's later discard. No promise, * bid, purse, reaction or physical card is fabricated. */ function source() { const f = fixture({advanced:true}), game = structuredClone(f.earlySource); const secondAt = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(secondAt >= 0); const [second] = game.deck.splice(secondAt,1); player(game,f.asker).hand.push(second); const take = (effect:string, owner:string) => { const at = game.richeseCache!.findIndex(card => card.effect === effect); assert.ok(at >= 0); const [card] = game.richeseCache!.splice(at,1); player(game,owner).hand.push(card); return card.id; }; const semuta = take('semutaDrug',f.richese), box = take('nullentropyBox',f.asker); assert.deepEqual(inventory(game),f.originalCards); return {f,game,second:second.id,semuta,box}; } function askPre(game:Game,f:TruthtranceRichesePromisesFixture,card:string, kind:'phaseAnyBid'|'earlyRicheseBid',minimum:number) { game = act(game,f.asker,{type:'card',card}); game = advance(game,state => state.truthtrance?.stage === 'ask'); game = act(game,f.asker,{type:'truthAsk',question:{kind,target:f.emperor,turn:game.turn,minimum}}); const view = viewGame(game,f.emperor); const options = kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers : view.truthEarlyRicheseBidAnswers; assert.ok(options?.includes('yes')); return act(game,f.emperor,{type:'truthAnswer',answer:'yes'}); } function parkedMarket(method:Method, kind:Kind) { const s = source(); let game = askPre(s.game,s.f,s.f.truth,'phaseAnyBid',4); const first = game.pendingTreacheryDiscard; assert.ok(first?.reaction); assert.ok(viewGame(game,s.f.richese).semutaReaction?.canCommit, JSON.stringify(viewGame(game,s.f.richese).semutaReaction)); game = act(game,s.f.richese,{type:'semutaCommit',event:first.batch.event}); assert.ok(player(game,s.f.richese).hand.some(card => card.id === s.f.truth)); assert.ok(game.discard.some(card => card.id === s.semuta)); // Recover the actual used Semuta with the actual Box and exact native cost. const purse = player(game,s.f.asker).spice; game = act(game,s.f.asker,{type:'card',card:s.box}); if (game.decision?.kind === 'nullentropy') { assert.ok(game.pendingNullentropy); game = act(game,s.f.asker,{type:'decision',event:game.pendingNullentropy.event,card:s.semuta}); } game = passTruthRicheseFreshDiscard(game); assert.equal(player(game,s.f.asker).spice,purse-2); assert.ok(player(game,s.f.asker).hand.some(card => card.id === s.semuta)); assert.ok(game.discard.some(card => card.id === s.box)); game = passTruthRicheseFreshDiscard(askPre(game,s.f,s.second,'earlyRicheseBid',6)); const early = game.earlyRicheseBidPromises?.find(row => row.player === s.f.emperor); assert.ok(early); const phase = game.phaseAnyBidPromises?.find(row => row.player === s.f.emperor); assert.ok(phase); game = advance(game,state => state.decision?.kind === 'richeseBlackMarket'); assert.ok(player(game,s.f.richese).hand.some(card => card.id === s.f.targetCard)); game = act(game,s.f.richese,{type:'decision',event:game.richeseBidding!.event, card:s.f.targetCard,method,...(method === 'onceAround' ? {direction:'clockwise'} : {})}); while (game.response) { const actor = game.players.find(seat => !game.response!.passed.includes(seat.id)); assert.ok(actor); game = act(game,actor.id,{type:'passResponse'}); } const lot = game.richeseAuction; assert.ok(lot && lot.source === 'blackMarket'); game = act(game,s.f.richese,{type:'card',card:s.f.truth}); game = advance(game,state => state.truthtrance?.stage === 'ask'); const base = {target:s.f.emperor,turn:game.turn,event:lot.event}; const question = kind === 'richeseBid' ? {kind:'richeseBid',...base,minimum:4} : {kind:'mixedRicheseBid',...base,mixed:{kind:kind === 'constantMixed' ? 'or' : 'and',terms:[ {kind:'fact',fact:{kind:'spice',compare:'gte',value:10}}, {kind:'action',action:{minimum:4}}, ]}}; game = act(game,s.f.richese,{type:'truthAsk',question}); const view = viewGame(game,s.f.emperor); const answers = kind === 'richeseBid' ? view.truthRicheseBidAnswers : view.truthMixedRicheseBidAnswers; assert.ok(answers?.includes('yes')); const parent = structuredClone({lot:game.richeseAuction,round:game.richeseBidding, funding:game.richeseFunding,aid:game.aid,turn:game.turn,phase:game.phase,active:game.active,order:game.order}); game = act(game,s.f.emperor,{type:'truthAnswer',answer:'yes'}); const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction); assert.equal(pending.continuation.kind,'truthtranceDiscard'); assert.equal(pending.batch.entries[0].card.id,s.f.truth); assert.ok(viewGame(game,s.f.asker).semutaReaction?.canCommit, JSON.stringify(viewGame(game,s.f.asker).semutaReaction)); assert.deepEqual(inventory(game),s.f.originalCards); return {...s,game,early,phase,lot:lot.event,pending:pending.batch.event,parent}; } function reject(game:Game,actor:string,action:Action) { const before = structuredClone(game); assert.throws(() => act(game,actor,action)); assert.deepEqual(game,before); } for (const method of ['silent','onceAround'] as const) for (const kind of ['richeseBid','mixedRicheseBid','constantMixed'] as const) for (const claim of [false,true]) void test( `Advanced ${method} ${kind} third answer: Semuta ${claim ? 'claim' : 'pass'} binds and resumes original ledgers`, () => { const s = parkedMarket(method,kind); const local = s.game.richeseBidPromises?.find(row => row.player === s.f.emperor && row.event === s.lot); const mixed = s.game.mixedRicheseBidQuestions?.find(row => row.player === s.f.emperor && row.event === s.lot); if (kind === 'constantMixed') { assert.ok(mixed && !mixed.binding,'A true fact settles the expression without inventing a bid'); assert.equal(local,undefined); } else { assert.ok(local && local.answer); if (kind === 'mixedRicheseBid') assert.ok(mixed?.binding); } const families: Array<'earlyRicheseBidPromises'|'phaseAnyBidPromises'|'richeseBidPromises'|'mixedRicheseBidQuestions'> = ['earlyRicheseBidPromises','phaseAnyBidPromises']; if (local) families.push('richeseBidPromises'); if (mixed) families.push('mixedRicheseBidQuestions'); for (const family of families) { const tampered = structuredClone(s.game); if (family === 'mixedRicheseBidQuestions') tampered.mixedRicheseBidQuestions![0].answer = false; else tampered[family]![0].minimum++; assert.throws(() => act(tampered,s.f.asker,{type:'semutaCommit',event:s.pending}), `The third answer binds its original ${family}`); } reject(s.game,s.f.asker,{type:'semutaCommit',event:'stale-discard'}); let game = claim ? act(s.game,s.f.asker,{type:'semutaCommit',event:s.pending}) : passTruthRicheseFreshDiscard(s.game); assert.equal(game.pendingTreacheryDiscard,null); assert.equal(game.truthtrance,null); assert.equal(player(game,s.f.asker).hand.some(card => card.id === s.f.truth),claim); assert.equal(game.discard.some(card => card.id === s.semuta),claim); assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === s.early.id && !row.boundEvent && !row.released)); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && !row.fulfilled && !row.released)); assert.deepEqual({lot:game.richeseAuction,round:game.richeseBidding,funding:game.richeseFunding,aid:game.aid, turn:game.turn,phase:game.phase,active:game.active,order:game.order},s.parent); if (method === 'onceAround') game = advance(game,state => clean(state) && state.richeseAuction?.active === s.f.emperor); const view = viewGame(game,s.f.emperor), guide = view.phaseAnyBidGuide?.action; assert.ok(guide); if (local) { assert.deepEqual(guide,view.richeseBidPromiseGuide?.action); assert.equal(guide.richeseBidPromiseId,local.id); assert.equal(guide.earlyRicheseBidPromiseId,s.early.id); reject(game,s.f.emperor,{...guide,richeseBidPromiseId:'stale-native'}); reject(game,s.f.emperor,{...guide,earlyRicheseBidPromiseId:'stale-early'}); } reject(game,s.f.emperor,{...guide,phaseAnyBidPromiseEvent:'stale-phase'}); reject(game,s.f.emperor,{...guide,event:'stale-market'}); game = act(game,s.f.emperor,guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && row.fulfilled)); if (local) assert.ok(game.richeseBidPromises?.some(row => row.id === local.id && row.fulfilled)); assert.deepEqual(inventory(game),s.f.originalCards); });