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 { TERRITORIES } from '../game/board'; import { traitorDeck } from '../game/traitors'; import { validateNexusTraitorSnapshot } from '../game/nexus-traitor-exchange'; import { nextHarkonnenBetrayalNativeStep, settleHarkonnenBetrayalNativeCounters } from './fixture-nexus-harkonnen-betrayal'; const act = (game: Game, actor: string, action: Action) => applyAction(game, actor, action); function player(game: Game, id: string) { const p = game.players.find(seat => seat.id === id); assert.ok(p); return p; } function step(game: Game): Game { const next = nextHarkonnenBetrayalNativeStep(game); try { return act(game, next.actor, next.action); } catch (error) { throw Error(`Native CHOAM Nexus step ${game.turn}/${game.phase}/${game.decision?.kind} ${JSON.stringify(next)}: ${String(error)}`); } } function advance(game: Game, predicate: (game: Game) => boolean): Game { for (let count = 0; count < 1800; count++) { if (predicate(game)) return game; assert.notEqual(game.status, 'finished'); game = step(game); } throw Error(`Native CHOAM Nexus journey stalled at ${game.status}/${game.turn}/${game.phase}/${game.decision?.kind}`); } function physical(game: Game) { validateNexusTraitorSnapshot({ reserve: game.traitorReserve!, players: game.players.map(({id, faction, traitors, faceDancers}) => ({ id, faction, traitors, ...(faceDancers ? {faceDancers} : {}) })) }, traitorDeck(game.players)); } /** Genuine public Advanced setup and Nexus draw. Only unopened Spice/Nexus * order, one conserved held/reserve Traitor exchange, and two physical reserve * forces per side are staged. Battle selection, both support commitments, * Traitor voting and the cancelable CHOAM income response are native. */ function beforeCall(): Game { let game = createGame('CHOAMNEXUSINCOME', newPlayer('c', 'CHOAM', 'choam'), true, ['choam', 'ecaz']); for (const [id, faction] of [['h', 'harkonnen'], ['g', 'guild'], ['a', 'atreides']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'c', {type:'moduleOption', module:'nexusCards', enabled:true}); for (const p of game.players) game = act(game, p.id, {type:'ready'}); game = act(game, 'c', {type:'start', advancedPreview:true}); assert.equal(game.nexusHarkonnenBetrayalPreview, true); game = advance(game, g => g.status === 'playing'); const identity = player(game, 'c').leaders.find(l => !l.dead && l.id !== 'choam-auditor')!.id; const harkonnen = player(game, 'h'); if (!harkonnen.traitors.includes(identity)) { const old = harkonnen.traitors[0]; assert.ok(old); const owner = game.players.find(p => p.id !== 'h' && p.traitors.includes(identity)); if (owner) owner.traitors[owner.traitors.indexOf(identity)] = old; else { const index = game.traitorReserve!.indexOf(identity); assert.ok(index >= 0); game.traitorReserve![index] = old; } harkonnen.traitors[0] = identity; } physical(game); const nexus = game.nexusCards!.cards!, nx = nexus.deck.indexOf('harkonnen'); assert.ok(nx >= 0); nexus.deck.unshift(nexus.deck.splice(nx, 1)[0]); for (let pos = 0; pos < 2; pos++) { const index = game.spiceDeck.findIndex((card, i) => i >= pos && 'territory' in card); assert.ok(index >= pos); game.spiceDeck.splice(pos, 0, game.spiceDeck.splice(index, 1)[0]); } game = advance(game, g => g.turn === 2 && g.phase === 1 && !g.response && !g.decision); const worm = game.spiceDeck.findIndex(card => 'worm' in card && !card.greatMaker && !card.suppressed); assert.ok(worm >= 0); game.spiceDeck.unshift(game.spiceDeck.splice(worm, 1)[0]); game = advance(game, g => g.nexus && !g.spiceWindow && !g.spiceResolution && !g.response && !g.decision); game = act(game, 'h', {type:'alliance', target:'g'}); game = act(game, 'g', {type:'alliance', target:'h'}); game = advance(game, g => g.nexusCards?.phase?.stage === 'drawing'); assert.ok(game.nexusCards!.phase!.eligible.includes('c')); game = act(game, 'c', {type:'nexusCardChoice', turn:game.turn, card:null, choice:'draw', ownRedraws:0}); assert.equal(game.nexusCards!.cards!.hands.c, 'harkonnen'); game = advance(game, g => g.phase === 5 && !g.response && !g.decision && !g.pendingGuildBetrayal); const territory = TERRITORIES.find(t => t.type === 'sand' && !t.sectors.includes(game.storm) && game.players.every(p => !Object.keys(p.forces).some(key => key.startsWith(`${t.id}:`)))); assert.ok(territory); for (const id of ['h', 'c']) { const p = player(game, id); assert.ok(p.reserves >= 2 && p.spice >= 2); p.reserves -= 2; p.forces[`${territory.id}:${territory.sectors[0]}`] = 2; } game = advance(game, g => g.phase === 6 && !!g.active && !g.phaseOpening && !g.response && !g.decision); const choice = viewGame(game, game.active!).battleChoices.find(b => b.territory === territory.id); assert.ok(choice); game = act(game, choice.chooser, {type:'chooseBattle', territory:territory.id, target:choice.attacker === choice.chooser ? choice.defender : choice.attacker}); game = advance(game, g => !!g.battle && !g.response && !g.decision && !g.battle.preparation && (!g.battle.preLeader || g.battle.preLeader.closed)); for (const id of [game.battle!.attacker, game.battle!.defender]) game = act(game, id, {type:'battlePlan', dial:2, support:2, leader:id === 'c' ? identity : player(game, id).leaders.find(l => !l.dead && !l.usedAt)!.id}); assert.equal(game.battle!.revealed, true); return advance(game, g => !g.response && !g.decision); } for (const cancel of [false, true]) void test(`Advanced CHOAM ${cancel ? 'forgoes income after a revealed and Nexus-canceled traitor' : 'receives income when neither side reveals a traitor'}`, () => { let game = beforeCall(); const event = game.battle!.event, choamSpice = player(game, 'c').spice, harkonnenSpice = player(game, 'h').spice; game = act(game, 'h', {type:'traitorCall', call:cancel}); if (cancel) { game = settleHarkonnenBetrayalNativeCounters(game); const reaction = viewGame(game, 'c').nexusHarkonnenBetrayalReaction; assert.ok(reaction?.canUse); assert.ok(player(game, 'h').revealedTraitors?.includes(reaction.identity)); game = act(game, 'c', {type:'nexusHarkonnenBetrayalUse', event:reaction.event}); } // Finish only the original vote and any income cancellation responses. Stop // before unrelated capture, collection, or subsequent phase income. for (let count = 0; count < 200; count++) { if (game.lastBattleContext?.event === event && !game.pendingChoamBattleIncome && game.response?.kind !== 'choamBattleIncome') break; assert.equal(game.phase, 6); game = step(game); } assert.equal(game.lastBattleContext?.event, event); assert.equal(game.lastBattleContext?.result, 'normal'); assert.equal(player(game, 'h').spice, harkonnenSpice - 2); assert.equal(player(game, 'c').spice, choamSpice - 2 + (cancel ? 0 : 1)); assert.equal(game.log.some(entry => entry.text.includes('received 1 spice from battle force payments')), !cancel); assert.equal(game.nexusCards!.cards!.hands.c, cancel ? null : 'harkonnen'); physical(game); });