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 { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; import { actFremenRevivalGrant as actBattle, advanceFremenRevivalGrant as advanceBattle, createFremenRevivalGrantFixture, fremenRevivalGrantPlayer as battlePlayer, fremenRevivalGrantInventory as battleInventory, } from './fixture-fremen-revival-grant'; import { actSemutaDisasterWorm as actWorm, allowSemutaDisasterWorm, createSemutaDisasterWormFixture, semutaDisasterWormPlayer as wormPlayer, semutaDisasterWormInventory as wormInventory, } from './fixture-semuta-disaster-worm'; const player = (game: Game, id: string) => { const seat = game.players.find(candidate => candidate.id === id); assert.ok(seat); return seat; }; const clean = (game: Game) => !game.phaseOpening && !game.response && !game.decision && !game.pendingTreacheryDiscard && !game.truthtrance; function reject(game: Game, act: (game: Game, id: string, action: Action) => Game, id: string, action: Action) { const before = structuredClone(game); assert.throws(() => act(game, id, action)); assert.deepEqual(game, before, 'A rejected payment must not mutate its input or pending parent'); } function expectEscrow(before: Game, after: Game, payer: string, recipient: string, amount: number) { assert.equal(player(after, payer).spice, player(before, payer).spice - amount); assert.equal(player(after, recipient).spice, player(before, recipient).spice); assert.equal(player(after, recipient).bribes, player(before, recipient).bribes + amount); } /** Genuine public Basic Atreides/Guild/Emperor setup and native phase controls. * Only unopened Treachery and Spice card positions are selected. Atreides pays * to ship one real reserve into Polar Sink; Emperor's original dealt Karama * keeps the native Guild-income response open. No game state is installed. */ function guildIncomeSource() { let game = createGame('BRIBEGUILDINCOME', newPlayer('atreides', 'Atreides', 'atreides'), false); joinGame(game, newPlayer('guild', 'Guild', 'guild')); joinGame(game, newPlayer('emperor', 'Emperor', 'emperor')); for (const seat of game.players) game = applyAction(game, seat.id, { type: 'ready' }); game = applyAction(game, game.host, { type: 'start' }); assert.equal(game.status, 'setup'); const ordinary = game.deck.filter(card => ['projectile', 'poison', 'shield', 'snooper'].includes(card.kind)).slice(0, 2); const karama = game.deck.find(card => card.effect === 'karama'); assert.equal(ordinary.length, 2); assert.ok(karama); for (const [position, card] of [...ordinary, karama].entries()) { const from = game.deck.findIndex(candidate => candidate.id === card.id); assert.ok(from >= position); [game.deck[position], game.deck[from]] = [game.deck[from], game.deck[position]]; } const reach = (until: (game: Game) => boolean) => { for (let count = 0; count < 1200 && !until(game); count++) { const step = nextStrongholdFactionsNativeStep(game); assert.ok(step); game = applyAction(game, step.actor, step.action); } assert.ok(until(game), 'The original Basic game must reach its native source'); }; reach(state => state.status === 'playing'); assert.ok(player(game, 'emperor').hand.some(card => card.id === karama.id)); for (const position of [0, 1]) { const from = game.spiceDeck.findIndex((card, index) => index >= position && 'territory' in card); assert.ok(from >= position); [game.spiceDeck[position], game.spiceDeck[from]] = [game.spiceDeck[from], game.spiceDeck[position]]; } reach(state => state.phase === 5 && state.active === 'atreides' && clean(state)); game = applyAction(game, 'atreides', {type:'ship', territory:'polar_sink', sector:0, amount:1, elite:0, allyPayment:0}); assert.equal(game.response?.kind, 'guildIncome'); assert.equal(game.response?.owner, 'guild'); return game; } void test('a Basic paid Guild shipment preserves its income response while a nonparticipant pays a bribe', () => { const source = guildIncomeSource(), payer = 'emperor', recipient = 'atreides'; const response = structuredClone(source.response), active = source.active; const shipmentForces = structuredClone(player(source, recipient).forces); const pendingShipment = structuredClone(source.pendingShipment), aid = structuredClone(source.aid); const auction = structuredClone(source.auction); assert.equal(viewGame(source, payer).bribeOptions.blocked, null); reject(source, applyAction, payer, {type:'bribe', target:payer, amount:1}); reject(source, applyAction, payer, {type:'bribe', target:recipient, amount:player(source, payer).spice + 1}); const paid = applyAction(source, payer, {type:'bribe', target:recipient, amount:1}); assert.deepEqual(paid.response, response); assert.equal(paid.active, active); assert.deepEqual(paid.pendingShipment, pendingShipment); assert.deepEqual(paid.aid, aid, 'Prepaid ally credit must not be rewritten'); assert.deepEqual(paid.auction, auction, 'Any original reserved lot and bid stay in place'); assert.deepEqual(player(paid, recipient).forces, shipmentForces); expectEscrow(source, paid, payer, recipient, 1); const control = applyAction(source, payer, {type:'passResponse'}); const continued = applyAction(paid, payer, {type:'passResponse'}); assert.deepEqual(continued.response, control.response); assert.equal(player(continued, 'guild').spice, player(control, 'guild').spice); assert.deepEqual(player(continued, recipient).forces, player(control, recipient).forces); assert.equal(player(continued, recipient).bribes, player(source, recipient).bribes + 1); }); /** Original classic Fremen/Atreides/Emperor paid Tabr battle. Atreides uses * its native Prescience against Fremen, with Emperor's originally dealt Karama * retaining the original cancellation opportunity. */ function prescienceSource() { const fixture = createFremenRevivalGrantFixture(); let game = actBattle(fixture.beforeBattle, fixture.battleStep.actor, fixture.battleStep.action); game = advanceBattle(game, state => !!state.battle && !state.phaseOpening && !state.response && !state.decision && state.battle.preparation?.kind === 'prescience' && state.battle.preparation.owner === fixture.ally); game = actBattle(game, fixture.ally, {type:'prescience', field:'weapon'}); assert.equal(game.response?.kind, 'prescience'); return {fixture, game}; } void test('Basic Prescience keeps its original battle and response while another faction pays a bribe', () => { const {fixture:f, game:source} = prescienceSource(), payer = f.observer, recipient = f.fremen; const response = structuredClone(source.response), battle = structuredClone(source.battle); const cards = battleInventory(source); assert.equal(viewGame(source, payer).bribeOptions.blocked, null); const paid = actBattle(source, payer, {type:'bribe', target:recipient, amount:1}); assert.deepEqual(paid.response, response); assert.deepEqual(paid.battle, battle); assert.deepEqual(battleInventory(paid), cards); expectEscrow(source, paid, payer, recipient, 1); const control = actBattle(source, payer, {type:'passResponse'}); const continued = actBattle(paid, payer, {type:'passResponse'}); assert.deepEqual(continued.response, control.response); assert.deepEqual(continued.battle, control.battle); assert.equal(battlePlayer(continued, recipient).bribes, battlePlayer(source, recipient).bribes + 1); assert.deepEqual(battleInventory(continued), cards); }); /** Public Basic selected-CHOAM setup and actual paid cache, native second-turn * Spice draw and natural Fremen protection response. The original fixture only * selects unopened card positions and original lotteries. */ void test('Basic worm survival keeps its natural source and outcome through an out-of-turn bribe', () => { const f = createSemutaDisasterWormFixture({advanced:false, kind:'wormSurvival'}); const source = f.game, payer = f.spender, recipient = f.guild; assert.equal(source.response?.kind, 'wormSurvival'); const response = structuredClone(source.response), cards = wormInventory(source); assert.equal(viewGame(source, payer).bribeOptions.blocked, null); const paid = actWorm(source, payer, {type:'bribe', target:recipient, amount:1}); assert.deepEqual(paid.response, response); assert.deepEqual(paid.spiceSequence, source.spiceSequence); assert.deepEqual(paid.wormRides, source.wormRides); assert.deepEqual(wormInventory(paid), cards); expectEscrow(source, paid, payer, recipient, 1); const control = allowSemutaDisasterWorm(source); const continued = allowSemutaDisasterWorm(paid); assert.deepEqual(continued.response, control.response); assert.deepEqual(continued.decision, control.decision); assert.deepEqual(continued.wormRides, control.wormRides); assert.deepEqual(wormPlayer(continued, f.owner).forces, wormPlayer(control, f.owner).forces); assert.equal(wormPlayer(continued, recipient).bribes, wormPlayer(source, recipient).bribes + 1); assert.deepEqual(wormInventory(continued), cards); });