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 { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { takeKullCard } from './fixture-choam-kull'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; import { advanceSemutaNexusTrade as advance } from './fixture-semuta-nexus-trade'; const act = (g: Game, actor: string, action: Action) => applyAction(g, actor, action); function seat(g: Game, id: string) { const player = g.players.find(p => p.id === id); assert.ok(player); return player; } const clean = (g: Game) => !g.response && !g.phaseOpening && !g.decision && !g.truthtrance && !g.pendingTreacheryDiscard; function inventory(g: Game) { const cards = [...g.deck, ...g.discard, ...(g.richeseCache ?? []), ...(g.richeseRemoved ?? []), ...(g.auction?.cards.slice(g.auction.index) ?? []), ...(g.ixAuction?.cards ?? []), ...g.players.flatMap(p => p.hand)].map(card => card.id).sort(); assert.equal(new Set(cards).size, cards.length); return cards; } /** Genuine Basic public setup and a real Nexus alliance. Only unopened Spice * order and conserved held Treachery Card locations are staged. */ function fixture() { let g = createGame('KULLRICHESEGIFT', newPlayer('r', 'Richese', 'richese'), false, ['choam', 'ix']); for (const faction of ['choam', 'atreides', 'emperor'] as const) joinGame(g, newPlayer(faction, faction, faction)); for (const p of g.players) g = act(g, p.id, { type: 'ready' }); g = act(g, 'r', { type: 'start' }); assert.equal(g.kullPreview, true); assert.equal(g.semutaPreview, true); g = advance(g, state => state.status === 'playing'); const originalCards = inventory(g); // Prevent an incidental stronghold victory during the actual turn-one path. for (const player of g.players) { player.reserves += Object.values(player.forces).reduce((sum, n) => sum + n, 0); player.forces = {}; } // Keep the two selected Richese cards in their original cache until staging. for (const id of [SEMUTA_DRUG_ID, 'richese-distrans']) { const index = g.richeseCache!.findIndex(card => card.id === id); assert.ok(index >= 0); g.richeseCache!.push(g.richeseCache!.splice(index, 1)[0]); } g = advance(g, beforeIxSpiceDraw); orderIxSpiceCards(g, ['land', 'land']); g = advance(g, state => state.turn === 2 && beforeIxSpiceDraw(state)); orderIxSpiceCards(g, ['worm', 'land', 'land']); g = advance(g, state => !!state.nexus && !state.spiceWindow && clean(state)); g = act(g, 'r', { type: 'alliance', target: 'emperor' }); g = act(g, 'emperor', { type: 'alliance', target: 'r' }); g = advance(g, state => state.turn === 2 && state.phase === 2 && clean(state)); for (const p of g.players) g.deck.push(...p.hand.splice(0)); seat(g, 'choam').hand.push(takeKullCard(g, 'ix-kull-wahad')); const karama = takeKullCard(g, 'karama'); seat(g, 'atreides').hand.push(karama); const counter = takeKullCard(g, 'karama'); seat(g, 'emperor').hand.push(counter); const semuta = takeKullCard(g, SEMUTA_DRUG_ID); seat(g, 'r').hand.push(semuta); const gift = takeKullCard(g, 'richese-distrans'); seat(g, 'r').hand.push(gift); assert.deepEqual(inventory(g), originalCards); const beforeGift = structuredClone(g); g = act(g, 'r', { type: 'richeseGift', card: gift.id }); assert.equal(g.response?.kind, 'richeseGift'); g = act(g, 'atreides', { type: 'card', mode: 'cancel', card: karama.id }); assert.ok(g.pendingKull, 'The original cancellation must open the neutral Kull opportunity'); return { g, gift: gift.id, karama: karama.id, counter: counter.id, kull: 'ix-kull-wahad', semuta: semuta.id, beforeGift, originalCards }; } function resolveSemuta(g: Game, claim: boolean) { const pending = g.pendingTreacheryDiscard; assert.ok(pending?.reaction?.stage === 'offer'); const event = pending.batch.event; if (claim && seat(g, 'r').hand.some(card => card.id === SEMUTA_DRUG_ID)) { g = act(g, 'r', { type: 'semutaCommit', event }); if (g.pendingTreacheryDiscard?.reaction?.stage === 'select') g = act(g, 'r', { type: 'semutaSelect', event, card: pending.batch.entries[0].card.id }); } else { for (const player of g.players) { if (g.pendingTreacheryDiscard?.batch.event !== event) break; g = act(g, player.id, { type: 'semutaPass', event }); } } assert.notEqual(g.pendingTreacheryDiscard?.batch.event, event); return g; } function allowKull(g: Game) { for (let n = 0; g.pendingKull && !g.pendingTreacheryDiscard && n < 20; n++) { const response = g.response; if (!response) { g = normalizeAutomaticGame(g); continue; } const actor = g.players.find(p => !response.passed.includes(p.id)); assert.ok(actor); g = act(g, actor.id, { type: 'passResponse' }); } return g; } for (const result of ['decline', 'success', 'counter'] as const) for (const claim of [false, true]) void test( `Basic Kull ${result} during a Richese gift retains Semuta ${claim ? 'claim' : 'pass'} and gift continuation`, () => { const f = fixture(); let g = f.g; const event = g.pendingKull!.event, offered = viewGame(g, 'choam').kullReaction; assert.ok(offered?.plays.some(play => play.source === 'printed' && play.card.id === f.kull)); if (result === 'success' && !claim) for (const difficulty of DIFFICULTIES) { const view = viewGame(g, 'choam'); view.players.find(p => p.id === 'choam')!.bot = difficulty; const action = botActions(view).find(row => row.type === 'kullDecision'); assert.ok(action); assert.doesNotThrow(() => act(g, 'choam', action), difficulty); } g = act(g, 'choam', result === 'decline' ? { type: 'kullDecision', event, decline: true } : { type: 'kullDecision', event, source: 'printed', card: f.kull }); if (result === 'counter') { assert.equal(g.response?.kind, 'choamWorthless'); g = act(g, 'emperor', { type: 'card', mode: 'cancel', card: f.counter }); } else if (result === 'success') g = allowKull(g); for (let n = 0; n < 6 && (g.pendingTreacheryDiscard || g.pendingKull); n++) { if (g.pendingTreacheryDiscard?.reaction) g = resolveSemuta(g, claim); else if (g.pendingKull) g = allowKull(g); else break; } assert.equal(g.pendingKull, null); assert.ok(!g.pendingTreacheryDiscard); if (result === 'success') { // Preventing one Karama does not consume the other players' original // chance to respond to the still-pending Richese alliance gift. assert.equal(g.response?.kind, 'richeseGift'); assert.equal(g.pendingRicheseGift?.intent.cardId, f.gift); assert.ok(seat(g, 'r').hand.some(card => card.id === f.gift)); for (let n = 0; g.response?.kind === 'richeseGift' && n < g.players.length; n++) { const response = g.response; const actor = g.players.find(p => !response.passed.includes(p.id)); assert.ok(actor); g = act(g, actor.id, { type: 'passResponse' }); } assert.notEqual(g.response?.kind, 'richeseGift'); } assert.equal(g.pendingRicheseGift, null); assert.deepEqual(inventory(g), f.originalCards); const succeeded = result === 'success'; assert.equal(seat(g, 'emperor').hand.some(card => card.id === f.gift), succeeded); assert.equal(seat(g, 'r').hand.some(card => card.id === f.gift), !succeeded); assert.equal(seat(g, 'atreides').hand.some(card => card.id === f.karama), succeeded); assert.equal(seat(g, 'choam').hand.some(card => card.id === f.kull), !succeeded); assert.equal(g.turn, f.beforeGift.turn); assert.equal(g.phase, f.beforeGift.phase); }, );