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 { createTruthtranceRichesePromisesFixture, advanceTruthtranceRichesePromises as advance, actTruthtranceRichesePromises as act, truthtranceRichesePromisesInventory as inventory } from './fixture-truthtrance-richese-promises'; import { truthOrderFrame as frame, playSaphoTruthOrder as play, finishTruthOrder as finish, finishOriginalTruthQuestions as answer, rejectSaphoTruthOrder as reject, passFreshDiscard } from './fixture-sapho-truth-order'; const player = (game: Game, id: string) => { const p = game.players.find(row => row.id === id); assert.ok(p); return p; }; type Method = 'normal' | 'onceAround' | 'silent'; /** Genuine Advanced R/A/E setup and live Black Market. Controlled conserved * supplements swap original cached Sapho for Emperor's held card and move an * undealt physical Truth to Emperor; the actual starting Truth stays Atreides'. */ function fixture(method: Method) { const source = createTruthtranceRichesePromisesFixture({advanced:true}); let game = structuredClone(source.game); const emperor = player(game,'emperor'), atreides = player(game,'atreides'); const cache = game.richeseCache!; const at = cache.findIndex(card => card.effect === 'juiceOfSapho'); assert.ok(at >= 0); const [sapho] = cache.splice(at,1), [swap] = emperor.hand.splice(0,1); emperor.hand.push(sapho); cache.push(swap); const truthAt = game.deck.findIndex(card => card.effect === 'truthtrance'); assert.ok(truthAt >= 0); const [truth] = game.deck.splice(truthAt,1); emperor.hand.push(truth); const otherTruth = atreides.hand.find(card => card.effect === 'truthtrance'); assert.ok(otherTruth); assert.deepEqual(inventory(game),source.originalCards); game = advance(game,state => state.decision?.kind === 'richeseBlackMarket'); const offered = player(game,'richese').hand.find(card => card.name === 'Shield'); assert.ok(offered); game = act(game,'richese',{type:'decision',event:game.richeseBidding!.event,card:offered.id,method, ...(method === 'onceAround' ? {direction:'clockwise'} : {})}); for (let n=0; game.response && n<20; n++) { const actor = game.players.find(row => !game.response!.passed.includes(row.id)); assert.ok(actor); game = act(game,actor.id,{type:'passResponse'}); } const lot = game.richeseAuction; assert.ok(lot && lot.source === 'blackMarket' && lot.method === method); const bidder = method === 'silent' ? 'richese' : lot.active; assert.ok(bidder); game = act(game,bidder,{type:'richeseBid',event:lot.event,amount:1,allyPayment:0}); assert.ok(game.richeseAuction && !game.richeseAuction.outcome); const parent = structuredClone({turn:game.turn,phase:game.phase,active:game.active,order:game.order, lot:game.richeseAuction,round:game.richeseBidding,funding:game.richeseFunding,aid:game.aid, offered:game.richeseOfferedCard,sellerHand:player(game,'richese').hand}); for (const [actor,card] of [['emperor',truth.id],['atreides',otherTruth.id]] as const) game = act(game,actor,{type:'card',card}); while (game.truthtrance?.stage === 'priority') { const actor = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(actor); game = act(game,actor.id,{type:'truthPass'}); } const current = frame(game); const mode: 'first' | 'last' = game.truthtrance!.queue[0].player === 'emperor' ? 'last' : 'first'; return {game,lot:lot.event,offered:offered.id,card:sapho.id,bidder,mode,parent,original:source.originalCards,current}; } function assertParent(game: Game, f: ReturnType) { assert.deepEqual({turn:game.turn,phase:game.phase,active:game.active,order:game.order, lot:game.richeseAuction,round:game.richeseBidding,funding:game.richeseFunding,aid:game.aid, offered:game.richeseOfferedCard,sellerHand:player(game,'richese').hand},f.parent); assert.ok(player(game,'richese').hand.some(card => card.id === f.offered)); } function assertSilent(game: Game, bidder: string) { const ownerView = viewGame(game,bidder).richeseAuction!, otherView = viewGame(game,'emperor').richeseAuction!; assert.equal(ownerView.ownBid,1); assert.equal(otherView.ownBid,null); assert.equal(otherView.revealedBids,null); assert.equal('sealed' in otherView,false); } for (const method of ['normal','onceAround','silent'] as const) void test( `Advanced ${method} Black Market retains its original lot when Sapho orders real Truth questions`, () => { const f = fixture(method), before = structuredClone(f.game); assertParent(f.game,f); if (method === 'silent') assertSilent(f.game,f.bidder); const offered = viewGame(f.game,'emperor').saphoTruthOrderChoices; assert.ok(offered.some(row => row.card === f.card && row.mode === f.mode)); reject(f.game,'emperor',{type:'saphoTruthOrder',card:f.card,event:'stale',revision:f.current.revision,mode:f.mode}); reject(f.game,'emperor',{type:'saphoTruthOrder',card:f.card,event:f.current.event,revision:f.current.revision+1,mode:f.mode}); for (const difficulty of DIFFICULTIES) { const view = viewGame(before,'emperor'); view.players.find(row => row.id === 'emperor')!.bot = difficulty; const action = botActions(view)[0]; assert.ok(action,difficulty); assert.ok(action.type === 'truthOrderPass' || action.type === 'saphoTruthOrder'); assert.doesNotThrow(() => act(before,'emperor',action)); } let game = play(f.game,'emperor',f.card,f.mode); assertParent(game,f); if (method === 'silent') assertSilent(game,f.bidder); game = passFreshDiscard(game); game = finish(game); assert.equal(f.mode === 'first' ? game.truthtrance!.queue[0].player : game.truthtrance!.queue.at(-1)!.player,'emperor'); game = answer(game); assert.equal(game.truthtrance,null); assertParent(game,f); assert.deepEqual(inventory(game),f.original); if (method === 'silent') assertSilent(game,f.bidder); const lot = game.richeseAuction!; const next = method === 'silent' ? lot.eligible.find(id => !Object.hasOwn(lot.sealed,id)) : lot.active; assert.ok(next); const action: Action = {type:'richeseBid',event:f.lot,amount:method === 'silent' ? 0 : null,allyPayment:0}; game = act(game,next,action); assert.deepEqual(inventory(game),f.original); }); void test('a Black Market seller cannot spend their offered Sapho to reorder their own queued Truth', () => { const source = createTruthtranceRichesePromisesFixture({advanced:true}); let game = structuredClone(source.game); const seller = player(game,'richese'), cache = game.richeseCache!; const at = cache.findIndex(card => card.effect === 'juiceOfSapho'); assert.ok(at >= 0); const [sapho] = cache.splice(at,1), [swap] = seller.hand.splice(0,1); seller.hand.push(sapho); cache.push(swap); const truthAt = game.deck.findIndex(card => card.effect === 'truthtrance'); assert.ok(truthAt >= 0); const [sellerTruth] = game.deck.splice(truthAt,1); seller.hand.push(sellerTruth); const otherTruth = player(game,'atreides').hand.find(card => card.effect === 'truthtrance'); assert.ok(otherTruth); assert.deepEqual(inventory(game),source.originalCards); game = advance(game,state => state.decision?.kind === 'richeseBlackMarket'); game = act(game,'richese',{type:'decision',event:game.richeseBidding!.event,card:sapho.id,method:'silent'}); for (let n=0; game.response && n<20; n++) { const actor = game.players.find(row => !game.response!.passed.includes(row.id)); assert.ok(actor); game = act(game,actor.id,{type:'passResponse'}); } const lot = game.richeseAuction; assert.ok(lot && lot.cardId === sapho.id); game = act(game,'emperor',{type:'richeseBid',event:lot.event,amount:1,allyPayment:0}); for (const [actor,card] of [['richese',sellerTruth.id],['atreides',otherTruth.id]] as const) game = act(game,actor,{type:'card',card}); while (game.truthtrance?.stage === 'priority') { const actor = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(actor); game = act(game,actor.id,{type:'truthPass'}); } const order = frame(game); assert.ok(order.holders.includes('richese')); assert.deepEqual(viewGame(game,'richese').saphoTruthOrderChoices,[]); for (const mode of ['first','last'] as const) reject(game,'richese',{type:'saphoTruthOrder',card:sapho.id,event:order.event,revision:order.revision,mode}); assert.ok(player(game,'richese').hand.some(card => card.id === sapho.id)); assert.equal(game.richeseAuction?.cardId,sapho.id); game = finish(game); game = answer(game); assert.ok(player(game,'richese').hand.some(card => card.id === sapho.id)); assert.equal(game.richeseAuction?.cardId,sapho.id); assert.deepEqual(inventory(game),source.originalCards); });