import assert from 'node:assert/strict'; import { test } from 'node:test'; import { applyAction, createGame, joinGame, newPlayer, normalizeAutomaticGame, viewGame, type Action, type Game } from '../game/engine'; import { TERRITORIES } from '../game/board'; import { NEXUS_FACTIONS } from '../game/nexus-cards'; 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 seat = game.players.find(p => p.id === id); assert.ok(seat); return seat; } function step(game: Game): Game { if (game.pendingGuildBetrayal) { const frame = game.pendingGuildBetrayal; const actor = frame.required.find(id => !frame.passed.includes(id)); assert.ok(actor); return act(game, actor, { type: 'guildBetrayalPass', event: frame.invoice.event }); } const next = nextHarkonnenBetrayalNativeStep(game); try { return act(game, next.actor, next.action); } catch (error) { throw Error(`Native E3 step ${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 E3 journey 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(p => p.hand)].map(card => card.id).sort(); } function physical(game: Game): void { const nx = game.nexusCards!.cards!; assert.deepEqual([...nx.deck, ...nx.discard, ...Object.values(nx.hands).filter(c => c !== null)].sort(), [...NEXUS_FACTIONS].sort()); const universe = traitorDeck(game.players).filter(id => id !== game.ecazLoyalty?.card); validateNexusTraitorSnapshot({ reserve: game.traitorReserve!, players: game.players.map(({id, faction, traitors, faceDancers}) => ({ id, faction, traitors, ...(faceDancers ? { faceDancers } : {}) })) }, universe); } /** Fresh public Advanced E3 setup and a real second-turn Nexus draw. The unopened * Nexus/Spice decks are reordered, one already-dealt Traitor is exchanged with * its actual owner or the reserve, and one reserve force from each battle side * is placed on an otherwise empty sand territory. For the Moritani variant, * a held Harkonnen identity is also exchanged with the reserve so a random * unrelated post-loss assassination cannot obscure this test's replacement. * No cards or forces are created. * Battle selection, sealed plans, Traitor call, response and Mentat are native. */ function fixture(native: 'ecaz' | 'moritani') { let game = createGame('HARKE3', newPlayer('e3', native, native), true, ['ecaz']); for (const [id, faction] of [['h', 'harkonnen'], ['g', 'guild'], ['a', 'atreides']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'e3', { type: 'moduleOption', module: 'nexusCards', enabled: true }); for (const seat of game.players) game = act(game, seat.id, { type: 'ready' }); game = act(game, 'e3', { type: 'start', advancedPreview: true }); assert.equal(game.advanced, true); assert.equal(game.nexusHarkonnenBetrayalPreview, true); game = advance(game, state => state.status === 'playing'); if (native === 'ecaz') { assert.equal(game.ecazLoyalty?.player, 'e3'); assert.ok(game.ecazLoyalty?.card); } else assert.equal(game.moritaniAssassinatePreview, true); const cards = inventory(game); physical(game); const identity = player(game, 'e3').leaders.find(l => !l.dead && l.id !== game.ecazLoyalty?.card)!.id; const h = player(game, 'h'); if (!h.traitors.includes(identity)) { const replacement = h.traitors[0]; assert.ok(replacement); const owner = game.players.find(seat => seat.id !== 'h' && seat.traitors.includes(identity)); if (owner) owner.traitors[owner.traitors.indexOf(identity)] = replacement; else { const index = game.traitorReserve!.indexOf(identity); assert.ok(index >= 0); game.traitorReserve![index] = replacement; } h.traitors[0] = identity; } if (native === 'moritani') { const nativeLeaderIds = new Set(h.leaders.map(leader => leader.id)); const moritani = player(game, 'e3'); for (let i = 0; i < moritani.traitors.length; i++) { if (!nativeLeaderIds.has(moritani.traitors[i])) continue; const index = game.traitorReserve!.findIndex(id => !nativeLeaderIds.has(id)); assert.ok(index >= 0); [moritani.traitors[i], game.traitorReserve![index]] = [game.traitorReserve![index], moritani.traitors[i]]; } } physical(game); const nexus = game.nexusCards!.cards!, index = nexus.deck.indexOf('harkonnen'); assert.ok(index >= 0); nexus.deck.unshift(nexus.deck.splice(index, 1)[0]); for (let pos = 0; pos < 2; pos++) { const i = game.spiceDeck.findIndex((card, n) => n >= pos && 'territory' in card); assert.ok(i >= pos); game.spiceDeck.splice(pos, 0, game.spiceDeck.splice(i, 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); game = act(game, 'h', { type: 'alliance', target: 'g' }); game = act(game, 'g', { type: 'alliance', target: 'h' }); game = advance(game, state => state.nexusCards?.phase?.stage === 'drawing'); assert.ok(game.nexusCards!.phase!.eligible.includes('e3')); game = act(game, 'e3', { type: 'nexusCardChoice', turn: game.turn, card: null, choice: 'draw', ownRedraws: 0 }); assert.equal(game.nexusCards!.cards!.hands.e3, 'harkonnen'); game = advance(game, state => state.phase === 5 && !state.response && !state.decision && !state.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', 'e3']) { const seat = player(game, id); assert.ok(seat.reserves > 0); seat.reserves--; seat.forces[`${territory.id}:${territory.sectors[0]}`] = 1; } game = advance(game, state => state.phase === 6 && !!state.active && !state.phaseOpening && !state.response && !state.decision); const chooser = game.active!; assert.ok(['h', 'e3'].includes(chooser)); game = act(game, chooser, { type: 'chooseBattle', territory: territory.id, target: chooser === 'h' ? 'e3' : 'h' }); game = advance(game, state => !!state.battle && !state.response && !state.decision && !state.battle.preparation && (!state.battle.preLeader || state.battle.preLeader.closed)); for (const id of [game.battle!.attacker, game.battle!.defender]) { const leader = id === 'e3' ? identity : player(game, id).leaders.find(l => !l.dead && !l.usedAt)!.id; game = act(game, id, { type: 'battlePlan', dial: 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, 'e3').nexusHarkonnenBetrayalReaction; assert.ok(reaction); assert.equal(reaction.canUse, true); assert.equal(reaction.identity, identity); assert.deepEqual(inventory(game), cards); physical(game); return { game, identity, event: reaction.event, cards }; } for (const native of ['ecaz', 'moritani'] as const) for (const use of [false, true]) void test(`Advanced public ${native} Harkonnen Nexus ${use ? 'Use' : 'Pass'} resolves the real native Traitor call and Mentat`, () => { const f = fixture(native); let game = f.game; if (use) game = act(game, 'e3', { 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.nexusCards!.cards!.hands.e3, use ? null : 'harkonnen'); assert.equal(game.battle!.traitorCalls.h, !use); assert.equal(player(game, 'h').traitors.includes(f.identity), !use); assert.equal(game.pendingNexusHarkonnenReplacement?.status ?? null, use ? 'due' : null); assert.deepEqual(inventory(game), f.cards); physical(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.ok(player(game, 'h').traitors.includes(top)); } assert.equal(player(game, 'h').traitors.length, 4); assert.deepEqual(inventory(game), f.cards); physical(game); const before = JSON.stringify(game); for (const p of game.players) viewGame(game, p.id); assert.equal(JSON.stringify(game), before); assert.deepEqual(normalizeAutomaticGame(game), game); assert.throws(() => act(game, 'e3', { type: 'nexusHarkonnenBetrayalUse', event: f.event })); assert.equal(JSON.stringify(game), before); });