import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import type { TruthQuestion } from '../game/truthtrance'; import { createBattleTerritoryExplosionFixture } from './fixture-battle-territory-explosion'; const act = (game: Game, actor: string, action: Action) => applyAction(game, actor, action); function player(game: Game, id: string) { const seat = game.players.find(candidate => candidate.id === id); assert.ok(seat); return seat; } function inventory(game: Game) { return [...game.deck, ...game.discard, ...game.players.flatMap(seat => seat.hand), ...(game.auction?.cards.slice(game.auction.index + Number(game.currentAuctionSale?.origin === 'normal')) ?? [])] .map(card => card.id).sort(); } function reject(game: Game, actor: string, action: Action) { const before = structuredClone(game); assert.throws(() => act(game, actor, action)); assert.deepEqual(game, before, 'A forbidden action must not alter its input'); } /** A genuine public Basic three-faction setup and native movement yields a * three-army fight in Imperial Basin. Only the existing physical Truthtrance * and one already-held card are exchanged between their real zones after that * native prefix; the closed original auctions and both physical card counts * remain unchanged. No battle, force, wallet or order state is assigned. */ function source() { const game = structuredClone(createBattleTerritoryExplosionFixture({advanced:false}).beforeFirst); assert.equal(game.phase, 6); assert.ok(!game.battle && game.active); const chooser = game.active, choices = viewGame(game, chooser).battleChoices.filter(choice => choice.chooser === chooser); assert.ok(choices.length >= 2, 'The actual current chooser has multiple real battles'); const [chosen, alternate] = choices; const target = (choice: typeof chosen) => choice.attacker === chooser ? choice.defender : choice.attacker; const asker = game.players.find(seat => seat.id !== chooser)!.id; const initial = inventory(game), hand = player(game, asker).hand; let truth = hand.find(card => card.effect === 'truthtrance'); if (!truth) { const replacement = hand[0]; assert.ok(replacement, 'The original dealt card supplies the conserved exchange'); const owner = game.players.find(seat => seat.id !== asker && seat.hand.some(card => card.effect === 'truthtrance')); if (owner) { const index = owner.hand.findIndex(card => card.effect === 'truthtrance'); truth = owner.hand[index]; owner.hand[index] = replacement; } else { const zone = [game.deck,game.discard].find(cards => cards.some(card => card.effect === 'truthtrance')); assert.ok(zone, 'The original Truthtrance must be in an actual hand, deck or discard'); const index = zone.findIndex(card => card.effect === 'truthtrance'); truth = zone[index]; zone[index] = replacement; } hand[0] = truth; } assert.deepEqual(inventory(game), initial); const event = viewGame(game, chooser).battleOrder?.event; assert.ok(event); const desired: Action = {type:'chooseBattle',territory:chosen.territory,target:target(chosen)}; const other: Action = {type:'chooseBattle',territory:alternate.territory,target:target(alternate)}; assert.notDeepEqual(desired, other); const question: TruthQuestion = {kind:'turnActions',target:chooser,turn:game.turn,mixed:{kind:'action', action:{kind:'battleChoice',source:event,territory:chosen.territory,target:target(chosen)}}}; return {game,chooser,asker,truth:truth.id,event,desired,other,question,initial}; } function ask(f: ReturnType, question: TruthQuestion = f.question, input: Game = f.game, asker = f.asker, truth = f.truth): Game { let game = act(input, asker, {type:'card',card:truth}); for (let count = 0; count < 8 && game.truthtrance?.stage === 'priority'; count++) { const seat = game.players.find(candidate => !game.truthtrance!.passed.includes(candidate.id)); assert.ok(seat); game = act(game, seat.id, {type:'truthPass'}); } assert.equal(game.truthtrance?.stage, 'ask'); game = act(game, asker, {type:'truthAsk',question}); return game; } function choiceQuestion(f: ReturnType, kind: 'and' | 'or'): TruthQuestion { const term = (action: Action) => ({kind:'action' as const, action:{kind:'battleChoice' as const, source:f.event,territory:action.territory as string,target:action.target as string}}); return {kind:'turnActions',target:f.chooser,turn:f.game.turn, mixed:{kind,terms:[term(f.desired),term(f.other)]}}; } void test('a real Basic chooser Yes rejects another battle and follows its exact private guide', () => { const f = source(), asked = ask(f); const game = act(asked, f.chooser, {type:'truthAnswer',answer:'yes'}); reject(game, f.chooser, f.other); const guide = viewGame(game, f.chooser).turnActionGuide; assert.ok(guide); assert.equal(guide.action.type, 'chooseBattle'); assert.equal(guide.action.territory, f.desired.territory); assert.equal(guide.action.target, f.desired.target); assert.equal(guide.action.turnActionPromiseEvent, guide.event); const chosen = act(game, f.chooser, guide.action); assert.equal(chosen.battle?.territory, f.desired.territory); assert.ok([chosen.battle?.attacker, chosen.battle?.defender].includes(f.desired.target as string)); assert.equal(chosen.turnActionPromises?.[0].fulfilled, true); assert.deepEqual(inventory(chosen), f.initial); }); void test('a real Basic chooser No forbids the named battle while preserving the other native choice', () => { const f = source(), game = act(ask(f), f.chooser, {type:'truthAnswer',answer:'no'}); reject(game, f.chooser, f.desired); const chosen = act(game, f.chooser, f.other); assert.equal(chosen.battle?.territory, f.other.territory); assert.ok([chosen.battle?.attacker, chosen.battle?.defender].includes(f.other.target as string)); assert.equal(chosen.turnActionPromises?.[0].fulfilled, true); assert.deepEqual(inventory(chosen), f.initial); }); void test('all four minimal bots answer and execute the real promised battle choice', () => { const f = source(), asked = ask(f); for (const difficulty of DIFFICULTIES) { const answering = viewGame(asked, f.chooser); answering.players.find(seat => seat.id === f.chooser)!.bot = difficulty; const answer = botActions(answering).find(action => action.type === 'truthAnswer'); assert.ok(answer, difficulty); const game = act(asked, f.chooser, answer); const view = viewGame(game, f.chooser); view.players.find(seat => seat.id === f.chooser)!.bot = difficulty; const options = botActions(view); assert.ok(options.length, difficulty); const legal = options.find(action => { try { act(game, f.chooser, action); return true; } catch { return false; } }); assert.ok(legal, `${difficulty} must offer at least one legal continuation`); const chosen = act(game, f.chooser, legal); assert.ok(chosen.battle, `${difficulty} must choose an actual battle`); assert.equal(chosen.turnActionPromises?.[0].fulfilled, true); assert.deepEqual(inventory(chosen), f.initial); } }); void test('two different current battle choices cannot both be chosen in one native opportunity', () => { const f = source(), asked = ask(f,choiceQuestion(f,'and')); const answers = viewGame(asked, f.chooser).truthTurnActionAnswers; assert.ok(answers); assert.ok(answers.includes('no')); assert.ok(!answers.includes('yes')); reject(asked, f.chooser, {type:'truthAnswer',answer:'yes'}); const bound = act(asked, f.chooser, {type:'truthAnswer',answer:'no'}); for (const choice of [f.desired,f.other]) { const chosen = act(bound, f.chooser, choice); assert.ok(chosen.battle); assert.equal(chosen.turnActionPromises?.[0].fulfilled, true); assert.deepEqual(inventory(chosen), f.initial); } }); void test('either real battle fulfills an OR of the two current choices', () => { const f = source(), asked = ask(f,choiceQuestion(f,'or')); const answers = viewGame(asked, f.chooser).truthTurnActionAnswers; assert.ok(answers?.includes('yes')); const bound = act(asked, f.chooser, {type:'truthAnswer',answer:'yes'}); const guide = viewGame(bound, f.chooser).turnActionGuide; assert.ok(guide); assert.equal(guide.action.type, 'chooseBattle'); assert.ok([f.desired,f.other].some(choice => choice.territory === guide.action.territory && choice.target === guide.action.target), 'The private guide must use one real qualifying choice'); for (const choice of [f.desired,f.other]) { const chosen = act(bound, f.chooser, choice); assert.ok(chosen.battle); assert.equal(chosen.turnActionPromises?.[0].fulfilled, true); assert.deepEqual(inventory(chosen), f.initial); } }); void test('an earlier accepted Yes excludes a later conflicting Yes while preserving the original choice', () => { const f = source(), secondAsker = f.game.players.find(seat => seat.id !== f.chooser && seat.id !== f.asker)!; const targetHand = secondAsker.hand; let second = targetHand.find(card => card.effect === 'truthtrance' && card.id !== f.truth); if (!second) { const replacement = targetHand[0]; assert.ok(replacement); const owner = f.game.players.find(seat => seat.id !== secondAsker.id && seat.hand.some(card => card.effect === 'truthtrance' && card.id !== f.truth)); if (owner) { const index = owner.hand.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); second = owner.hand[index]; owner.hand[index] = replacement; } else { const zone = [f.game.deck,f.game.discard].find(cards => cards.some(card => card.effect === 'truthtrance' && card.id !== f.truth)); assert.ok(zone); const index = zone.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); second = zone[index]; zone[index] = replacement; } targetHand[0] = second; } assert.deepEqual(inventory(f.game), f.initial, 'Both actual Truth cards are staged by conserved swaps'); let game = act(ask(f), f.chooser, {type:'truthAnswer',answer:'yes'}); const otherQuestion: TruthQuestion = {kind:'turnActions',target:f.chooser,turn:game.turn,mixed:{kind:'action', action:{kind:'battleChoice',source:f.event,territory:f.other.territory as string,target:f.other.target as string}}}; game = ask(f,otherQuestion,game,secondAsker.id,second.id); const answers = viewGame(game,f.chooser).truthTurnActionAnswers; assert.ok(answers); assert.ok(answers.includes('no')); assert.ok(!answers.includes('yes')); reject(game,f.chooser,{type:'truthAnswer',answer:'yes'}); game = act(game,f.chooser,{type:'truthAnswer',answer:'no'}); reject(game,f.chooser,f.other); const chosen = act(game,f.chooser,f.desired); assert.ok(chosen.battle); assert.equal(chosen.turnActionPromises?.length, 2); assert.ok(chosen.turnActionPromises?.every(promise => promise.fulfilled)); assert.deepEqual(inventory(chosen),f.initial); });