import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early'; import { truthMovementInventory } from './fixture-truth-movement'; const player = (game:Game,id:string) => { const seat = game.players.find(row => row.id === id); assert.ok(seat); return seat; }; function advance(game:Game, until:(game:Game)=>boolean):Game { for (let count = 0; count < 600; count++) { if (until(game)) return game; const step = nextTruthMovementNativeEarlyStep(game); assert.ok(step, `The native source needs a continuation at ${game.turn}/${game.phase}/${game.decision?.kind}`); game = act(game,step.actor,step.action); } throw Error('The native source did not reach the requested market boundary.'); } /** Genuine Advanced CHOAM/Richese setup and native phase progression. After * the real starting deal, explicitly move physical cards from the real deck * to supplement Richese's hand and stage the reciprocal CHOAM alliance; no * market, decision, spice or cache state is installed. */ function source(emperor:boolean) { let game = createGame('RICHMARKET',newPlayer('r','Richese','richese'),true,['choam']); joinGame(game,newPlayer('c','CHOAM','choam')); joinGame(game,newPlayer('a','Atreides','atreides')); if (emperor) joinGame(game,newPlayer('e','Emperor','emperor')); for (const seat of game.players) game = act(game,seat.id,{type:'ready'}); game = act(game,'r',{type:'start',advancedPreview:true}); assert.equal(game.status,'setup'); const shield = game.deck.find(card => card.kind === 'shield'); assert.ok(shield); const ordinary = game.deck.filter(card => ['projectile','poison','snooper'].includes(card.kind)).slice(0,game.players.length - 1); assert.equal(ordinary.length,game.players.length - 1); for (const [position,card] of [shield,...ordinary].entries()) { const from = game.deck.findIndex(candidate => candidate.id === card.id); assert.ok(from >= position); [game.deck[position],game.deck[from]] = [game.deck[from],game.deck[position]]; } game = advance(game,state => state.status === 'playing'); const original = truthMovementInventory(game); const hold = (id:string, predicate:(card:Game['deck'][number])=>boolean) => { const index = game.deck.findIndex(predicate); assert.ok(index >= 0); const [card] = game.deck.splice(index,1); player(game,id).hand.push(card); return card.id; }; const karama = hold('r',card => card.effect === 'karama'); const spare = hold('a',card => card.effect === 'karama'); const cancel = emperor ? spare : undefined; player(game,'r').ally = 'c'; player(game,'c').ally = 'r'; assert.deepEqual(truthMovementInventory(game),original); game = advance(game,state => state.decision?.kind === 'choamMarket' && !state.response && !state.phaseOpening); assert.equal(game.choamMarket?.owner,'c'); const acquire = game.richeseCache?.[0]; assert.ok(acquire); return {game,karama,spare,cancel,acquire:acquire.id,inventory:truthMovementInventory(game)}; } const cache = new Map>(); function fixture(emperor:boolean) { let f = cache.get(emperor); if (!f) { f = source(emperor); cache.set(emperor,f); } return {...f,game:structuredClone(f.game)}; } const purchase = (card:string,acquire:string):Action => ({type:'card',mode:'special',card,acquire}); function trade(game:Game, returned?:string) { const offered = player(game,'c').hand[0]; assert.ok(offered); game = act(game,'c',{type:'decision',mode:'trade',card:offered.id}); assert.equal(game.decision?.kind,'choamTradeReply'); if (returned !== undefined) { game = act(game,'r',{type:'decision',card:returned}); assert.equal(game.decision?.kind,'choamTradeConfirm'); } return {game,offered:offered.id}; } for (const emperor of [false,true]) for (const phase of ['market','reply','confirm'] as const) void test(`Advanced Richese purchase preserves ${phase} with Emperor ${emperor ? 'present' : 'absent'}`, () => { const f = fixture(emperor); let game = f.game; const returned = player(game,'r').hand.find(card => card.id !== f.karama && card.id !== f.spare); assert.ok(returned); const offered = phase === 'market' ? undefined : trade(game,phase === 'confirm' ? returned.id : undefined); if (offered) game = offered.game; const parent = structuredClone(game.decision), market = structuredClone(game.choamMarket); const beforeSpice = player(game,'r').spice, income = emperor ? player(game,'e').spice : 0; assert.ok(viewGame(game,'r').richeseSpecialKarama?.cards.some(card => card.id === f.acquire)); game = act(game,'r',purchase(f.karama,f.acquire)); assert.equal(player(game,'r').spice,beforeSpice - 3); assert.equal(player(game,'r').specialKaramaUsed,true); assert.ok(player(game,'r').hand.some(card => card.id === f.acquire)); assert.equal(game.discard.filter(card => card.id === f.karama).length,1); if (emperor) { assert.equal(game.response?.kind,'richesePurchaseIncome'); game = advance(game,state => !state.response && !!state.decision); assert.equal(player(game,'e').spice,income + 3); } assert.deepEqual(game.decision,parent); assert.deepEqual(game.choamMarket,market); assert.deepEqual(truthMovementInventory(game),f.inventory); if (phase === 'reply') game = act(game,'r',{type:'decision',card:returned.id}); if (phase !== 'market') { game = act(game,'c',{type:'decision',accept:true}); assert.ok(player(game,'r').hand.some(card => card.id === offered!.offered)); assert.ok(player(game,'c').hand.some(card => card.id === returned.id)); } assert.equal(game.decision?.kind,'choamMarket'); game = act(game,'c',{type:'decision',done:true}); assert.equal(game.choamMarket,null); assert.equal(game.discard.filter(card => card.id === f.karama).length,1); assert.deepEqual(truthMovementInventory(game),f.inventory); }); void test('an agreed return Karama remains reserved while another Karama can fund the purchase', () => { const f = fixture(true); const extra = player(f.game,'a').hand.findIndex(card => card.id === f.spare); assert.ok(extra >= 0); player(f.game,'r').hand.push(...player(f.game,'a').hand.splice(extra,1)); const result = trade(f.game,f.karama); const game = result.game, before = structuredClone(game); assert.throws(() => act(game,'r',purchase(f.karama,f.acquire))); assert.deepEqual(game,before); let next = act(game,'r',purchase(f.spare,f.acquire)); next = advance(next,state => !state.response && state.decision?.kind === 'choamTradeConfirm'); next = act(next,'c',{type:'decision',accept:true}); assert.ok(player(next,'c').hand.some(card => card.id === f.karama)); assert.equal(next.discard.filter(card => card.id === f.spare).length,1); assert.deepEqual(truthMovementInventory(next),f.inventory); }); void test('preventing Emperor income keeps the completed purchase and original market decision', () => { const f = fixture(true); assert.ok(f.cancel); const parent = structuredClone(f.game.decision), market = structuredClone(f.game.choamMarket); const income = player(f.game,'e').spice; let game = act(f.game,'r',purchase(f.karama,f.acquire)); assert.equal(game.response?.kind,'richesePurchaseIncome'); game = act(game,'a',{type:'card',mode:'cancel',card:f.cancel}); game = advance(game,state => !state.response && !!state.decision); assert.equal(player(game,'e').spice,income); assert.equal(player(game,'r').specialKaramaUsed,true); assert.ok(player(game,'r').hand.some(card => card.id === f.acquire)); assert.deepEqual(game.decision,parent); assert.deepEqual(game.choamMarket,market); assert.deepEqual(truthMovementInventory(game),f.inventory); });