import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { battlePlanCardOptions } from '../components/battle-preparation'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; import { advanceSemutaNexusTrade } from './fixture-semuta-nexus-trade'; import { finishBattleOutcomeWindow } from './fixture-battle-outcome-window'; const act = (g: Game, id: string, action: Action) => applyAction(g, id, action); const seat = (g: Game, id: string) => { const p = g.players.find(row => row.id === id); assert.ok(p); return p; }; const clean = (g: Game) => !g.response && !g.decision && !g.phaseOpening && !g.truthtrance && !g.pendingTreacheryDiscard; function inventory(g: Game) { const ids = [...g.deck, ...g.discard, ...(g.richeseCache ?? []), ...(g.richeseRemoved ?? []), ...(g.auction?.cards.slice(g.auction.index) ?? []), ...(g.ixAuction?.cards ?? []), ...g.players.flatMap(player => player.hand)].map(card => card.id).sort(); assert.equal(new Set(ids).size, ids.length); return ids; } /** Genuine Basic public setup and native first Spice Blow. Only unopened Spice * order, physical board counters and held card locations are staged. */ function fixture(kind: 'mirrorWeapon' | 'stoneBurner' = 'mirrorWeapon') { let g = createGame('WAITINGMIRROR', newPlayer('p', 'Emperor', 'emperor'), false, ['ix', 'choam']); joinGame(g, newPlayer('q', 'Guild', 'guild')); joinGame(g, newPlayer('r', 'Richese', 'richese')); for (const player of g.players) g = act(g, player.id, {type:'ready'}); g = act(g, 'p', {type:'start'}); g = advanceSemutaNexusTrade(g, state => state.status === 'playing'); const cards = inventory(g); g = advanceSemutaNexusTrade(g, beforeIxSpiceDraw); orderIxSpiceCards(g, ['sandtrout', 'land']); g = advanceSemutaNexusTrade(g, state => state.phase === 5 && clean(state)); assert.equal(g.sandtrout, true); assert.ok(g.log.some(entry => String(entry.text ?? '').includes('Sandtrout'))); const position = g.storm === 5 ? {territory:'hagga_basin', sector:12} : {territory:'pasty_mesa', sector:5}; for (const player of g.players) { player.reserves += Object.values(player.forces).reduce((sum, amount) => sum + amount, 0); player.forces = {}; } for (const id of ['p', 'q']) { seat(g, id).reserves -= 5; seat(g, id).forces[`${position.territory}:${position.sector}`] = 5; } g = advanceSemutaNexusTrade(g, state => state.phase === 6 && clean(state)); const choice = viewGame(g, g.active!).battleChoices.find(row => row.territory === position.territory); assert.ok(choice); g = act(g, choice.chooser, {type:'chooseBattle', territory:position.territory, target:choice.chooser === choice.attacker ? choice.defender : choice.attacker}); g = advanceSemutaNexusTrade(g, state => !!state.battle && !state.battle.preparation && state.battle.preLeader?.closed !== false && clean(state)); const place = (id: string, wanted: (card: Game['deck'][number]) => boolean) => { const player = seat(g, id); let card: Game['deck'][number] | undefined; const cache = g.richeseCache?.findIndex(wanted) ?? -1; if (cache >= 0) card = g.richeseCache!.splice(cache, 1)[0]; else { const fromDeck = g.deck.findIndex(wanted); if (fromDeck >= 0) card = g.deck.splice(fromDeck, 1)[0]; else { const fromDiscard = g.discard.findIndex(wanted); if (fromDiscard >= 0) card = g.discard.splice(fromDiscard, 1)[0]; else { const holder = g.players.find(row => row.hand.some(wanted)); assert.ok(holder); card = holder.hand.splice(holder.hand.findIndex(wanted), 1)[0]; } } } assert.ok(card); if (player.hand.length >= 4) g.deck.push(player.hand.shift()!); player.hand.push(card); return card.id; }; const weapon = place('p', card => card.effect === kind); const projectile = place('q', card => card.kind === 'projectile'); assert.deepEqual(inventory(g), cards); return { g, weapon, projectile, cards }; } void test('waiting Sandtrout does not stop a physical Mirror from copying an ordinary attack', () => { const f = fixture(), view = viewGame(f.g, 'p'); assert.equal(f.g.sandtrout, true); const mirror = seat(f.g, 'p').hand.find(card => card.id === f.weapon)!; const options = battlePlanCardOptions(view, 'weapon', seat(f.g, 'p').leaders[0].id, '', ''); assert.ok(options.some(option => option.id === mirror.id)); for (const difficulty of DIFFICULTIES) { const botView = viewGame(f.g, 'p'); botView.players.find(p => p.id === 'p')!.bot = difficulty; const action = botActions(botView).find(row => row.type === 'battlePlan' && row.weapon === f.weapon); assert.ok(action, difficulty); assert.doesNotThrow(() => act(f.g, 'p', action)); } let g = act(f.g, 'p', {type:'battlePlan', dial:2, support:0, leader:seat(f.g, 'p').leaders[0].id, weapon:f.weapon}); g = act(g, 'q', {type:'battlePlan', dial:2, support:0, leader:seat(g, 'q').leaders[0].id, weapon:f.projectile}); assert.equal(g.battle?.revealed, true); for (const id of ['q', 'p']) g = act(g, id, {type:'traitorCall', call:false}); g = finishBattleOutcomeWindow(g); assert.equal(seat(g, 'p').leaders[0].dead, true); assert.equal(seat(g, 'q').leaders[0].dead, true); assert.ok(g.lastBattleContext?.winner); assert.equal(g.sandtrout, true); assert.deepEqual(inventory(g), f.cards); }); void test('waiting Sandtrout does not stop Stone Burner from resolving an ordinary battle', () => { const f = fixture('stoneBurner'), view = viewGame(f.g, 'p'); assert.equal(f.g.sandtrout, true); assert.ok(battlePlanCardOptions(view, 'weapon', seat(f.g, 'p').leaders[0].id, '', '') .some(card => card.id === f.weapon)); for (const difficulty of DIFFICULTIES) { const botView = viewGame(f.g, 'p'); botView.players.find(p => p.id === 'p')!.bot = difficulty; const action = botActions(botView).find(row => row.type === 'battlePlan' && row.weapon === f.weapon); assert.ok(action, difficulty); assert.doesNotThrow(() => act(f.g, 'p', action)); } let g = act(f.g, 'p', {type:'battlePlan', dial:1, support:0, leader:seat(f.g, 'p').leaders[0].id, weapon:f.weapon}); g = act(g, 'q', {type:'battlePlan', dial:2, support:0, leader:seat(g, 'q').leaders[0].id, weapon:f.projectile}); assert.equal(g.decision?.kind, 'stoneBurner'); g = act(g, 'p', {type:'decision', event:g.battle!.event, mode:'kill'}); for (const id of ['q', 'p']) g = act(g, id, {type:'traitorCall', call:false}); g = finishBattleOutcomeWindow(g); assert.equal(seat(g, 'p').leaders[0].dead, true); assert.equal(seat(g, 'q').leaders[0].dead, true); assert.equal(g.lastBattleContext?.winner, 'p'); assert.equal(g.sandtrout, true); assert.deepEqual(inventory(g), f.cards); });