컴퓨터 방 자동 정리 + 게스트 계정 삭제(로그아웃·하루 미접속) + 테스트 10분→1분

- 방 화면을 벗어나면(홈·규칙) detach: 자리·접속 상태는 유지, 방 접속 수에서 빠짐
- 컴퓨터가 있는 방: 사람이 모두 [방 나가기]하면 바로 닫고, 아무도 안 보면 대기실·끝난 방 1분, 게임 중 30분 뒤 정리
- 게스트 로그아웃 = 계정 삭제(자리·전적·세션·소켓), 사이트를 닫고 24시간 지난 게스트도 1분 주기로 삭제
- users.last_seen_at을 요청·소켓 종료 때 갱신(이전엔 갱신 안 됨), 003 마이그레이션에서 기존 게스트 시계 초기화
- 테스트: 무작위 대국 시드를 playoutSeeds()로 평소 1/10, test:full은 전부. bun test --parallel=4, 테스트 서버 종료 대기 1s→50ms

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-07 17:10:52 +09:00
parent 373231b553
commit 39223bacdd
49 changed files with 629 additions and 155 deletions

View File

@@ -0,0 +1,128 @@
/** Guest accounts are deleted on logout and after a day away (docs/05 §5); leaving a room page detaches. */
import { afterEach, describe, expect, test } from 'bun:test';
import { Client, api, boot, guest, sleep } from './test-utils';
import { GUEST_IDLE_MS } from './server';
type Booted = ReturnType<typeof boot>;
const running: Booted[] = [];
const clients: Client[] = [];
afterEach(async () => {
for (const c of clients.splice(0)) c.close();
for (const s of running.splice(0)) await s.stop();
});
function start(): Booted {
const s = boot();
running.push(s);
return s;
}
async function connect(s: Booted, g: { cookie: string; id: string }): Promise<Client> {
const c = await new Client(s.base, g.cookie, g.id).connect();
clients.push(c);
return c;
}
async function practiceRoom(s: Booted, c: Client): Promise<string> {
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: c.cookie, body: JSON.stringify({ gameId: 'omok', practice: true }) });
expect(r.res.status).toBe(200);
c.send({ t: 'join', code: r.body.code, as: 'player' });
await c.waitFor((m) => m.t === 'room');
return r.body.code;
}
const userRow = (s: Booted, id: string) => s.db.query('SELECT id FROM users WHERE id = ?').get(id);
const setSeen = (s: Booted, id: string, at: number) => s.db.query('UPDATE users SET last_seen_at = ? WHERE id = ?').run(at, id);
describe('guest logout', () => {
test('deletes the guest: account, sessions, seat (the practice room closes) and open sockets', async () => {
const s = start();
const g = await guest(s.base, '지울게스트');
const c = await connect(s, g);
const code = await practiceRoom(s, c);
expect(s.rooms.get(code)).toBeDefined();
const out = await api(s.base, '/api/auth/logout', { method: 'POST', cookie: g.cookie });
expect(out.res.status).toBe(200);
expect(userRow(s, g.id)).toBeNull();
expect(s.db.query('SELECT 1 FROM sessions WHERE user_id = ?').get(g.id)).toBeNull();
expect(s.rooms.get(code)).toBeUndefined();
for (let i = 0; i < 50 && !c.closed; i++) await sleep(10);
expect(c.closed).not.toBeNull();
expect((await api(s.base, '/api/me', { cookie: g.cookie })).body.me).toBeNull();
});
test('a member logging out keeps the account', async () => {
const s = start();
const g = await guest(s.base, '회원');
s.users.promoteToMember(g.id, null);
await api(s.base, '/api/auth/logout', { method: 'POST', cookie: g.cookie });
expect(userRow(s, g.id)).not.toBeNull();
});
});
describe('idle guests', () => {
test('a guest away for a day is deleted; one still on the site, holding a seat, or seen recently is kept', async () => {
const s = start();
const old = Date.now() - GUEST_IDLE_MS - 60_000;
const gone = await guest(s.base, '떠난게스트');
const online = await guest(s.base, '접속중');
const seated = await guest(s.base, '자리있음');
const recent = await guest(s.base, '최근');
await connect(s, online);
const sc = await connect(s, seated);
// A human lobby (no computers): the seat survives the socket closing.
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: seated.cookie, body: JSON.stringify({ gameId: 'omok' }) });
sc.send({ t: 'join', code: r.body.code, as: 'player' });
await sc.waitFor((m) => m.t === 'room');
sc.close();
for (let i = 0; i < 50 && !sc.closed; i++) await sleep(10);
await sleep(20);
for (const g of [gone, online, seated]) setSeen(s, g.id, old);
s.purgeIdleGuests();
expect(userRow(s, gone.id)).toBeNull();
expect(userRow(s, online.id)).not.toBeNull();
expect(userRow(s, seated.id)).not.toBeNull();
expect(userRow(s, recent.id)).not.toBeNull();
// Coming back counts as activity.
setSeen(s, recent.id, old);
expect((await api(s.base, '/api/me', { cookie: recent.cookie })).body.me.id).toBe(recent.id);
s.purgeIdleGuests();
expect(userRow(s, recent.id)).not.toBeNull();
});
});
describe('leaving the room page', () => {
test('detach keeps the seat but the room no longer counts the connection', async () => {
const s = start();
const g = await guest(s.base, '홈으로');
const c = await connect(s, g);
const code = await practiceRoom(s, c);
const room = s.rooms.get(code)!;
expect(room.conns.size).toBe(1);
c.send({ t: 'detach' });
for (let i = 0; i < 50 && room.conns.size; i++) await sleep(10);
expect(room.conns.size).toBe(0);
expect(room.seatOf(g.id)).toBeGreaterThanOrEqual(0);
expect(room.emptySince).not.toBeNull();
// Still on the site: counts as connected (no "연결 끊김", no auto moves).
expect(room.presence.get(g.id)?.connected).toBe(true);
// Detaching twice is harmless.
c.send({ t: 'detach' });
c.send({ t: 'ping', ts: 1 });
await c.waitFor((m) => m.t === 'pong');
expect(c.messages.some((m) => m.t === 'error')).toBe(false);
// Coming back to the page re-attaches the same socket.
c.send({ t: 'join', code, as: 'player' });
for (let i = 0; i < 50 && !room.conns.size; i++) await sleep(10);
expect(room.conns.size).toBe(1);
expect(room.emptySince).toBeNull();
// Closing the site while detached: the room learns the person went offline.
c.send({ t: 'detach' });
for (let i = 0; i < 50 && room.conns.size; i++) await sleep(10);
c.close();
for (let i = 0; i < 50 && room.presence.get(g.id)?.connected; i++) await sleep(10);
expect(room.presence.get(g.id)?.connected).toBe(false);
expect(room.detached.size).toBe(0);
});
});