process.env.DATABASE_PATH = ':memory:'; process.env.THEMES_DIR = './themes'; process.env.FRONTEND_DIST = './frontend/dist'; process.env.NODE_ENV = 'test'; import { Test } from '@nestjs/testing'; import { INestApplication, ValidationPipe } from '@nestjs/common'; import { HttpAdapterHost } from '@nestjs/core'; import cookieParser from 'cookie-parser'; import * as express from 'express'; import request from 'supertest'; import { AppModule } from '../../backend/src/app.module'; import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter'; import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware'; import { SseHubService } from '../../backend/src/common/services/sse-hub.service'; import { ConfigService } from '@nestjs/config'; import { initDb } from './db-helper'; describe('GET /api/v1/event/stream forwards general topic (Job 892)', () => { let app: INestApplication; let hub: SseHubService; beforeAll(async () => { const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile(); app = moduleRef.createNestApplication(); app.use(cookieParser()); app.use(express.json({ limit: '1mb' })); const csrfMw = new CsrfMiddleware(app.get(ConfigService)); app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next)); app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: false, transform: true })); const httpAdapterHost = app.get(HttpAdapterHost); app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost)); await app.init(); await initDb(app); await request(app.getHttpServer()) .post('/api/v1/auth/register-first-admin') .send({ username: 'admin', password: 'Sup3rSecret!Pass' }) .expect(201); hub = app.get(SseHubService); }); afterAll(async () => { await app.close(); }); it('emits a { topic: "general", themeKey, registrationsEnabled } frame through the public stream', (done) => { const server = app.getHttpServer(); const req = request(server).get('/api/v1/event/stream'); let received = false; req .buffer(true) .parse((res, cb) => { const chunks: Buffer[] = []; const finalize = () => cb(null, Buffer.concat(chunks)); res.on('data', (chunk: Buffer) => { chunks.push(chunk); if (received) return; const text = Buffer.concat(chunks).toString('utf8'); const match = /data: (\{[^{]*?"topic":\s*"general"[^{]*\})\n/.exec(text); if (!match) return; try { const payload = JSON.parse(match[1]); expect(payload.topic).toBe('general'); expect(payload).toHaveProperty('themeKey'); expect(payload).toHaveProperty('registrationsEnabled'); received = true; (res as any).destroy(); done(); } catch (e) { (res as any).destroy(); done(e); } }); res.on('end', finalize); res.on('error', (err) => cb(err, null)); }) .end(() => {}); // Push the hub event shortly after the consumer attaches. setTimeout(() => { hub.emitEvent({ topic: 'general', themeKey: 'midnight', registrationsEnabled: true }); }, 150); setTimeout(() => { if (!received) { done(new Error('Did not receive a general frame within 5s')); } }, 5000); }, 10_000); });