124 lines
6.7 KiB
TypeScript
124 lines
6.7 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
createMultipartyPublicDeal, createPublicDeal, parsePublicDealTravel, PublicDealError,
|
|
releasePublicDeal, respondPublicDeal, setPublicDealTravel, validatePublicDeals, withdrawPublicDeal,
|
|
type PublicDeal, type PublicDealSeat,
|
|
} from '../game/public-deals';
|
|
|
|
const seats: PublicDealSeat[] = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
|
|
const term = { kind: 'ship', territory: 'polar_sink', minimum: 1, answer: true } as const;
|
|
const fresh = () => createPublicDeal(seats, 'a', 'b', 'Travel this turn.', 3, 'offer');
|
|
|
|
void test('travel terms accept only exact bounded native actions and an explicit answer', () => {
|
|
for (const kind of ['ship', 'move'] as const) for (const answer of [true, false]) {
|
|
for (const minimum of [1, 20]) {
|
|
const input = { kind, territory: 'polar_sink', minimum, answer };
|
|
assert.deepEqual(parsePublicDealTravel(input), input);
|
|
assert.notEqual(parsePublicDealTravel(input), input);
|
|
}
|
|
}
|
|
for (const invalid of [null, undefined, [], {},
|
|
{ ...term, kind: 'fly' }, { ...term, territory: 'not_a_territory' },
|
|
...[0, -1, 21, 1.5, Infinity, Number.MAX_SAFE_INTEGER + 1, '1'].map(minimum => ({ ...term, minimum })),
|
|
{ ...term, answer: 'yes' }, { ...term, answer: undefined },
|
|
{ ...term, sector: 0 }, { ...term, turn: 4 }, { ...term, actor: 'a' },
|
|
]) assert.throws(() => parsePublicDealTravel(invalid), PublicDealError);
|
|
});
|
|
|
|
void test('a recipient accepts and both release an unchanged current-turn travel term', () => {
|
|
const source = fresh();
|
|
const input = { kind: 'ship' as const, territory: 'polar_sink', minimum: 1, answer: true };
|
|
const offered = setPublicDealTravel(source, input, seats);
|
|
input.minimum = 2;
|
|
assert.equal(source.travel, undefined);
|
|
assert.deepEqual(offered.travel, term);
|
|
assert.equal(offered.turn, 3);
|
|
assert.throws(() => respondPublicDeal(offered, seats, 'a', true), PublicDealError);
|
|
assert.throws(() => respondPublicDeal(offered, seats, 'c', true), PublicDealError);
|
|
const active = respondPublicDeal(offered, seats, 'b', true);
|
|
assert.equal(active.status, 'active');
|
|
assert.deepEqual(active.travel, term);
|
|
const first = releasePublicDeal(active, seats, 'a', true);
|
|
assert.equal(first.status, 'active');
|
|
const released = releasePublicDeal(first, seats, 'b', true);
|
|
assert.equal(released.status, 'released');
|
|
assert.deepEqual(released.travel, term);
|
|
const roundtrip = JSON.parse(JSON.stringify([offered, active, released])) as PublicDeal[];
|
|
// Ledger IDs must be unique; each lifecycle snapshot is validated separately.
|
|
for (const deal of roundtrip) {
|
|
const before = JSON.stringify(deal);
|
|
validatePublicDeals([deal], seats);
|
|
assert.equal(JSON.stringify(deal), before);
|
|
assert.deepEqual(deal.travel, term);
|
|
assert.equal(Object.hasOwn(deal, 'travelStatus'), false);
|
|
}
|
|
assert.deepEqual(respondPublicDeal(offered, seats, 'b', false).travel, term);
|
|
assert.deepEqual(withdrawPublicDeal(offered, seats, 'a').travel, term);
|
|
});
|
|
|
|
void test('travel is incompatible with multi-party, replacement and malformed persisted terms', () => {
|
|
const multi = createMultipartyPublicDeal(seats, 'a', ['b', 'c'], 'Terms', 3, 'multi');
|
|
const offered = setPublicDealTravel(fresh(), term, seats);
|
|
const active = respondPublicDeal(offered, seats, 'b', true);
|
|
for (const deal of [multi, offered, active])
|
|
assert.throws(() => setPublicDealTravel(deal, term, seats), PublicDealError);
|
|
for (const invalid of [
|
|
{ ...multi, travel: term },
|
|
{ ...fresh(), travel: undefined }, { ...fresh(), travel: { ...term, sector: 0 } },
|
|
{ ...offered, paymentStatus: 'pending' }, { ...offered, travelStatus: 'pending' },
|
|
]) assert.throws(() => validatePublicDeals([invalid], seats), PublicDealError);
|
|
});
|
|
|
|
void test('a proposer-to-recipient immediate payment can accompany travel through the complete deal lifecycle', () => {
|
|
const payment = { kind: 'immediateBribe', payer: 'a', recipient: 'b', amount: 3 } as const;
|
|
const original = createPublicDeal(seats, 'a', 'b', 'Payment for travel.', 3, 'paid-travel', payment);
|
|
const offered = setPublicDealTravel(original, term, seats);
|
|
assert.equal(original.travel, undefined);
|
|
const active = respondPublicDeal(offered, seats, 'b', true);
|
|
const released = releasePublicDeal(releasePublicDeal(active, seats, 'a', true), seats, 'b', true);
|
|
for (const deal of [offered, active, released, respondPublicDeal(offered, seats, 'b', false),
|
|
withdrawPublicDeal(offered, seats, 'a')]) {
|
|
const roundtrip = JSON.parse(JSON.stringify(deal)) as PublicDeal;
|
|
const before = JSON.stringify(roundtrip);
|
|
validatePublicDeals([roundtrip], seats);
|
|
assert.equal(JSON.stringify(roundtrip), before);
|
|
assert.deepEqual(roundtrip.payment, payment);
|
|
assert.deepEqual(roundtrip.travel, term);
|
|
assert.equal(Object.hasOwn(roundtrip, 'paymentStatus'), false);
|
|
}
|
|
assert.equal(active.status, 'active');
|
|
assert.equal(released.status, 'released');
|
|
});
|
|
|
|
void test('paid travel rejects reverse, deferred, multiparty and forged persisted payment shapes', () => {
|
|
const reverse = { kind: 'immediateBribe', payer: 'b', recipient: 'a', amount: 1 } as const;
|
|
const deferred = { kind: 'deferredBribe', payer: 'a', recipient: 'b', amount: 1, dueTurn: 3 } as const;
|
|
const paid = { kind: 'immediateBribe', payer: 'a', recipient: 'b', amount: 1 } as const;
|
|
for (const payment of [reverse, deferred]) {
|
|
const offer = createPublicDeal(seats, 'a', 'b', 'Terms', 3, `invalid-${payment.kind}`, payment);
|
|
assert.throws(() => setPublicDealTravel(offer, term, seats), PublicDealError);
|
|
assert.throws(() => validatePublicDeals([{ ...offer, travel: term }], seats), PublicDealError);
|
|
}
|
|
const multi = createMultipartyPublicDeal(seats, 'a', ['b', 'c'], 'Terms', 3, 'multi-paid', paid);
|
|
assert.throws(() => setPublicDealTravel(multi, term, seats), PublicDealError);
|
|
for (const invalid of [
|
|
{ ...multi, travel: term },
|
|
{ ...fresh(), payment: { ...paid, recipient: 'c' }, travel: term },
|
|
{ ...fresh(), payment: { ...paid, amount: 0 }, travel: term },
|
|
{ ...fresh(), payment: { ...paid, extra: true }, travel: term },
|
|
{ ...fresh(), payment: undefined, travel: term },
|
|
{ ...fresh(), payment: paid, paymentStatus: 'paid', travel: term },
|
|
]) assert.throws(() => validatePublicDeals([invalid], seats), PublicDealError);
|
|
});
|
|
|
|
void test('legacy two-party and multi-party records validate without acquiring travel fields', () => {
|
|
const legacy = fresh();
|
|
const multi = createMultipartyPublicDeal(seats, 'a', ['b', 'c'], 'Terms', 3, 'multi');
|
|
const before = JSON.stringify([legacy, multi]);
|
|
validatePublicDeals([legacy, multi], seats);
|
|
assert.equal(JSON.stringify([legacy, multi]), before);
|
|
assert.equal(Object.hasOwn(legacy, 'travel'), false);
|
|
assert.equal(Object.hasOwn(multi, 'travel'), false);
|
|
});
|