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 Position = 'first' | 'last'; /** The genuine Advanced R/A/E fixture selects only unopened deal/Spice order. * Joint questions stage the second original Truthtrance from the unopened deck; * the near-full-hand case stages two original cards from there. No wallet, * promise, bid, phase or auction is installed. */ function source(joint = false, nearFull = false) { const f = fixture({advanced:true}); const game = structuredClone(f.earlySource); let second: string | undefined; if (joint) { const index = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(index >= 0); const [card] = game.deck.splice(index,1); second = card.id; player(game,f.asker).hand.push(card); } if (nearFull) { for (let i = 0; i < 2; i++) { const index = game.deck.findIndex(card => card.effect !== 'truthtrance'); assert.ok(index >= 0); const [card] = game.deck.splice(index,1); player(game,f.emperor).hand.push(card); } } assert.deepEqual(inventory(game),f.originalCards); return {f,game,second}; } function answer(game:Game, f:TruthtranceRichesePromisesFixture, card:string, kind:'earlyRicheseBid'|'phaseAnyBid', 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 choices = kind === 'earlyRicheseBid' ? view.truthEarlyRicheseBidAnswers : view.truthPhaseAnyBidAnswers; assert.ok(choices?.includes('yes'), `${kind} must admit the funded Yes`); return passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer:'yes'})); } function begin(joint = false, nearFull = false) { const f = source(joint,nearFull); let game = answer(f.game,f.f,f.f.truth,'earlyRicheseBid',4); if (f.second) game = answer(game,f.f,f.second,'phaseAnyBid',6); const early = game.earlyRicheseBidPromises?.find(row => row.player === f.f.emperor); assert.ok(early); const phase = joint ? game.phaseAnyBidPromises?.find(row => row.player === f.f.emperor) : undefined; if (joint) assert.ok(phase); assert.deepEqual(inventory(game),f.f.originalCards); return {f:f.f,game,early,phase}; } 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 reachBid(game:Game, f:TruthtranceRichesePromisesFixture, method:Method) { if (method === 'onceAround') game = advance(game,state => clean(state) && state.richeseAuction?.active === f.emperor); return game; } function reject(game:Game, actor:string, action:Action) { const original = structuredClone(game); assert.throws(() => act(game,actor,action)); assert.deepEqual(game,original,'A rejected auction action keeps its original receipt and funds'); } function closeMarket(game:Game, f:TruthtranceRichesePromisesFixture, method:Method) { const event = game.richeseAuction!.event; 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 advance(game,state => player(state,f.emperor).hand.some(card => card.id === f.targetCard)); } function completeCache(game:Game, f:TruthtranceRichesePromisesFixture, id:string, method:Method, position:Position) { game = advance(game,state => clean(state) && gameIsCache(state,method),[],{position,method}); const event = game.richeseAuction!.event, card = game.richeseAuction!.cardId; if (method === 'onceAround') game = advance(game,state => clean(state) && state.richeseAuction?.active === f.emperor); assert.equal(game.earlyRicheseBidPromises?.find(row => row.id === id)?.boundEvent,event); const guide = viewGame(game,f.emperor).richeseBidPromiseGuide; assert.ok(guide); assert.equal(guide.action.event,event); assert.equal(guide.action.amount,4); const spice = player(game,f.emperor).spice; game = act(game,f.emperor,guide.action); assert.equal(player(game,f.emperor).spice,spice,'The accepted bid is not prepaid'); assert.ok(game.richeseBidPromises?.some(row => row.id === id && row.fulfilled)); game = advance(game,state => player(state,f.emperor).hand.some(item => item.id === card),[],{position,method}); assert.equal(player(game,f.emperor).spice,spice-4); assert.deepEqual(inventory(game),f.originalCards); return game; } function gameIsCache(game:Game, method:Method) { return !!game.richeseAuction && game.richeseAuction.source === 'cache' && game.richeseAuction.method === method && !game.richeseAuction.outcome; } for (const method of ['onceAround','silent'] as const) for (const position of ['first','last'] as const) void test( `Advanced ${position} ${method}: Black Market purchase reserves the funded later cache`, () => { const f = begin(true), before = player(f.game,f.f.emperor).spice; assert.equal(before,10); let game = reachBid(openMarket(f.game,f.f,method),f.f,method); const event = game.richeseAuction!.event; reject(game,f.f.emperor,{type:'richeseBid',event,amount:7,allyPayment:0}); game = act(game,f.f.emperor,{type:'richeseBid',event,amount:6,allyPayment:0}); assert.equal(player(game,f.f.emperor).spice,before); assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === f.early.id)?.released); assert.ok(game.phaseAnyBidPromises?.find(row => row.event === f.phase!.event)?.fulfilled); if (method === 'silent') { assert.equal(viewGame(game,f.f.asker).richeseAuction?.revealedBids,null); assert.equal(viewGame(game,f.f.asker).richeseAuction?.ownBid,null); } game = closeMarket(game,f.f,method); assert.equal(player(game,f.f.emperor).spice,before-6); assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === f.early.id)?.released); assert.deepEqual(inventory(game),f.f.originalCards); completeCache(game,f.f,f.early.id,method,position); }); for (const method of ['onceAround','silent'] as const) void test( `Advanced ${method}: a Black Market win cannot consume the final cache hand slot`, () => { const f = begin(false,true); let game = reachBid(openMarket(f.game,f.f,method),f.f,method); const event = game.richeseAuction!.event; assert.equal(player(game,f.f.emperor).hand.length,3); reject(game,f.f.emperor,{type:'richeseBid',event,amount:1,allyPayment:0}); game = act(game,f.f.emperor,{type:'richeseBid',event,amount:method === 'silent' ? 0 : null,allyPayment:0}); 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}); } assert.ok(player(game,f.f.richese).hand.some(card => card.id === f.f.targetCard)); assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === f.early.id)?.released); completeCache(game,f.f,f.early.id,method,'first'); }); void test('an opponent winning the Advanced Black Market preserves the respondent’s later cache Yes', () => { const f = begin(), method:Method = 'silent'; let game = openMarket(f.game,f.f,method); const event = game.richeseAuction!.event, spice = player(game,f.f.emperor).spice; game = act(game,f.f.emperor,{type:'richeseBid',event,amount:0,allyPayment:0}); game = act(game,f.f.asker,{type:'richeseBid',event,amount:1,allyPayment:0}); game = act(game,f.f.richese,{type:'richeseBid',event,amount:0,allyPayment:0}); game = advance(game,state => player(state,f.f.asker).hand.some(card => card.id === f.f.targetCard)); assert.equal(player(game,f.f.emperor).spice,spice); assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === f.early.id)?.released); completeCache(game,f.f,f.early.id,method,'first'); });