/** Replay HTTP API (docs/09 §8): my games list and replays, players only, finished games only. */ import { afterEach, describe, expect, test } from 'bun:test'; import { applyViewPatch, type ReplayData } from '@bg/shared'; import { Client, api, boot, guest } from './test-utils'; const running: ReturnType[] = []; const clients: Client[] = []; afterEach(async () => { for (const c of clients.splice(0)) c.close(); for (const s of running.splice(0)) await s.stop(); }); describe('replay API', () => { test('players of a finished game can replay it; others, logged-out users and running games are refused', async () => { const s = boot(); running.push(s); const ga = await guest(s.base, '호랑이'); const gb = await guest(s.base, '고양이'); const gc = await guest(s.base, '구경꾼'); const a = await new Client(s.base, ga.cookie, ga.id).connect(); const b = await new Client(s.base, gb.cookie, gb.id).connect(); clients.push(a, b); const code = (await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: JSON.stringify({ gameId: 'omok' }) })).body.code; a.send({ t: 'join', code, as: 'player' }); await a.next('room'); b.send({ t: 'join', code, as: 'player' }); await b.next('room'); a.send({ t: 'config', options: { colorAssignment: 'hostBlack', ruleSet: 'free' } }); await a.next('room', (m) => (m.room.options as any).colorAssignment === 'hostBlack'); const started = b.next('state'); a.send({ t: 'start' }); await started; const gamePk = s.db.query<{ id: string }, []>('SELECT id FROM games').get()!.id; const running409 = await api(s.base, `/api/replays/${gamePk}`, { cookie: a.cookie }); expect(running409.res.status).toBe(409); for (let i = 0; i < 4; i++) { let p = b.next('state', (m) => (m.view as any).turn === 'white'); a.act({ type: 'place', x: 3 + i, y: 7 }); await p; p = a.next('state', (m) => (m.view as any).turn === 'black'); b.act({ type: 'place', x: 3 + i, y: 0 }); await p; } const room = a.next('room', (m) => m.room.status === 'finished'); a.act({ type: 'place', x: 7, y: 7 }); expect((await room).room.lastGamePk).toBe(gamePk); const r = await api(s.base, `/api/replays/${gamePk}`, { cookie: a.cookie }); expect(r.res.status).toBe(200); expect(r.res.headers.get('cache-control')).toBe('private, no-store'); const data = r.body as ReplayData; expect(data).toMatchObject({ gamePk, gameId: 'omok', me: a.id }); expect(data.players[b.id]!.nickname).toBe('고양이'); expect(data.frames).toHaveLength(10); expect(data.frames[1]!.actor).toBe(a.id); let view: unknown; for (const f of data.frames) view = f.view ?? applyViewPatch(view, f.patch); expect(view).toEqual(a.lastOf('state')!.view); const seed = s.db.query<{ seed: string }, []>('SELECT seed FROM games').get()!.seed; expect(JSON.stringify(r.body)).not.toContain(seed); expect((await api(s.base, `/api/replays/${gamePk}`, { cookie: b.cookie })).body.me).toBe(b.id); expect((await api(s.base, `/api/replays/${gamePk}`, { cookie: gc.cookie })).res.status).toBe(403); expect((await api(s.base, `/api/replays/${gamePk}`)).res.status).toBe(401); expect((await api(s.base, '/api/replays/01J0000000000000000000000X', { cookie: a.cookie })).res.status).toBe(404); expect((await api(s.base, '/api/replays/..%2Fadmin', { cookie: a.cookie })).res.status).toBe(404); const mine = await api(s.base, '/api/me/games', { cookie: b.cookie }); expect(mine.body.games).toHaveLength(1); expect(mine.body.games[0]).toMatchObject({ gamePk, gameId: 'omok', myRank: 2, replayable: true }); expect(mine.body.games[0].players.map((p: { nickname: string }) => p.nickname)).toEqual(['호랑이', '고양이']); expect((await api(s.base, '/api/me/games', { cookie: gc.cookie })).body).toEqual({ games: [], next: null }); expect((await api(s.base, '/api/me/games')).res.status).toBe(401); expect((await api(s.base, '/api/me/games?before=bad', { cookie: a.cookie })).res.status).toBe(400); }); });