import assert from 'node:assert/strict'; import test from 'node:test'; import { createMultipartyPublicDeal, createPublicDeal, markPublicDealPaid, publicDealDeferredTerms, PublicDealError, releasePublicDeal, respondPublicDeal, setPublicDealAdditionalPayments, validatePublicDeals, voidPublicDealPayment, type PublicDeal, type PublicDealSeat, } from '../game/public-deals'; import { outstandingPublicDealPayments, pendingPublicDealPayments, publicDealPaymentOptionKey, publicDealPaymentTotal } from '../game/public-deal-obligations'; const seats: PublicDealSeat[] = [{id:'a'}, {id:'b'}, {id:'c'}]; const term = (payer: string, recipient: string, amount: number, dueTurn: number) => ({kind:'deferredBribe' as const, payer, recipient, amount, dueTurn}); function offered() { const initial = createMultipartyPublicDeal(seats,'a',['b','c'],'Multiple payments',2,'deal',term('a','b',1,2)); const terms = [term('a','b',2,2), term('a','b',2,2), term('b','c',3,3)]; const next = setPublicDealAdditionalPayments(initial,terms,seats); terms[0].amount = 99; assert.equal(next.additionalPayments![0].payment.amount,2, 'Signed terms cannot alias caller objects.'); return next; } function active() { const first = respondPublicDeal(offered(),seats,'b',true); assert.equal(first.status,'offered'); assert.deepEqual(first.additionalPayments!.map(row => row.paymentStatus),[undefined,undefined,undefined]); return respondPublicDeal(first,seats,'c',true); } void test('distinct indexed payment terms bind only after unanimous consent and preserve legacy terms', () => { const game = active(); assert.equal(game.paymentStatus,'pending'); assert.deepEqual(game.additionalPayments!.map(row => row.paymentStatus),['pending','pending','pending']); assert.deepEqual(publicDealDeferredTerms(game).map(row => row.paymentIndex),[undefined,0,1,2]); assert.deepEqual(pendingPublicDealPayments([game],'a',2).map(row => [row.paymentIndex,row.amount]), [[undefined,1],[0,2],[1,2]]); assert.equal(publicDealPaymentTotal([game],'a',2),5); assert.equal(publicDealPaymentTotal([game],'b',2),0); assert.deepEqual(outstandingPublicDealPayments([game],'b',2).map(row => [row.paymentIndex,row.dueTurn]),[[2,3]]); assert.equal(publicDealPaymentOptionKey('deal'),'deal'); assert.equal(publicDealPaymentOptionKey('deal',0),'deal:payment:0'); assert.notEqual(publicDealPaymentOptionKey('deal',0),publicDealPaymentOptionKey('deal',1)); assert.deepEqual(JSON.parse(JSON.stringify(game)),game); validatePublicDeals([game],seats); }); void test('only the original payer can pay an exact term once; void and release affect each term independently', () => { const original = active(), before = structuredClone(original); for (const index of [-1,3,1.5,Number.NaN]) assert.throws(() => markPublicDealPaid(original,seats,'a',index),PublicDealError); assert.throws(() => markPublicDealPaid(original,seats,'b',0),PublicDealError); let game = markPublicDealPaid(original,seats,'a',1); assert.deepEqual(game.additionalPayments!.map(row => row.paymentStatus),['pending','paid','pending']); assert.throws(() => markPublicDealPaid(game,seats,'a',1),PublicDealError); game = voidPublicDealPayment(game,seats,0); assert.deepEqual(game.additionalPayments!.map(row => row.paymentStatus),['void','paid','pending']); assert.throws(() => voidPublicDealPayment(game,seats,0),PublicDealError); assert.throws(() => markPublicDealPaid(game,seats,'a',0),PublicDealError); assert.equal(game.paymentStatus,'pending'); for (const actor of ['a','b','c']) game = releasePublicDeal(game,seats,actor,true); assert.equal(game.status,'released'); assert.equal(game.paymentStatus,'released'); assert.deepEqual(game.additionalPayments!.map(row => row.paymentStatus),['released','paid','released']); validatePublicDeals([game],seats); assert.deepEqual(original,before, 'Failed attempts and later transitions preserve original records.'); }); void test('malformed additional arrays, term packets and lifecycle states are rejected', () => { const initial = createPublicDeal(seats,'a','b','Terms',2,'deal'); for (const terms of [[],Array.from({length:13},() => term('a','b',1,2)),[term('a','c',1,2)], [term('a','b',0,2)],[term('a','b',1,1)],[{kind:'immediateBribe',payer:'a',recipient:'b',amount:1}]]) assert.throws(() => setPublicDealAdditionalPayments(initial,terms,seats),PublicDealError); const offer = setPublicDealAdditionalPayments(initial,[term('a','b',2,2)],seats); const accepted = respondPublicDeal(offer,seats,'b',true); const malformed = [ {...offer,additionalPayments:[]}, {...offer,additionalPayments:[{payment:term('a','b',2,2),paymentStatus:'pending'}]}, {...accepted,additionalPayments:[{payment:term('a','b',2,2)}]}, {...accepted,additionalPayments:[{payment:term('a','b',2,2),paymentStatus:'released'}]}, {...accepted,additionalPayments:[{payment:term('a','b',2,2),paymentStatus:'pending',extra:true}]}, {...accepted,additionalPayments:[{payment:{...term('a','b',2,2),extra:true},paymentStatus:'pending'}]}, ]; for (const game of malformed) assert.throws(() => validatePublicDeals([game],seats),PublicDealError); assert.throws(() => setPublicDealAdditionalPayments(accepted,[term('a','b',1,2)],seats),PublicDealError); assert.throws(() => setPublicDealAdditionalPayments(offer,[term('a','b',1,2)],seats),PublicDealError); validatePublicDeals([offer, {...accepted,id:'active'}],seats); }); void test('older single deferred payment saves and no-payment deals retain exact legacy shape', () => { const payment = term('a','b',4,3); const legacy: PublicDeal = respondPublicDeal(createPublicDeal(seats,'a','b','Legacy',3,'legacy',payment),seats,'b',true); const before = JSON.stringify(legacy); assert.deepEqual(publicDealDeferredTerms(legacy),[{payment,paymentStatus:'pending'}]); assert.deepEqual(outstandingPublicDealPayments([legacy],'a',3), [{deal:'legacy',payer:'a',recipient:'b',dueTurn:3,amount:4}]); assert.equal(JSON.stringify(legacy),before); assert.equal(markPublicDealPaid(legacy,seats,'a').paymentStatus,'paid'); const empty = createPublicDeal(seats,'a','b','No payment',2,'empty'); assert.deepEqual(publicDealDeferredTerms(empty),[]); assert.equal(Object.hasOwn(empty,'additionalPayments'),false); });