// @dune-suite integration import assert from 'node:assert/strict'; import test from 'node:test'; import { botActions } from '../game/bots'; import type { Action, GameView } from '../game/engine'; const base = process.env.DUNE_TEST_URL ?? 'http://localhost:3000'; async function request(path: string, cookie?: string, body?: unknown) { 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 } : {}), }, ...(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 }, }; } /** Only this new QA room is used. Both factions are real human seats; the * existing bot adapter proposes setup/phase choices from each seat's private * view without changing the server's seat control or staging game state. */ void test('authenticated Once Around cache clock expires to the native pass and rejects a late bid', async () => { const host = await request('/api/rooms', undefined, { name: 'Richese clock QA', faction: 'richese', advanced: false, expansions: ['choam'], }); assert.equal(host.status, 201, JSON.stringify(host.data)); assert.ok(host.cookie); const path = `/api/rooms/${host.data.code}`; const peer = await request(path, undefined, {type:'join', name:'Clock QA opponent', faction:'atreides'}); assert.equal(peer.status, 200, JSON.stringify(peer.data)); assert.ok(peer.cookie); const seats = [{id:host.data.me, cookie:host.cookie}, {id:peer.data.me, cookie:peer.cookie}]; let view = peer.data; async function read(cookie: string) { const result = await request(path, cookie); assert.equal(result.status, 200, JSON.stringify(result.data)); view = result.data; return view; } async function act(cookie: string, action: Action) { const result = await request(path, cookie, {version:view.version, action}); assert.equal(result.status, 200, `Rejected ${action.type}: ${JSON.stringify(result.data)}`); view = result.data; return view; } for (const seat of seats) await act(seat.cookie, {type:'ready'}); await act(host.cookie, {type:'start'}); // Stop at the native response before the first cache, then choose its // printed sequential method. Every other action comes from real views. let clocked: GameView | null = null; for (let count = 0; count < 400; count++) { const owner = await read(host.cookie); assert.notEqual(owner.status, 'finished', 'The QA game ended before its first cache lot.'); if (owner.richeseAuction?.source === 'cache' && owner.richeseAuction.method === 'onceAround' && owner.richeseAuction.active && owner.auctionClock?.event && owner.auctionClock.running && !owner.response && !owner.decision && !owner.phaseOpening) { clocked = owner; break; } if (owner.decision?.kind === 'richeseDeclaration' && owner.decision.player === owner.me && owner.richeseBidding) { await act(host.cookie, {type:'decision', event:owner.richeseBidding.event, position:'first'}); continue; } if (owner.decision?.kind === 'richeseCache' && owner.decision.player === owner.me && owner.richeseBidding) { const card = owner.richeseBidding.cache?.[0]; assert.ok(card); await act(host.cookie, {type:'decision', event:owner.richeseBidding.event, card:card.id, method:'onceAround', direction:'clockwise'}); continue; } let choice: {cookie:string; action:Action} | null = null; for (const seat of seats) { const current = await read(seat.cookie); const local = structuredClone(current); const self = local.players.find(player => player.id === seat.id); assert.ok(self); self.bot = 'Easy'; const action = botActions(local)[0]; if (action) { choice = {cookie:seat.cookie, action}; break; } } assert.ok(choice, `No native continuation at ${owner.status}/${owner.phase}/${owner.decision?.kind ?? 'no decision'}.`); if (view.response && !view.response.passed.includes(view.me)) choice.action = {type:'passResponse'}; await act(choice.cookie, choice.action); } assert.ok(clocked, 'The dedicated room did not reach an active Once Around clock.'); const lot = clocked.richeseAuction; assert.ok(lot?.active); const clock = clocked.auctionClock; assert.ok(clock?.event); assert.equal(clock.player, lot.active); const bidder = seats.find(seat => seat.id === lot.active); assert.ok(bidder); const oldVersion = clocked.version; const lotEvent = lot.event; // The HTTP boundary uses the real server clock. The room worker or the // authenticated late-action preflight must expire the original bid once. await new Promise(resolve => setTimeout(resolve, 11_500)); const late = await request(path, bidder.cookie, {version:oldVersion, action:{type:'richeseBid', event:lotEvent, amount:1, allyPayment:0}}); assert.ok(late.status >= 400, 'A bid from the expired version must not be accepted.'); const after = await read(host.cookie); assert.ok(after.version > oldVersion); assert.equal(after.richeseAuction?.event, lotEvent); assert.ok(after.richeseAuction?.passed.includes(bidder.id)); assert.equal(after.richeseAuction?.bid, 0); assert.equal(after.richeseAuction?.outcome?.kind, 'unbid'); assert.equal(after.decision?.kind, 'richeseUnbid'); });