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 { DISCOVERY_CARD_PLACEMENTS, validateDiscoveryState } from '../game/discoveries'; import { discoveryStormActions } from '../game/discovery-storm-options'; import { discoveryStormDistance } from '../game/discovery-storm'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { advanceTestingStationContest } from './fixture-testing-station-contest'; import { moveSemutaEmperorCard } from './fixture-semuta-emperor-share'; import { reactSemutaSpecial } from './fixture-semuta-special-karama'; const SAPHO = 'richese-juice-of-sapho'; const STATION = 'ecological-testing-station'; const seat = (game: Game, id: string) => { const player = game.players.find(candidate => candidate.id === id); assert.ok(player); return player; }; const inventory = (game: Game) => [...game.deck, ...game.discard, ...(game.richeseCache ?? []), ...(game.richeseRemoved ?? []), ...game.players.flatMap(player => player.hand)].map(card => card.id).sort(); /** Public lobby and original faction setup, then a controlled physical Station * placement and two real reserve counters. Sapho and Semuta are moved from their * existing physical zones. The station queue and Storm are reached by normal * actions; no completed choice, reaction, receipt or replay is fabricated. */ function fixture(advanced: boolean) { let game = createGame('SAPHOSTATIONREPLAY', newPlayer('richese', 'Richese', 'richese'), advanced, ['choam']); for (const faction of ['atreides', 'emperor'] as const) joinGame(game, newPlayer(faction, faction, faction)); game = applyAction(game, game.host, {type:'moduleOption', module:'discoveries', enabled:true}); for (const player of game.players) game = applyAction(game, player.id, {type:'ready'}); game = applyAction(game, game.host, {type:'start', ...(advanced ? {advancedPreview:true} : {})}); game = advanceTestingStationContest(game, state => state.status === 'playing'); assert.equal(game.phase, 0); const beforeCards = inventory(game); assert.equal(beforeCards.filter(card => card === SAPHO).length, 1); assert.equal(beforeCards.filter(card => card === SEMUTA_DRUG_ID).length, 1); const token = game.discoveries!.tokens.find(candidate => candidate.face === STATION); assert.ok(token && token.status === 'supply'); const placement = Object.values(DISCOVERY_CARD_PLACEMENTS).find(candidate => candidate.type === token.type); assert.ok(placement); Object.assign(token, {status:'placed', territory:placement.territory, sector:placement.sector, revealedTurn:game.turn}); validateDiscoveryState(game.discoveries!); for (const id of ['richese', 'emperor']) { const player = seat(game, id); assert.ok(player.reserves > 0); player.reserves--; player.forces[`${STATION}:0`] = (player.forces[`${STATION}:0`] ?? 0) + 1; } for (const id of ['richese', 'emperor']) { const player = seat(game, id); while (player.hand.length >= 4) game.deck.push(player.hand.pop()!); } moveSemutaEmperorCard(game, SAPHO, 'emperor'); moveSemutaEmperorCard(game, SEMUTA_DRUG_ID, 'richese'); assert.deepEqual(inventory(game), beforeCards); game = advanceTestingStationContest(game, state => state.decision?.kind === 'ecologicalStorm'); assert.equal(game.semutaPreview, true); assert.deepEqual(game.ecologicalStorm!.queue!.order, game.order.filter(id => id === 'richese' || id === 'emperor')); return {game, beforeCards}; } function option(game: Game, holder: string) { const result = viewGame(game, holder).saphoOptions.find(choice => choice.scope === 'testingStation'); assert.ok(result, `${holder} must have a legal physical Sapho order`); return {type:'card', card:SAPHO, scope:result.scope, event:result.event, mode:result.mode} as Action; } for (const advanced of [false, true]) void test( `${advanced ? 'Advanced' : 'Basic'} recovered Sapho can reorder the same Station a second time`, () => { const f = fixture(advanced); let game = f.game; const originalOrder = [...game.ecologicalStorm!.queue!.order]; const originalEvent = game.ecologicalStorm!.event; const base = game.stormPending!; const firstAction = option(game, 'emperor'); game = applyAction(game, 'emperor', firstAction); const firstReceipt = structuredClone(game.ecologicalStorm!.sapho); assert.ok(firstReceipt); assert.deepEqual(game.ecologicalStorm!.queue!.order, [...originalOrder].reverse()); assert.equal(game.pendingTreacheryDiscard?.batch.cause, 'sapho:station'); assert.equal(game.pendingTreacheryDiscard?.continuation.kind, 'saphoStationDiscard'); game = reactSemutaSpecial(game, 'richese', true); assert.ok(seat(game, 'richese').hand.some(card => card.id === SAPHO)); assert.ok(!seat(game, 'richese').hand.some(card => card.id === SEMUTA_DRUG_ID)); assert.equal(game.ecologicalStorm!.queue!.choices.length, 0); const secondAction = option(game, 'richese'); assert.notEqual(secondAction.event, originalEvent); const secondEvent = secondAction.event; game = applyAction(game, 'richese', secondAction); assert.deepEqual(game.ecologicalStorm!.queue!.order, originalOrder); assert.deepEqual(game.ecologicalStorm!.saphoHistory, [firstReceipt]); assert.equal(game.ecologicalStorm!.sapho?.event, secondEvent); assert.equal(game.ecologicalStorm!.sapho?.holder, 'richese'); assert.equal(game.pendingTreacheryDiscard?.batch.cause, 'sapho:station'); game = reactSemutaSpecial(game, 'richese', false); assert.equal(game.pendingTreacheryDiscard, null); assert.equal(game.saphoStationPriority?.event, game.ecologicalStorm!.event); assert.equal(game.discard.filter(card => card.id === SAPHO).length, 1); assert.equal(game.discard.filter(card => card.id === SEMUTA_DRUG_ID).length, 1); const stale = structuredClone(game); assert.throws(() => applyAction(game, 'richese', secondAction)); assert.deepEqual(game, stale); for (const occupant of game.ecologicalStorm!.queue!.order) game = applyAction(game, occupant, {type:'saphoStationPass', event:game.ecologicalStorm!.event}); const firstDelta = base < 40 ? 1 : -1; for (const delta of [firstDelta, -firstDelta]) { const actor = game.ecologicalStorm!.owner; const choice = discoveryStormActions(viewGame(game, actor)).find(candidate => candidate.delta === delta); assert.ok(choice); game = applyAction(game, actor, choice); } assert.equal(game.ecologicalStorm!.stage, 'complete'); assert.deepEqual(game.ecologicalStorm!.queue!.choices.map(choice => choice.owner), originalOrder); assert.equal(discoveryStormDistance(game.ecologicalStorm!), base); assert.equal(game.phase, 1); assert.deepEqual(inventory(game), f.beforeCards); }, );