Merge branch 'wip/game-case-file' into codex/owner/grp_discord_joke-app
# Conflicts: # apps/web/src/games/help.test.ts # apps/web/src/games/registry.ts # apps/web/src/lib/catalog-ui.ts # docs/games/README.md # packages/games/package.json # packages/games/src/index.ts # packages/shared/src/catalog.ts
This commit is contained in:
89
apps/server/src/rooms/case-file.room.test.ts
Normal file
89
apps/server/src/rooms/case-file.room.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
/** 사건 파일 through a real Room: briefing clock, captain passes, report clock, race clock, and views that keep unread texts on the server (docs/games/case-file.md §8.5). */
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { ServerMessage } from '@bg/shared';
|
||||
import { GAMES } from '@bg/games';
|
||||
import { CASES, type CaseFileOptions, type CaseFileState } from '@bg/games/case-file';
|
||||
import { openDb } from '../db';
|
||||
import { Room, type Conn, type RoomConfig } from './room';
|
||||
import { RoomStore } from './store';
|
||||
|
||||
function harness(players: string[], opts: Partial<CaseFileOptions> = {}) {
|
||||
const db = openDb(':memory:');
|
||||
const store = new RoomStore(db);
|
||||
let now = 1_000_000;
|
||||
const clock = { now: () => now, set: (t: number) => (now = t), advance: (ms: number) => (now += ms) };
|
||||
const games = { 'case-file': GAMES['case-file']! };
|
||||
const deps = { store, games, users: { publicUser: (id: string) => ({ id, nickname: id, avatar: null, kind: 'guest' as const }) }, now: clock.now };
|
||||
const config: RoomConfig = { gameId: 'case-file', options: games['case-file'].optionsSchema.parse(opts), maxPlayers: players.length, visibility: 'private', allowSpectators: true, chatEnabled: true, chatFilter: true, graceSec: 60 };
|
||||
const room = new Room('room1', '482913', config, players[0]!, deps);
|
||||
store.insertRoom({ id: 'room1', code: '482913', hostId: players[0]!, gameId: 'case-file', status: 'lobby', visibility: 'private', config, stateJson: '{}', now });
|
||||
const conns: Record<string, Conn & { msgs: ServerMessage[] }> = {};
|
||||
for (const p of players) {
|
||||
const msgs: ServerMessage[] = [];
|
||||
conns[p] = { userId: p, msgs, send: (m) => msgs.push(m), close: () => {} };
|
||||
room.join(conns[p], 'player');
|
||||
}
|
||||
expect(room.start(players[0]!)).toBeNull();
|
||||
room.clearTimer();
|
||||
const state = () => room.game!.state as CaseFileState;
|
||||
/** Jumps to the current deadline and lets the room fire it. */
|
||||
const expire = () => {
|
||||
clock.set(Math.max(clock.now(), room.effectiveDeadline()!));
|
||||
room.onTimer();
|
||||
room.clearTimer();
|
||||
};
|
||||
let cs = 0;
|
||||
/** Sends an action like the client and fails on a reject. */
|
||||
const act = (p: string, a: unknown) => {
|
||||
const before = conns[p]!.msgs.length;
|
||||
room.act(p, ++cs, a);
|
||||
room.clearTimer();
|
||||
const rej = conns[p]!.msgs.slice(before).find((m) => m.t === 'reject');
|
||||
if (rej) throw new Error(`${p} ${JSON.stringify(a)} rejected: ${JSON.stringify(rej)}`);
|
||||
};
|
||||
return { room, clock, conns, state, expire, act };
|
||||
}
|
||||
|
||||
const allText = (msgs: ServerMessage[]) => JSON.stringify(msgs);
|
||||
|
||||
describe('사건 파일 room flow', () => {
|
||||
test('의뢰 60초 → 수사, 반장 시간 초과는 넘기기만, 보고서 시간 초과 → 채점', () => {
|
||||
const { room, state, expire, conns, act } = harness(['A', 'B'], { turnSeconds: 60, reportSeconds: 180 });
|
||||
expect(state().phase).toBe('briefing');
|
||||
expire();
|
||||
expect(state().phase).toBe('investigate');
|
||||
const cap = state().players[state().decider]!.id;
|
||||
expire();
|
||||
expect(state().players[state().decider]!.id).not.toBe(cap);
|
||||
expect(state().investigations.team!.visited).toHaveLength(0);
|
||||
const now = state().players[state().decider]!.id;
|
||||
act(now, { type: 'pick', lead: 'c1-03' });
|
||||
// The read text went out; texts nobody read did not.
|
||||
const c = CASES.c1;
|
||||
expect(allText(conns.A!.msgs)).toContain(JSON.stringify(c.leads[2]!.text).slice(1, 40));
|
||||
expect(allText(conns.B!.msgs)).not.toContain(JSON.stringify(c.leads[10]!.text).slice(1, 40));
|
||||
expect(allText(conns.B!.msgs)).not.toContain(JSON.stringify(c.solution).slice(1, 40));
|
||||
const next = state().players[state().decider]!.id;
|
||||
act(next, { type: 'report' });
|
||||
expect(state().phase).toBe('report');
|
||||
act('A', { type: 'answer', q: 'q1', choice: 0 });
|
||||
expire();
|
||||
expire();
|
||||
expect(room.game!.finished).toBe(true);
|
||||
expect(room.lastResult!.summary).toContain('범인은 강민재였어요');
|
||||
});
|
||||
|
||||
test('따로 겨루기: 전체 시간이 끝나면 모두 보고서, 끝나면 순위', () => {
|
||||
const { room, state, expire, act } = harness(['A', 'B'], { mode: 'race', raceMinutes: 30, reportSeconds: 180 });
|
||||
expire();
|
||||
expect(state().raceEndsAt).not.toBeNull();
|
||||
act('A', { type: 'pick', lead: 'c1-01' });
|
||||
expire();
|
||||
expect(state().phase).toBe('report');
|
||||
act('B', { type: 'answer', q: 'q1', choice: 0 });
|
||||
act('B', { type: 'submit' });
|
||||
expire();
|
||||
expect(room.game!.finished).toBe(true);
|
||||
expect(room.lastResult!.ranking).toEqual([['B'], ['A']]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user