Files
weirsoe-party-protocol/frontend/tests/api-client.integration.test.ts
Asger Geel Weirsoee de5007943e
All checks were successful
CI / test-and-quality (push) Successful in 2m29s
CI / test-and-quality (pull_request) Successful in 2m20s
feat(spa): add typed API response mappers and contract guards
2026-03-01 15:32:26 +00:00

167 lines
5.4 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { AddressInfo } from 'node:net';
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import { createApiClient } from '../src/api/client';
let server: Server;
let baseUrl: string;
beforeAll(async () => {
server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
if (req.url === '/healthz') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ ok: true, service: 'weirsoe-party-protocol' }));
return;
}
if (req.url === '/lobby/sessions/ABCD12' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
session: { code: 'ABCD12', status: 'lobby', host_id: 1, current_round: 1, players_count: 3 },
players: [],
round_question: null,
phase_view_model: {
status: 'lobby',
round_number: 1,
players_count: 3,
constraints: {
min_players_to_start: 3,
max_players_mvp: 5,
min_players_reached: true,
max_players_allowed: true
},
host: {
can_start_round: true,
can_show_question: false,
can_mix_answers: false,
can_calculate_scores: false,
can_reveal_scoreboard: false,
can_start_next_round: false,
can_finish_game: false
},
player: {
can_join: true,
can_submit_lie: false,
can_submit_guess: false,
can_view_final_result: false
}
}
})
);
return;
}
if (req.url === '/lobby/sessions/join' && req.method === 'POST') {
res.writeHead(201, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
player: { id: 9, nickname: 'Maja', session_token: 'token-1', score: 0 },
session: { code: 'ABCD12', status: 'lobby' }
})
);
return;
}
if (req.url === '/lobby/sessions/ABCD12/rounds/start' && req.method === 'POST') {
res.writeHead(201, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
round: { number: 1, category: { slug: 'history', name: 'History' } }
})
);
return;
}
if (req.url === '/lobby/sessions/BADMAP' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ session: { code: 'BADMAP' } }));
return;
}
if (req.url?.startsWith('/lobby/sessions/')) {
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ error: 'Session not found' }));
return;
}
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ error: 'unexpected route' }));
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', () => resolve()));
const { port } = server.address() as AddressInfo;
baseUrl = `http://127.0.0.1:${port}`;
});
afterAll(async () => {
await new Promise<void>((resolve, reject) =>
server.close((err?: Error) => (err ? reject(err) : resolve()))
);
});
describe('createApiClient', () => {
it('reads health + session detail through typed wrappers', async () => {
const client = createApiClient(baseUrl);
const health = await client.health();
expect(health.ok).toBe(true);
const session = await client.getSession('abcd12');
expect(session.ok).toBe(true);
if (session.ok) {
expect(session.data.session.code).toBe('ABCD12');
expect(session.data.phase_view_model.host.can_start_round).toBe(true);
}
});
it('supports join + start round writes for lobby vertical slice', async () => {
const client = createApiClient(baseUrl);
const join = await client.joinSession({ code: 'abcd12', nickname: 'Maja' });
expect(join.ok).toBe(true);
const start = await client.startRound('abcd12', { category_slug: 'history' });
expect(start.ok).toBe(true);
if (start.ok) {
expect(start.data.session.status).toBe('lie');
}
});
it('returns parse error when response violates typed contract', async () => {
const client = createApiClient(baseUrl);
const invalid = await client.getSession('badmap');
expect(invalid.ok).toBe(false);
if (!invalid.ok) {
expect(invalid.status).toBe(200);
expect(invalid.error.kind).toBe('parse');
expect(invalid.error.message).toContain('Invalid API contract');
}
});
it('returns consistent HTTP error shape for 4xx/5xx', async () => {
const client = createApiClient(baseUrl);
const missing = await client.getSession('missing');
expect(missing.ok).toBe(false);
if (!missing.ok) {
expect(missing.status).toBe(404);
expect(missing.error.kind).toBe('http');
expect(missing.error.payload).toEqual({ error: 'Session not found' });
}
});
it('returns consistent network error shape', async () => {
const client = createApiClient('http://127.0.0.1:9');
const health = await client.health();
expect(health.ok).toBe(false);
if (!health.ok) {
expect(health.error.kind).toBe('network');
expect(health.status).toBe(0);
}
});
});