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 { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { nextDiscoveryNoFieldBlow } from './fixture-discovery-no-field-blow'; const SAPHO = 'richese-juice-of-sapho'; const SEMUTA = 'richese-semuta-drug'; const seat = (game: Game, id: string) => { const player = game.players.find(row => row.id === id); assert.ok(player); return player; }; const act = (game: Game, player: string, action: Action) => applyAction(game, player, action); const physical = (game: Game) => [ ...game.deck, ...game.discard, ...(game.richeseCache ?? []), ...(game.richeseRemoved ?? []), ...(game.removedTreachery ?? []), ...(game.auction?.cards.slice(game.auction.index) ?? []), ...game.players.flatMap(player => player.hand), ].map(card => card.id).sort(); function step(game: Game): Game { const next = nextDiscoveryNoFieldBlow(game); assert.ok(next, `Native setup/phase continuation missing at ${game.status}/${game.turn}/${game.phase}/${game.decision?.kind}`); return act(game, next.actor, next.action); } function advance(game: Game, until: (game: Game) => boolean): Game { for (let count = 0; count < 1800; count++) { if (until(game)) return game; assert.notEqual(game.status, 'finished', 'Game ended before its Great Maker vote'); game = step(game); } throw Error(`The real Great Maker chronology did not reach its target: ${game.turn}/${game.phase}/${game.decision?.kind}`); } function front(game: Game, position: number, testCard: (card: Game['spiceDeck'][number]) => boolean) { const index = game.spiceDeck.findIndex((card, n) => n >= position && testCard(card)); assert.ok(index >= position); game.spiceDeck.splice(position, 0, game.spiceDeck.splice(index, 1)[0]); } /** Swap a physical Richese cache card for one held card; no new card or hand * slot is created. This is explicitly staged acquisition, not a purchase. */ function stageHeld(game: Game, player: string, id: string) { const target = seat(game, player), cache = game.richeseCache!; if (target.hand.some(card => card.id === id)) return; const index = cache.findIndex(card => card.id === id); assert.ok(index >= 0, `${id} must remain in the original cache`); const replacement = target.hand.pop(); assert.ok(replacement, 'The chosen actual seat has a dealt card to exchange'); const [card] = cache.splice(index, 1); target.hand.push(card); cache.push(replacement); } /** Actual public setup, opt-in Discovery, native turn-one phases, turn-two * Storm and Great Maker. Only original unopened Spice order and physical * cache/hand swaps are staged. The first fixture selects Ix+CHOAM decks to * avoid the separate Semuta protocol; single-CHOAM uses its native protocol. */ function source(advanced: boolean, mode: 'first' | 'last', semuta = false) { let game = createGame('SAPHOGREATMAKER', newPlayer('richese', 'Richese', 'richese'), advanced, semuta ? ['choam'] : ['choam', 'ix']); for (const [id, faction] of [['atreides', 'atreides'], ['emperor', 'emperor'], ['guild', 'guild']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'richese', {type:'moduleOption', module:'discoveries', enabled:true}); for (const player of game.players) game = act(game, player.id, {type:'ready'}); game = act(game, 'richese', advanced ? {type:'start', advancedPreview:true} : {type:'start'}); game = advance(game, state => state.status === 'playing'); assert.equal(game.discoveryEnabled, true); if (!semuta) assert.ok(!game.semutaPreview && !game.semutaAdvancedOrdinaryPreview && !game.semutaRecruitsPreview); const original = physical(game); for (let position = 0; position < (advanced ? 2 : 1); position++) front(game, position, card => 'territory' in card && !card.discovery); game = advance(game, state => state.turn === 2 && state.phase === 0); front(game, 0, card => 'worm' in card && card.greatMaker === true); for (let position = 1; position <= (advanced ? 2 : 1); position++) front(game, position, card => 'territory' in card && !card.discovery); // The public storm order is settled before the ordinary Spice Blow begins. game = advance(game, state => state.turn === 2 && state.phase === 1 && !state.greatMaker && !state.spiceResolution && !state.spiceSequence && !state.decision && !state.response); const holder = mode === 'first' ? game.order.at(-1)! : game.order[0]; const semutaHolder = game.order.find(id => id !== holder)!; stageHeld(game, holder, SAPHO); if (semuta) stageHeld(game, semutaHolder, SEMUTA); assert.deepEqual(physical(game), original); game = advance(game, state => state.greatMaker?.stage === 'vote'); assert.equal(game.decision?.kind, 'greatMakerVote'); assert.ok(game.saphoGreatMakerPriority, 'The actual Great Maker opens neutral ordering priority'); assert.deepEqual(game.greatMaker!.order, game.order); return {game, holder, semutaHolder, original}; } function passPriority(state: Game): Game { let game = state; for (let count = 0; count < 20 && game.saphoGreatMakerPriority; count++) { const priority = game.saphoGreatMakerPriority; const player = game.players.find(row => viewGame(game, row.id).saphoGreatMakerPriority?.canPass); assert.ok(player, 'An unfinished shared priority has a legal pass owner'); game = act(game, player.id, {type:'saphoGreatMakerPass', event:priority.event}); } assert.equal(game.saphoGreatMakerPriority ?? null, null); return game; } function vote(state: Game, yes: boolean): Game { const game = passPriority(state), decision = game.decision; assert.ok(decision?.kind === 'greatMakerVote'); return act(game, decision.player, {type:'decision', event:decision.event, yes}); } function reject(state: Game, player: string, action: Action) { const before = structuredClone(state); assert.throws(() => act(state, player, action)); assert.deepEqual(state, before); } function finishVote(state: Game): Game { let game = state; for (let count = 0; count < 10 && game.greatMaker?.stage === 'vote'; count++) game = vote(game, game.greatMaker.votes.length < 3); assert.ok(game.greatMaker && game.greatMaker.stage !== 'vote', 'Every original voter must finish exactly once'); return game; } for (const advanced of [false, true]) for (const mode of ['first', 'last'] as const) void test( `${advanced ? 'Advanced' : 'Basic'} Great Maker Sapho takes ${mode} among unanswered votes`, () => { const f = source(advanced, mode); let game = f.game; const originalOrder = [...game.order], before = structuredClone(game.greatMaker!); const priority = game.saphoGreatMakerPriority!; const use: Action = {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode}; reject(game, f.holder, {...use, event:'stale'}); reject(game, game.order.find(id => id !== f.holder)!, use); reject(game, f.holder, {type:'saphoGreatMakerPass', event:'stale'}); game = act(game, f.holder, use); assert.equal(game.discard.filter(card => card.id === SAPHO).length, 1); assert.ok(!seat(game, f.holder).hand.some(card => card.id === SAPHO)); assert.deepEqual(game.order, originalOrder); assert.deepEqual(game.greatMaker!.votes, before.votes); assert.equal(mode === 'first' ? game.greatMaker!.order[0] : game.greatMaker!.order.at(-1), f.holder); assert.ok(game.saphoGreatMakerPriority && game.saphoGreatMakerPriority.event !== priority.event); reject(game, f.holder, use); game = finishVote(game); assert.equal(game.greatMaker!.votes.length, originalOrder.length); assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, originalOrder.length); assert.equal(game.greatMaker!.votes.filter(row => row.yes).length, 3); assert.equal(game.nexus, true); assert.deepEqual(physical(game), f.original); }); void test('a Semuta claimant can use the recovered physical Sapho again at the new Great Maker priority', () => { const f = source(false, 'first', true); let game = f.game; const originalVotes = structuredClone(game.greatMaker!.votes); const originalPriority = game.saphoGreatMakerPriority!; game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:originalPriority.event, mode:'first'}); const discard = game.pendingTreacheryDiscard; assert.ok(discard?.reaction?.stage === 'offer'); game = act(game, f.semutaHolder, {type:'semutaCommit', event:discard.batch.event}); assert.equal(game.pendingTreacheryDiscard ?? null, null); assert.ok(seat(game, f.semutaHolder).hand.some(card => card.id === SAPHO)); assert.deepEqual(game.greatMaker!.votes, originalVotes); const nextPriority = game.saphoGreatMakerPriority; assert.ok(nextPriority); assert.notEqual(nextPriority.event, originalPriority.event); const secondMode = game.greatMaker!.order.at(-1) === f.semutaHolder ? 'first' : 'last'; reject(game, f.semutaHolder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:originalPriority.event, mode:secondMode}); game = act(game, f.semutaHolder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:nextPriority.event, mode:secondMode}); assert.deepEqual(game.greatMaker!.votes, originalVotes); assert.equal(secondMode === 'first' ? game.greatMaker!.order[0] : game.greatMaker!.order.at(-1), f.semutaHolder); assert.ok(game.pendingTreacheryDiscard?.reaction?.stage === 'offer'); for (let count = 0; count < 10 && game.pendingTreacheryDiscard?.reaction?.stage === 'offer'; count++) { const pending = game.pendingTreacheryDiscard, reaction = pending.reaction; assert.ok(reaction?.stage === 'offer'); const player = game.players.find(row => !reaction.passed.includes(row.id)); assert.ok(player); game = act(game, player.id, {type:'semutaPass', event:pending.batch.event}); } assert.equal(game.pendingTreacheryDiscard ?? null, null); assert.ok(game.saphoGreatMakerPriority && game.saphoGreatMakerPriority.event !== nextPriority.event); game = finishVote(game); assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length); assert.deepEqual(physical(game), f.original); }); void test('all four minimal bots legally pass the neutral Great Maker ordering priority', () => { const f = source(false, 'first'); const actor = f.game.players.find(player => player.id !== f.holder && viewGame(f.game, player.id).saphoGreatMakerPriority?.canPass); assert.ok(actor); for (const difficulty of DIFFICULTIES) { const game = structuredClone(f.game), view = viewGame(game, actor.id); view.players.find(player => player.id === actor.id)!.bot = difficulty; const action = botActions(view)[0]; assert.ok(action, difficulty); assert.equal(action.type, 'saphoGreatMakerPass', difficulty); const after = act(game, actor.id, action); assert.ok(after.saphoGreatMakerPriority!.passed.includes(actor.id)); assert.deepEqual(after.greatMaker!.votes, game.greatMaker!.votes); assert.deepEqual(physical(after), f.original); } }); void test('the completed Great Maker order remains valid when the next real storm changes player order', () => { const f = source(false, 'first'); let game = f.game; const priority = game.saphoGreatMakerPriority!; game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'}); game = finishVote(game); assert.equal(game.greatMaker!.stage, 'complete'); const maker = structuredClone(game.greatMaker), oldOrder = [...game.order]; game = advance(game, state => state.turn === 3 && state.phase === 0); for (let count = 0; count < 100 && game.turn === 3 && game.phase === 0; count++) { const next = nextDiscoveryNoFieldBlow(game); assert.ok(next); // The two real Basic storm dials are independently chosen, not staged as // a board mutation. A six-sector advance passes occupied player circles. const action = next.action.type === 'stormDial' ? {...next.action, amount:3} : next.action; game = act(game, next.actor, action); } assert.equal(game.turn, 3); assert.equal(game.phase, 1); assert.notDeepEqual(game.order, oldOrder, 'The native Storm changed the player priority'); assert.deepEqual(game.greatMaker, maker, 'The completed signed Maker retains its own historical order'); for (const player of game.players) assert.doesNotThrow(() => viewGame(game, player.id)); assert.deepEqual(physical(game), f.original); }); void test('a later Sapho choice preserves the already-cast Great Maker vote', () => { const f = source(false, 'first'); let game = vote(f.game, false); const prior = structuredClone(game.greatMaker!.votes), initial = [...game.greatMaker!.order]; assert.equal(prior.length, 1); assert.notEqual(prior[0].player, f.holder); const priority = game.saphoGreatMakerPriority!; game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'}); assert.deepEqual(game.greatMaker!.votes, prior); assert.deepEqual(game.greatMaker!.order.slice(0, 1), initial.slice(0, 1)); assert.equal(game.greatMaker!.order[1], f.holder); game = finishVote(game); assert.equal(game.greatMaker!.votes[0].yes, false); assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length); assert.deepEqual(physical(game), f.original); }); for (const advanced of [false, true]) for (const claim of [false, true]) void test( `${advanced ? 'Advanced' : 'Basic'} Great Maker Sapho resumes its vote after Semuta ${claim ? 'claim' : 'passes'}`, () => { const f = source(advanced, 'first', true); let game = f.game; const original = structuredClone(game.greatMaker!), priority = game.saphoGreatMakerPriority!; assert.equal(game.semutaPreview, true); game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'}); assert.ok(game.pendingTreacheryDiscard?.reaction); if (claim) { const reaction = game.pendingTreacheryDiscard!.reaction!; game = act(game, f.semutaHolder, {type:'semutaCommit', event:game.pendingTreacheryDiscard!.batch.event}); assert.ok(reaction.stage === 'offer'); } for (let count = 0; count < 10 && game.pendingTreacheryDiscard?.reaction?.stage === 'offer'; count++) { const pending = game.pendingTreacheryDiscard, reaction = pending.reaction; assert.ok(reaction?.stage === 'offer'); const player = game.players.find(row => !reaction.passed.includes(row.id)); assert.ok(player); game = act(game, player.id, {type:'semutaPass', event:pending.batch.event}); } // The native claim control selects the offered discarded card when asked. if (game.pendingTreacheryDiscard?.reaction?.stage === 'select') { const pending = game.pendingTreacheryDiscard; const reaction = pending.reaction; assert.ok(reaction?.stage === 'select'); game = act(game, reaction.player, {type:'semutaSelect', event:pending.batch.event, card:SAPHO}); } assert.equal(game.pendingTreacheryDiscard ?? null, null); assert.equal(game.greatMaker!.event, original.event); assert.deepEqual(game.greatMaker!.votes, original.votes); assert.ok(game.saphoGreatMakerPriority); game = finishVote(game); assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length); assert.deepEqual(physical(game), f.original); });