137 lines
7.7 KiB
TypeScript
137 lines
7.7 KiB
TypeScript
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 seat = game.players.find(p => p.id === id);
|
|
assert.ok(seat);
|
|
return seat;
|
|
}
|
|
function step(game: Game): Game {
|
|
const next = nextHarkonnenBetrayalNativeStep(game);
|
|
try { return act(game, next.actor, next.action); }
|
|
catch (error) { throw Error(`Native Moritani 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', 'Unexpected victory during the staged battle');
|
|
game = step(game);
|
|
}
|
|
throw Error(`Moritani journey stalled at ${game.status}/${game.turn}/${game.phase}/${game.decision?.kind}`);
|
|
}
|
|
function checkTraitors(game: Game): void {
|
|
validateNexusTraitorSnapshot({ reserve: game.traitorReserve!, players: game.players.map(({id, faction, traitors, faceDancers}) =>
|
|
({ id, faction, traitors, ...(faceDancers ? { faceDancers } : {}) })) }, traitorDeck(game.players));
|
|
}
|
|
/** Exchange an existing dealt card with its actual owner or reserve; never add a card. */
|
|
function hold(game: Game, id: string, identity: string): void {
|
|
const owner = player(game, id);
|
|
if (owner.traitors.includes(identity)) return;
|
|
const old = owner.traitors[0]; assert.ok(old);
|
|
const other = game.players.find(p => p.id !== id && p.traitors.includes(identity));
|
|
if (other) other.traitors[other.traitors.indexOf(identity)] = old;
|
|
else {
|
|
const index = game.traitorReserve!.indexOf(identity); assert.ok(index >= 0);
|
|
game.traitorReserve![index] = old;
|
|
}
|
|
owner.traitors[0] = identity;
|
|
}
|
|
|
|
/** Fresh public Advanced setup; only unopened Nexus/Spice ordering, conserved
|
|
* dealt Traitor swaps and one reserve force on each side are staged. The real
|
|
* second-turn Nexus closes, Moritani receives its face, and normal controls
|
|
* choose the battle, seal plans, vote and resolve it. Harkonnen's strength-6
|
|
* Feyd beats Moritani's strength-1 Vando with both forces undialed. */
|
|
function beforeCall(): Game {
|
|
let game = createGame('MORITANIREVEAL', newPlayer('m', 'Moritani', 'moritani'), true, ['ecaz']);
|
|
for (const [id, faction] of [['h', 'harkonnen'], ['g', 'guild'], ['a', 'atreides']] as const)
|
|
joinGame(game, newPlayer(id, faction, faction));
|
|
game = act(game, 'm', {type:'moduleOption', module:'nexusCards', enabled:true});
|
|
for (const seat of game.players) game = act(game, seat.id, {type:'ready'});
|
|
game = act(game, 'm', {type:'start', advancedPreview:true});
|
|
assert.equal(game.nexusHarkonnenBetrayalPreview, true);
|
|
game = advance(game, g => g.status === 'playing');
|
|
assert.equal(game.moritaniAssassinatePreview, true);
|
|
hold(game, 'm', 'harkonnen-1');
|
|
hold(game, 'h', 'moritani-4');
|
|
checkTraitors(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, 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('m'));
|
|
game = act(game, 'm', {type:'nexusCardChoice', turn:game.turn, card:null, choice:'draw', ownRedraws:0});
|
|
assert.equal(game.nexusCards!.cards!.hands.m, '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', 'm']) {
|
|
const seat = player(game, id); assert.ok(seat.reserves > 0);
|
|
seat.reserves--; seat.forces[`${territory.id}:${territory.sectors[0]}`] = 1;
|
|
}
|
|
game = advance(game, g => g.phase === 6 && !!g.active && !g.phaseOpening && !g.response && !g.decision);
|
|
const chooser = game.active!; assert.ok(['h', 'm'].includes(chooser));
|
|
game = act(game, chooser, {type:'chooseBattle', territory:territory.id, target:chooser === 'h' ? 'm' : 'h'});
|
|
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:0, leader:id === 'h' ? 'harkonnen-0' : 'moritani-4'});
|
|
assert.equal(game.battle!.revealed, true);
|
|
return advance(game, g => !g.response && !g.decision);
|
|
}
|
|
|
|
void test('a publicly revealed Harkonnen traitor canceled by Nexus still prevents Moritani assassination', () => {
|
|
let game = beforeCall();
|
|
const battleEvent = game.battle!.event;
|
|
game = act(game, 'h', {type:'traitorCall', call:true});
|
|
game = settleHarkonnenBetrayalNativeCounters(game);
|
|
const reaction = viewGame(game, 'm').nexusHarkonnenBetrayalReaction; assert.ok(reaction?.canUse);
|
|
assert.equal(reaction.identity, 'moritani-4');
|
|
assert.ok(player(game, 'h').revealedTraitors?.includes('moritani-4'), 'The original call reveals the physical Traitor');
|
|
game = act(game, 'm', {type:'nexusHarkonnenBetrayalUse', event:reaction.event});
|
|
assert.equal(game.battle!.traitorCalls.h, false);
|
|
assert.equal(game.pendingNexusHarkonnenReplacement?.status, 'due');
|
|
for (let i = 0; i < 500 && game.turn === 2 && game.phase !== 8; i++) {
|
|
assert.notEqual(game.decision?.kind, 'moritaniAssassinate', 'A canceled call does not make its revealed Traitor unseen');
|
|
game = step(game);
|
|
}
|
|
assert.equal(game.phase, 8);
|
|
assert.equal(game.lastBattleContext?.event, battleEvent);
|
|
assert.equal(game.lastBattleContext?.result, 'normal');
|
|
assert.equal(game.lastBattleContext?.winner, 'h');
|
|
assert.deepEqual(game.moritaniAssassinate!.opportunities, []);
|
|
assert.equal(game.nexusHarkonnenReplacementHistory!.length, 1);
|
|
checkTraitors(game);
|
|
});
|
|
|
|
void test('the same normal Moritani loss without a traitor reveal offers assassination', () => {
|
|
let game = beforeCall();
|
|
game = act(game, 'h', {type:'traitorCall', call:false});
|
|
game = advance(game, g => g.decision?.kind === 'moritaniAssassinate');
|
|
assert.equal(game.lastBattleContext?.result, 'normal');
|
|
assert.equal(game.lastBattleContext?.winner, 'h');
|
|
const offer = viewGame(game, 'm').moritaniAssassinate?.pending;
|
|
assert.ok(offer);
|
|
assert.ok(offer.cards.some(card => card.card === 'harkonnen-1'));
|
|
assert.equal(game.nexusCards!.cards!.hands.m, 'harkonnen');
|
|
assert.equal(game.pendingNexusHarkonnenReplacement, null);
|
|
checkTraitors(game);
|
|
});
|