84 lines
4.4 KiB
TypeScript
84 lines
4.4 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, payment, replacement and malformed persisted terms', () => {
|
|
const multi = createMultipartyPublicDeal(seats, 'a', ['b', 'c'], 'Terms', 3, 'multi');
|
|
const payment = createPublicDeal(seats, 'a', 'b', 'Terms', 3, 'paid',
|
|
{ kind: 'immediateBribe', payer: 'a', recipient: 'b', amount: 1 });
|
|
const offered = setPublicDealTravel(fresh(), term, seats);
|
|
const active = respondPublicDeal(offered, seats, 'b', true);
|
|
for (const deal of [multi, payment, offered, active])
|
|
assert.throws(() => setPublicDealTravel(deal, term, seats), PublicDealError);
|
|
for (const invalid of [
|
|
{ ...multi, travel: term }, { ...payment, travel: term },
|
|
{ ...fresh(), travel: undefined }, { ...fresh(), travel: { ...term, sector: 0 } },
|
|
{ ...offered, paymentStatus: 'pending' }, { ...offered, travelStatus: 'pending' },
|
|
]) 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);
|
|
});
|