import { describe, expect, it } from 'bun:test' import { SSEManager } from './sseManager' import type { SyncEvent } from '../sync/syncEngine' describe('SSEManager namespace filtering', () => { it('routes events to matching namespace', () => { const manager = new SSEManager(0) const receivedAlpha: SyncEvent[] = [] const receivedBeta: SyncEvent[] = [] manager.subscribe({ id: 'alpha', namespace: 'alpha', all: true, send: (event) => { receivedAlpha.push(event) }, sendHeartbeat: () => {} }) manager.subscribe({ id: 'beta', namespace: 'beta', all: true, send: (event) => { receivedBeta.push(event) }, sendHeartbeat: () => {} }) manager.broadcast({ type: 'session-updated', sessionId: 's1', namespace: 'alpha' }) expect(receivedAlpha).toHaveLength(1) expect(receivedBeta).toHaveLength(0) }) it('broadcasts connection-changed to all namespaces', () => { const manager = new SSEManager(0) const received: Array<{ id: string; event: SyncEvent }> = [] manager.subscribe({ id: 'alpha', namespace: 'alpha', all: true, send: (event) => { received.push({ id: 'alpha', event }) }, sendHeartbeat: () => {} }) manager.subscribe({ id: 'beta', namespace: 'beta', all: true, send: (event) => { received.push({ id: 'beta', event }) }, sendHeartbeat: () => {} }) manager.broadcast({ type: 'connection-changed', data: { status: 'connected' } }) expect(received).toHaveLength(2) expect(received.map((entry) => entry.id).sort()).toEqual(['alpha', 'beta']) }) })