189 lines
8.3 KiB
TypeScript
189 lines
8.3 KiB
TypeScript
/** 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('alone with no game running (뒤로가기 from the lobby): leaving the page leaves the room and it closes at once', async () => {
|
|
const s = start();
|
|
const g = await guest(s.base, '혼자');
|
|
const c = await connect(s, g);
|
|
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: c.cookie, body: JSON.stringify({ gameId: 'omok' }) });
|
|
c.send({ t: 'join', code: r.body.code, as: 'player' });
|
|
await c.waitFor((m) => m.t === 'room');
|
|
expect(s.rooms.get(r.body.code)).toBeDefined();
|
|
c.send({ t: 'detach' });
|
|
for (let i = 0; i < 50 && s.rooms.get(r.body.code); i++) await sleep(10);
|
|
expect(s.rooms.get(r.body.code)).toBeUndefined();
|
|
expect(s.rooms.activeRoomFor(g.id)).toBeNull();
|
|
// The same socket is fine afterwards.
|
|
c.send({ t: 'ping', ts: 1 });
|
|
await c.waitFor((m) => m.t === 'pong');
|
|
});
|
|
|
|
test('someone else is in the lobby: leaving the page keeps my seat (I can come back)', async () => {
|
|
const s = start();
|
|
const a = await guest(s.base, '방장');
|
|
const b = await guest(s.base, '친구');
|
|
const ca = await connect(s, a);
|
|
const cb = await connect(s, b);
|
|
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: ca.cookie, body: JSON.stringify({ gameId: 'omok' }) });
|
|
ca.send({ t: 'join', code: r.body.code, as: 'player' });
|
|
await ca.waitFor((m) => m.t === 'room');
|
|
cb.send({ t: 'join', code: r.body.code, as: 'player' });
|
|
await cb.waitFor((m) => m.t === 'room');
|
|
const room = s.rooms.get(r.body.code)!;
|
|
ca.send({ t: 'detach' });
|
|
for (let i = 0; i < 50 && room.conns.size > 1; i++) await sleep(10);
|
|
expect(room.conns.size).toBe(1);
|
|
expect(room.seatOf(a.id)).toBeGreaterThanOrEqual(0);
|
|
expect(s.rooms.get(r.body.code)).toBe(room);
|
|
});
|
|
|
|
test('detach during a game 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)!;
|
|
c.send({ t: 'start' });
|
|
for (let i = 0; i < 50 && room.status !== 'playing'; i++) await sleep(10);
|
|
expect(room.status).toBe('playing');
|
|
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);
|
|
});
|
|
});
|
|
|
|
describe('on the site or not', () => {
|
|
test('offlineSince: null while any socket is open, the close time once the last one closes', async () => {
|
|
const s = start();
|
|
const g = await guest(s.base, '사이트');
|
|
expect(s.gateway.offlineSince(g.id)).toBe(0); // never connected since the server started
|
|
const c1 = await connect(s, g);
|
|
const c2 = await connect(s, g);
|
|
expect(s.gateway.offlineSince(g.id)).toBeNull();
|
|
c1.close();
|
|
for (let i = 0; i < 50 && !c1.closed; i++) await sleep(10);
|
|
await sleep(20);
|
|
expect(s.gateway.offlineSince(g.id)).toBeNull(); // another tab is still open
|
|
const before = Date.now();
|
|
c2.close();
|
|
for (let i = 0; i < 50 && s.gateway.offlineSince(g.id) === null; i++) await sleep(10);
|
|
expect(s.gateway.offlineSince(g.id)).toBeGreaterThanOrEqual(before);
|
|
await connect(s, g);
|
|
expect(s.gateway.offlineSince(g.id)).toBeNull();
|
|
});
|
|
});
|