79 lines
4.4 KiB
TypeScript
79 lines
4.4 KiB
TypeScript
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}));
|
||
});
|