import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, viewGame, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { actTruthBid as act, advanceTruthBid as advance, truthBidClean as clean, truthBidInventory as inventory, truthBidPlayer as player } from './fixture-truth-bid'; import { createTruthTurnBidFixture } from './fixture-truth-turn-bid'; /** Public Basic Harkonnen/Atreides/Emperor setup. The two Truthtrances and * initial purses are genuinely dealt; only unopened Spice order is staged. */ 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)); assert.equal(player(game, f.target).spice, 10); assert.ok(player(game, f.target).reserves >= 4); 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', 'shipment'] as const) for (const affordable of [true, false]) { void test(`Basic phase bid and paid shipment 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 shipment = {kind:'shipment', target:f.target, territory:'carthag', minimum:4}; const questions = first === 'bid' ? [bid, shipment] : [shipment, 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).truthShipmentAnswers; 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).truthShipmentAnswers : viewGame(game, f.target).truthPhaseBidAnswers; assert.ok(secondAnswers?.includes('no')); assert.equal(secondAnswers?.includes('yes'), affordable, 'The same spice cannot fund the bid and shipment 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); const withoutPromises = structuredClone(game); withoutPromises.phaseBidPromises = []; withoutPromises.shipmentPromises = []; 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); game = advance(game, state => clean(state) && state.phase === 3 && state.auction?.active === f.target); for (const difficulty of DIFFICULTIES) { const view = viewGame(game, f.target); view.players.find(row => row.id === f.target)!.bot = difficulty; const action = botActions(view)[0]; assert.ok(action); assert.doesNotThrow(() => applyAction(game, f.target, action), `${difficulty} retains a legal bid response`); } 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 === 5 && state.active === f.target); assert.equal(player(game, f.target).spice, 4, 'The original winning bid is paid once'); const action = viewGame(game, f.target).shipmentCompletion?.actions.at(-1); assert.ok(action && action.type === 'ship'); assert.equal(action.amount, 4); game = act(game, f.target, action); game = advance(game, state => clean(state) && !!state.shipmentPromises?.some(row => row.player === f.target && row.fulfilled)); assert.equal(player(game, f.target).spice, 0); assert.equal(player(game, f.target).forces['carthag:11'], 4); assert.deepEqual(inventory(game), f.originalCards); }); }