import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance, createTruthtranceRichesePromisesFixture as fixture, passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player, type TruthtranceRichesePromisesFixture, } from './fixture-truthtrance-richese-promises'; type Answer = 'yes' | 'no'; type Kind = 'phaseAnyBid' | 'earlyRicheseBid'; /** The real public-start Richese/Atreides/Emperor fixture orders unopened * setup and Spice cards. For two actual questions, move one additional original * Truthtrance from the unopened deck into Atreides' hand; no wallets, auctions, * or receipts are fabricated. */ function source() { const f = fixture({ advanced: false }); const game = structuredClone(f.earlySource); const at = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(at >= 0, 'A second original Truthtrance must remain in the unopened deck'); const [second] = game.deck.splice(at, 1); player(game, f.asker).hand.push(second); assert.deepEqual(inventory(game), f.originalCards); return { f, game, second: second.id }; } function openQuestion(game: Game, f: TruthtranceRichesePromisesFixture, card: string, kind: Kind, minimum: number) { game = act(game, f.asker, { type: 'card', card }); game = advance(game, state => state.truthtrance?.stage === 'ask'); return act(game, f.asker, { type: 'truthAsk', question: { kind, target: f.emperor, turn: game.turn, minimum } }); } function options(game: Game, f: TruthtranceRichesePromisesFixture, kind: Kind) { const view = viewGame(game, f.emperor); return kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers : view.truthEarlyRicheseBidAnswers; } function answer(game: Game, f: TruthtranceRichesePromisesFixture, kind: Kind, value: Answer) { assert.ok(options(game, f, kind)?.includes(value), `${kind} ${value} must be offered when jointly feasible`); return passTruthRicheseFreshDiscard(act(game, f.emperor, { type: 'truthAnswer', answer: value })); } function reject(game: Game, actor: string, action: Action) { const before = structuredClone(game); assert.throws(() => act(game, actor, action)); assert.deepEqual(game, before, 'A rejected declaration or stale guide preserves its source'); } for (const method of ['onceAround', 'silent'] as const) for (const position of ['first', 'last'] as const) for (const first of ['phaseAnyBid', 'earlyRicheseBid'] as const) for (const phaseAnswer of ['yes', 'no'] as const) for (const earlyAnswer of ['yes', 'no'] as const) void test( `Basic ${position} ${method}: ${first} first, phase ${phaseAnswer}, early ${earlyAnswer}`, () => { const { f, second, ...initial } = source(); // Both Yes requirements can share a bid of 6. A No caps at 6, // leaving the other Yes's bid of 4 possible in the same cache. const phaseMinimum = phaseAnswer === 'yes' ? 4 : 6; const earlyMinimum = earlyAnswer === 'yes' && phaseAnswer === 'yes' ? 6 : earlyAnswer === 'yes' ? 4 : 6; const secondKind: Kind = first === 'phaseAnyBid' ? 'earlyRicheseBid' : 'phaseAnyBid'; const minimum = (kind: Kind) => kind === 'phaseAnyBid' ? phaseMinimum : earlyMinimum; const response = (kind: Kind) => kind === 'phaseAnyBid' ? phaseAnswer : earlyAnswer; let game = openQuestion(initial.game, f, f.truth, first, minimum(first)); game = answer(game, f, first, response(first)); game = openQuestion(game, f, second, secondKind, minimum(secondKind)); game = answer(game, f, secondKind, response(secondKind)); const early = game.earlyRicheseBidPromises?.find(row => row.player === f.emperor); const phase = game.phaseAnyBidPromises?.find(row => row.player === f.emperor); assert.ok(early && phase); assert.equal(early.answer, earlyAnswer === 'yes'); assert.equal(phase.answer, phaseAnswer === 'yes'); assert.equal(early.boundEvent, undefined, 'The future cache has no invented event'); game = advance(game, state => clean(state) && !!state.richeseAuction && state.richeseAuction.source === 'cache' && state.richeseAuction.method === method && !state.richeseAuction.outcome, [], { position, method }); const lot = game.richeseAuction!; const event = lot.event, card = lot.cardId; assert.equal(game.earlyRicheseBidPromises?.find(row => row.id === early.id)?.boundEvent, event); if (method === 'onceAround') game = advance(game, state => clean(state) && state.richeseAuction?.active === f.emperor); const native = game.richeseBidPromises?.find(row => row.id === early.id); assert.ok(native); assert.equal(native.event, event); const view = viewGame(game, f.emperor); const guide = view.phaseAnyBidGuide?.action ?? view.richeseBidPromiseGuide?.action; assert.ok(guide, 'A jointly legal native guide is available'); assert.equal(guide.type, 'richeseBid'); assert.doesNotThrow(() => act(game, f.emperor, guide)); if (guide.phaseAnyBidPromiseEvent !== undefined) reject(game, f.emperor, { ...guide, phaseAnyBidPromiseEvent: 'stale-phase' }); if (guide.richeseBidPromiseId !== undefined) reject(game, f.emperor, { ...guide, richeseBidPromiseId: 'stale-cache' }); for (const difficulty of DIFFICULTIES) { const bot = viewGame(game, f.emperor); bot.players.find(row => row.id === f.emperor)!.bot = difficulty; const chosen = botActions(bot)[0]; assert.ok(chosen, difficulty); assert.doesNotThrow(() => act(game, f.emperor, chosen), difficulty); } if (phaseAnswer === 'no') reject(game, f.emperor, { type: 'richeseBid', event, amount: phaseMinimum, allyPayment: 0 }); if (earlyAnswer === 'no') reject(game, f.emperor, { type: 'richeseBid', event, amount: earlyMinimum, allyPayment: 0 }); if (earlyAnswer === 'yes') reject(game, f.emperor, { type: 'richeseBid', event, amount: method === 'silent' ? 0 : null }); const amount = earlyAnswer === 'yes' ? earlyMinimum : phaseAnswer === 'yes' ? phaseMinimum : 2; const before = player(game, f.emperor).spice; game = act(game, f.emperor, { type: 'richeseBid', event, amount, allyPayment: 0 }); assert.equal(player(game, f.emperor).spice, before, 'A declaration does not prepay the invoice'); assert.equal(!!game.phaseAnyBidPromises?.find(row => row.event === phase.event)?.fulfilled, phaseAnswer === 'yes'); assert.ok(game.richeseBidPromises?.find(row => row.id === early.id)?.fulfilled); game = advance(game, state => player(state, f.emperor).hand.some(item => item.id === card), [], { position, method }); assert.equal(player(game, f.emperor).spice, before - amount); assert.deepEqual(inventory(game), f.originalCards); }); for (const first of ['phaseAnyBid', 'earlyRicheseBid'] as const) void test( `A phase No 6 conflicts with an early cache Yes 6 when ${first} is asked first`, () => { const { f, second, ...initial } = source(); const secondKind: Kind = first === 'phaseAnyBid' ? 'earlyRicheseBid' : 'phaseAnyBid'; const firstAnswer = first === 'phaseAnyBid' ? 'no' : 'yes'; let game = openQuestion(initial.game, f, f.truth, first, 6); game = answer(game, f, first, firstAnswer); game = openQuestion(game, f, second, secondKind, 6); const impossible = secondKind === 'phaseAnyBid' ? 'no' : 'yes'; assert.ok(!options(game, f, secondKind)?.includes(impossible), 'A definite answer cannot demand a cache bid that its phase-wide No forbids'); reject(game, f.emperor, { type: 'truthAnswer', answer: impossible }); assert.deepEqual(inventory(game), f.originalCards); }); void test('a voluntary bribe preserves the common funded Yes witness', () => { const { f, second, ...initial } = source(); let game = openQuestion(initial.game, f, f.truth, 'phaseAnyBid', 6); game = answer(game, f, 'phaseAnyBid', 'yes'); game = openQuestion(game, f, second, 'earlyRicheseBid', 4); game = answer(game, f, 'earlyRicheseBid', 'yes'); reject(game, f.emperor, {type:'bribe',target:f.asker,amount:5}); game = act(game, f.emperor, {type:'bribe',target:f.asker,amount:4}); assert.equal(player(game, f.emperor).spice, 6); game = advance(game, state => clean(state) && !!state.richeseAuction && state.richeseAuction.source === 'cache' && state.richeseAuction.method === 'silent', [], {position:'first',method:'silent'}); const guide = viewGame(game, f.emperor).phaseAnyBidGuide?.action; assert.ok(guide); assert.equal(guide.type, 'richeseBid'); assert.equal(guide.amount, 6); game = act(game, f.emperor, guide); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.emperor && row.fulfilled)); assert.ok(game.richeseBidPromises?.some(row => row.player === f.emperor && row.fulfilled)); assert.deepEqual(inventory(game), f.originalCards); }); for (const position of ['first', 'last'] as const) for (const method of ['onceAround', 'silent'] as const) for (const first of ['phaseAnyBid', 'earlyRicheseBid'] as const) for (const earlyAnswer of ['yes', 'no'] as const) void test( `Basic ${position} ${method}: ${first} first, phase Yes 6 and cache ${earlyAnswer} 4 may use separately funded bids`, () => { const { f, second, ...initial } = source(); const secondKind: Kind = first === 'phaseAnyBid' ? 'earlyRicheseBid' : 'phaseAnyBid'; const min = (kind: Kind) => kind === 'phaseAnyBid' ? 6 : 4; const response = (kind: Kind) => kind === 'phaseAnyBid' ? 'yes' : earlyAnswer; let game = openQuestion(initial.game, f, f.truth, first, min(first)); game = answer(game, f, first, response(first)); game = openQuestion(game, f, second, secondKind, min(secondKind)); game = answer(game, f, secondKind, response(secondKind)); const originalSpice = player(game, f.emperor).spice; assert.equal(originalSpice, 10, 'Use the real Emperor starting funds'); const round = {position,method}; const cacheBid = earlyAnswer === 'yes' ? 4 : 2; const ordinary = (sourceGame: Game) => advance(sourceGame, state => clean(state) && !!state.auction && state.auction.active === f.emperor, [], round); const cache = (sourceGame: Game) => { let current = advance(sourceGame, state => clean(state) && !!state.richeseAuction && state.richeseAuction.source === 'cache' && !state.richeseAuction.outcome, [], round); if (method === 'onceAround') current = advance(current, state => clean(state) && state.richeseAuction?.active === f.emperor, [], round); return current; }; const payOrdinary = (current: Game) => { const guide = viewGame(current, f.emperor).phaseAnyBidGuide?.action; assert.ok(guide); assert.equal(guide.type, 'bid'); assert.equal(guide.amount, 6); if (earlyAnswer === 'yes' && position === 'last') reject(current, f.emperor, {type:'bid',amount:7,allyPayment:0}); const card = current.auction!.cards[current.auction!.index].id; const placed = act(current, f.emperor, guide); assert.ok(placed.phaseAnyBidPromises?.some(row => row.player === f.emperor && row.fulfilled)); return advance(placed, state => player(state, f.emperor).hand.some(item => item.id === card), [], round); }; const payCache = (current: Game) => { const lot = current.richeseAuction!, card = lot.cardId; if (earlyAnswer === 'no') reject(current, f.emperor, {type:'richeseBid',event:lot.event,amount:4,allyPayment:0}); else reject(current, f.emperor, {type:'richeseBid',event:lot.event,amount:method === 'silent' ? 0 : null}); const placed = act(current, f.emperor, {type:'richeseBid',event:lot.event,amount:cacheBid,allyPayment:0}); if (position === 'first') assert.ok(placed.phaseAnyBidPromises?.some(row => row.player === f.emperor && !row.fulfilled && !row.released)); assert.ok(placed.richeseBidPromises?.some(row => row.player === f.emperor && row.fulfilled)); return advance(placed, state => player(state, f.emperor).hand.some(item => item.id === card), [], round); }; game = position === 'first' ? payCache(cache(game)) : payOrdinary(ordinary(game)); game = position === 'first' ? payOrdinary(ordinary(game)) : payCache(cache(game)); assert.equal(player(game, f.emperor).spice, originalSpice - cacheBid - 6); assert.deepEqual(inventory(game), f.originalCards); });