- 서버: 끝난 게임을 공개된 시드 + room_log 행동으로 다시 돌려 플레이어별 view 장면을 만든다 (rooms/replay.ts, 장면은 view 차이만 전송: shared/view-patch.ts). 행동 로그의 at = 적용한 정확한 시각, start 로그에 stateVersion. GET /api/me/games, GET /api/replays/:gamePk(그 판 플레이어만, no-store), RoomView.lastGamePk. 로그가 지워졌거나 규칙 버전이 바뀌었거나 결과가 다르면 한국어 안내(410). - 웹: 다시 보기 화면(/replay/:gamePk, 게임 Board/Side 읽기 전용, 처음/이전/재생·멈춤/다음/끝, 슬라이더), 내 정보의 내 게임 기록 목록, 결과 화면의 [처음부터 다시 보기]. - 테스트: 모든 게임을 Room으로 끝까지 둔 뒤 다시 만든 상태·결과·장면이 같은지, 권한, view-patch, e2e/replay.e2e.ts. - 문서: docs/09 §8, docs/07 §10. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
4.0 KiB
TypeScript
79 lines
4.0 KiB
TypeScript
/** 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<typeof boot>[] = [];
|
|
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);
|
|
});
|
|
});
|