Files
Dune/tests/public-deals-multiple-immediate-bureaucrat.test.ts
T
Quintine b03efd2371
Verify and publish checkpoint container / container (push) Successful in 10m59s
Settle multiple immediate deal payments
2026-11-16 14:44:06 +11:00

81 lines
4.9 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { applyAction, normalizeAutomaticGame, viewGame, type Action, type Game } from '../game/engine';
import { bureaucratPaymentGame, bureaucratPlayer as player, bureaucratReload } from './bureaucrat-payment-fixture';
const payment = (amount: number) => ({ kind:'immediateBribe', payer:'p', recipient:'e', amount } as const);
function reject(game: Game, actor: string, action: Action) {
const before = structuredClone(game);
assert.throws(() => applyAction(game,actor,action));
assert.deepEqual(game,before,'Rejected actions cannot change the saved settlement.');
}
/** The shared fixture uses genuine Leader Skills setup, then stages a conserved
* quiet position and balances. All deal consent, native bribes, Bureaucrat
* decisions and continuation below use real actions. */
for (const trigger of ['first','second'] as const) for (const redirect of [false,true]) {
void test(`multiple immediate payments resume after the ${trigger} Bureaucrat opportunity (${redirect ? 'redirect' : 'pass'})`, () => {
const initial = bureaucratPaymentGame(), first = trigger === 'first' ? 5 : 1;
const second = trigger === 'second' ? 5 : 1;
let game = applyAction(initial,'p',{type:'dealOffer',target:'e',terms:'Two immediate bribes.',
payment:payment(first),additionalImmediatePayments:[payment(second)]});
const id = game.deals?.at(-1)?.id; assert.ok(id);
assert.equal(player(game,'p').spice,30); assert.equal(player(game,'e').bribes,0);
game = applyAction(game,'e',{type:'dealRespond',deal:id,accept:true});
assert.equal(game.deals?.find(deal => deal.id === id)?.status,'active');
assert.equal(game.decision?.kind,'bureaucratPayment');
const event = game.bureaucratPaymentEvent; assert.ok(event);
assert.equal(player(game,'p').spice,trigger === 'first' ? 25 : 24);
assert.equal(player(game,'e').bribes,trigger === 'first' ? 0 : 1);
assert.equal(viewGame(game,'b').bureaucrat.pending?.event,event);
reject(game,'p',{type:'bribe',target:'e',amount:1});
reject(game,'e',{type:'dealRelease',deal:id,agree:true});
const saved = bureaucratReload(game);
assert.deepEqual(viewGame(saved,'b').bureaucrat.pending,viewGame(game,'b').bureaucrat.pending);
assert.equal(normalizeAutomaticGame(saved).bureaucratPaymentEvent,event);
game = applyAction(saved,'b',{type:'decision',event,redirect});
assert.equal(game.bureaucratPayments?.pending,undefined);
assert.equal(player(game,'p').spice,24,'Both original gross payments are debited exactly once.');
assert.equal(player(game,'e').bribes,redirect ? 4 : 6,'Only the native redirect is withheld.');
assert.equal(viewGame(game,'b').bureaucrat.usedThisPhase,redirect);
reject(game,'b',{type:'decision',event,redirect});
reject(game,'e',{type:'dealRespond',deal:id,accept:true});
assert.equal(player(game,'p').spice,24);
assert.equal(player(game,'e').bribes,redirect ? 4 : 6);
});
}
void test('passing the first Bureaucrat opportunity preserves the second distinct payment and its decision', () => {
let game = applyAction(bureaucratPaymentGame(),'p',{type:'dealOffer',target:'e',terms:'Two five-spice bribes.',
payment:payment(5),additionalImmediatePayments:[payment(5)]});
const id = game.deals?.at(-1)?.id; assert.ok(id);
game = applyAction(game,'e',{type:'dealRespond',deal:id,accept:true});
const first = game.bureaucratPaymentEvent; assert.ok(first);
assert.equal(player(game,'p').spice,25); assert.equal(player(game,'e').bribes,0);
const forgedCursor = bureaucratReload(game);
assert.ok(forgedCursor.pendingPublicDealSettlement);
forgedCursor.pendingPublicDealSettlement.next = 2;
reject(forgedCursor,'b',{type:'decision',event:first,redirect:false});
const forgedTerms = bureaucratReload(game);
forgedTerms.deals!.find(deal => deal.id === id)!.additionalImmediatePayments![0].amount = 6;
reject(forgedTerms,'b',{type:'decision',event:first,redirect:false});
game = applyAction(bureaucratReload(game),'b',{type:'decision',event:first,redirect:false});
const second = game.bureaucratPaymentEvent; assert.ok(second); assert.notEqual(second,first);
assert.equal(game.decision?.kind,'bureaucratPayment');
assert.equal(player(game,'p').spice,20); assert.equal(player(game,'e').bribes,5);
assert.equal(viewGame(game,'b').bureaucrat.usedThisPhase,false);
reject(game,'b',{type:'decision',event:first,redirect:true});
reject(game,'e',{type:'dealRelease',deal:id,agree:true});
game = applyAction(bureaucratReload(game),'b',{type:'decision',event:second,redirect:true});
assert.equal(game.bureaucratPayments?.pending,undefined);
assert.equal(player(game,'p').spice,20); assert.equal(player(game,'e').bribes,8);
assert.equal(viewGame(game,'b').bureaucrat.usedThisPhase,true);
reject(game,'b',{type:'decision',event:second,redirect:true});
reject(game,'e',{type:'dealRespond',deal:id,accept:true});
assert.equal(player(game,'p').spice,20); assert.equal(player(game,'e').bribes,8);
});