Files
Dune/tests/public-deals-proposer-travel-http.test.ts
T
Quintine 13c337a24a
Verify and publish checkpoint container / container (push) Successful in 11m23s
Allow proposers to commit their own travel
2026-11-16 15:10:17 +11:00

71 lines
3.9 KiB
TypeScript

// @dune-suite integration
import assert from 'node:assert/strict';
import test from 'node:test';
import type { GameView } from '../game/engine';
import type { PublicDeal } from '../game/public-deals';
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}};
}
/** Creates a fresh authenticated test room; never reuses or removes a saved
* room. The real proposer submits its own next-turn travel and both players
* view the same offer and scheduled commitment after the recipient consents. */
void test('authenticated proposer can offer their own travel for recipient consent', async () => {
const host = await request('/api/rooms',undefined,{
name:'Proposer traveler',faction:'harkonnen',advanced:false,expansions:[],
});
assert.equal(host.status,201,JSON.stringify(host.data)); assert.ok(host.cookie);
const path = `/api/rooms/${host.data.code}`;
const guest = await request(path,undefined,{type:'join',name:'Travel recipient',faction:'atreides'});
assert.equal(guest.status,200,JSON.stringify(guest.data)); assert.ok(guest.cookie);
const seats = [{id:host.data.me,cookie:host.cookie},{id:guest.data.me,cookie:guest.cookie}];
let view = guest.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 dueTurn = view.turn+1;
const travel = {actor:host.data.me,kind:'ship',territory:'polar_sink',minimum:1,answer:true,dueTurn};
const payment = {kind:'deferredBribe',payer:host.data.me,recipient:guest.data.me,amount:1,dueTurn};
await act(host.cookie,{type:'dealOffer',target:guest.data.me,
terms:'I will ship to Polar Sink and pay one spice next turn.',travel,payment});
const offered = view.deals.at(-1); assert.ok(offered);
assert.equal(offered.status,'offered'); assert.deepEqual(offered.travel,travel);
assert.deepEqual(offered.payment,payment);
const acceptance = {type:'dealRespond',deal:offered.id,accept:true};
const self = await request(path,host.cookie,{version:view.version,action:acceptance});
assert.equal(self.status,409,JSON.stringify(self.data));
const forged = await request(path,host.cookie,{version:view.version,action:acceptance},guest.data.me);
assert.equal(forged.status,409,JSON.stringify(forged.data));
await act(guest.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: PublicDeal | undefined = read.data.deals.find(deal => deal.id === offered.id); assert.ok(active);
assert.equal(active.status,'active'); assert.equal(active.paymentStatus,'pending');
assert.deepEqual(active.travel,travel); assert.deepEqual(active.payment,payment);
assert.equal(read.data.dealTravelStatus[offered.id],'scheduled');
}
});