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 { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { takeKullCard } from './fixture-choam-kull'; import { advanceSemutaNexusTrade, swapSemutaNexusTradeIdentity } from './fixture-semuta-nexus-trade'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; const act = (game: Game, actor: string, action: Action) => applyAction(game, actor, action); const seat = (game: Game, id: string) => { const player = game.players.find(row => row.id === id); assert.ok(player); return player; }; const clean = (game: Game) => !game.phaseOpening && !game.response && !game.decision && !game.truthtrance && !game.pendingGuildBetrayal; function inventory(game: Game): string[] { const cards = [...game.deck, ...game.discard, ...(game.richeseCache ?? []), ...(game.richeseRemoved ?? []), ...(game.auction?.cards.slice(game.auction.index + Number(game.currentAuctionSale?.origin === 'normal')) ?? []), ...(game.ixAuction?.cards ?? []), ...game.players.flatMap(player => player.hand)].map(card => card.id).sort(); assert.equal(new Set(cards).size, cards.length, 'Each original physical card has one location'); return cards; } /** Fresh public Basic setup and a genuinely earned CHOAM Nexus. Unopened * Spice order, the earned Nexus identity, original card custody and starting * board counters are staged without creating physical components. */ function fixture() { let game = createGame('NEXKULLSEMUTA', newPlayer('c', 'CHOAM', 'choam'), false, ['ix', 'choam']); for (const [id, faction] of [['r','richese'], ['a','atreides'], ['g','guild'], ['h','harkonnen']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'c', {type:'moduleOption', module:'nexusCards', enabled:true}); for (const player of game.players) game = act(game, player.id, {type:'ready'}); game = act(game, 'c', {type:'start'}); assert.equal(game.semutaPreview, true); assert.equal(game.kullPreview, true); assert.equal(game.nexusKullPreview, true); game = advanceSemutaNexusTrade(game, state => state.status === 'playing'); assert.ok(Object.values(game.nexusCards!.cards!.hands).every(card => card === null)); const original = inventory(game); for (const player of game.players) { player.reserves += Object.values(player.forces).reduce((sum, amount) => sum + amount, 0); player.forces = {}; if (player.elites) { player.elites.reserves += Object.values(player.elites.forces).reduce((sum, amount) => sum + amount, 0); player.elites.forces = {}; } } game = advanceSemutaNexusTrade(game, beforeIxSpiceDraw); orderIxSpiceCards(game, ['land','land']); game = advanceSemutaNexusTrade(game, state => state.turn === 2 && beforeIxSpiceDraw(state)); orderIxSpiceCards(game, ['worm','land','land']); game = advanceSemutaNexusTrade(game, state => !!state.nexus && !state.spiceWindow && clean(state)); game = act(game, 'a', {type:'alliance', target:'h'}); game = act(game, 'h', {type:'alliance', target:'a'}); game = advanceSemutaNexusTrade(game, state => state.nexusCards?.phase?.stage === 'drawing' && clean(state)); assert.ok(game.nexusCards!.phase!.eligible.includes('c')); game = act(game, 'c', {type:'nexusCardChoice', turn:game.turn, card:null, choice:'draw', ownRedraws:0}); swapSemutaNexusTradeIdentity(game, 'c', 'choam'); game = advanceSemutaNexusTrade(game, state => state.phase === 5 && state.active === 'g' && clean(state)); assert.equal(game.nexusCards!.cards!.hands.c, 'choam'); // Move only original cards; stage after prior CHOAM market actions finish. for (const player of game.players) game.deck.push(...player.hand.splice(0)); const printed = takeKullCard(game, 'ix-kull-wahad'); seat(game, 'c').hand.push(printed); const fuel = takeKullCard(game, 'ix-hunter-seeker'); seat(game, 'c').hand.push(fuel); const originalKarama = takeKullCard(game, 'karama'); seat(game, 'g').hand.push(originalKarama); const counter = takeKullCard(game, 'karama'); seat(game, 'h').hand.push(counter); const semuta = takeKullCard(game, SEMUTA_DRUG_ID); seat(game, 'r').hand.push(semuta); assert.deepEqual(inventory(game), original); game = act(game, 'g', {type:'card', mode:'shipment', card:originalKarama.id, target:'g'}); assert.equal(game.pendingKull?.stage, 'offer'); return {game, original, printed:printed.id, fuel:fuel.id, karama:originalKarama.id, counter:counter.id, semuta:semuta.id, event:game.pendingKull!.event}; } function finishCounter(state: Game): Game { let game = state; for (let n = 0; game.pendingKull && n < 20; n++) { const actor = game.players.find(player => {const controls = viewGame(game, player.id).responseControls; return controls && !controls.hasPassed && controls.cancelCards.length > 0;}); game = actor ? act(game, actor.id, {type:'passResponse'}) : normalizeAutomaticGame(game); } assert.equal(game.pendingKull, null); return game; } function passFreshEvent(state: Game): Game { let game = state; const event = game.pendingTreacheryDiscard?.batch.event; assert.ok(event); while (game.pendingTreacheryDiscard?.batch.event === event && game.pendingTreacheryDiscard.reaction?.stage === 'offer') { const reaction = game.pendingTreacheryDiscard.reaction; const actor = game.players.find(player => !reaction.passed.includes(player.id)); assert.ok(actor); game = act(game, actor.id, {type:'semutaPass', event}); } assert.notEqual(game.pendingTreacheryDiscard?.batch.event, event); return game; } for (const source of ['printed','nexus'] as const) for (const claim of [false,true]) void test( `Basic public Richese Nexus ${source} Kull success offers Semuta ${claim ? 'claim' : 'pass'}`, () => { const f = fixture(), cost = source === 'printed' ? f.printed : f.fuel; const controls = viewGame(f.game, 'c').kullReaction!; assert.ok(controls.plays.some(row => row.source === source && row.card.id === cost)); for (const difficulty of DIFFICULTIES) { const view = viewGame(f.game, 'c'); view.players.find(player => player.id === 'c')!.bot = difficulty; const choice = botActions(view).find(action => action.type === 'kullDecision'); assert.ok(choice); assert.doesNotThrow(() => act(f.game, 'c', choice)); } const startLog = f.game.log.length; let game = act(f.game, 'c', {type:'kullDecision', event:f.event, source, card:cost}); game = finishCounter(game); const pending = game.pendingTreacheryDiscard; assert.ok(pending); assert.equal(pending.reaction?.stage, 'offer'); assert.deepEqual(pending.batch.entries.map(entry => ({id:entry.card.id, discardedBy:entry.discardedBy, publicFace:entry.publicFace})), [{id:cost, discardedBy:'c', publicFace:source === 'printed'}]); assert.equal(game.karamaShipping, null); assert.ok(seat(game, 'g').hand.some(card => card.id === f.karama)); assert.equal(game.nexusCards!.cards!.hands.c, source === 'printed' ? 'choam' : null); assert.equal(game.nexusCards!.cards!.discard.filter(card => card === 'choam').length, Number(source === 'nexus')); if (source === 'nexus') { assert.deepEqual(viewGame(game, 'r').semutaReaction!.candidates, []); assert.deepEqual(viewGame(game, 'g').semutaReaction!.candidates, []); assert.equal(game.log.slice(startLog).some(row => row.text.includes('Hunter-Seeker')), false); assert.equal(game.nexusChoamHistory!.at(-1)!.stage, 'complete'); } assert.equal(viewGame(game, 'r').semutaReaction!.canCommit, true); const event = pending.batch.event; if (claim) { game = act(game, 'r', {type:'semutaCommit', event}); if (game.pendingTreacheryDiscard?.reaction?.stage === 'select') { assert.deepEqual(viewGame(game, 'r').semutaReaction!.candidates.map(card => card.id), [cost]); assert.deepEqual(viewGame(game, 'g').semutaReaction!.candidates, []); game = act(game, 'r', {type:'semutaSelect', event, card:cost}); } } else while (game.pendingTreacheryDiscard?.batch.event === event && game.pendingTreacheryDiscard.reaction?.stage === 'offer') { const reaction = game.pendingTreacheryDiscard.reaction; const actor = game.players.find(player => !reaction.passed.includes(player.id)); assert.ok(actor); game = act(game, actor.id, {type:'semutaPass', event}); } assert.equal(game.pendingTreacheryDiscard, null); assert.equal(game.pendingKull, null); assert.equal(game.karamaShipping, null); assert.ok(seat(game, 'g').hand.some(card => card.id === f.karama)); assert.ok(viewGame(game, 'g').karamaBlocked); assert.equal(seat(game, 'r').hand.some(card => card.id === cost), claim); assert.equal(game.discard.some(card => card.id === cost), !claim); assert.equal(game.discard.some(card => card.id === f.semuta), claim); assert.deepEqual(inventory(game), f.original); }); for (const source of ['printed','nexus'] as const) for (const claim of [false,true]) void test( `Basic public Richese Nexus ${source} Kull counter offers its distinct Karama to Semuta ${claim ? 'claim' : 'pass'}`, () => { const f = fixture(), cost = source === 'printed' ? f.printed : f.fuel; let game = act(f.game, 'c', {type:'kullDecision', event:f.event, source, card:cost}); assert.ok(viewGame(game, 'h').responseControls?.cancelCards.includes(f.counter)); game = act(game, 'h', {type:'card', mode:'cancel', card:f.counter}); const pending = game.pendingTreacheryDiscard; assert.ok(pending); assert.equal(pending.reaction?.stage, 'offer'); assert.deepEqual(pending.batch.entries.map(entry => ({id:entry.card.id, discardedBy:entry.discardedBy, publicFace:entry.publicFace})), [{id:f.counter, discardedBy:'h', publicFace:true}]); assert.ok(seat(game, 'c').hand.some(card => card.id === cost)); assert.ok(seat(game, 'g').hand.some(card => card.id === f.karama)); assert.equal(game.nexusCards!.cards!.hands.c, source === 'printed' ? 'choam' : null); const event = pending.batch.event; if (claim) { game = act(game, 'r', {type:'semutaCommit', event}); if (game.pendingTreacheryDiscard?.reaction?.stage === 'select') game = act(game, 'r', {type:'semutaSelect', event, card:f.counter}); } else while (game.pendingTreacheryDiscard?.batch.event === event && game.pendingTreacheryDiscard.reaction?.stage === 'offer') { const reaction = game.pendingTreacheryDiscard.reaction; const actor = game.players.find(player => !reaction.passed.includes(player.id)); assert.ok(actor); game = act(game, actor.id, {type:'semutaPass', event}); } // The original allowed Karama is a distinct fresh public discard after the // counter's own batch; acknowledge it before checking shipment continuation. assert.notEqual(game.pendingTreacheryDiscard?.batch.event, event); assert.equal(game.pendingTreacheryDiscard?.batch.entries[0].card.id, f.karama); game = passFreshEvent(game); assert.equal(game.pendingTreacheryDiscard, null); assert.equal(game.pendingKull, null); assert.equal(game.karamaShipping?.owner, 'g'); assert.ok(seat(game, 'c').hand.some(card => card.id === cost)); assert.equal(seat(game, 'r').hand.some(card => card.id === f.counter), claim); assert.equal(game.discard.some(card => card.id === f.counter), !claim); assert.equal(game.discard.some(card => card.id === f.semuta), claim); assert.equal(game.nexusCards!.cards!.discard.filter(card => card === 'choam').length, Number(source === 'nexus')); assert.deepEqual(inventory(game), f.original); }); void test('Basic public Richese Nexus Kull may decline without spending its cards', () => { const f = fixture(); let game = act(f.game, 'c', {type:'kullDecision', event:f.event, decline:true}); assert.equal(game.pendingTreacheryDiscard?.batch.entries[0].card.id, f.karama); game = passFreshEvent(game); assert.equal(game.pendingKull, null); assert.equal(game.karamaShipping?.owner, 'g'); assert.equal(game.nexusCards!.cards!.hands.c, 'choam'); assert.ok(seat(game, 'c').hand.some(card => card.id === f.printed)); assert.ok(seat(game, 'c').hand.some(card => card.id === f.fuel)); assert.ok(seat(game, 'r').hand.some(card => card.id === f.semuta)); assert.deepEqual(inventory(game), f.original); }); void test('Basic public Richese Nexus keeps Semuta when the host opts out of Kull', () => { let game = createGame('NEXKULLOPTOUT', newPlayer('c', 'CHOAM', 'choam'), false, ['ix', 'choam']); for (const [id, faction] of [['r','richese'], ['a','atreides'], ['g','guild'], ['h','harkonnen']] as const) joinGame(game, newPlayer(id, faction, faction)); game = act(game, 'c', {type:'moduleOption', module:'nexusCards', enabled:true}); game = act(game, 'c', {type:'moduleOption', module:'kullPreview', enabled:false}); for (const player of game.players) game = act(game, player.id, {type:'ready'}); game = act(game, 'c', {type:'start'}); assert.equal(game.semutaPreview, true); assert.notEqual(game.kullPreview, true); assert.notEqual(game.nexusKullPreview, true); game = advanceSemutaNexusTrade(game, state => state.status === 'playing'); const original = inventory(game); for (const player of game.players) { player.reserves += Object.values(player.forces).reduce((sum, amount) => sum + amount, 0); player.forces = {}; if (player.elites) { player.elites.reserves += Object.values(player.elites.forces).reduce((sum, amount) => sum + amount, 0); player.elites.forces = {}; } } game = advanceSemutaNexusTrade(game, beforeIxSpiceDraw); orderIxSpiceCards(game, ['land','land']); game = advanceSemutaNexusTrade(game, state => state.phase === 5 && state.active === 'g' && clean(state)); // Conserved original hand/deck/cache cards; Hajr and Semuta are used by // their real owners after public setup and the actual movement opening. for (const player of game.players) game.deck.push(...player.hand.splice(0)); const hajr = takeKullCard(game, 'hajr'); seat(game, 'g').hand.push(hajr); const semuta = takeKullCard(game, SEMUTA_DRUG_ID); seat(game, 'r').hand.push(semuta); assert.deepEqual(inventory(game), original); const pending = act(game, 'g', {type:'card', card:hajr.id}); assert.equal(pending.pendingTreacheryDiscard?.batch.cause, 'ordinary:hajr'); assert.equal(viewGame(pending, 'r').semutaReaction?.canCommit, true); for (const claim of [false,true]) { let resumed = structuredClone(pending); const event = pending.pendingTreacheryDiscard!.batch.event; if (claim) { resumed = act(resumed, 'r', {type:'semutaCommit', event}); if (resumed.pendingTreacheryDiscard?.reaction?.stage === 'select') resumed = act(resumed, 'r', {type:'semutaSelect', event, card:hajr.id}); } else resumed = passFreshEvent(resumed); assert.equal(resumed.pendingTreacheryDiscard, null); assert.ok(resumed.hajr.includes('g')); assert.equal(seat(resumed, 'r').hand.some(card => card.id === hajr.id), claim); assert.equal(resumed.discard.some(card => card.id === semuta.id), claim); assert.deepEqual(inventory(resumed), original); resumed = act(resumed, 'g', {type:'ship', territory:'polar_sink', sector:0, amount:1}); assert.equal((seat(resumed, 'g').forces.polar_sink ?? 0) + (seat(resumed, 'g').forces['polar_sink:0'] ?? 0), 1); } });