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 { placeAmbassador, validateAmbassadors } from '../game/ecaz-ambassadors'; import { placeTerror } from '../game/moritani-terror'; import { guildAmbassadorShipments } from '../game/guild-ambassador'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade'; type First = 'ambassador' | 'terror'; const act = (g: Game, id: string, action: Action) => applyAction(g, id, action); const seat = (g: Game, id: string) => { const p = g.players.find(row => row.id === id); assert.ok(p); return p; }; const clean = (g: Game) => !g.response && !g.phaseOpening && !g.decision && !g.truthtrance && !g.pendingTreacheryDiscard; /** Genuine Basic public setup/training and entrant turn. Only one unopened * Spice card, original Ambassador cohort and legal token locations are staged. */ function fixture(first: First, nexus: boolean, independent: boolean) { let g = createGame('KULLSKILLSOVERLAP', newPlayer('ecaz', 'Ecaz', 'ecaz'), false, ['ix', 'choam', 'ecaz']); for (const id of ['choam', 'tleilaxu', 'guild', 'moritani'] as const) joinGame(g, newPlayer(id, id, id)); g = act(g, 'guild', { type: 'seatPosition', position: 6 }); if (first === 'terror') g = act(g, 'moritani', { type: 'seatPosition', position: 4 }); g = act(g, 'ecaz', { type: 'seatPosition', position: first === 'ambassador' ? 4 : 5 }); g = act(g, 'choam', { type: 'seatPosition', position: 1 }); g = act(g, 'tleilaxu', { type: 'seatPosition', position: 2 }); for (const name of [...(nexus ? ['nexusCards'] : []), ...(independent ? ['ecazTreachery'] : []), 'leaderSkills', 'kullPreview']) g = act(g, 'ecaz', { type: 'moduleOption', module: name, enabled: true }); for (const p of g.players) g = act(g, p.id, { type: 'ready' }); g = act(g, 'ecaz', { type: 'start' }); const reach = (until: (g: Game) => boolean) => { for (let n = 0; n < 1800; n++) { if (until(g)) return; assert.notEqual(g.status, 'finished'); if (g.status === 'setup' && !g.setupStage && g.decision?.kind === 'moritaniSetup') g = act(g, 'moritani', { type: 'decision', territory: 'oh_gap', sector: 9 }); else if (g.status === 'setup' && g.setupStage === 'forces' && seat(g, 'ecaz').reserves === 20) g = act(g, 'ecaz', { type: 'ecazSetup', placements: { 'imperial_basin:9': 6 } }); else if (g.decision?.kind === 'ecazPlacement' && !g.response && !g.phaseOpening) g = act(g, 'ecaz', { type: 'decision', decline: true }); else { const next = nextSemutaNexusTradeNativeStep(g); g = act(g, next.actor, next.action); } } throw Error('Native play did not reach the entrant turn'); }; reach(state => state.status === 'playing'); assert.equal(g.leaderSkills?.assignments.length, g.players.length); assert.equal(g.kullPreview, true); assert.equal(g.nexusKullPreview === true, nexus); const spice = g.spiceDeck.findIndex(card => 'territory' in card && !card.discovery); assert.ok(spice >= 0); g.spiceDeck.unshift(g.spiceDeck.splice(spice, 1)[0]); reach(state => state.turn === 1 && state.phase === 5 && state.active === 'tleilaxu' && clean(state)); assert.equal(g.order.indexOf('ecaz') < g.order.indexOf('moritani'), first === 'ambassador'); const candidates = [{ territory: 'habbanya_ridge_sietch', sector: 17 }, { territory: 'sietch_tabr', sector: 14 }, { territory: 'arrakeen', sector: 10 }]; const target = candidates.find(row => row.sector !== g.storm); assert.ok(target); const state = g.ecazAmbassadors!; const token = state.tokens.find(row => row.effect === 'guild'); assert.ok(token); if (token.zone === 'pool') { const replaced = state.tokens.find(row => row.zone === 'supply' && row.effect !== 'ecaz'); assert.ok(replaced); const index = state.cohort.indexOf(replaced.id); assert.ok(index >= 0); state.cohort[index] = token.id; replaced.zone = 'pool'; token.zone = 'supply'; } validateAmbassadors(state); g.ecazAmbassadors = placeAmbassador(state, token.id, { turn: g.turn, availableSpice: seat(g, 'ecaz').spice, destination: { id: target.territory, stronghold: true, inStorm: false, allowed: true } }).state; const terror = g.moritaniTerror!.tokens.find(row => row.kind === 'robbery' && row.status === 'available'); assert.ok(terror); g.moritaniTerror = placeTerror(g.moritaniTerror!, terror.id, target.territory, g.turn); return { g, target, token: token.id, terror: terror.id }; } for (const nexus of [false, true]) for (const independent of [false, true]) for (const first of ['ambassador', 'terror'] as const) for (const childReveal of [false, true]) void test(`Basic ${nexus ? 'Nexus' : 'printed'} Kull/Skills ${independent ? 'independent ' : ''}${first} first: Guild child ${childReveal ? 'reveals' : 'declines'} Terror`, () => { const f = fixture(first, nexus, independent), before = structuredClone(f.g); const destination = `${f.target.territory}:${f.target.sector}`; const spice = seat(f.g, 'tleilaxu').spice, reserves = seat(f.g, 'ecaz').reserves; let g = act(f.g, 'tleilaxu', { type: 'ship', territory: f.target.territory, sector: f.target.sector, amount: 1, elite: 0, allyPayment: 0 }); assert.deepEqual(f.g, before); // A held Karama can open the ordinary Guild-income response before the // already committed shipment's ordered entry reactions. Close that real // response through the existing native driver, without changing the hand. for (let n = 0; !g.pendingArrivalOverlap && n < 20; n++) { assert.equal(g.response?.kind, 'guildIncome'); const next = nextSemutaNexusTradeNativeStep(g); g = act(g, next.actor, next.action); } assert.equal(g.pendingArrivalOverlap?.first, first); assert.equal(seat(g, 'tleilaxu').forces[destination], 1); const overlap = g.pendingArrivalOverlap!.event; if (first === 'terror') g = act(g, 'moritani', { type: 'decision', decline: true }); const entry = g.pendingAmbassador; assert.ok(entry); assert.equal(entry.token, f.token); const ecazView = viewGame(g, 'ecaz'); assert.ok(ecazView.ambassadorEntry); for (const difficulty of DIFFICULTIES) { const view = viewGame(g, 'ecaz'); view.players.find(row => row.id === 'ecaz')!.bot = difficulty; const action = botActions(view)[0]; assert.ok(action, difficulty); assert.doesNotThrow(() => act(g, 'ecaz', action)); } g = act(g, 'ecaz', { type: 'decision', event: entry.event, trigger: true, beneficiary: 'ecaz' }); assert.equal(g.pendingAmbassador?.stage, 'ship'); const target = guildAmbassadorShipments(g, 'ecaz').destinations.find(row => row.territory === f.target.territory && row.sector === f.target.sector); assert.ok(target); g = act(g, 'ecaz', { type: 'decision', event: entry.event, amount: 1, elite: 0, territory: target.territory, sector: target.sector }); assert.equal(seat(g, 'ecaz').reserves, reserves - 1); assert.equal(g.pendingArrivalOverlap?.event, overlap); assert.equal(g.pendingTerrorEntry?.entrant, 'ecaz'); assert.equal(g.pendingTerrorEntry?.token, f.terror); if (childReveal) { g = act(g, 'moritani', { type: 'decision', reveal: true }); assert.equal(g.pendingTerrorEntry?.stage, 'robbery'); g = act(g, 'moritani', { type: 'decision', choice: 'spice' }); assert.equal(g.moritaniTerror!.tokens.find(row => row.id === f.terror)!.status, 'removed'); } else g = act(g, 'moritani', { type: 'decision', decline: true }); if (first === 'ambassador' && !childReveal) { assert.equal(g.pendingTerrorEntry?.entrant, 'tleilaxu'); g = act(g, 'moritani', { type: 'decision', decline: true }); } assert.equal(g.pendingArrivalOverlap, null); assert.equal(g.pendingAmbassador, null); assert.equal(g.pendingTerrorEntry, null); assert.equal(seat(g, 'tleilaxu').forces[destination], 1); assert.equal(seat(g, 'tleilaxu').spice, spice - 1); assert.equal(seat(g, 'ecaz').forces[destination], 1); assert.equal(g.ecazAmbassadors!.tokens.find(row => row.id === f.token)!.zone, 'used'); assert.equal(g.active, 'tleilaxu'); g = act(g, 'tleilaxu', { type: 'endMovement' }); assert.ok(!g.movementRemaining?.includes('tleilaxu')); });