// @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}}; } /** Create a fresh test room; do not use or remove pre-existing games. */ void test('authenticated final consent settles each immediate payment exactly once', async () => { const host = await request('/api/rooms',undefined,{name:'Immediate 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:'Immediate 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 beforePayer = await request(path,payer.cookie), beforeRecipient = await request(path,host.cookie); assert.equal(beforePayer.status,200); assert.equal(beforeRecipient.status,200); view = beforePayer.data; const spice = beforePayer.data.players.find(player => player.id === payer.data.me)?.spice; const incoming = beforeRecipient.data.bribeOptions.incoming; assert.ok(typeof spice === 'number' && spice >= 5); const terms = [2,3].map(amount => ({kind:'immediateBribe',payer:payer.data.me,recipient:host.data.me,amount})); await act(host.cookie,{type:'dealOffer',target:payer.data.me,terms:'Pay two immediate amounts.',additionalImmediatePayments:terms}); const offered = view.deals.at(-1); assert.ok(offered); assert.equal(offered.status,'offered'); assert.deepEqual(offered.additionalImmediatePayments,terms); const acceptance = {type:'dealRespond',deal:offered.id,accept:true}; const wrongActor = await request(path,host.cookie,{version:view.version,action:acceptance}); assert.equal(wrongActor.status,409,JSON.stringify(wrongActor.data)); const forged = await request(path,host.cookie,{version:view.version,action:acceptance},payer.data.me); assert.equal(forged.status,409,JSON.stringify(forged.data)); const beforeVersion = view.version; await act(payer.cookie,acceptance); assert.equal(view.deals.find(row => row.id === offered.id)?.status,'active'); const paidPayer = await request(path,payer.cookie), paidRecipient = await request(path,host.cookie); assert.equal(paidPayer.status,200); assert.equal(paidRecipient.status,200); assert.equal(paidPayer.data.players.find(player => player.id === payer.data.me)?.spice,spice-5); assert.equal(paidRecipient.data.bribeOptions.incoming,incoming+5); const stale = await request(path,payer.cookie,{version:beforeVersion,action:acceptance}); assert.equal(stale.status,409,JSON.stringify(stale.data)); const replay = await request(path,payer.cookie,{version:view.version,action:acceptance}); assert.equal(replay.status,409,JSON.stringify(replay.data)); assert.equal((await request(path,host.cookie)).data.bribeOptions.incoming,incoming+5); });