82 lines
4.7 KiB
TypeScript
82 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 test room is created; no existing room is used or removed. Both
|
|
* terms retain the same original agreement identity. */
|
|
void test('authenticated players can consent to and pay a deferred travel agreement', async () => {
|
|
const host = await request('/api/rooms', undefined, {
|
|
name: 'Deferred travel 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 guest = await request(path, undefined, { type: 'join', name: 'Deferred traveler', faction: 'harkonnen' });
|
|
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 before = await request(path, guest.cookie);
|
|
assert.equal(before.status, 200, JSON.stringify(before.data)); view = before.data;
|
|
const spice = before.data.players.find(player => player.id === guest.data.me)?.spice;
|
|
assert.ok(typeof spice === 'number' && spice >= 2);
|
|
const dueTurn = view.turn + 1;
|
|
const payment = {kind:'deferredBribe',payer:guest.data.me,recipient:host.data.me,amount:2,dueTurn};
|
|
const travel = {kind:'ship',territory:'polar_sink',minimum:1,answer:true,dueTurn};
|
|
await act(host.cookie, {type:'dealOffer',target:guest.data.me,
|
|
terms:'Ship to Polar Sink and pay two spice next turn.',payment,travel});
|
|
const offered = view.deals.at(-1); assert.ok(offered);
|
|
assert.equal(offered.status, 'offered'); assert.deepEqual(offered.payment, payment);
|
|
assert.deepEqual(offered.travel, travel);
|
|
const acceptance = {type:'dealRespond',deal:offered.id,accept:true};
|
|
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: GameView['deals'][number] | 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.payment, payment); assert.deepEqual(active.travel, travel);
|
|
assert.equal(read.data.dealTravelStatus[offered.id], 'scheduled');
|
|
if (seat.id === guest.data.me)
|
|
assert.equal(read.data.players.find(player => player.id === guest.data.me)?.spice, spice);
|
|
}
|
|
const wrongPayer = await request(path, host.cookie,
|
|
{version:view.version,action:{type:'dealPay',deal:offered.id}});
|
|
assert.equal(wrongPayer.status, 409, JSON.stringify(wrongPayer.data));
|
|
await act(guest.cookie, {type:'dealPay',deal:offered.id});
|
|
const paid = await request(path, guest.cookie);
|
|
assert.equal(paid.status, 200, JSON.stringify(paid.data));
|
|
assert.equal(paid.data.deals.find(deal => deal.id === offered.id)?.paymentStatus, 'paid');
|
|
assert.equal(paid.data.dealTravelStatus[offered.id], 'scheduled');
|
|
assert.equal(paid.data.players.find(player => player.id === guest.data.me)?.spice, spice - 2);
|
|
});
|