import assert from 'node:assert/strict'; import test from 'node:test'; import { advanceAuctionClock, synchronizeAuctionClock, viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { createTruthRicheseBidFixture } from './fixture-truth-richese-bid'; import { createTruthRicheseSilentFixture } from './fixture-truth-richese-silent'; import { actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance, createTruthtranceRichesePromisesFixture as baseFixture, passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player } from './fixture-truthtrance-richese-promises'; type Method = 'onceAround' | 'silent'; type Kind = 'richeseBid' | 'mixedRicheseBid'; type Answer = 'yes' | 'no'; type Fixture = ReturnType | ReturnType; function reject(game:Game, actor:string, action:Action) { const before = structuredClone(game); assert.throws(() => act(game,actor,action)); assert.deepEqual(game,before,'A rejected native action keeps its input unchanged'); } /** The starting games, sale and native funds are real. To obtain two questions * in one turn, move the second original Truthtrance from the unopened deck to * the same asker's hand; the underlying fixture stages its original deal, * unopened Spice order and native scalar rolls. */ function source(method:Method, advanced = false) { const f:Fixture = method === 'silent' ? createTruthRicheseSilentFixture(advanced) : createTruthRicheseBidFixture(advanced); let game = structuredClone(f.game); 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); if (method === 'onceAround') game = act(game,f.asker,{type:'richeseBid',event:f.lot,amount:null}); assert.ok(method === 'silent' || game.richeseAuction?.active === f.target); return {f,game,second:second.id}; } function ask(game:Game, f:Fixture, card:string, question:Record, answer:Answer) { let current = act(game,f.asker,{type:'card',card}); for (let n = 0; current.truthtrance?.stage === 'priority' && n < current.players.length; n++) { const actor = current.players.find(row => !current.truthtrance!.passed.includes(row.id)); assert.ok(actor); current = act(current,actor.id,{type:'truthPass'}); } assert.equal(current.truthtrance?.stage,'ask'); current = act(current,f.asker,{type:'truthAsk',question}); const view = viewGame(current,f.target); const options = question.kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers : question.kind === 'richeseBid' ? view.truthRicheseBidAnswers : view.truthMixedRicheseBidAnswers; assert.ok(options?.includes(answer),`The compatible ${String(question.kind)} ${answer} must be offered`); return passTruthRicheseFreshDiscard(act(current,f.target,{type:'truthAnswer',answer})); } function questions(f:Fixture, game:Game, kind:Kind, phaseAnswer:Answer, _lotAnswer:Answer) { // Every pair has a joint funded witness: Yes/Yes requires six; Yes/No and // No/Yes each permit four; No/No permits a two-spice bid or a pass. const phaseMinimum = phaseAnswer === 'yes' ? 4 : 6; const lotMinimum = phaseAnswer === 'yes' ? 6 : 4; const phase = {kind:'phaseAnyBid',target:f.target,turn:game.turn,minimum:phaseMinimum}; const lot = kind === 'richeseBid' ? {kind,target:f.target,turn:game.turn,event:f.lot,minimum:lotMinimum} : {kind,target:f.target,turn:game.turn,event:f.lot,mixed:{kind:'and',terms:[ {kind:'fact',fact:{kind:'spice',compare:'gte',value:10}}, {kind:'action',action:{minimum:lotMinimum}}, ]}}; return {phase,lot,phaseMinimum,lotMinimum}; } for (const method of ['onceAround','silent'] as const) for (const kind of ['richeseBid','mixedRicheseBid'] as const) for (const first of ['phase','lot'] as const) for (const phaseAnswer of ['yes','no'] as const) for (const lotAnswer of ['yes','no'] as const) void test( `Basic ${method} ${kind} ${first} first: phase ${phaseAnswer} and lot ${lotAnswer} share native bid`, () => { const f = source(method), q = questions(f.f,f.game,kind,phaseAnswer,lotAnswer); const firstQuestion = first === 'phase' ? q.phase : q.lot; const secondQuestion = first === 'phase' ? q.lot : q.phase; let game = ask(f.game,f.f,f.f.truth,firstQuestion,first === 'phase' ? phaseAnswer : lotAnswer); game = ask(game,f.f,f.second,secondQuestion,first === 'phase' ? lotAnswer : phaseAnswer); const phase = game.phaseAnyBidPromises?.find(row => row.player === f.f.target); assert.ok(phase); const native = game.richeseBidPromises?.find(row => row.player === f.f.target); assert.ok(native); assert.equal(phase.answer,phaseAnswer === 'yes'); assert.equal(native.answer,lotAnswer === 'yes'); const view = viewGame(game,f.f.target), phaseGuide = view.phaseAnyBidGuide?.action; const lotGuide = view.richeseBidPromiseGuide?.action; assert.ok(phaseGuide || lotGuide,'The combined answer has a native guide'); for (const guide of [phaseGuide,lotGuide]) if (guide) { assert.equal(guide.type,'richeseBid'); assert.doesNotThrow(() => act(game,f.f.target,guide),'Each offered guide must honor both answers'); } for (const difficulty of DIFFICULTIES) { const bot = viewGame(game,f.f.target); bot.players.find(row => row.id === f.f.target)!.bot = difficulty; const chosen = botActions(bot)[0]; assert.ok(chosen,difficulty); assert.equal(chosen.type,'richeseBid'); assert.doesNotThrow(() => act(game,f.f.target,chosen),difficulty); } const packet = phaseGuide ?? lotGuide!; if (packet.phaseAnyBidPromiseEvent !== undefined) reject(game,f.f.target,{...packet,phaseAnyBidPromiseEvent:'stale-phase-event'}); if (packet.richeseBidPromiseId !== undefined) reject(game,f.f.target,{...packet,richeseBidPromiseId:'stale-lot-promise'}); if (lotAnswer === 'yes') reject(game,f.f.target,{type:'richeseBid',event:f.f.lot,amount:method === 'silent' ? 0 : null}); if (phaseAnswer === 'no') reject(game,f.f.target,{type:'richeseBid',event:f.f.lot,amount:q.phaseMinimum,allyPayment:0}); if (lotAnswer === 'no') reject(game,f.f.target,{type:'richeseBid',event:f.f.lot,amount:q.lotMinimum,allyPayment:0}); const amount = lotAnswer === 'yes' ? q.lotMinimum : phaseAnswer === 'yes' ? q.phaseMinimum : 2; const before = player(game,f.f.target).spice; game = act(game,f.f.target,{type:'richeseBid',event:f.f.lot,amount,allyPayment:0}); assert.equal(player(game,f.f.target).spice,before,'A bid is not its eventual invoice'); assert.equal(!!game.phaseAnyBidPromises?.find(row => row.event === phase.event)?.fulfilled,phaseAnswer === 'yes'); const accepted = game.richeseBidPromises?.find(row => row.id === native.id); assert.ok(accepted?.fulfilled); if (method === 'silent') { assert.equal(viewGame(game,f.f.target).richeseAuction?.ownBid,amount); assert.equal(viewGame(game,f.f.owner).richeseAuction?.revealedBids,null); assert.equal(viewGame(game,f.f.owner).richeseAuction?.ownBid,null); game = act(game,f.f.asker,{type:'richeseBid',event:f.f.lot,amount:0,allyPayment:0}); game = act(game,f.f.owner,{type:'richeseBid',event:f.f.lot,amount:0,allyPayment:0}); } else if (game.richeseAuction?.event === f.f.lot && game.richeseAuction.active === f.f.owner) { game = act(game,f.f.owner,{type:'richeseBid',event:f.f.lot,amount:null}); } assert.ok(player(game,f.f.target).hand.some(card => card.id === f.f.card)); assert.equal(player(game,f.f.target).spice,before-amount); assert.deepEqual(inventory(game),f.f.originalCards); }); for (const method of ['onceAround','silent'] as const) for (const first of ['phase','lot'] as const) void test( `Advanced ${method} ${first} first preserves two independently answered Yes requirements`, () => { const f = source(method,true), q = questions(f.f,f.game,'richeseBid','yes','yes'); let game = ask(f.game,f.f,f.f.truth,first === 'phase' ? q.phase : q.lot,'yes'); game = ask(game,f.f,f.second,first === 'phase' ? q.lot : q.phase,'yes'); const before = player(game,f.f.target).spice; const guide = viewGame(game,f.f.target).phaseAnyBidGuide?.action ?? viewGame(game,f.f.target).richeseBidPromiseGuide?.action; assert.ok(guide); assert.equal(guide.type,'richeseBid'); assert.equal(guide.amount,6); game = act(game,f.f.target,guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.f.target && row.fulfilled)); assert.ok(game.richeseBidPromises?.some(row => row.player === f.f.target && row.fulfilled)); assert.equal(player(game,f.f.target).spice,before); if (method === 'silent') { assert.equal(viewGame(game,f.f.owner).richeseAuction?.revealedBids,null); game = act(game,f.f.asker,{type:'richeseBid',event:f.f.lot,amount:0,allyPayment:0}); game = act(game,f.f.owner,{type:'richeseBid',event:f.f.lot,amount:0,allyPayment:0}); } else if (game.richeseAuction?.event === f.f.lot && game.richeseAuction.active === f.f.owner) game = act(game,f.f.owner,{type:'richeseBid',event:f.f.lot,amount:null}); assert.ok(player(game,f.f.target).hand.some(card => card.id === f.f.card)); assert.equal(player(game,f.f.target).spice,before-6); assert.deepEqual(inventory(game),f.f.originalCards); }); for (const method of ['onceAround','silent'] as const) for (const first of ['phase','lot'] as const) void test( `${method} live lot No permits an all-auction Yes in the actual later ordinary auction, ${first} first`, () => { const f = source(method), phase = {kind:'phaseAnyBid',target:f.f.target,turn:f.game.turn,minimum:6}; const lot = {kind:'richeseBid',target:f.f.target,turn:f.game.turn,event:f.f.lot,minimum:4}; let game = ask(f.game,f.f,f.f.truth,first === 'phase' ? phase : lot,first === 'phase' ? 'yes' : 'no'); game = ask(game,f.f,f.second,first === 'phase' ? lot : phase,first === 'phase' ? 'no' : 'yes'); const guide = viewGame(game,f.f.target).phaseAnyBidGuide?.action ?? viewGame(game,f.f.target).richeseBidPromiseGuide?.action; assert.ok(guide); assert.doesNotThrow(() => act(game,f.f.target,guide)); if (typeof guide.amount === 'number') assert.ok(guide.amount < 4,'The current-lot No is never bypassed'); game = act(game,f.f.target,guide); const promise = game.phaseAnyBidPromises?.find(row => row.player === f.f.target); assert.ok(promise); assert.equal(!!promise.fulfilled,false); assert.equal(!!promise.released,false); game = advance(game,state => clean(state) && !!state.auction && state.auction.active === f.f.target, [], {position:'first',method}); const later = viewGame(game,f.f.target).phaseAnyBidGuide; assert.ok(later); assert.equal(later.action.type,'bid'); assert.ok(Number(later.action.amount) >= 6); game = act(game,f.f.target,later.action); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === promise.event && row.fulfilled)); assert.deepEqual(inventory(game),f.f.originalCards); }); void test('legacy ordinary-only No does not restrict the physical Richese cache bid', () => { const f = baseFixture({advanced:false}); let game = act(structuredClone(f.earlySource),f.asker,f.declarationStep.action); game = advance(game,state => state.truthtrance?.stage === 'ask'); game = act(game,f.asker,{type:'truthAsk',question:{kind:'phaseBid',target:f.emperor,turn:game.turn,minimum:4}}); game = passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer:'no'})); game = advance(game,state => clean(state) && !!state.richeseAuction && !state.richeseAuction.outcome, [], {position:'first',method:'silent'}); const lot = game.richeseAuction!; game = act(game,f.emperor,{type:'richeseBid',event:lot.event,amount:4,allyPayment:0}); const legacy = game.phaseBidPromises?.find(row => row.player === f.emperor); assert.ok(legacy); assert.equal(legacy.answer,false); assert.equal(!!legacy.fulfilled,false); assert.equal(!!legacy.released,false); assert.deepEqual(inventory(game),f.originalCards); }); for (const advanced of [false,true]) for (const method of ['onceAround','silent'] as const) for (const first of ['phase','lot'] as const) for (const claim of [false,true]) void test( `${advanced ? 'Advanced' : 'Basic'} ${method} ${first} first: second queued Truth offers Semuta ${claim ? 'claim' : 'pass'}`, () => { const f = source(method,advanced), cache = f.game.richeseCache!; const at = cache.findIndex(card => card.effect === 'semutaDrug'); assert.ok(at >= 0); // Additional conserved supplement: move the original Semuta from the // cache to Richese's hand; the native offered cache card is untouched. const [semuta] = cache.splice(at,1); player(f.game,f.f.owner).hand.push(semuta); assert.deepEqual(inventory(f.game),f.f.originalCards); const lot = structuredClone(f.game.richeseAuction), funding = structuredClone(f.game.richeseFunding); const q = questions(f.f,f.game,'richeseBid','yes','yes'); let game = act(f.game,f.f.asker,{type:'card',card:f.f.truth,cards:[f.second]}); for (let n = 0; game.truthtrance?.stage === 'priority' && n < game.players.length; n++) { const actor = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(actor); game = act(game,actor.id,{type:'truthPass'}); } assert.equal(game.truthtrance?.stage,'ask'); assert.deepEqual(game.truthtrance?.queue.map(row => row.card),[f.f.truth,f.second]); game = act(game,f.f.asker,{type:'truthAsk',question:first === 'phase' ? q.phase : q.lot}); game = act(game,f.f.target,{type:'truthAnswer',answer:'yes'}); assert.equal(game.pendingTreacheryDiscard?.continuation.kind,'truthtranceDiscard'); game = passTruthRicheseFreshDiscard(game); assert.equal(game.truthtrance?.stage,'ask'); assert.deepEqual(game.truthtrance?.queue.map(row => row.card),[f.second]); game = act(game,f.f.asker,{type:'truthAsk',question:first === 'phase' ? q.lot : q.phase}); game = act(game,f.f.target,{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,f.second); assert.equal(viewGame(game,f.f.owner).semutaReaction?.canCommit,true); const phase = structuredClone(game.phaseAnyBidPromises), native = structuredClone(game.richeseBidPromises); assert.deepEqual(game.richeseAuction,lot); assert.deepEqual(game.richeseFunding,funding); game = claim ? act(game,f.f.owner,{type:'semutaCommit',event:pending.batch.event}) : passTruthRicheseFreshDiscard(game); assert.equal(game.pendingTreacheryDiscard,null); assert.equal(game.truthtrance,null); assert.deepEqual(game.phaseAnyBidPromises,phase); assert.deepEqual(game.richeseBidPromises,native); assert.deepEqual(game.richeseAuction,lot); assert.deepEqual(game.richeseFunding,funding); assert.equal(player(game,f.f.owner).hand.some(card => card.id === f.second),claim); assert.equal(game.discard.some(card => card.id === semuta.id),claim); const guide = viewGame(game,f.f.target).phaseAnyBidGuide?.action ?? viewGame(game,f.f.target).richeseBidPromiseGuide?.action; assert.ok(guide); assert.equal(guide.amount,6); game = act(game,f.f.target,guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.f.target && row.fulfilled)); assert.ok(game.richeseBidPromises?.some(row => row.player === f.f.target && row.fulfilled)); assert.deepEqual(inventory(game),f.f.originalCards); }); void test('an opposing raise releases the impossible local Yes but preserves and later fulfills phase-wide Yes', () => { const f = createTruthRicheseBidFixture(false); let game = structuredClone(f.game); // The original second Truth is moved from the unopened deck, without // injecting a bid, wallet, promise or later auction state. 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); assert.equal(game.richeseAuction?.active,f.asker); game = ask(game,f,f.truth,{kind:'phaseAnyBid',target:f.target,turn:game.turn,minimum:4},'yes'); game = ask(game,f,second.id,{kind:'richeseBid',target:f.target,turn:game.turn,event:f.lot,minimum:4},'yes'); const lot = game.richeseBidPromises?.find(row => row.player === f.target); assert.ok(lot); const phase = game.phaseAnyBidPromises?.find(row => row.player === f.target); assert.ok(phase); game = act(game,f.asker,{type:'richeseBid',event:f.lot,amount:10,allyPayment:0}); assert.equal(game.richeseAuction?.active,f.target); const released = game.richeseBidPromises?.find(row => row.id === lot.id); assert.ok(released); assert.equal(!!released.released,true); assert.equal(!!released.fulfilled,false); const retained = game.phaseAnyBidPromises?.find(row => row.event === phase.event); assert.ok(retained); assert.equal(!!retained.released,false); assert.equal(!!retained.fulfilled,false); game = act(game,f.target,{type:'richeseBid',event:f.lot,amount:null}); game = advance(game,state => clean(state) && !!state.auction && state.auction.active === f.target, [], {position:'first',method:'onceAround'}); const guide = viewGame(game,f.target).phaseAnyBidGuide; assert.ok(guide); assert.equal(guide.action.type,'bid'); assert.ok(Number(guide.action.amount) >= 4); game = act(game,f.target,guide.action); assert.ok(game.phaseAnyBidPromises?.some(row => row.event === phase.event && row.fulfilled)); assert.ok(game.richeseBidPromises?.some(row => row.id === lot.id && row.released)); assert.deepEqual(inventory(game),f.originalCards); }); /** Genuine last-position cache, preceded by a real ordinary purchase. The * undealt second Truth is the only physical custody supplement. */ void test('final Once Around cache clock executes one bid satisfying both live Yes answers', () => { const f = baseFixture({advanced:false}), options = {position:'last' as const,method:'onceAround' as const}; let game = advance(structuredClone(f.earlySource),state => clean(state) && !!state.auction,[],options); const secondIndex = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(secondIndex >= 0); const [second] = game.deck.splice(secondIndex,1); player(game,f.asker).hand.push(second); assert.deepEqual(inventory(game),f.originalCards); game = act(game,f.richese,{type:'bid',amount:1,allyPayment:0}); game = advance(game,state => clean(state) && !!state.richeseAuction && !state.richeseAuction.outcome,[],options); const lot = game.richeseAuction!; game = act(game,f.emperor,{type:'richeseBid',event:lot.event,amount:1,allyPayment:0}); assert.equal(game.richeseAuction?.active,f.asker); const sf = {game,asker:f.asker,target:f.richese,owner:f.richese,lot:lot.event,card:lot.cardId,truth:f.truth, original:f,originalCards:f.originalCards,staging:f.staging}; game = ask(game,sf,f.truth,{kind:'phaseAnyBid',target:f.richese,turn:game.turn,minimum:3},'yes'); game = ask(game,sf,second.id,{kind:'richeseBid',target:f.richese,turn:game.turn,event:lot.event,minimum:4},'yes'); game = act(game,f.asker,{type:'richeseBid',event:lot.event,amount:null}); assert.equal(game.richeseAuction?.active,f.richese); reject(game,f.richese,{type:'richeseBid',event:lot.event,amount:null}); const guide = viewGame(game,f.richese).phaseAnyBidGuide?.action ?? viewGame(game,f.richese).richeseBidPromiseGuide?.action; assert.ok(guide); assert.equal(guide.amount,4); const armed = synchronizeAuctionClock(game,1_000,'joint-richese:0'); const expired = advanceAuctionClock(armed,11_000,'joint-richese:0'); assert.ok(expired.phaseAnyBidPromises?.some(row => row.player === f.richese && row.fulfilled)); assert.ok(expired.richeseBidPromises?.some(row => row.player === f.richese && row.fulfilled)); assert.deepEqual(inventory(expired),f.originalCards); });