import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, createGame, newPlayer, viewGame, type Action, type Game, } from '../game/engine'; import { baseDeck } from '../game/cards'; import { richeseCards } from '../game/richese-cards'; import type { RicheseAuctionMethod } from '../game/richese-auction'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; const seat = (g: Game, id: string) => g.players.find(p => p.id === id)!; const physical = (g: Game) => [ ...g.deck, ...g.discard, ...g.players.flatMap(p => p.hand), ...(g.richeseCache ?? []), ...(g.richeseRemoved ?? []), ...(g.auction?.cards.slice(g.auction.index) ?? []), ].map(c => c.id).sort(); function act(g: Game, id: string, action: Action) { const before = structuredClone(g), inventory = physical(g); const next = applyAction(g, id, action); assert.deepEqual(g, before); assert.deepEqual(physical(next), inventory); assert.equal(new Set(inventory).size, inventory.length); return next; } function hold(g: Game, id: string, predicate: (card: Game['deck'][number]) => boolean) { const index = g.deck.findIndex(predicate); assert.ok(index >= 0); const card = g.deck.splice(index, 1)[0]; seat(g, id).hand.push(card); return card.id; } /** Conserved staged Advanced Spice Blow position; all auction actions below are native. */ function offered(method: RicheseAuctionMethod, extraSellerCard: boolean) { let g = createGame('RESERVED', newPlayer('r', 'Richese', 'richese')); g.players.push(newPlayer('h', 'Harkonnen', 'harkonnen'), newPlayer('e', 'Emperor', 'emperor')); Object.assign(g, { status: 'playing', phase: 2, turn: 2, advanced: true, order: ['e', 'h', 'r'], active: null, ready: [], deck: baseDeck(), richeseCache: richeseCards() }); for (const p of g.players) Object.assign(p, { hand: [], spice: 20, forces: {}, traitors: [] }); const reserved = hold(g, 'r', card => card.kind === 'shield'); if (extraSellerCard) hold(g, 'r', card => card.kind === 'snooper'); const karama = hold(g, 'h', card => card.effect === 'karama'); const unrelated = hold(g, 'e', card => card.kind === 'projectile'); for (const id of g.order) g = act(g, id, { type: 'ready' }); assert.equal(g.decision?.kind, 'richeseBlackMarket'); g = act(g, 'r', { type: 'decision', event: g.richeseBidding!.event, card: reserved, method, direction: 'clockwise', claim: 'A card' }); assert.equal(g.richeseAuction?.source, 'blackMarket'); assert.equal(g.richeseAuction?.cardId, reserved); return { g, reserved, karama, unrelated }; } function special(card: string, target: string): Action { return { type: 'card', mode: 'special', card, target, amount: 1 }; } function rejectSeller(g: Game, karama: string, reserved: string) { const before = structuredClone(g), inventory = physical(g); assert.throws(() => applyAction(g, 'h', special(karama, 'r')), /black market|reserved|lot/i); assert.deepEqual(g, before, 'Rejected special Karama leaves the complete original auction untouched.'); assert.deepEqual(physical(g), inventory); assert.ok(seat(g, 'r').hand.some(card => card.id === reserved)); assert.ok(seat(g, 'h').hand.some(card => card.id === karama)); assert.notEqual(seat(g, 'h').specialKaramaUsed, true); } function passMarketResponse(g: Game) { for (const id of g.order) if (g.response?.kind === 'richeseBlackMarket' && !g.response.passed.includes(id)) g = act(g, id, { type: 'passResponse' }); assert.equal(g.response, null); return g; } void test('Harkonnen cannot draw any card from a seller with a live reserved Black Market lot', () => { for (const method of ['normal', 'onceAround', 'silent'] as const) { for (const extraSellerCard of [false, true]) { const f = offered(method, extraSellerCard); rejectSeller(f.g, f.karama, f.reserved); const bidding = passMarketResponse(f.g); rejectSeller(bidding, f.karama, f.reserved); assert.deepEqual(bidding.richeseAuction, f.g.richeseAuction); } } }); void test('an unrelated exchange preserves the real Black Market auction and its reserved card through settlement', () => { const f = offered('silent', true); let g = passMarketResponse(f.g); const lot = structuredClone(g.richeseAuction); g = act(g, 'h', special(f.karama, 'e')); assert.equal(g.pendingExchange, null, 'The sole blind draw is automatically returned.'); assert.deepEqual(g.richeseAuction, lot); assert.ok(seat(g, 'e').hand.some(card => card.id === f.unrelated)); assert.ok(seat(g, 'r').hand.some(card => card.id === f.reserved)); const event = g.richeseAuction!.event; const eligible = [...g.richeseAuction!.eligible]; for (const id of eligible) g = act(g, id, { type: 'richeseBid', event, amount: id === 'e' ? 1 : 0, allyPayment: 0 }); assert.equal(g.richeseAuction, null); assert.ok(seat(g, 'e').hand.some(card => card.id === f.reserved)); assert.ok(!seat(g, 'r').hand.some(card => card.id === f.reserved)); assert.equal(g.richeseBidding?.blackMarketSold, true); }); void test('the public lot flag guides all four Harkonnen bot levels to an eligible unrelated target', () => { const f = offered('silent', true); const g = passMarketResponse(f.g); const before = structuredClone(g); for (const viewer of ['r', 'h', 'e']) { const lot = viewGame(g, viewer).richeseAuction!; assert.equal(lot.owner, 'r'); assert.equal(lot.harkonnenExchangeBlocked, true); if (viewer !== 'r') { assert.equal(lot.cardId, null); assert.equal(lot.card, null); } } for (const difficulty of DIFFICULTIES) { const view = viewGame(g, 'h'); view.players.find(p => p.id === 'h')!.bot = difficulty; const actions = botActions(view); const exchange = actions.find(action => action.type === 'card' && action.mode === 'special'); assert.ok(exchange, `${difficulty} should choose its available special exchange`); assert.equal(exchange.target, 'e', `${difficulty} must avoid the reserved seller`); assert.doesNotThrow(() => act(g, 'h', exchange)); } assert.deepEqual(g, before); });