diff --git a/apps/server/src/rooms.test.ts b/apps/server/src/rooms.test.ts index a94a93f..bb51f9e 100644 --- a/apps/server/src/rooms.test.ts +++ b/apps/server/src/rooms.test.ts @@ -206,10 +206,28 @@ describe('rooms over WebSocket', () => { await cb.waitFor((m) => m.t === 'chat' && m.line.text === '도배29'); expect(ca.messages.some((m) => m.t === 'error')).toBe(false); s.settings.setSite({ ...s.settings.getSite(), chatFloodGuard: true, chatRatePerSec: 1, chatBurst: 3 }, a.id); - for (let i = 0; i < 6; i++) ca.send({ t: 'chat', text: `또${i}` }); - expect((await errorOf(ca)).message).toContain('너무 빨리'); + for (let i = 0; i < 6; i++) ca.send({ t: 'chat', text: `또${i}`, n: `n${i}` }); + const refused = await errorOf(ca); + expect(refused.message).toContain('너무 빨리'); await sleep(100); expect(chatOf(cb).filter((l) => l.text.startsWith('또')).length).toBe(3); + // every refused line names its nonce so the sender can drop its "보내는 중" copy + expect(ca.messages.filter((m) => m.t === 'error').map((m) => (m as { n?: string }).n)).toEqual(['n3', 'n4', 'n5']); + // a line that is empty after cleaning is refused with its nonce too + s.settings.setSite({ ...s.settings.getSite(), chatFloodGuard: false }, a.id); + ca.send({ t: 'chat', text: '\u0007', n: 'blank' }); + expect(await ca.waitFor((m) => m.t === 'error' && m.n === 'blank')).toMatchObject({ message: '빈 메시지는 보낼 수 없어요.' }); + }); + + test('a room created but never entered expires like an empty room', async () => { + s = boot(); + const a = await guest(s.base, '만들기만'); + const code = (await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: '{}' })).body.code; + s.rooms.sweep(Date.now() + 60_000); + expect(s.rooms.get(code)).toBeDefined(); // still there for the creator who is on the way in + s.rooms.sweep(Date.now() + 5 * 60_000 + 1000); + expect(s.rooms.get(code)).toBeUndefined(); + expect((await api(s.base, `/api/rooms/${code}`)).res.status).toBe(404); }); test('lobby song count follows catalog changes made by another process', async () => { diff --git a/apps/server/src/rooms/manager.ts b/apps/server/src/rooms/manager.ts index c9bd33f..a79ac4c 100644 --- a/apps/server/src/rooms/manager.ts +++ b/apps/server/src/rooms/manager.ts @@ -133,6 +133,8 @@ export class RoomManager { const cfg = applyConfigPatch({ ...DEFAULT_ROOM_CONFIG, title: `${nick}님의 방`.slice(0, 40) }, init); if (typeof cfg === 'string') return cfg; const room = new Room(this.newCode(), hostId, cfg, this.deps); + // Empty until someone joins: a room created and never entered expires like any empty room. + room.emptySince = Date.now(); this.rooms.set(room.code, room); this.deps.log?.('room created', { code: room.code, host: hostId }); return room; @@ -294,7 +296,7 @@ export class RoomManager { const room = this.roomOf(ws.data.userId); if (!room || ws.data.roomCode !== room.code) return '방에 들어가 있지 않아요.'; const t = text.replace(/[\u0000-\u001f\u007f]/g, ' ').trim(); - if (!t) return null; + if (!t) return '빈 메시지는 보낼 수 없어요.'; room.say(ws.data.userId, t, n); return null; } diff --git a/apps/server/src/ws/gateway.ts b/apps/server/src/ws/gateway.ts index 6f44392..0a6b623 100644 --- a/apps/server/src/ws/gateway.ts +++ b/apps/server/src/ws/gateway.ts @@ -103,9 +103,11 @@ export class Gateway { if (g?.on) { let b = this.buckets.get(ws.data.connId); if (!b) this.buckets.set(ws.data.connId, (b = new TokenBucket(g.ratePerSec, g.burst))); - if (!b.take()) return this.send(ws, { t: 'error', message: '채팅을 너무 빨리 보내고 있어요. 잠깐 쉬었다 보내 주세요.' }); + if (!b.take()) return this.send(ws, { t: 'error', message: '채팅을 너무 빨리 보내고 있어요. 잠깐 쉬었다 보내 주세요.', n: m.n }); } - return err(rooms.chat(ws, m.text, m.n)); + const e = rooms.chat(ws, m.text, m.n); + if (e) this.send(ws, { t: 'error', message: e, n: m.n }); + return; } } }, diff --git a/apps/web/src/net/socket.ts b/apps/web/src/net/socket.ts index ffc0915..2d74ac1 100644 --- a/apps/web/src/net/socket.ts +++ b/apps/web/src/net/socket.ts @@ -120,7 +120,11 @@ function onMessage(m: ServerMessage): void { return; case 'error': { const st = useRoom.getState(); - if (joining && st.code) { + if (m.n) { + // A refused chat line: drop its "보내는 중" copy and say why. + useRoom.setState({ pending: st.pending.filter((p) => p.n !== m.n) }); + toast(m.message, 'error'); + } else if (joining && st.code) { joining = false; // The room was there a moment ago (this tab was in it): the server restarted and rooms live in memory (docs/04 결정 1). // Kept after that, so a refresh still explains it. diff --git a/e2e/rooms.e2e.ts b/e2e/rooms.e2e.ts index 05be2be..240a8d9 100644 --- a/e2e/rooms.e2e.ts +++ b/e2e/rooms.e2e.ts @@ -150,6 +150,23 @@ try { expect((await a.getByLabel('채팅 입력').inputValue()) === '', '보낸 뒤 입력창이 비지 않음'); }); + await step('도배 방지를 켜면 거절된 말은 "보내는 중"으로 남지 않고 안내만 뜸', async () => { + const site = app.settings.getSite(); + app.settings.setSite({ ...site, chatFloodGuard: true, chatRatePerSec: 1, chatBurst: 2 }, 'e2e'); + const box = b.getByLabel('채팅 입력'); + for (let i = 0; i < 5; i++) { + await box.fill(`빠른말${i}`); + await box.press('Enter'); + } + await b.getByText('채팅을 너무 빨리 보내고 있어요. 잠깐 쉬었다 보내 주세요.').first().waitFor(); + await a.locator('.chat-line', { hasText: '빠른말1' }).waitFor(); + await b.waitForFunction(() => document.querySelectorAll('.chat-line.pending').length === 0, null, { timeout: 3000 }); + const shown = await a.locator('.chat-line', { hasText: '빠른말' }).count(); + expect(shown === 2, `받은 쪽에 ${shown}줄`); + expect((await b.locator('.chat-line', { hasText: '빠른말' }).count()) === 2, '거절된 말이 보낸 쪽 화면에 남음'); + app.settings.setSite({ ...site, chatFloodGuard: false }, 'e2e'); + }); + await step('비밀번호 방: 목록에 🔒, 들어갈 때 비밀번호(틀리면 안내), 맞으면 입장하고 지난 채팅이 보임', async () => { await a.getByRole('radiogroup', { name: '방 공개' }).getByRole('radio', { name: '비밀번호' }).click(); await a.getByText('비밀번호를 정해야 잠겨요').waitFor(); diff --git a/packages/shared/src/protocol.ts b/packages/shared/src/protocol.ts index 701c4c7..e46e708 100644 --- a/packages/shared/src/protocol.ts +++ b/packages/shared/src/protocol.ts @@ -67,7 +67,8 @@ export type ServerMessage = | { t: 'chatlog'; code: string; lines: ChatLine[] } | { t: 'notice'; text: string } | { t: 'pong'; c: number; s: number } - | { t: 'error'; message: string }; + /** `n`: the chat nonce when a chat line was refused, so the sender drops its "보내는 중" line. */ + | { t: 'error'; message: string; n?: string }; /** * Server clock offset from ping samples (docs/12 Q1): use the sample with the smallest round trip.