Preserve travel funding in deal payment quotes
Verify and publish checkpoint container / container (push) Canceled after 0s

This commit is contained in:
Quintine committed 2026-11-16 14:22:26 +11:00
1 parent 5e294215be
commit 0b8a147388
5 files changed
+191 -2

No files matched your search

@@ -0,0 +1,78 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { viewGame, type Action, type Game } from '../game/engine';
import { bribeAction } from '../game/bribe-options';
import { actTruthTravel as act, createTruthTravelFixture as fixture,
settleTruthTravel as settle, truthTravelInventory as inventory,
truthTravelPlayer as player } from './fixture-truth-travel';
/** Public Basic native setup and a genuine active shipment opportunity.
* Only the shared fixture's unopened starting deal and Spice order are staged. */
function combined(payerRole: 'traveler' | 'proposer', remainder: number, later = false) {
const f = fixture(), payer = payerRole === 'traveler' ? f.target : f.asker;
const recipient = payerRole === 'traveler' ? f.asker : f.target;
const cash = player(f.game,payer).spice;
const travelCost = payer === f.target ? f.shipmentCost : 0;
const amount = cash - travelCost - remainder;
assert.ok(amount > 0);
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Pay the agreed spice and make the shipment.',
payment:{kind:'deferredBribe',payer,recipient,amount,dueTurn:f.game.turn + Number(later)},
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true}});
const id = game.deals!.at(-1)!.id;
game = act(game,f.target,{type:'dealRespond',deal:id,accept:true});
assert.equal(game.deals!.find(row => row.id === id)!.paymentStatus,'pending');
return {f,game,payer,recipient,cash,amount,id,travelCost};
}
function reject(game: Game, actor: string, action: Action) {
const before = structuredClone(game);
assert.throws(() => act(game,actor,action));
assert.deepEqual(game,before);
}
for (const payerRole of ['traveler','proposer'] as const) {
void test(`public bribe quote reserves current deal debt and travel when ${payerRole} pays`, () => {
const {f,game:initial,payer,recipient,cash,amount,id,travelCost} = combined(payerRole,1);
const quote = viewGame(initial,payer).bribeOptions;
const target = quote.targets.find(row => row.id === recipient); assert.ok(target);
assert.equal(target.maximum,1,'Only the spice left after both current commitments is discretionary');
assert.ok(target.limitation,'The player can see why a smaller payment is available');
assert.deepEqual(bribeAction(quote,recipient,1),{type:'bribe',target:recipient,amount:1});
assert.equal(bribeAction(quote,recipient,2),null);
reject(initial,payer,{type:'bribe',target:recipient,amount:2});
let game = act(initial,payer,{type:'bribe',target:recipient,amount:1});
assert.equal(player(game,payer).spice,cash-1);
assert.equal(viewGame(game,payer).bribeOptions.targets.find(row => row.id === recipient)?.maximum,0);
game = act(game,payer,{type:'dealPay',deal:id});
assert.equal(game.deals!.find(row => row.id === id)!.paymentStatus,'paid');
assert.equal(player(game,payer).spice,cash-1-amount);
game = settle(act(game,f.target,f.shipmentAction));
assert.equal(viewGame(game,f.target).dealTravelStatus[id],'fulfilled');
assert.equal(player(game,payer).spice,cash-1-amount-travelCost);
assert.deepEqual(inventory(game),f.originalCards);
});
}
void test('a fully committed purse offers no ordinary bribe while the exact deal payment remains available', () => {
const {f,game:initial,payer,recipient,id,amount,travelCost} = combined('traveler',0);
const quote = viewGame(initial,payer).bribeOptions;
assert.equal(quote.targets.find(row => row.id === recipient)?.maximum,0);
assert.equal(bribeAction(quote,recipient,1),null);
reject(initial,payer,{type:'bribe',target:recipient,amount:1});
assert.equal(viewGame(initial,payer).deferredDealPaymentOptions[id]?.blocked,null);
let game = act(initial,payer,{type:'dealPay',deal:id});
assert.equal(player(game,payer).spice,travelCost);
assert.equal(game.deals!.find(row => row.id === id)!.paymentStatus,'paid');
game = settle(act(game,f.target,f.shipmentAction));
assert.equal(viewGame(game,f.target).dealTravelStatus[id],'fulfilled');
assert.equal(player(game,payer).spice,0);
assert.ok(amount > 0);
});
void test('future debt does not reserve today’s discretionary spice', () => {
const {f,game,payer,recipient} = combined('traveler',0,true);
const quote = viewGame(game,payer).bribeOptions.targets.find(row => row.id === recipient);
assert.ok(quote);
assert.equal(quote.maximum,player(game,payer).spice-f.shipmentCost);
assert.doesNotThrow(() => act(game,payer,{type:'bribe',target:recipient,amount:1}));
});
@@ -0,0 +1,74 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { viewGame, type Action, type Game } from '../game/engine';
import { actTruthTravel as act, advanceTruthTravel as advance, createTruthTravelFixture as fixture,
settleTruthTravel as settle, truthTravelClean as clean, truthTravelInventory as inventory,
truthTravelPlayer as player } from './fixture-truth-travel';
function reject(game: Game, actor: string, action: Action) {
const before = structuredClone(game);
assert.throws(() => act(game,actor,action));
assert.deepEqual(game,before);
}
/** Genuine Basic setup, one accepted next-turn shipment, and real advancement
* to its due Movement phase. The original fixture only orders unopened cards. */
function dueTravel() {
const f = fixture(), dueTurn = f.game.turn + 1;
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Ship next turn.',
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true,dueTurn}});
const travelId = game.deals!.at(-1)!.id;
game = act(game,f.target,{type:'dealRespond',deal:travelId,accept:true});
game = settle(act(game,f.target,f.currentEnd));
game = advance(game,state => state.turn === dueTurn && state.phase === 5 &&
state.active === f.target && clean(state) && !state.pendingShipment);
assert.equal(viewGame(game,f.target).dealTravelStatus[travelId],'pending');
assert.ok(player(game,f.target).spice > f.shipmentCost);
assert.equal(player(game,f.target).hand.some(card => card.effect === 'karama'),false);
return {f,game,travelId,dueTurn,cards:inventory(game)};
}
for (const combined of [false,true]) for (const affordable of [false,true]) void test(
`a due scheduled shipment and ${combined ? 'combined' : 'separate'} current payment ${affordable ? 'share sufficient funds' : 'reject overspending'}`, () => {
const {f,game:initial,travelId,dueTurn,cards} = dueTravel();
const cash = player(initial,f.target).spice;
const amount = affordable ? cash - f.shipmentCost : cash;
let game = act(initial,f.asker,{type:'dealOffer',target:f.target,terms:'Pay with optional matching travel.',
payment:{kind:'deferredBribe',payer:f.target,recipient:f.asker,amount,dueTurn},
...(combined ? {travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true}} : {})});
const paymentId = game.deals!.at(-1)!.id;
if (!affordable) {
reject(game,f.target,{type:'dealRespond',deal:paymentId,accept:true});
assert.equal(game.deals!.find(deal => deal.id === paymentId)?.status,'offered');
assert.equal(viewGame(game,f.target).dealTravelStatus[travelId],'pending');
assert.equal(player(game,f.target).spice,cash);
assert.deepEqual(inventory(game),cards);
return;
}
game = act(game,f.target,{type:'dealRespond',deal:paymentId,accept:true});
assert.equal(game.deals!.find(deal => deal.id === paymentId)?.paymentStatus,'pending');
game = act(game,f.target,{type:'dealPay',deal:paymentId});
assert.equal(player(game,f.target).spice,f.shipmentCost);
game = settle(act(game,f.target,f.shipmentAction));
assert.equal(viewGame(game,f.target).dealTravelStatus[travelId],'fulfilled');
if (combined) assert.equal(viewGame(game,f.target).dealTravelStatus[paymentId],'fulfilled');
assert.equal(game.deals!.find(deal => deal.id === paymentId)?.paymentStatus,'paid');
assert.equal(player(game,f.target).spice,0);
assert.deepEqual(inventory(game),cards);
},
);
void test('a due payment already accepted prevents adding an unaffordable current shipment', () => {
const f = fixture(), original = inventory(f.game), cash = player(f.game,f.target).spice;
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Pay this turn.',
payment:{kind:'deferredBribe',payer:f.target,recipient:f.asker,amount:cash,dueTurn:f.game.turn}});
const paymentId = game.deals!.at(-1)!.id;
game = act(game,f.target,{type:'dealRespond',deal:paymentId,accept:true});
game = act(game,f.asker,{type:'dealOffer',target:f.target,terms:'Also ship this turn.',
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true}});
const travelId = game.deals!.at(-1)!.id;
reject(game,f.target,{type:'dealRespond',deal:travelId,accept:true});
assert.equal(game.deals!.find(deal => deal.id === paymentId)?.paymentStatus,'pending');
assert.equal(game.deals!.find(deal => deal.id === travelId)?.status,'offered');
assert.deepEqual(inventory(game),original);
});