import assert from 'node:assert/strict'; import test from 'node:test'; import { applyAction, viewGame, type Game } from '../game/engine'; import { TERRITORIES } from '../game/board'; import { presenceAt } from '../game/force-presence'; import { strongholdControllers, type StrongholdId } from '../game/stronghold-cards'; import { createMixedPhaseModulesFixture, mixedPhaseModulesClean, mixedPhaseModulesInventory, mixedPhaseModulesPlayer, nextMixedPhaseModules, } from './fixture-mixed-phase-modules'; const owner = 'richese'; const seat = mixedPhaseModulesPlayer; /** Drive only existing public controls; leave a clean current movement turn * and the target original CHOAM market decision to the actual test. */ function reach(game: Game, until: (state: Game) => boolean): Game { for (let count = 0; count < 1800; count++) { if (until(game)) return game; assert.notEqual(game.status, 'finished', 'The real game ended before its target decision'); const step = nextMixedPhaseModules(game); assert.ok(step, `Missing native continuation at ${game.turn}/${game.phase}/${game.decision?.kind}`); game = applyAction(game, step.actor, step.action); } throw Error('The native phase-module game did not reach its target'); } /** Public start, normal setup/phase controls and one paid No-Field shipment. * Value three goes to the sheltered Polar Sink; zero goes to an empty legal * stronghold. The shared fixture selects unopened Spice positions only. */ function source(advanced: boolean, tech: boolean, strongholds: boolean, value: 0 | 3 = 3) { const started = createMixedPhaseModulesFixture({ factions: ['richese', 'choam', 'emperor'], expansions: ['choam'], advanced, tech, strongholds, }); let game = started.game; assert.equal(game.status, 'playing'); assert.equal(game.advanced, advanced); assert.equal(!!game.techTokens, tech); assert.equal(!!game.strongholdCards, strongholds); game = reach(game, state => state.turn === 1 && state.phase === 5 && state.active === owner && mixedPhaseModulesClean(state)); const player = seat(game, owner), token = player.noField!.tokens.find(item => item.value === value); assert.ok(token && player.noFieldEvent); const shipmentEvent = player.noFieldEvent, beforeSpice = player.spice; // Shield Wall shelter keeps the original marker safe through the next // actual Storm; no future storm or faction control is staged. const destination = value === 0 ? TERRITORIES.find(territory => ['arrakeen','carthag'].includes(territory.id) && !territory.sectors.includes(game.storm) && game.players.every(p => !Object.entries(p.forces).some(([location, count]) => count > 0 && location.startsWith(`${territory.id}:`)))) : TERRITORIES.find(territory => territory.id === 'polar_sink'); assert.ok(destination); const sector = destination.sectors[0]; game = applyAction(game, owner, {type:'ship', noField:token.id, event:shipmentEvent, territory:destination.id, sector, allyPayment:0}); game = reach(game, state => state.turn === 1 && state.phase === 5 && state.active === owner && mixedPhaseModulesClean(state)); assert.equal(seat(game, owner).spice, beforeSpice - (value === 0 ? 1 : 2)); assert.equal(seat(game, owner).noField?.deployed?.tokenId, token.id); game = applyAction(game, owner, {type:'endMovement'}); game = reach(game, state => state.turn === 2 && state.decision?.kind === 'choamMarket' && !state.response && !state.phaseOpening); const event = seat(game, owner).noFieldEvent; assert.ok(event && seat(game, owner).noField?.deployed?.tokenId === token.id); return { game, token: token.id, event, inventory: started.originalInventory, territory: destination.id }; } for (const options of [ {name:'Basic Tech', advanced:false, tech:true, strongholds:false}, {name:'Advanced Strongholds', advanced:true, tech:false, strongholds:true}, {name:'Advanced Tech and Strongholds', advanced:true, tech:true, strongholds:true}, ] as const) void test(`${options.name}: native CHOAM market continues after voluntary No-Field reveal`, () => { const f = source(options.advanced, options.tech, options.strongholds); const before = structuredClone(f.game), marker = seat(before, owner); const decision = structuredClone(before.decision), market = structuredClone(before.choamMarket); const tech = structuredClone(before.techTokens), strongholds = structuredClone(before.strongholdCards); assert.equal(viewGame(before, owner).richeseNoField?.canReveal, true); const after = applyAction(before, owner, {type:'revealNoField', token:f.token, event:f.event}); assert.deepEqual(after.decision, decision); assert.deepEqual(after.choamMarket, market); assert.deepEqual(after.techTokens, tech); assert.deepEqual(after.strongholdCards, strongholds); assert.equal(seat(after, owner).noField?.deployed, null); assert.equal(seat(after, owner).reserves, marker.reserves - 3); assert.equal(seat(after, owner).forces['polar_sink:0'], (marker.forces['polar_sink:0'] ?? 0) + 3); assert.equal(seat(after, owner).spice, marker.spice); assert.deepEqual(mixedPhaseModulesInventory(after), f.inventory); assert.throws(() => applyAction(after, owner, {type:'revealNoField', token:f.token, event:f.event})); assert.ok(decision?.kind === 'choamMarket'); const continued = applyAction(after, decision.player, {type:'decision', done:true}); assert.notDeepEqual(continued.decision, decision); assert.equal(seat(continued, owner).forces['polar_sink:0'], 3); assert.deepEqual(continued.techTokens, tech); assert.deepEqual(continued.strongholdCards, strongholds); assert.deepEqual(mixedPhaseModulesInventory(continued), f.inventory); }); void test('Advanced phase modules keep their claimed card when a zero marker vacates an empty stronghold during CHOAM market', () => { const f = source(true, true, true, 0); const before = structuredClone(f.game), decision = structuredClone(before.decision); const market = structuredClone(before.choamMarket), tech = structuredClone(before.techTokens); const strongholds = structuredClone(before.strongholdCards), territory = f.territory as StrongholdId; assert.equal(presenceAt(seat(before, owner), territory), 1); assert.equal(strongholdControllers(before.players, false)[territory], owner); assert.equal(strongholds!.owners[territory], owner, 'The earlier actual Mentat claimed the occupied card'); assert.equal(viewGame(before, owner).richeseNoField?.canReveal, true); const after = applyAction(before, owner, {type:'revealNoField', token:f.token, event:f.event}); assert.equal(presenceAt(seat(after, owner), territory), 0); assert.equal(strongholdControllers(after.players, false)[territory], null); assert.equal(seat(after, owner).reserves, seat(before, owner).reserves); assert.deepEqual(after.decision, decision); assert.deepEqual(after.choamMarket, market); assert.deepEqual(after.techTokens, tech); assert.deepEqual(after.strongholdCards, strongholds, 'Card ownership waits for the normal next Mentat'); assert.deepEqual(mixedPhaseModulesInventory(after), f.inventory); assert.ok(decision?.kind === 'choamMarket'); const continued = applyAction(after, decision.player, {type:'decision', done:true}); assert.equal(presenceAt(seat(continued, owner), territory), 0); assert.deepEqual(continued.techTokens, tech); assert.deepEqual(continued.strongholdCards, strongholds); });