// Copyright (C) 2026 Doan Luu Duc Tai // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import assert from 'node:assert/strict'; import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'; export function required(value: T | undefined | null, message: string): T { assert.ok(value !== undefined && value !== null, message); return value; } function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value); } export function record(value: unknown): Record { assert.ok(isRecord(value)); return value; } export function string(value: unknown): string { assert.equal(typeof value, 'string'); if (typeof value !== 'string') throw new Error('Expected string'); return value; } export function list(value: unknown): unknown[] { assert.ok(Array.isArray(value)); return value; } export function serverOrigin(server: Server): string { const address = server.address(); assert.ok(address !== null && typeof address !== 'string'); return `http://127.0.0.1:${String(address.port)}`; } export function requestBody(text: string): { op: string; count: number; [key: string]: unknown } { const value = record(JSON.parse(text)); return { ...value, op: string(value['op']), count: typeof value['count'] === 'number' ? value['count'] : 0 }; } export function libreJSAddon(text: string): { active: boolean; version: string } { const addon = list(record(JSON.parse(text))['addons']).map(record).find(value => value['id'] === 'jid1-KtlZuoiikVfFew@jetpack'); assert.ok((addon !== undefined)); assert.equal(addon['active'], true); return { active: true, version: string(addon['version']) }; } declare global { interface Window { unlicensedControl?: boolean; chatInjection?: boolean } const wasm: Promise; } export function asyncServer(handler: (request: IncomingMessage, response: ServerResponse) => Promise): Server { return createServer((request, response) => { handler(request, response).catch((error: unknown) => { response.destroy(error instanceof Error ? error : new Error('Fixture request failed', { cause: error })); }); }); } export function closeBrowser(browser: { close(): Promise }): void { browser.close().catch((error: unknown) => { process.exitCode = 1; process.stderr.write(`Browser shutdown failed: ${String(error)}\n`); }); }