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 { withoutEcazLoyalty } from '../game/ecaz-loyalty'; 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); const seat = (game: Game, id: string) => { const player = game.players.find(p => p.id === id); assert.ok(player); return player; }; function step(game: Game): Game { const next = nextHarkonnenBetrayalNativeStep(game); try { return act(game, next.actor, next.action); } catch (error) { throw Error(`Native Occupy/Nexus ${game.turn}/${game.phase}/${game.decision?.kind} ${JSON.stringify(next)}: ${String(error)}`); } } function advance(game: Game, until: (game: Game) => boolean): Game { for (let i = 0; i < 1800; i++) { if (until(game)) return game; assert.notEqual(game.status, 'finished'); game = step(game); } throw Error(`Native Occupy/Nexus stalled at ${game.status}/${game.turn}/${game.phase}/${game.decision?.kind}`); } function inventory(game: Game): string[] { return [...game.deck, ...game.discard, ...(game.auction?.cards.slice(game.auction.index) ?? []), ...game.players.flatMap(player => player.hand)].map(card => card.id).sort(); } function traitors(game: Game): void { validateNexusTraitorSnapshot({ reserve: game.traitorReserve!, players: game.players.map(({ id, faction, traitors, faceDancers }) => ({ id, faction, traitors, ...(faceDancers ? { faceDancers } : {}) })) }, withoutEcazLoyalty(traitorDeck(game.players), game.ecazLoyalty)); } /** Swap one original held or reserve card; never create a Traitor identity. */ function stageTraitor(game: Game, identity: string): void { const owner = seat(game, 'h'); if (owner.traitors.includes(identity)) return; const previous = owner.traitors[0]; assert.ok(previous); const holder = game.players.find(player => player.id !== 'h' && player.traitors.includes(identity)); if (holder) holder.traitors[holder.traitors.indexOf(identity)] = previous; else { const index = game.traitorReserve!.indexOf(identity); assert.ok(index >= 0); game.traitorReserve![index] = previous; } owner.traitors[0] = identity; } /** Actual public Advanced E3 setup, natural second-turn Nexus, reciprocal * alliance, Nexus draw, native Occupy lead, sealed plans and traitor call. * Only unopened Nexus/Spice deck order, one conserved held Traitor exchange * and one existing reserve force per battle participant are staged. */ function fixture(side: 'ally' | 'opponent', lead: 'ecaz' | 'ally') { let game = createGame('HARKOCCUPY', newPlayer('e', 'Ecaz', 'ecaz'), true, ['ecaz']); for (const [id, faction] of [['h', 'harkonnen'], ['g', 'guild'], ['a', 'atreides']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'e', { type: 'moduleOption', module: 'nexusCards', enabled: true }); for (const p of game.players) game = act(game, p.id, { type: 'ready' }); game = act(game, 'e', { type: 'start', advancedPreview: true }); assert.equal(game.nexusHarkonnenBetrayalPreview, true); game = advance(game, state => state.status === 'playing'); const cards = inventory(game); traitors(game); const nexus = game.nexusCards!.cards!, face = nexus.deck.indexOf('harkonnen'); assert.ok(face >= 0); nexus.deck.unshift(nexus.deck.splice(face, 1)[0]); for (let pos = 0; pos < 2; pos++) { const index = game.spiceDeck.findIndex((card, n) => n >= pos && 'territory' in card); assert.ok(index >= pos); game.spiceDeck.splice(pos, 0, game.spiceDeck.splice(index, 1)[0]); } game = advance(game, state => state.turn === 2 && state.phase === 1 && !state.response && !state.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, state => state.nexus && !state.spiceWindow && !state.spiceResolution && !state.response && !state.decision); const ally = side === 'ally' ? 'h' : 'g'; const opponent = side === 'ally' ? 'a' : 'h'; const holder = side === 'ally' ? 'g' : 'a'; game = act(game, 'e', { type: 'alliance', target: ally }); game = act(game, ally, { type: 'alliance', target: 'e' }); game = advance(game, state => state.nexusCards?.phase?.stage === 'drawing'); assert.ok(game.nexusCards!.phase!.eligible.includes(holder)); game = act(game, holder, { type: 'nexusCardChoice', turn: game.turn, card: null, choice: 'draw', ownRedraws: 0 }); assert.equal(game.nexusCards!.cards!.hands[holder], 'harkonnen'); game = advance(game, state => state.phase === 5 && !state.response && !state.decision); 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 ['e', ally, opponent]) { const p = seat(game, id); assert.ok(p.reserves > 0); p.reserves--; p.forces[`${territory.id}:${territory.sectors[0]}`] = 1; } game = advance(game, state => state.phase === 6 && !!state.active && !state.phaseOpening && !state.response && !state.decision); const choice = viewGame(game, game.active!).battleChoices.find(item => item.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 }); const decision = game.decision; assert.ok(decision?.kind === 'ecazBattleLead'); game = act(game, 'e', { type: 'decision', event: decision.event, lead: lead === 'ecaz' ? 'e' : ally }); // Public Nexus setup may retain ordinary pre-leader or other native battle // decisions after the Occupy lead and counter. Drive each original window; // the standalone Occupy helper assumes those windows are already absent. game = advance(game, state => !!state.battle && !state.response && !state.decision && !state.battle.preparation && (!state.battle.preLeader || state.battle.preLeader.closed)); assert.ok(game.battle?.ecazOccupy); const actor = lead === 'ecaz' ? 'e' : ally; const target = side === 'ally' ? opponent : actor; const identity = seat(game, target).leaders.find(leader => !leader.dead && !leader.usedAt && leader.id !== game.ecazLoyalty?.card)!.id; stageTraitor(game, identity); traitors(game); const profile = viewGame(game, actor).battle!.ecazOccupy!.profile!; for (const id of [game.battle!.attacker, game.battle!.defender]) { const leader = id === target ? identity : seat(game, id).leaders.find(l => !l.dead && !l.usedAt)!.id; game = act(game, id, { type: 'battlePlan', dial: id === actor ? profile.fixedEcazDial : 0, leader }); } assert.equal(game.battle!.revealed, true); game = advance(game, state => !state.response && !state.decision); game = act(game, 'h', { type: 'traitorCall', call: true }); game = settleHarkonnenBetrayalNativeCounters(game); const reaction = viewGame(game, holder).nexusHarkonnenBetrayalReaction; assert.ok(reaction, 'A genuine Occupy Harkonnen call must offer the held Nexus.'); assert.equal(reaction.identity, identity); assert.equal(reaction.target, target); assert.equal(reaction.beneficiary, side === 'ally' ? actor : 'h'); assert.equal(reaction.canUse, true); assert.deepEqual(inventory(game), cards); traitors(game); return { game, holder, identity, event: reaction.event, cards }; } for (const side of ['ally', 'opponent'] as const) for (const lead of ['ecaz', 'ally'] as const) for (const use of [false, true]) void test(`Advanced Ecaz Occupy, Harkonnen ${side}, ${lead} lead: native Nexus ${use ? 'Use' : 'Pass'}`, () => { const f = fixture(side, lead); let game = f.game; if (use) game = act(game, f.holder, { type: 'nexusHarkonnenBetrayalUse', event: f.event }); else for (const id of game.pendingNexusHarkonnenBetrayal!.required) game = act(game, id, { type: 'nexusHarkonnenBetrayalPass', event: f.event }); assert.equal(game.pendingNexusHarkonnenBetrayal, null); assert.equal(game.battle!.traitorCalls.h, !use); assert.equal(seat(game, 'h').traitors.includes(f.identity), !use); assert.equal(game.pendingNexusHarkonnenReplacement?.status ?? null, use ? 'due' : null); assert.equal(game.nexusCards!.cards!.hands[f.holder], use ? null : 'harkonnen'); assert.deepEqual(inventory(game), f.cards); traitors(game); const top = game.traitorReserve![0]; game = advance(game, state => state.phase === 8 && !state.pendingNexusHarkonnenReplacement && !state.phaseOpening); assert.equal(game.lastBattleContext!.result, use ? 'normal' : 'traitor'); assert.equal(game.nexusHarkonnenReplacementHistory!.length, use ? 1 : 0); if (use) assert.equal(game.nexusHarkonnenReplacementHistory![0].drawn, top); assert.equal(seat(game, 'h').traitors.length, 4); assert.deepEqual(inventory(game), f.cards); traitors(game); });