62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
process.env.DATABASE_PATH = ':memory:';
|
|
process.env.THEMES_DIR = './themes';
|
|
process.env.FRONTEND_DIST = './frontend/dist';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
import { INestApplication } from '@nestjs/common';
|
|
import { HttpAdapterHost } from '@nestjs/core';
|
|
import request from 'supertest';
|
|
import { AppModule } from '../../backend/src/app.module';
|
|
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
|
|
|
|
describe('SSE flattened payloads', () => {
|
|
let app: INestApplication;
|
|
|
|
beforeAll(async () => {
|
|
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
|
app = moduleRef.createNestApplication();
|
|
const httpAdapterHost = app.get(HttpAdapterHost);
|
|
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
|
await app.init();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('event/stream emits a flattened { status, countdownMs, serverNowUtc, startUtc, endUtc } payload', (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[] = [];
|
|
res.on('data', (chunk: Buffer) => {
|
|
chunks.push(chunk);
|
|
if (received) return;
|
|
const text = Buffer.concat(chunks).toString('utf8');
|
|
const match = /data: ({.*?})\n/.exec(text);
|
|
if (match) {
|
|
try {
|
|
const payload = JSON.parse(match[1]);
|
|
expect(payload).toHaveProperty('status');
|
|
expect(payload).toHaveProperty('countdownMs');
|
|
expect(payload).toHaveProperty('serverNowUtc');
|
|
expect(payload).toHaveProperty('startUtc');
|
|
expect(payload).toHaveProperty('endUtc');
|
|
expect(['Stopped', 'Running']).toContain(payload.status);
|
|
received = true;
|
|
(res as any).destroy();
|
|
done();
|
|
} catch (e) {
|
|
done(e);
|
|
}
|
|
}
|
|
});
|
|
res.on('end', () => cb(null, Buffer.concat(chunks)));
|
|
res.on('error', (err) => cb(err, null));
|
|
})
|
|
.end(() => {});
|
|
}, 10_000);
|
|
}); |