import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; 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 EarlyAnswer = 'yes' | 'no'; type Kind = 'earlyRicheseBid' | 'phaseAnyBid'; /** Original public Advanced R/A/E setup. The original second Truthtrance moves * from the unopened deck to Atreides and the original Semuta from Richese's * cache to its hand. The actual first Truthtrance is reclaimed by Richese via * Semuta, then played as the third question; no receipt, wallet, bid, auction, * phase or card is invented. */ function source() { const f = fixture({advanced:true}), game = structuredClone(f.earlySource); const at = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(at >= 0); const [second] = game.deck.splice(at,1); player(game,f.asker).hand.push(second); const semutaAt = game.richeseCache!.findIndex(card => card.effect === 'semutaDrug'); assert.ok(semutaAt >= 0); const [semuta] = game.richeseCache!.splice(semutaAt,1); player(game,f.richese).hand.push(semuta); assert.deepEqual(inventory(game),f.originalCards); return {f,game,second:second.id,semuta:semuta.id}; } function openQuestion(game:Game, asker:string, card:string) { game = act(game,asker,{type:'card',card}); return advance(game,state => state.truthtrance?.stage === 'ask'); } function answerEarly(game:Game, f:TruthtranceRichesePromisesFixture, card:string, kind:Kind, minimum:number, answer:EarlyAnswer, claim:boolean) { game = openQuestion(game,f.asker,card); 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 === 'earlyRicheseBid' ? view.truthEarlyRicheseBidAnswers : view.truthPhaseAnyBidAnswers; assert.ok(options?.includes(answer),`${kind} ${answer} must have a funded native witness`); game = act(game,f.emperor,{type:'truthAnswer',answer}); if (!claim) return passTruthRicheseFreshDiscard(game); const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction); assert.ok(viewGame(game,f.richese).semutaReaction?.canCommit); game = act(game,f.richese,{type:'semutaCommit',event:pending.batch.event}); assert.ok(player(game,f.richese).hand.some(item => item.id === card)); return game; } function openMarket(game:Game,f:TruthtranceRichesePromisesFixture,method:Method) { game = advance(game,state => state.decision?.kind === 'richeseBlackMarket'); assert.ok(player(game,f.richese).hand.some(card => card.id === f.targetCard)); game = act(game,f.richese,{type:'decision',event:game.richeseBidding!.event, card: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'}); } assert.equal(game.richeseAuction?.source,'blackMarket'); assert.equal(game.richeseAuction?.method,method); return game; } function prebound(method:Method, first:Kind, early:EarlyAnswer, earlyMinimum:number, phaseMinimum:number) { const s = source(), secondKind:Kind = first === 'earlyRicheseBid' ? 'phaseAnyBid' : 'earlyRicheseBid'; const value = (kind:Kind) => kind === 'earlyRicheseBid' ? early : 'yes'; const minimum = (kind:Kind) => kind === 'earlyRicheseBid' ? earlyMinimum : phaseMinimum; let game = answerEarly(s.game,s.f,s.f.truth,first,minimum(first),value(first),true); assert.ok(game.discard.some(card => card.id === s.semuta)); game = answerEarly(game,s.f,s.second,secondKind,minimum(secondKind),value(secondKind),false); const earlyRow = game.earlyRicheseBidPromises?.find(row => row.player === s.f.emperor); assert.ok(earlyRow); const phase = game.phaseAnyBidPromises?.find(row => row.player === s.f.emperor); assert.ok(phase); game = openMarket(game,s.f,method); assert.equal(player(game,s.f.emperor).spice,10); assert.deepEqual(inventory(game),s.f.originalCards); return {...s,game,earlyRow,phase}; } function askCurrent(game:Game,f:TruthtranceRichesePromisesFixture,minimum:number) { const lot = game.richeseAuction; assert.ok(lot); assert.ok(!lot.acted.includes(f.emperor),'Respondent must retain its original market opportunity'); game = openQuestion(game,f.richese,f.truth); game = act(game,f.richese,{type:'truthAsk',question:{kind:'richeseBid',target:f.emperor, turn:game.turn,event:lot.event,minimum}}); return game; } function reject(game:Game,actor:string,action:Action) { const before = structuredClone(game); assert.throws(() => act(game,actor,action)); assert.deepEqual(game,before,'An invalid action must retain both physical and promise state'); } function bindCurrent(game:Game,f:TruthtranceRichesePromisesFixture,answer:EarlyAnswer) { assert.ok(viewGame(game,f.emperor).truthRicheseBidAnswers?.includes(answer), `A funded current market ${answer} must be available`); game = passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer})); const native = game.richeseBidPromises?.find(row => row.player === f.emperor && row.event === game.richeseAuction?.event); assert.ok(native); assert.equal(native.answer,answer === 'yes'); return {game,native}; } function reachMarketBid(game:Game,f:TruthtranceRichesePromisesFixture,method:Method) { return method === 'silent' ? game : advance(game,state => clean(state) && state.richeseAuction?.active === f.emperor); } function finishMarket(game:Game,method:Method,event:string) { while (game.richeseAuction?.event === event && !game.richeseAuction.outcome) { const lot = game.richeseAuction; const actor = method === 'silent' ? lot.eligible.find(id => !Object.hasOwn(lot.sealed,id)) : lot.active; assert.ok(actor); game = act(game,actor,{type:'richeseBid',event,amount:method === 'silent' ? 0 : null,allyPayment:0}); } return game; } for (const method of ['silent','onceAround'] as const) for (const first of ['earlyRicheseBid','phaseAnyBid'] as const) void test( `Advanced ${method} ${first} first: real market Yes and phase Yes reserve the early cache Yes`, () => { const s = prebound(method,first,'yes',6,4); let game = askCurrent(s.game,s.f,4); const current = bindCurrent(game,s.f,'yes'); game = reachMarketBid(current.game,s.f,method); const lot = game.richeseAuction!, view = viewGame(game,s.f.emperor); const nativeGuide = view.richeseBidPromiseGuide?.action, phaseGuide = view.phaseAnyBidGuide?.action; assert.ok(nativeGuide && phaseGuide); assert.deepEqual(nativeGuide,phaseGuide); assert.equal(nativeGuide.type,'richeseBid'); assert.equal(nativeGuide.event,lot.event); assert.equal(nativeGuide.amount,4); assert.equal(nativeGuide.richeseBidPromiseId,current.native.id); assert.equal(nativeGuide.earlyRicheseBidPromiseId,s.earlyRow.id); assert.equal(nativeGuide.phaseAnyBidPromiseEvent,s.phase.event); assert.equal(nativeGuide.phaseAnyBidLotEvent,lot.event); for (const bad of [ {...nativeGuide,richeseBidPromiseId:'stale-native'}, {...nativeGuide,earlyRicheseBidPromiseId:'stale-early'}, {...nativeGuide,phaseAnyBidPromiseEvent:'stale-phase'}, {...nativeGuide,event:'stale-lot'}, ]) reject(game,s.f.emperor,bad); reject(game,s.f.emperor,{type:'richeseBid',event:lot.event,amount:5,allyPayment:0}); for (const difficulty of DIFFICULTIES) { const bot = viewGame(game,s.f.emperor); bot.players.find(row => row.id === s.f.emperor)!.bot = difficulty; const action = botActions(bot)[0]; assert.ok(action,difficulty); assert.doesNotThrow(() => act(game,s.f.emperor,action),difficulty); } game = act(game,s.f.emperor,nativeGuide); assert.equal(player(game,s.f.emperor).spice,10,'The bid is reserved, not prepaid'); assert.ok(game.richeseBidPromises?.some(row => row.id === current.native.id && row.fulfilled)); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && row.fulfilled)); assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === s.earlyRow.id && !row.boundEvent && !row.released)); if (method === 'silent') assert.equal(viewGame(game,s.f.asker).richeseAuction?.revealedBids,null); game = finishMarket(game,method,lot.event); game = advance(game,state => player(state,s.f.emperor).hand.some(card => card.id === s.f.targetCard)); assert.equal(player(game,s.f.emperor).spice,6); game = advance(game,state => clean(state) && state.richeseAuction?.source === 'cache' && state.richeseAuction.method === 'silent' && !state.richeseAuction.outcome,[],{position:'first',method:'silent'}); assert.equal(game.earlyRicheseBidPromises?.find(row => row.id === s.earlyRow.id)?.boundEvent,game.richeseAuction!.event); const cache = viewGame(game,s.f.emperor).richeseBidPromiseGuide?.action; assert.ok(cache); assert.equal(cache.amount,6); game = act(game,s.f.emperor,cache); assert.ok(game.richeseBidPromises?.some(row => row.id === s.earlyRow.id && row.fulfilled)); const cacheCard = game.richeseAuction!.cardId; game = advance(game,state => player(state,s.f.emperor).hand.some(card => card.id === cacheCard), [],{position:'first',method:'silent'}); assert.equal(player(game,s.f.emperor).spice,0); assert.deepEqual(inventory(game),s.f.originalCards); }); for (const method of ['silent','onceAround'] as const) void test( `Advanced ${method}: unfunded market Yes is unavailable, while No preserves a real phase bid`, () => { const s = prebound(method,'earlyRicheseBid','yes',6,4); let game = askCurrent(s.game,s.f,6); const answers = viewGame(game,s.f.emperor).truthRicheseBidAnswers; assert.ok(!answers?.includes('yes'),'Market 6 and cache 6 cannot both be funded from 10'); assert.ok(answers?.includes('no')); reject(game,s.f.emperor,{type:'truthAnswer',answer:'yes'}); const current = bindCurrent(game,s.f,'no'); game = reachMarketBid(current.game,s.f,method); const lot = game.richeseAuction!, guide = viewGame(game,s.f.emperor).phaseAnyBidGuide?.action; assert.ok(guide); assert.equal(guide.type,'richeseBid'); assert.equal(guide.amount,4); assert.equal(guide.richeseBidPromiseId,current.native.id); assert.equal(guide.earlyRicheseBidPromiseId,s.earlyRow.id); game = act(game,s.f.emperor,guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && row.fulfilled)); assert.ok(game.richeseBidPromises?.some(row => row.id === current.native.id && row.fulfilled)); game = finishMarket(game,method,lot.event); game = advance(game,state => player(state,s.f.emperor).hand.some(card => card.id === s.f.targetCard)); game = advance(game,state => clean(state) && state.richeseAuction?.source === 'cache' && state.richeseAuction.method === 'silent' && !state.richeseAuction.outcome,[],{position:'first',method:'silent'}); const cache = viewGame(game,s.f.emperor).richeseBidPromiseGuide?.action; assert.ok(cache); assert.equal(cache.amount,6); game = act(game,s.f.emperor,cache); assert.ok(game.richeseBidPromises?.some(row => row.id === s.earlyRow.id && row.fulfilled)); assert.deepEqual(inventory(game),s.f.originalCards); }); for (const method of ['silent','onceAround'] as const) void test( `Advanced ${method}: current and early No retain a separately funded later phase Yes`, () => { const s = prebound(method,'phaseAnyBid','no',4,6); let game = askCurrent(s.game,s.f,6); const current = bindCurrent(game,s.f,'no'); game = reachMarketBid(current.game,s.f,method); const lot = game.richeseAuction!, guide = viewGame(game,s.f.emperor).phaseAnyBidGuide?.action; assert.ok(guide); assert.equal(guide.type,'richeseBid'); assert.ok(guide.amount === null || typeof guide.amount === 'number' && guide.amount < 6); assert.equal(guide.richeseBidPromiseId,current.native.id); assert.equal(guide.earlyRicheseBidPromiseId,s.earlyRow.id); game = act(game,s.f.emperor,guide); assert.ok(game.richeseBidPromises?.some(row => row.id === current.native.id && row.fulfilled)); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && !row.fulfilled && !row.released)); game = finishMarket(game,method,lot.event); game = advance(game,state => clean(state) && !!state.auction && state.auction.active === s.f.emperor, [],{position:'first',method:'silent'}); assert.ok(game.richeseBidPromises?.some(row => row.id === s.earlyRow.id && row.fulfilled)); const ordinary = viewGame(game,s.f.emperor).phaseAnyBidGuide?.action; assert.ok(ordinary); assert.equal(ordinary.type,'bid'); assert.equal(ordinary.amount,6); game = act(game,s.f.emperor,ordinary); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === s.phase.event && row.fulfilled)); assert.deepEqual(inventory(game),s.f.originalCards); }); void test('Advanced current Yes with an early Yes needs both market and cache funding even without a phase promise', () => { const s = source(); let game = answerEarly(s.game,s.f,s.f.truth,'earlyRicheseBid',6,'yes',true); game = openMarket(game,s.f,'silent'); game = askCurrent(game,s.f,4); const current = bindCurrent(game,s.f,'yes'); game = current.game; const early = game.earlyRicheseBidPromises?.find(row => row.player === s.f.emperor); assert.ok(early); const lot = game.richeseAuction!, guide = viewGame(game,s.f.emperor).richeseBidPromiseGuide?.action; assert.ok(guide); assert.equal(guide.amount,4); assert.equal(guide.richeseBidPromiseId,current.native.id); assert.equal(guide.earlyRicheseBidPromiseId,early.id); reject(game,s.f.emperor,{...guide,earlyRicheseBidPromiseId:'stale-early'}); reject(game,s.f.emperor,{type:'richeseBid',event:lot.event,amount:5,allyPayment:0}); game = act(game,s.f.emperor,guide); assert.ok(game.richeseBidPromises?.some(row => row.id === current.native.id && row.fulfilled)); assert.ok(game.earlyRicheseBidPromises?.some(row => row.id === early.id && !row.released)); assert.deepEqual(inventory(game),s.f.originalCards); });