Files
Dune/tests/public-deals-multiple-payments-http.test.ts
T
Quintine cacc88906c
Verify and publish checkpoint container / container (push) Successful in 10m22s
Support multiple deferred payments in public deals
2026-11-16 14:30:36 +11:00

72 lines
4.7 KiB
TypeScript

// @dune-suite integration
import assert from 'node:assert/strict';
import test from 'node:test';
import type { GameView } from '../game/engine';
const base = process.env.DUNE_TEST_URL ?? 'http://localhost:3000';
async function request(path:string,cookie?:string,body?:unknown,selected?:string) {
const response = await fetch(base+path,{
method:body === undefined ? 'GET' : 'POST',signal:AbortSignal.timeout(15_000),
headers:{...(body === undefined ? {} : {'Content-Type':'application/json'}),
...(cookie ? {cookie} : {}),...(selected ? {'X-Dune-Seat':selected} : {})},
...(body === undefined ? {} : {body:JSON.stringify(body)}),
});
return {status:response.status,cookie:response.headers.get('set-cookie')?.split(';')[0],
data:await response.json() as GameView & {error?:string}};
}
/** A fresh room only; no pre-existing room is read, changed or removed. */
void test('authenticated participants accept and settle each indexed deal payment once', async () => {
const host = await request('/api/rooms',undefined,{name:'Multiple recipient',faction:'atreides',advanced:false,expansions:[]});
assert.equal(host.status,201,JSON.stringify(host.data)); assert.ok(host.cookie);
const path = `/api/rooms/${host.data.code}`;
const payer = await request(path,undefined,{type:'join',name:'Multiple payer',faction:'harkonnen'});
assert.equal(payer.status,200,JSON.stringify(payer.data)); assert.ok(payer.cookie);
const seats = [{id:host.data.me,cookie:host.cookie},{id:payer.data.me,cookie:payer.cookie}];
let view = payer.data;
const act = async (cookie:string,action:object) => {
const next = await request(path,cookie,{version:view.version,action});
assert.equal(next.status,200,JSON.stringify(next.data)); view = next.data;
};
for (const seat of seats) await act(seat.cookie,{type:'ready'});
await act(host.cookie,{type:'start'});
for (const seat of seats) {
const own = await request(path,seat.cookie); assert.equal(own.status,200,JSON.stringify(own.data)); view = own.data;
const choices = own.data.players.find(player => player.id === seat.id)?.traitorChoices;
if (choices?.length) await act(seat.cookie,{type:'traitor',leader:choices[0]});
}
assert.equal(view.status,'playing');
const before = await request(path,payer.cookie); assert.equal(before.status,200); view = before.data;
const spice = before.data.players.find(player => player.id === payer.data.me)?.spice;
assert.ok(typeof spice === 'number' && spice >= 5);
const dueTurn = view.turn+1;
const terms = [2,3].map(amount => ({kind:'deferredBribe',payer:payer.data.me,recipient:host.data.me,amount,dueTurn}));
await act(host.cookie,{type:'dealOffer',target:payer.data.me,terms:'Pay two separate amounts.',additionalPayments:terms});
const offered = view.deals.at(-1); assert.ok(offered);
assert.equal(offered.status,'offered');
assert.deepEqual(offered.additionalPayments?.map(line => line.payment),terms);
const acceptance = {type:'dealRespond',deal:offered.id,accept:true};
const forged = await request(path,host.cookie,{version:view.version,action:acceptance},payer.data.me);
assert.equal(forged.status,409,JSON.stringify(forged.data));
await act(payer.cookie,acceptance);
for (const seat of seats) {
const read = await request(path,seat.cookie); assert.equal(read.status,200,JSON.stringify(read.data));
const active:GameView['deals'][number] | undefined = read.data.deals.find(row => row.id === offered.id); assert.ok(active);
assert.equal(active.status,'active');
assert.deepEqual(active.additionalPayments?.map(line => line.paymentStatus),['pending','pending']);
}
const second = {type:'dealPay',deal:offered.id,paymentIndex:1};
const wrongPayer = await request(path,host.cookie,{version:view.version,action:second});
assert.equal(wrongPayer.status,409,JSON.stringify(wrongPayer.data));
const impersonation = await request(path,host.cookie,{version:view.version,action:second},payer.data.me);
assert.equal(impersonation.status,409,JSON.stringify(impersonation.data));
await act(payer.cookie,second);
assert.deepEqual(view.deals.find(row => row.id === offered.id)?.additionalPayments?.map(line => line.paymentStatus),['pending','paid']);
const replay = await request(path,payer.cookie,{version:view.version,action:second});
assert.equal(replay.status,409,JSON.stringify(replay.data));
await act(payer.cookie,{type:'dealPay',deal:offered.id,paymentIndex:0});
const paid = await request(path,payer.cookie); assert.equal(paid.status,200,JSON.stringify(paid.data));
assert.deepEqual(paid.data.deals.find(row => row.id === offered.id)?.additionalPayments?.map(line => line.paymentStatus),['paid','paid']);
assert.equal(paid.data.players.find(player => player.id === payer.data.me)?.spice,spice-5);
});