AI Implementation feature(892): Admin Area General Settings and Categories 1.10 (#32)
This commit was merged in pull request #32.
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
const webStreams = require('node:stream/web');
|
||||
const util = require('node:util');
|
||||
|
||||
if (typeof (globalThis as any).ReadableStream === 'undefined') {
|
||||
(globalThis as any).ReadableStream = webStreams.ReadableStream;
|
||||
}
|
||||
if (typeof (globalThis as any).TextEncoder === 'undefined') {
|
||||
(globalThis as any).TextEncoder = util.TextEncoder;
|
||||
}
|
||||
if (typeof (globalThis as any).TextDecoder === 'undefined') {
|
||||
(globalThis as any).TextDecoder = util.TextDecoder;
|
||||
}
|
||||
|
||||
import {
|
||||
isBootstrapGeneralFrame,
|
||||
makeBootstrapEventSource as defaultCreateSource,
|
||||
} from '../../frontend/src/app/core/services/bootstrap-event.pure';
|
||||
|
||||
function makeCaptureableFetch(frames: string[]): { fetchMock: jest.Mock; readCaptured(): { url: string } | null } {
|
||||
const encoder = new TextEncoder();
|
||||
const state: { captured: { url: string } | null } = { captured: null };
|
||||
const fetchMock = jest.fn(async (url: string) => {
|
||||
state.captured = { url };
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
for (const f of frames) controller.enqueue(encoder.encode(f));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
return { ok: true, status: 200, body } as unknown as Response;
|
||||
});
|
||||
return { fetchMock, readCaptured: () => state.captured };
|
||||
}
|
||||
|
||||
describe('isBootstrapGeneralFrame', () => {
|
||||
it('accepts a frame with topic === "general"', () => {
|
||||
expect(isBootstrapGeneralFrame({ topic: 'general', registrationsEnabled: true })).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects frames that do not carry topic === "general"', () => {
|
||||
expect(isBootstrapGeneralFrame({ topic: 'scoreboard' })).toBe(false);
|
||||
expect(isBootstrapGeneralFrame({ registrationsEnabled: true })).toBe(false);
|
||||
expect(isBootstrapGeneralFrame({ status: 'running' })).toBe(false);
|
||||
expect(isBootstrapGeneralFrame(null)).toBe(false);
|
||||
expect(isBootstrapGeneralFrame(undefined)).toBe(false);
|
||||
expect(isBootstrapGeneralFrame('general')).toBe(false);
|
||||
expect(isBootstrapGeneralFrame(42)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('defaultCreateSource (public SSE opener)', () => {
|
||||
let originalFetch: typeof globalThis.fetch | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
originalFetch = (globalThis as any).fetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
(globalThis as any).fetch = originalFetch;
|
||||
});
|
||||
|
||||
it('opens /api/v1/event/stream with credentials and Accept text/event-stream', async () => {
|
||||
const f = makeCaptureableFetch([]);
|
||||
(globalThis as any).fetch = f.fetchMock as any;
|
||||
const src = defaultCreateSource('/api/v1/event/stream');
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
const captured = f.readCaptured();
|
||||
expect(captured).not.toBeNull();
|
||||
expect(captured!.url).toBe('/api/v1/event/stream');
|
||||
src.close();
|
||||
});
|
||||
|
||||
it('dispatches SSE message frames to message listeners', async () => {
|
||||
const frame =
|
||||
'data: {"status":"Running","countdownMs":1,"serverNowUtc":"2026-07-22T00:00:00.000Z","startUtc":null,"endUtc":null}\n\n';
|
||||
const f = makeCaptureableFetch([frame]);
|
||||
(globalThis as any).fetch = f.fetchMock as any;
|
||||
const src = defaultCreateSource('/api/v1/event/stream');
|
||||
const received: any[] = [];
|
||||
src.addEventListener('message', (ev) => {
|
||||
const me = ev as MessageEvent;
|
||||
received.push(typeof me.data === 'string' ? JSON.parse(me.data) : me.data);
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
expect(received.length).toBeGreaterThanOrEqual(1);
|
||||
expect(received[0].status).toBe('Running');
|
||||
src.close();
|
||||
});
|
||||
|
||||
it('buffers early frames and flushes them when a message listener attaches', async () => {
|
||||
const frame =
|
||||
'data: {"topic":"general","themeKey":"classic","registrationsEnabled":true}\n\n';
|
||||
const f = makeCaptureableFetch([frame]);
|
||||
(globalThis as any).fetch = f.fetchMock as any;
|
||||
const src = defaultCreateSource('/api/v1/event/stream');
|
||||
// Wait for the underlying reader to drain before adding the listener
|
||||
// so we exercise the buffering path.
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
const received: any[] = [];
|
||||
src.addEventListener('message', (ev) => {
|
||||
const me = ev as MessageEvent;
|
||||
received.push(typeof me.data === 'string' ? JSON.parse(me.data) : me.data);
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
expect(received.length).toBeGreaterThanOrEqual(1);
|
||||
expect(received.some((r) => r && r.topic === 'general')).toBe(true);
|
||||
src.close();
|
||||
});
|
||||
|
||||
it('forwards error events when the underlying fetch reports failure', async () => {
|
||||
const fetchMock = jest.fn(async () => ({ ok: false, status: 500, body: null } as unknown as Response));
|
||||
(globalThis as any).fetch = fetchMock as any;
|
||||
const src = defaultCreateSource('/api/v1/event/stream');
|
||||
const errors: string[] = [];
|
||||
src.addEventListener('error', (ev) => errors.push((ev as Event).type));
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
expect(errors).toContain('error');
|
||||
src.close();
|
||||
});
|
||||
|
||||
it('close() is idempotent and safe to call twice', async () => {
|
||||
const f = makeCaptureableFetch([]);
|
||||
(globalThis as any).fetch = f.fetchMock as any;
|
||||
const src = defaultCreateSource('/api/v1/event/stream');
|
||||
src.close();
|
||||
expect(() => src.close()).not.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user