import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, createGame, joinGame, newPlayer, viewGame, type Game, type Action } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; import { advanceSemutaNexusTrade, nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade'; const act = (g: Game, actor: string, action: Action) => applyAction(g, actor, action); const seat = (g: Game, actor: string) => { const p = g.players.find(row => row.id === actor); assert.ok(p); return p; }; const cards = (g: Game) => [...g.deck, ...g.discard, ...(g.auction?.cards.slice(g.auction.index) ?? []), ...(g.ixAuction?.cards ?? []), ...g.players.flatMap(p => p.hand)].map(c => c.id).sort(); const clean = (g: Game) => !g.response && !g.phaseOpening && !g.decision && !g.truthtrance && !g.pendingTreacheryDiscard; /** Real public Basic setup and Sandtrout draw. The unopened Spice prefix, * later armies and one physical battle card are deliberately staged. */ function fixture() { let g = createGame('REINFORCEMENTSTROUT', newPlayer('a', 'Atreides', 'atreides'), false, ['ix']); joinGame(g, newPlayer('e', 'Emperor', 'emperor')); joinGame(g, newPlayer('g', 'Guild', 'guild')); g = act(g, 'a', { type: 'moduleOption', module: 'ecazTreachery', enabled: true }); for (const p of g.players) g = act(g, p.id, { type: 'ready' }); g = act(g, 'a', { type: 'start' }); g = advanceSemutaNexusTrade(g, state => state.status === 'playing'); const inventory = cards(g); g = advanceSemutaNexusTrade(g, beforeIxSpiceDraw); orderIxSpiceCards(g, ['sandtrout', 'land', 'land']); g = advanceSemutaNexusTrade(g, state => state.turn === 1 && state.phase === 5 && clean(state)); assert.equal(g.sandtrout, true); assert.ok(g.log.some(row => row.text.includes('Sandtrout broke all alliances'))); assert.ok(g.players.every(p => p.ally === null)); const territory = g.storm === 10 ? 'carthag' : 'arrakeen'; const sector = territory === 'arrakeen' ? 10 : 11, location = `${territory}:${sector}`; for (const p of g.players) { p.reserves += Object.values(p.forces).reduce((sum, n) => sum + n, 0); p.forces = {}; } for (const [id, amount] of [['a', 4], ['e', 2]] as const) { const p = seat(g, id); p.reserves -= amount; p.forces[location] = amount; } const id = 'ecaz-reinforcements'; const holder = g.players.find(p => p.hand.some(c => c.id === id)); let card; if (holder) card = holder.hand.splice(holder.hand.findIndex(c => c.id === id), 1)[0]; else { const pile = [g.deck, g.discard].find(source => source.some(c => c.id === id)); assert.ok(pile); card = pile.splice(pile.findIndex(c => c.id === id), 1)[0]; } const owner = seat(g, 'a'); if (owner.hand.length >= 4) g.deck.push(owner.hand.shift()!); owner.hand.push(card); assert.deepEqual(cards(g), inventory); g = advanceSemutaNexusTrade(g, state => state.turn === 1 && state.phase === 6 && !state.battle && clean(state)); const choice = viewGame(g, g.active!).battleChoices.find(row => row.territory === territory); assert.ok(choice); g = act(g, choice.chooser, { type: 'chooseBattle', territory, target: choice.chooser === choice.attacker ? choice.defender : choice.attacker }); for (let n = 0; n < 100 && (!g.battle || g.battle.preparation || g.battle.preLeader?.closed === false || !clean(g)); n++) { const step = nextSemutaNexusTradeNativeStep(g); g = act(g, step.actor, step.action); } assert.ok(g.battle && !g.battle.revealed && clean(g)); assert.equal(g.sandtrout, true); return { g, inventory, location }; } for (const slot of ['weapon', 'defense'] as const) void test( `Basic Reinforcements in the ${slot} slot remains usable while genuine Sandtrout waits`, () => { const f = fixture(), before = structuredClone(f.g), owner = seat(f.g, 'a'), opponent = seat(f.g, 'e'); const quote = viewGame(f.g, 'a').battle?.reinforcements; assert.ok(quote); assert.equal(quote.blocked, null); assert.equal(quote.normal, 3); for (const difficulty of DIFFICULTIES) { const view = viewGame(f.g, 'a'); view.players.find(p => p.id === 'a')!.bot = difficulty; const action = botActions(view).find(row => row.type === 'battlePlan'); assert.ok(action, difficulty); assert.doesNotThrow(() => act(f.g, 'a', action), difficulty); } const ourLeader = owner.leaders.find(l => !l.dead && !l.usedAt); assert.ok(ourLeader); const enemyLeader = opponent.leaders.find(l => !l.dead && !l.usedAt); assert.ok(enemyLeader); let g = act(f.g, 'a', { type:'battlePlan', dial: 1, support: 0, leader:ourLeader.id, weapon:slot === 'weapon' ? 'ecaz-reinforcements' : null, defense:slot === 'defense' ? 'ecaz-reinforcements' : null }); assert.deepEqual(f.g, before); assert.equal(viewGame(g, 'e').battle!.plans.a, undefined); g = act(g, 'e', { type:'battlePlan', dial:0, support:0, leader:enemyLeader.id }); assert.equal(g.battle?.revealed, true); for (let n = 0; n < 160 && (g.battle || !clean(g)); n++) { const step = nextSemutaNexusTradeNativeStep(g); g = act(g, step.actor, step.action); } assert.equal(g.battle, null); assert.equal(g.sandtrout, true); assert.equal(seat(g, 'a').reserves, owner.reserves - 3); assert.ok(seat(g, 'a').tanks >= owner.tanks + 3); assert.equal(g.discard.filter(c => c.id === 'ecaz-reinforcements').length, 1); assert.deepEqual(cards(g), f.inventory); for (const p of g.players) assert.equal(p.reserves + p.tanks + Object.values(p.forces).reduce((a,b) => a+b,0), 20); }, );