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 result = game.players.find(seat => seat.id === id); assert.ok(result); return result; }; function advance(game:Game, until:(game:Game)=>boolean):Game { for (let n = 0; n < 500; n++) { if (until(game)) return game; const next = nextTruthMovementNativeEarlyStep(game); assert.ok(next); game = act(game,next.actor,next.action); } throw Error('The original native game did not reach its opening.'); } /** Native Advanced Richese/Ix setup. Select only original unopened starting * cards; after setup, swap conserved held cards if needed so Richese and a * non-Emperor responder hold the two actual Karama cards. */ function source(emperor:boolean) { let game = createGame('RICHOPEN',newPlayer('r','Richese','richese'),true,['choam','ix']); joinGame(game,newPlayer('i','Ixians','ixians')); 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}); const shield = game.deck.find(card => card.kind === 'shield'); assert.ok(shield); const karamas = game.deck.filter(card => card.effect === 'karama'); assert.ok(karamas.length >= 2); const ordinary = game.deck.find(card => card.kind === 'projectile'); assert.ok(ordinary); const initial = [shield,karamas[0],karamas[1],ordinary].slice(0,game.players.length); for (const [position,card] of initial.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' && !!state.phaseOpening && state.amalWindowOpen === true && !state.response && !state.decision); const held = (id:string) => player(game,id).hand; const assignKarama = (id:string, protectedSeat?:string) => { if (held(id).some(card => card.effect === 'karama')) return; const donor = game.players.find(seat => seat.id !== id && seat.id !== protectedSeat && seat.hand.some(card => card.effect === 'karama')); assert.ok(donor); const from = donor.hand.findIndex(card => card.effect === 'karama'); assert.ok(held(id).length > 0); [held(id)[0],donor.hand[from]] = [donor.hand[from],held(id)[0]]; }; assignKarama('r'); assignKarama('a','r'); assert.ok(player(game,'r').spice >= 3); const card = held('r').find(row => row.effect === 'karama'); assert.ok(card); const acquire = game.richeseCache?.[0]; assert.ok(acquire); const canceledBy = held('a').find(row => row.effect === 'karama'); assert.ok(canceledBy); return {game,card:card.id,acquire:acquire.id,canceledBy:canceledBy.id}; } const cached = new Map>(); const fixture = (emperor:boolean) => { let item = cached.get(emperor); if (!item) {item = source(emperor); cached.set(emperor,item);} return {...item,game:structuredClone(item.game)}; }; const purchase = (card:string,acquire:string):Action => ({type:'card',mode:'special',card,acquire}); for (const emperor of [false,true]) void test(`Advanced Richese special purchase retains the Ix opening with Emperor ${emperor ? 'present' : 'absent'}`, () => { const f = fixture(emperor); const before = act(f.game,'i',{type:'ready'}); const opening = structuredClone(before.phaseOpening), inventory = truthMovementInventory(before); const spice = player(before,'r').spice, emperorSpice = emperor ? player(before,'e').spice : 0; assert.ok(opening); assert.ok(opening.passed.includes('i')); assert.equal(viewGame(before,'r').richeseSpecialKarama?.blocked,null); let after = act(before,'r',purchase(f.card,f.acquire)); assert.equal(player(after,'r').spice,spice - 3); assert.equal(player(after,'r').specialKaramaUsed,true); assert.ok(player(after,'r').hand.some(card => card.id === f.acquire)); assert.ok(!after.richeseCache?.some(card => card.id === f.acquire)); assert.deepEqual(truthMovementInventory(after),inventory); if (emperor) { assert.equal(after.response?.kind,'richesePurchaseIncome'); after = advance(after,state => !!state.phaseOpening && !state.response && !state.decision); assert.equal(player(after,'e').spice,emperorSpice + 3); } assert.deepEqual(after.phaseOpening,opening); assert.equal(after.amalWindowOpen,true); assert.equal(viewGame(after,'i').players.find(seat => seat.id === 'r')!.hand,undefined); for (const seat of after.players.filter(seat => !opening.passed.includes(seat.id))) after = act(after,seat.id,{type:'ready'}); assert.equal(after.phaseOpening,null); assert.ok(player(after,'r').hand.some(card => card.id === f.acquire)); }); void test('preventing Emperor income preserves the purchased card and the exact Ix opening', () => { const f = fixture(true); const before = act(f.game,'i',{type:'ready'}), opening = structuredClone(before.phaseOpening); const richeseSpice = player(before,'r').spice, emperorSpice = player(before,'e').spice; let game = act(before,'r',purchase(f.card,f.acquire)); assert.equal(game.response?.kind,'richesePurchaseIncome'); game = act(game,'a',{type:'card',mode:'cancel',card:f.canceledBy}); game = advance(game,state => !!state.phaseOpening && !state.response && !state.decision); assert.deepEqual(game.phaseOpening,opening); assert.equal(player(game,'r').spice,richeseSpice - 3); assert.equal(player(game,'e').spice,emperorSpice); assert.equal(player(game,'r').specialKaramaUsed,true); assert.ok(player(game,'r').hand.some(card => card.id === f.acquire)); });