import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { territory } from '../game/board'; import { botActions, runBots } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; import { truthTurnAmalInventory as inventory } from './fixture-truth-turn-amal'; const act = (game: Game, id: string, action: Action) => applyAction(game, id, action); function seat(game: Game, id: string) { const player = game.players.find(row => row.id === id); assert.ok(player); return player; } function advance(game: Game, until: (state: Game) => boolean): Game { for (let n = 0; n < 2000; n++) { if (until(game)) return game; assert.notEqual(game.status, 'finished'); if (game.status === 'setup' && game.setupStage === 'forces' && seat(game, 'ecaz').reserves === 20) game = act(game, 'ecaz', {type:'ecazSetup', placements:{'imperial_basin:9':6}}); else if (game.decision?.kind === 'ecazPlacement' && !game.response && !game.phaseOpening) game = act(game, 'ecaz', {type:'decision', decline:true}); else { const next = nextStrongholdFactionsNativeStep(game); assert.ok(next, `No continuation at ${game.phase}/${game.decision?.kind}`); game = act(game, next.actor, next.action); } } throw Error('Did not reach the requested native opening'); } function passResponses(game: Game): Game { for (let n = 0; game.response && n < 20; n++) { const actor = game.players.find(player => !game.response!.passed.includes(player.id)); assert.ok(actor); game = act(game, actor.id, {type:'passResponse'}); } assert.equal(game.response, null); return game; } /** Original public Basic Ecaz/Ix setup, real Sandtrout draw and actual Ecaz * Ambassador alliance before the next opening. Only the unopened Spice prefix, * conserved initial board forces and the counter card's physical custody are * staged; neither the waiting flag nor the alliance is installed directly. */ function fixture() { let game = createGame('OPENINGSANDTROUT', newPlayer('ecaz','Ecaz','ecaz'), false, ['ecaz','ix']); for (const id of ['emperor','atreides','guild'] as const) joinGame(game, newPlayer(id,id,id)); for (const player of game.players) game = act(game, player.id, {type:'ready'}); game = act(game, 'ecaz', {type:'start'}); game = advance(game, state => state.status === 'playing'); for (const player of game.players) { player.reserves += Object.values(player.forces).reduce((sum, count) => sum + count, 0); player.forces = {}; } game = advance(game, beforeIxSpiceDraw); orderIxSpiceCards(game, ['sandtrout','land']); game = advance(game, state => state.phase === 4 && !state.response && !state.phaseOpening && state.decision?.kind === 'ecazPlacement'); assert.equal(game.sandtrout, true); const spot = ['habbanya_ridge_sietch','sietch_tabr','carthag','arrakeen','tueks_sietch'] .map(id => ({territory:id, sector:territory(id).sectors.find(sector => sector !== game.storm)})) .find((row): row is {territory:string; sector:number} => row.sector !== undefined); assert.ok(spot); const ambassador = game.ecazAmbassadors!.tokens.find(row => row.effect === 'ecaz'); assert.ok(ambassador); game = act(game, 'ecaz', {type:'decision', token:ambassador.id, territory:spot.territory}); game = advance(game, state => !state.response && state.decision?.kind === 'ecazPlacement'); game = act(game, 'ecaz', {type:'decision', decline:true}); game = advance(game, state => state.phase === 5 && state.active === 'emperor' && !state.response && !state.decision && !state.phaseOpening); game = act(game, 'emperor', {type:'ship', amount:1, territory:spot.territory, sector:spot.sector}); game = advance(game, state => !state.response && state.pendingAmbassador?.stage === 'offer'); const event = game.pendingAmbassador!.event; game = act(game, 'ecaz', {type:'decision', event, trigger:true, beneficiary:'ecaz', choice:'alliance'}); game = act(game, 'emperor', {type:'decision', event, accept:true}); assert.equal(game.pendingAmbassador?.stage, 'loan'); game = act(game, 'ecaz', {type:'decision', event, loan:false}); assert.equal(seat(game, 'ecaz').ally, 'emperor'); assert.equal(seat(game, 'emperor').ally, 'ecaz'); game = advance(game, state => state.phase === 6 && !!state.phaseOpening); assert.equal(game.sandtrout, true); assert.ok(game.phaseOpening); const original = inventory(game), guild = seat(game, 'guild'); let counter = guild.hand.find(card => card.effect === 'karama'); if (!counter) { const source = game.players.find(player => player.id !== 'guild' && player.hand.some(card => card.effect === 'karama'))?.hand ?? game.deck; const index = source.findIndex(card => card.effect === 'karama'); assert.ok(index >= 0); if (guild.hand.length >= 4) game.deck.push(guild.hand.pop()!); [counter] = source.splice(index, 1); guild.hand.push(counter); } assert.deepEqual(inventory(game), original); game = act(game, 'emperor', {type:'ready'}); game = act(game, 'ecaz', {type:'ready'}); return {game, original, counter:counter.id}; } for (const cancel of [false, true]) void test(`Basic phase-opening gift with waiting Sandtrout ${cancel ? 'cancels' : 'settles'} and resumes`, () => { const f = fixture(), before = structuredClone(f.game), opening = structuredClone(f.game.phaseOpening); const owner = seat(f.game, 'emperor').spice, ally = seat(f.game, 'ecaz').spice; assert.ok(owner >= 1); assert.deepEqual(viewGame(f.game, 'emperor').emperorPhaseOpeningGift, {recipient:'ecaz', maximum:owner}); assert.equal(viewGame(f.game, 'ecaz').emperorPhaseOpeningGift, null); assert.equal(viewGame(f.game, 'guild').emperorPhaseOpeningGift, null); let game = act(f.game, 'emperor', {type:'emperorGift', amount:1}); assert.equal(game.response?.kind, 'emperorGift'); assert.equal(game.phaseOpening, null); game = cancel ? act(game, 'guild', {type:'card', card:f.counter, mode:'cancel'}) : passResponses(game); assert.deepEqual(game.phaseOpening, opening); assert.equal(game.phase, before.phase); assert.equal(game.turn, before.turn); assert.equal(seat(game, 'emperor').spice, owner - Number(!cancel)); assert.equal(seat(game, 'ecaz').spice, ally + Number(!cancel)); assert.equal(game.sandtrout, true); assert.deepEqual(inventory(game), f.original); game = act(game, 'atreides', {type:'ready'}); game = act(game, 'guild', {type:'ready'}); assert.notEqual(game.phase, before.phase); assert.equal(game.sandtrout, true); }); void test('all bot levels preserve the human Emperor opening opportunity while Sandtrout waits', () => { const f = fixture(); const initial = act(f.game, 'atreides', {type:'ready'}); for (const difficulty of DIFFICULTIES) { let game = structuredClone(initial); seat(game, 'guild').bot = difficulty; const priority = viewGame(game, 'emperor').emperorPhaseOpeningPriority; assert.ok(priority?.canContinue); assert.equal(priority.recipient, 'ecaz'); assert.ok(!botActions(viewGame(game, 'guild')).some(action => action.type === 'ready')); const waiting = runBots(game, 2); assert.deepEqual(waiting.phaseOpening, game.phaseOpening); game = act(game, 'emperor', {type:'emperorPhaseOpeningContinue', event:priority.event}); const action = botActions(viewGame(game, 'guild')).find(row => row.type === 'ready'); assert.ok(action, difficulty); game = act(game, 'guild', action); assert.notEqual(game.phase, initial.phase); assert.equal(game.sandtrout, true); } });