import assert from 'node:assert/strict'; import test from 'node:test'; import { normalizeAutomaticGame, viewGame, type Game } from '../game/engine'; import { forceRevivalQuote } from '../game/revival'; import { actTruthBid as act, advanceTruthBid as advance, truthBidClean as clean, truthBidInventory as inventory, truthBidPlayer as player } from './fixture-truth-bid'; import { advanceTruthRevival, truthRevivalClean } from './fixture-truth-revival'; import { createTruthTurnBidFixture } from './fixture-truth-turn-bid'; /** Public Harkonnen/Atreides/Emperor setup, with its real starting deal and * Charity phase. Three existing Emperor reserve counters are moved to Tanks; * the unopened Spice order is staged, without adding money or cards. */ function fixture() { const f = createTruthTurnBidFixture(); let game = structuredClone(f.afterSetup); for (const {position, from, card} of f.spiceOrder) { const [selected] = game.spiceDeck.splice(from, 1); assert.deepEqual(selected, card); game.spiceDeck.splice(position, 0, selected); } game = advance(game, state => state.turn === 1 && state.phase === 2 && clean(state)); const emperor = player(game, f.target); assert.equal(emperor.spice, 10); assert.ok(emperor.reserves >= 3); emperor.reserves -= 3; emperor.tanks += 3; assert.deepEqual(inventory(game), f.originalCards); return {f, game}; } function ask(game: Game, asker: string, card: string, question: object): Game { game = act(game, asker, {type:'card', card}); for (let count = 0; game.truthtrance?.stage === 'priority' && count < game.players.length; count++) { const seat = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(seat); game = act(game, seat.id, {type:'truthPass'}); } assert.equal(game.truthtrance?.stage, 'ask'); return act(game, asker, {type:'truthAsk', question}); } for (const first of ['bid', 'revival'] as const) for (const affordable of [true, false]) { void test(`Basic phase bid and paid Revival in ${first}-first order ${affordable ? 'share enough spice' : 'cannot overcommit spice'}`, () => { const {f, game: initial} = fixture(); const bid = {kind:'phaseBid', target:f.target, turn:initial.turn, minimum:affordable ? 6 : 7}; const revival = {kind:'revival', target:f.target, turn:initial.turn, counter:'total', minimum:3}; const questions = first === 'bid' ? [bid, revival] : [revival, bid]; let game = ask(initial, f.asker, f.truths[0], questions[0]); const firstAnswers = first === 'bid' ? viewGame(game, f.target).truthPhaseBidAnswers : viewGame(game, f.target).truthRevivalAnswers; assert.ok(firstAnswers?.includes('yes'), 'Each single obligation is feasible by itself'); game = act(game, f.target, {type:'truthAnswer', answer:'yes'}); game = ask(game, f.asker, f.truths[1], questions[1]); const secondAnswers = first === 'bid' ? viewGame(game, f.target).truthRevivalAnswers : viewGame(game, f.target).truthPhaseBidAnswers; assert.ok(secondAnswers?.includes('no')); assert.equal(secondAnswers?.includes('yes'), affordable, 'The same spice cannot be reserved twice'); if (!affordable) { const before = structuredClone(game); assert.throws(() => act(game, f.target, {type:'truthAnswer', answer:'yes'})); assert.deepEqual(game, before); return; } game = act(game, f.target, {type:'truthAnswer', answer:'yes'}); assert.equal(game.truthtrance, null); // A voluntary public payment cannot spend spice needed by the two Yes // answers. The rejected action must leave both original records intact. const withoutPromises = structuredClone(game); withoutPromises.phaseBidPromises = []; withoutPromises.revivalPromises = []; assert.doesNotThrow(() => act(withoutPromises, f.target, {type:'bribe', target:f.asker, amount:1})); const beforeSpend = structuredClone(game); assert.throws(() => act(game, f.target, {type:'bribe', target:f.asker, amount:1})); assert.deepEqual(game, beforeSpend); // Simulate an external debit by transferring one existing spice; this is // not a player-authorized spending action or evidence of a native debit // route. Preserve the existing unknown-future policy: an incomplete // planner alone cannot declare the phase bid released. const externallyChanged = structuredClone(game); player(externallyChanged, f.target).spice -= 1; player(externallyChanged, f.asker).spice += 1; const settled = normalizeAutomaticGame(externallyChanged); const pendingBid = settled.phaseBidPromises?.find(row => row.player === f.target && row.minimum === 6); assert.ok(pendingBid); assert.equal(pendingBid.released ?? false, false); assert.equal(pendingBid.fulfilled ?? false, false); assert.equal(player(settled, f.target).spice, 9); assert.deepEqual(inventory(settled), f.originalCards); game = advance(game, state => clean(state) && state.phase === 3 && state.auction?.active === f.target); game = act(game, f.target, {type:'bid', amount:6, allyPayment:0}); assert.ok(game.phaseBidPromises?.some(row => row.player === f.target && row.fulfilled)); game = advance(game, state => clean(state) && state.phase === 4); assert.equal(player(game, f.target).spice, 4, 'The real winning invoice consumed six'); const quote = forceRevivalQuote(game, player(game, f.target), 3); assert.equal(quote.free, 1); assert.equal(quote.cost, 4); game = act(game, f.target, {type:'revive', amount:3, elite:0}); game = advanceTruthRevival(game, truthRevivalClean); assert.equal(player(game, f.target).spice, 0); assert.equal(player(game, f.target).tanks, 0); assert.ok(game.revivalPromises?.some(row => row.player === f.target && row.fulfilled)); assert.deepEqual(inventory(game), f.originalCards); }); }