summaryrefslogtreecommitdiff
path: root/tests/browser-support.ts
blob: d4ecef926c43d083c1d5036fd0bb9c5088f12850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (C) 2026 Doan Luu Duc Tai <doan@luuduct.ai>
//
// 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 <https://www.gnu.org/licenses/>.

import assert from 'node:assert/strict';
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';

export function required<T>(value: T | undefined | null, message: string): T {
    assert.ok(value !== undefined && value !== null, message);
    return value;
}

function isRecord(value: unknown): value is Record<string, unknown> {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}

export function record(value: unknown): Record<string, unknown> {
    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<ArrayBuffer>;
}

export function asyncServer(handler: (request: IncomingMessage, response: ServerResponse) => Promise<void>): 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> }): void {
    browser.close().catch((error: unknown) => {
        process.exitCode = 1;
        process.stderr.write(`Browser shutdown failed: ${String(error)}\n`);
    });
}