Support deferred payments with travel deals
Verify and publish checkpoint container / container (push) Successful in 11m29s
Verify and publish checkpoint container / container (push) Successful in 11m29s
This commit is contained in:
1 parent
9536c77062
commit
5e294215be
16 files changed
+603
-100
No files matched your search
@@ -1,14 +1,9 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { viewGame, type Action, type Game } from '../game/engine';
|
||||
import { actTruthTravel as act, createTruthTravelFixture as fixture,
|
||||
import { viewGame, 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, 'Conflicting consent must leave the source game unchanged.');
|
||||
}
|
||||
function travelOffer(game: Game, proposer: string, traveler: string, dueTurn: number) {
|
||||
const next = act(game, proposer, {type:'dealOffer', target:traveler, terms:'Travel in a future turn.',
|
||||
travel:{kind:'ship', territory:'polar_sink', minimum:1, answer:true, dueTurn}});
|
||||
@@ -31,38 +26,45 @@ function deal(game: Game, id: string) {
|
||||
/** Each case uses the genuine Basic Atreides/Emperor setup and actual native
|
||||
* first-turn pre-shipment window. These are independent two-party offers and
|
||||
* acceptances; no wallet, force, pending debt or travel receipt is staged. */
|
||||
void test('a same-turn deferred debt cannot be accepted after the payer schedules travel', () => {
|
||||
const f = fixture(), dueTurn = f.game.turn + 1, originalCards = inventory(f.game);
|
||||
const future = travelOffer(f.game, f.asker, f.target, dueTurn);
|
||||
let game = accept(future.game, f.target, future.id);
|
||||
assert.equal(viewGame(game, f.target).dealTravelStatus[future.id], 'scheduled');
|
||||
const payment = paymentOffer(game, f.asker, f.target, f.target, f.asker, dueTurn);
|
||||
game = payment.game;
|
||||
const purse = player(game, f.target).spice;
|
||||
reject(game, f.target, {type:'dealRespond', deal:payment.id, accept:true});
|
||||
assert.equal(deal(game, future.id).status, 'active');
|
||||
assert.equal(deal(game, payment.id).status, 'offered');
|
||||
assert.equal(game.travelPromises?.filter(row => row.sourceDeal === future.id).length, 1);
|
||||
assert.equal(player(game, f.target).spice, purse);
|
||||
assert.deepEqual(inventory(game), originalCards);
|
||||
});
|
||||
for (const order of ['travel-first', 'payment-first'] as const) void test(
|
||||
`the same future due turn retains travel and deferred payment when accepted ${order}`, () => {
|
||||
const f = fixture(), dueTurn = f.game.turn + 1, originalCards = inventory(f.game);
|
||||
let game = f.game, travelId: string, paymentId: string;
|
||||
if (order === 'travel-first') {
|
||||
const future = travelOffer(game, f.asker, f.target, dueTurn); travelId = future.id;
|
||||
game = accept(future.game, f.target, travelId);
|
||||
const payment = paymentOffer(game, f.asker, f.target, f.target, f.asker, dueTurn); paymentId = payment.id;
|
||||
game = accept(payment.game, f.target, paymentId);
|
||||
} else {
|
||||
const payment = paymentOffer(game, f.asker, f.target, f.target, f.asker, dueTurn); paymentId = payment.id;
|
||||
game = accept(payment.game, f.target, paymentId);
|
||||
const future = travelOffer(game, f.asker, f.target, dueTurn); travelId = future.id;
|
||||
game = accept(future.game, f.target, travelId);
|
||||
}
|
||||
assert.equal(deal(game, travelId).status, 'active');
|
||||
assert.equal(viewGame(game, f.target).dealTravelStatus[travelId], 'scheduled');
|
||||
assert.equal(game.travelPromises?.filter(row => row.sourceDeal === travelId).length, 1);
|
||||
assert.equal(deal(game, paymentId).paymentStatus, 'pending');
|
||||
assert.equal(player(game, f.target).spice, player(f.game, f.target).spice);
|
||||
assert.deepEqual(inventory(game), originalCards);
|
||||
|
||||
void test('a same-turn scheduled journey cannot be accepted after the traveler owes a deferred debt', () => {
|
||||
const f = fixture(), dueTurn = f.game.turn + 1, originalCards = inventory(f.game);
|
||||
const payment = paymentOffer(f.game, f.asker, f.target, f.target, f.asker, dueTurn);
|
||||
let game = accept(payment.game, f.target, payment.id);
|
||||
assert.equal(deal(game, payment.id).paymentStatus, 'pending');
|
||||
const future = travelOffer(game, f.asker, f.target, dueTurn);
|
||||
game = future.game;
|
||||
const purse = player(game, f.target).spice;
|
||||
reject(game, f.target, {type:'dealRespond', deal:future.id, accept:true});
|
||||
assert.equal(deal(game, payment.id).status, 'active');
|
||||
assert.equal(deal(game, payment.id).paymentStatus, 'pending');
|
||||
assert.equal(deal(game, future.id).status, 'offered');
|
||||
assert.equal(game.travelPromises?.some(row => row.sourceDeal === future.id) ?? false, false);
|
||||
assert.equal(player(game, f.target).spice, purse);
|
||||
assert.deepEqual(inventory(game), originalCards);
|
||||
});
|
||||
// Finish the original current turn, then settle the actual separate
|
||||
// payment and shipment on their shared future due turn.
|
||||
game = settle(act(game, f.target, f.currentEnd));
|
||||
game = advance(game, state => state.turn === dueTurn && state.phase === 2 && clean(state));
|
||||
assert.equal(viewGame(game, f.target).dealTravelStatus[travelId], 'pending');
|
||||
const beforePayment = player(game, f.target).spice;
|
||||
game = settle(act(game, f.target, {type:'dealPay', deal:paymentId}));
|
||||
assert.equal(deal(game, paymentId).paymentStatus, 'paid');
|
||||
assert.equal(player(game, f.target).spice, beforePayment - 2);
|
||||
game = advance(game, state => state.turn === dueTurn && state.phase === 5 &&
|
||||
state.active === f.target && clean(state) && !state.pendingShipment);
|
||||
game = settle(act(game, f.target, f.shipmentAction));
|
||||
assert.equal(viewGame(game, f.target).dealTravelStatus[travelId], 'fulfilled');
|
||||
assert.equal(deal(game, paymentId).paymentStatus, 'paid');
|
||||
assert.deepEqual(inventory(game), originalCards);
|
||||
},
|
||||
);
|
||||
|
||||
for (const order of ['travel-first', 'payment-first'] as const) void test(
|
||||
`different future due turns coexist when accepted ${order}`, () => {
|
||||
|
||||
Reference in new issue
Block a user