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 { territory } from '../game/board'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; import { nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade'; const act = (game: Game, actor: string, action: Action) => applyAction(game, actor, action); function seat(game: Game, id: string) { const player = game.players.find(p => p.id === id); assert.ok(player); return player; } const clean = (game: Game) => !game.response && !game.decision && !game.phaseOpening && !game.truthtrance && !game.pendingTreacheryDiscard; function cards(game: Game) { const ids = [...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(p => p.hand)].map(card => card.id).sort(); assert.equal(new Set(ids).size, ids.length); return ids; } function advance(state: Game, until: (game: Game) => boolean): Game { let game = state; for (let n = 0; n < 2000; n++) { if (until(game)) return game; assert.notEqual(game.status, 'finished', 'Native journey must reach the auction'); if (game.status === 'setup' && game.setupStage === 'forces' && seat(game, 'ecaz').reserves === 20) game = act(game, 'ecaz', { type: 'ecazSetup', placements: { 'imperial_basin:9': 6 } }); else if (game.decision?.kind === 'ecazPlacement' && !game.response && !game.phaseOpening) game = act(game, 'ecaz', { type: 'decision', decline: true }); else { const next = nextSemutaNexusTradeNativeStep(game); game = act(game, next.actor, next.action); } } throw Error('Native journey did not reach its auction boundary'); } function passResponse(state: Game): Game { let game = state; while (game.response) { const player = game.players.find(p => !game.response!.passed.includes(p.id)); assert.ok(player); game = act(game, player.id, { type: 'passResponse' }); } return game; } /** A real first-turn Sandtrout breaks alliances. An Ecaz Ambassador then forms * the Emperor alliance; a land-only second Spice Blow leaves Sandtrout waiting. * Unopened Spice order, conserved board forces, cards and wallets are staged. */ function alliance() { let game = createGame('AUCTION-SANDTROUT', newPlayer('ecaz', 'Ecaz', 'ecaz'), false, ['choam', 'ix', 'ecaz']); for (const faction of ['emperor', 'richese', 'choam', 'atreides'] as const) joinGame(game, newPlayer(faction, faction, faction)); for (const p of game.players) game = act(game, p.id, { type: 'ready' }); game = act(game, 'ecaz', { type: 'start' }); game = advance(game, g => g.status === 'playing'); const original = cards(game); for (const p of game.players) { p.reserves += Object.values(p.forces).reduce((a, b) => a + b, 0); p.forces = {}; } game = advance(game, beforeIxSpiceDraw); orderIxSpiceCards(game, ['sandtrout', 'land']); game = advance(game, g => g.phase === 4 && !g.response && !g.phaseOpening && g.decision?.kind === 'ecazPlacement'); assert.equal(game.sandtrout, true); const location = ['habbanya_ridge_sietch', 'sietch_tabr', 'carthag', 'arrakeen', 'tueks_sietch'] .map(id => ({ territory: id, sector: territory(id).sectors.find(s => s !== game.storm) })) .find((row): row is { territory: string; sector: number } => row.sector !== undefined); assert.ok(location); const token = game.ecazAmbassadors!.tokens.find(row => row.effect === 'ecaz'); assert.ok(token); game = act(game, 'ecaz', { type: 'decision', token: token.id, territory: location.territory }); game = advance(game, g => g.phase === 5 && clean(g) && g.active === 'emperor'); game = act(game, 'emperor', { type: 'ship', amount: 1, ...location }); game = advance(game, g => !g.response && g.pendingAmbassador?.stage === 'offer'); const event = game.pendingAmbassador!.event; game = act(game, 'ecaz', { type: 'decision', event, trigger: true, beneficiary: 'ecaz', choice: 'alliance' }); game = act(game, 'emperor', { type: 'decision', event, accept: true }); game = act(game, 'ecaz', { type: 'decision', event, loan: false }); assert.equal(seat(game, 'ecaz').ally, 'emperor'); assert.equal(seat(game, 'emperor').ally, 'ecaz'); game = advance(game, g => g.turn === 2 && beforeIxSpiceDraw(g)); orderIxSpiceCards(game, ['land', 'land']); game = advance(game, g => g.phase === 3 && g.phaseOpening?.initialize === true); assert.equal(game.sandtrout, true); assert.equal(seat(game, 'ecaz').ally, 'emperor'); // Leave room for a purchased card and allocate a distinct physical Karama to the countering seat. for (const p of game.players) game.deck.push(...p.hand.splice(0)); const index = game.deck.findIndex(card => card.effect === 'karama'); assert.ok(index >= 0); const [counter] = game.deck.splice(index, 1); seat(game, 'atreides').hand.push(counter); seat(game, 'ecaz').spice = 7; seat(game, 'emperor').spice = 8; assert.deepEqual(cards(game), original); return { game, counter: counter.id, original }; } function fixture(source: 'onceAround' | 'silent' | 'ordinary') { const result = alliance(); let game = result.game; if (source !== 'ordinary') { game = advance(game, g => g.decision?.kind === 'richeseCache'); const card = game.richeseCache![0]; assert.ok(card); game = act(game, 'richese', { type: 'decision', event: game.richeseBidding!.event, card: card.id, method: source, ...(source === 'onceAround' ? { direction: 'clockwise' as const } : {}) }); game = act(game, 'emperor', { type: 'pledgeAid', amount: 1 }); let bid = false; for (let n = 0; !game.pendingEmperorAuctionGift && n < 300; n++) { const lot = game.richeseAuction; if (lot && !lot.outcome && !game.response && !game.decision && lot.method === 'onceAround' && lot.active) { const buyer = lot.active === 'ecaz' && !bid; game = act(game, lot.active, { type: 'richeseBid', event: lot.event, amount: buyer ? 3 : null, ...(buyer ? { allyPayment: 1 } : {}) }); if (buyer) bid = true; } else if (lot && !lot.outcome && !game.response && !game.decision && lot.method === 'silent') { const actor = lot.eligible.find(id => !Object.hasOwn(lot.sealed, id)); assert.ok(actor); const buyer = actor === 'ecaz' && !bid; game = act(game, actor, { type: 'richeseBid', event: lot.event, amount: buyer ? 3 : 0, ...(buyer ? { allyPayment: 1 } : {}) }); if (buyer) bid = true; } else { const next = nextSemutaNexusTradeNativeStep(game); game = act(game, next.actor, next.action); } } assert.ok(bid); assert.ok(game.pendingEmperorAuctionGift, 'The original cache sale exposes an Emperor opportunity'); return { ...result, game, card: card.id }; } game = advance(game, g => g.phase === 3 && !!g.auction && !g.richeseAuction && clean(g)); while (game.auction!.active !== 'ecaz') game = act(game, game.auction!.active, { type: 'passBid' }); const card = game.auction!.cards[game.auction!.index].id; game = act(game, 'emperor', { type: 'pledgeAid', amount: 1 }); game = act(game, 'ecaz', { type: 'bid', amount: 3, allyPayment: 1 }); for (let n = 0; game.auction && !game.response && !game.decision && n < 15; n++) game = act(game, game.auction.active, { type: 'passBid' }); assert.equal(game.decision?.kind, 'auctionPayment'); assert.ok(game.pendingEmperorAuctionGift, 'The original ordinary auction exposes an Emperor opportunity'); return { ...result, game, card }; } for (const source of ['onceAround', 'silent', 'ordinary'] as const) for (const cancel of [false, true]) void test( `Basic ${source} auction Emperor gift with genuine waiting Sandtrout ${cancel ? 'cancels' : 'allows'} and continues`, () => { const f = fixture(source), before = structuredClone(f.game); const offer = viewGame(f.game, 'emperor').emperorAuctionGift; assert.ok(offer); assert.equal(offer.recipient, 'ecaz'); assert.equal(before.aid.emperor?.amount, 1); if (source === 'ordinary') { assert.equal(before.auction?.bid, 3); assert.equal(before.auction?.allyPayment, 1); } else assert.ok(before.richeseFunding?.ecaz); let game = act(f.game, 'emperor', { type: 'emperorGift', amount: 2 }); if (cancel) { game = act(game, 'atreides', { type: 'card', mode: 'cancel', card: f.counter }); if (game.pendingKull) game = act(game, 'choam', { type: 'kullDecision', event: game.pendingKull.event, decline: true }); while (game.pendingTreacheryDiscard?.reaction?.stage === 'offer') { const reaction = game.pendingTreacheryDiscard.reaction; const player = game.players.find(p => !reaction.passed.includes(p.id)); assert.ok(player); game = act(game, player.id, { type: 'semutaPass', event: game.pendingTreacheryDiscard.batch.event }); } } else game = passResponse(game); assert.equal(game.sandtrout, true); assert.equal(seat(game, 'ecaz').spice, seat(before, 'ecaz').spice + (cancel ? 0 : 2)); assert.equal(seat(game, 'emperor').spice, seat(before, 'emperor').spice - (cancel ? 0 : 2)); assert.deepEqual(game.richeseAuction, before.richeseAuction); assert.deepEqual(game.auction, before.auction); assert.deepEqual(game.richeseFunding, before.richeseFunding); assert.deepEqual(game.aid, before.aid); assert.equal(viewGame(game, 'emperor').emperorAuctionGift?.event, offer.event); game = act(game, 'emperor', { type: 'emperorAuctionGiftContinue', event: offer.event }); if (game.decision?.kind === 'auctionPayment') game = act(game, 'ecaz', { type: 'decision', karama: false }); game = passResponse(game); assert.ok(seat(game, 'ecaz').hand.some(card => card.id === f.card)); assert.equal(seat(game, 'ecaz').spice, seat(before, 'ecaz').spice + (cancel ? 0 : 2) - 2); assert.equal(game.aid.emperor?.amount ?? 0, 0); if (source === 'ordinary') assert.equal(seat(game, 'emperor').spice, seat(before, 'emperor').spice - (cancel ? 0 : 2) + 3); else { assert.equal(seat(game, 'emperor').spice, seat(before, 'emperor').spice - (cancel ? 0 : 2)); assert.equal(seat(game, 'richese').spice, seat(before, 'richese').spice + 3); } assert.equal(game.sandtrout, true); assert.deepEqual(cards(game), f.original); });