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:
2026-07-22 16:16:22 +00:00
parent 4656a5b082
commit c0a01eb58e
17 changed files with 664 additions and 63 deletions
+129
View File
@@ -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();
});
});
+16 -1
View File
@@ -1,4 +1,7 @@
import { buildLoginFailureMessage } from '../../frontend/src/app/features/landing/login-modal.service';
import {
buildLoginFailureMessage,
landingModalShowsRegister,
} from '../../frontend/src/app/features/landing/login-modal.service';
describe('buildLoginFailureMessage', () => {
it.each([
@@ -50,4 +53,16 @@ describe('buildLoginFailureMessage', () => {
expect(buildLoginFailureMessage({ code: 'EXOTIC', message: 'Quux' }).text).toBe('Quux');
expect(buildLoginFailureMessage({ code: 'EXOTIC', message: '' }).text).toBe('Something went wrong. Please try again.');
});
});
describe('landingModalShowsRegister', () => {
it('returns true only when registrationsEnabled is exactly true', () => {
expect(landingModalShowsRegister(true)).toBe(true);
expect(landingModalShowsRegister(false)).toBe(false);
expect(landingModalShowsRegister(null)).toBe(false);
expect(landingModalShowsRegister(undefined)).toBe(false);
expect(landingModalShowsRegister('true')).toBe(false);
expect(landingModalShowsRegister(1)).toBe(false);
expect(landingModalShowsRegister({})).toBe(false);
});
});