feat(m8): SSE hub + realtime chat with mentions, tests, e2e

- SseHub (project-scoped client mgmt + broadcast, implements SseBroadcaster)
  with SSE-formatted payload; singleton getSseHub
- ChatRepository (message CRUD, soft-delete, search, pagination, project
  isolation) + ChatService (membership permission, send/edit/delete,
  @email mention -> mention notification, SSE broadcast of created/updated/deleted)
- SSE stream endpoint (ReadableStream + heartbeat + abort cleanup, membership)
- Chat message API routes (history/send/edit/delete)
- ChatWindow client (EventSource auto-reconnect, history fetch, realtime
  append/update/delete), chat page, ProjectNav チャット link
- SseEvent type now carries ChatMessage for chat events
- Unit (SseHub, ChatRepository, ChatService) + integration chat-sse-broadcast
  + e2e chat-sse (two contexts realtime)
This commit is contained in:
Ken Yasue
2026-06-25 01:45:34 +02:00
parent 83a02265f5
commit ddad5ae78c
16 changed files with 1183 additions and 7 deletions

View File

@ -0,0 +1,92 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { SseHub, type SseClient } from '@/lib/sse/hub';
function makeClient(): SseClient & { received: string[] } {
const received: string[] = [];
return {
received,
enqueue: (chunk: string) => received.push(chunk),
close: () => undefined,
};
}
describe('SseHub', () => {
let hub: SseHub;
beforeEach(() => {
hub = new SseHub();
});
it('adds and counts clients per project', () => {
hub.addClient(1, makeClient());
hub.addClient(1, makeClient());
hub.addClient(2, makeClient());
expect(hub.getClientCount(1)).toBe(2);
expect(hub.getClientCount(2)).toBe(1);
expect(hub.getClientCount(999)).toBe(0);
});
it('removes a client', () => {
const c = makeClient();
hub.addClient(1, c);
hub.removeClient(1, c);
expect(hub.getClientCount(1)).toBe(0);
});
it('broadcasts only to clients of the target project', () => {
const a1 = makeClient();
const a2 = makeClient();
const b1 = makeClient();
hub.addClient(1, a1);
hub.addClient(1, a2);
hub.addClient(2, b1);
hub.broadcast(1, {
type: 'chat.message.created',
data: { projectId: 1, message: { id: 1 } as never },
});
expect(a1.received).toHaveLength(1);
expect(a2.received).toHaveLength(1);
expect(b1.received).toHaveLength(0);
expect(a1.received[0]).toContain('chat.message.created');
});
it('broadcast payload is SSE formatted (data: ...\\n\\n)', () => {
const c = makeClient();
hub.addClient(1, c);
hub.broadcast(1, {
type: 'chat.message.deleted',
data: { projectId: 1, id: 5 },
});
expect(c.received[0]).toMatch(/^data: .+\n\n$/);
const parsed = JSON.parse(
c.received[0].replace(/^data: /, '').replace(/\n\n$/, '')
);
expect(parsed.type).toBe('chat.message.deleted');
expect(parsed.data.id).toBe(5);
});
it('does nothing when there are no clients for the project', () => {
expect(() =>
hub.broadcast(42, {
type: 'note.updated',
data: { projectId: 42 },
})
).not.toThrow();
});
it('removes a client that throws on enqueue', () => {
const broken: SseClient = {
enqueue: () => {
throw new Error('disconnected');
},
close: () => undefined,
};
hub.addClient(1, broken);
hub.broadcast(1, { type: 'note.updated', data: { projectId: 1 } });
expect(hub.getClientCount(1)).toBe(0);
});
});