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 and actual opening. Only the * existing unopened Spice cards are reordered; both Truthtrances are dealt. */ 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 kind of ['movement','travel'] as const) for (const first of ['bid','movement'] as const) for (const affordable of [true,false]) { void test(`Basic phase bid and ${kind} 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 move = kind === 'movement' ? {kind:'movement',target:f.target,territory:'carthag',minimum:4} : {kind:'travel',target:f.target,turn:initial.turn,mixed:{kind:'and',terms:[ {kind:'action',action:{kind:'ship',territory:'arrakeen',minimum:4}}, {kind:'action',action:{kind:'move',territory:'carthag',minimum:4}}, ]}}; const questions = first === 'bid' ? [bid,move] : [move,bid]; const movementAnswers = (state:Game) => kind === 'movement' ? viewGame(state,f.target).truthMovementAnswers : viewGame(state,f.target).truthTravelAnswers; let game = ask(initial,f.asker,f.truths[0],questions[0]); const firstAnswers = first === 'bid' ? viewGame(game,f.target).truthPhaseBidAnswers : movementAnswers(game); assert.ok(firstAnswers?.includes('yes'),'Either single commitment is feasible'); game = act(game,f.target,{type:'truthAnswer',answer:'yes'}); game = ask(game,f.asker,f.truths[1],questions[1]); const answers = first === 'bid' ? movementAnswers(game) : viewGame(game,f.target).truthPhaseBidAnswers; assert.ok(answers?.includes('no')); assert.equal(answers?.includes('yes'),affordable,'Bid and physical shipment share the same purse'); 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'}); 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); const done = applyAction(game,f.target,action); assert.equal(done.phaseBidPromises?.some(row => row.player === f.target && row.fulfilled),true, `${difficulty} makes the required legal bid`); } 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 real winning invoice is paid once'); 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} can begin legal travel`); } game = advance(act(game,f.target,{type:'ship',territory:'arrakeen',sector:10,amount:4,elite:0,allyPayment:0}),clean); assert.equal(player(game,f.target).spice,0); game = advance(act(game,f.target,{type:'move',from:'arrakeen:10',territory:'carthag',sector:11,amount:4}),clean); assert.equal(player(game,f.target).forces['carthag:11'],4); assert.equal(kind === 'movement' ? game.movementPromises?.[0]?.fulfilled : game.travelPromises?.[0]?.fulfilled,true); assert.deepEqual(inventory(game),f.originalCards); }); } for (const first of ['bid','expression'] as const) { void test(`Basic phase bid and grouped movement in ${first}-first order keep a legal bot guide`, () => { const {f,game:initial} = fixture(); const bid = {kind:'phaseBid',target:f.target,turn:initial.turn,minimum:6}; const expression = {kind:'movementExpression',target:f.target,turn:initial.turn,claim:{kind:'or',terms:[ {kind:'move',territory:'carthag',minimum:4}, {kind:'move',territory:'imperial_basin',minimum:4}, ]}}; const questions = first === 'bid' ? [bid,expression] : [expression,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).truthMovementExpressionAnswers; assert.ok(firstAnswers?.includes('yes')); game = act(game,f.target,{type:'truthAnswer',answer:'yes'}); game = ask(game,f.asker,f.truths[1],questions[1]); const answers = first === 'bid' ? viewGame(game,f.target).truthMovementExpressionAnswers : viewGame(game,f.target).truthPhaseBidAnswers; assert.ok(answers?.includes('yes')); game = act(game,f.target,{type:'truthAnswer',answer:'yes'}); 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); const done = applyAction(game,f.target,action); assert.equal(done.phaseBidPromises?.some(row => row.player === f.target && row.fulfilled),true, `${difficulty} follows a legal required bid`); } game = act(game,f.target,{type:'bid',amount:6,allyPayment:0}); game = advance(game,state => clean(state) && state.phase === 5 && state.active === f.target); game = advance(act(game,f.target,{type:'ship',territory:'arrakeen',sector:10,amount:4,elite:0,allyPayment:0}),clean); 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} can make a legal movement choice`); } game = advance(act(game,f.target,{type:'move',from:'arrakeen:10',territory:'carthag',sector:11,amount:4}),clean); assert.equal(game.movementExpressionPromises?.[0]?.fulfilled,true); assert.equal(player(game,f.target).spice,0); assert.deepEqual(inventory(game),f.originalCards); }); } /** The public deal and both Truthtrance answers are real. Only three of the * Emperor's existing reserve forces are staged into Tanks to create a paid * Revival need; no cards, spice, accepted promises or force counters are added. */ for (const first of ['bid','revival'] as const) { void test(`Basic public travel deal, bid and Revival in ${first}-first order retain legal native bot actions`, () => { const {f,game:initial} = fixture(); player(initial,f.target).reserves -= 3; player(initial,f.target).tanks += 3; let game = act(initial,f.asker,{type:'dealOffer',target:f.target,terms:'Ship two forces to Arrakeen.', travel:{kind:'ship',territory:'arrakeen',minimum:2,answer:true}}); const deal = game.deals?.at(-1); assert.ok(deal); game = act(game,f.target,{type:'dealRespond',deal:deal.id,accept:true}); assert.equal(viewGame(game,f.target).dealTravelStatus[deal.id],'pending'); const bid = {kind:'phaseBid',target:f.target,turn:game.turn,minimum:4}; const revival = {kind:'revival',target:f.target,turn:game.turn,counter:'total',minimum:3}; const questions = first === 'bid' ? [bid,revival] : [revival,bid]; game = ask(game,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')); game = act(game,f.target,{type:'truthAnswer',answer:'yes'}); game = ask(game,f.asker,f.truths[1],questions[1]); const answers = first === 'bid' ? viewGame(game,f.target).truthRevivalAnswers : viewGame(game,f.target).truthPhaseBidAnswers; assert.ok(answers?.includes('yes'),'Four bid, four paid Revival and two shipment share ten spice'); game = act(game,f.target,{type:'truthAnswer',answer:'yes'}); 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.equal(applyAction(game,f.target,action).phaseBidPromises?.some(row => row.fulfilled),true); } game = act(game,f.target,{type:'bid',amount:4,allyPayment:0}); game = advance(game,state => clean(state) && state.phase === 4); 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.equal(action.type,'revive'); assert.equal(applyAction(game,f.target,action).revivalPromises?.some(row => row.fulfilled),true); } game = act(game,f.target,{type:'revive',amount:3,elite:0}); game = advance(game,state => clean(state) && state.phase === 5 && state.active === f.target); game = advance(act(game,f.target,{type:'ship',territory:'arrakeen',sector:10,amount:2,elite:0,allyPayment:0}),clean); assert.equal(viewGame(game,f.target).dealTravelStatus[deal.id],'fulfilled'); assert.equal(player(game,f.target).spice,0); assert.deepEqual(inventory(game),f.originalCards); }); }