Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

2.9 KB
Raw Blame
import {it} from "node:test";
import * as assert from "node:assert/strict";
import {loadLanguages} from "./loadLanguages";

it("stops startup and shows the failed language URL when the server returns 404", async () => {
    const originalFetch = globalThis.fetch;
    const originalDocument = globalThis.document;
    const originalConsoleError = console.error;
    let displayed = "";
    let role = "";
    let className = "";
    let booted = false;
    globalThis.fetch = async () => new Response("", {status: 404});
    console.error = () => {};
    (globalThis as {document: Document}).document = {
        createElement: (tag: string) => {
            assert.equal(tag, "pre");
            return {
                className: "",
                textContent: "",
                setAttribute: (name: string, value: string) => {
                    if (name === "role") {
                        role = value;
                    }
                },
            };
        },
        body: {
            appendChild: (element: {className: string; textContent: string}) => {
                displayed = element.textContent;
                className = element.className;
            },
            replaceChildren: () => assert.fail("startup markup must remain available"),
        },
    } as unknown as Document;
    try {
        await loadLanguages("zh-CN", "3.8.6", () => booted = true);
        assert.equal(booted, false);
        assert.equal(role, "alert");
        assert.equal(className, "language-load-error");
        assert.match(displayed, /\/appearance\/langs\/zh-CN\.json\?v=3\.8\.6\nError: HTTP 404/);
    } finally {
        globalThis.fetch = originalFetch;
        if (originalDocument === undefined) {
            delete (globalThis as {document?: Document}).document;
        } else {
            globalThis.document = originalDocument;
        }
        console.error = originalConsoleError;
    }
});

it("passes a valid language pack to startup", async () => {
    const originalFetch = globalThis.fetch;
    globalThis.fetch = async () => new Response(JSON.stringify({siyuanNote: "SiYuan"}), {status: 200});
    let loaded: IObject | undefined;
    try {
        await loadLanguages("en", "3.8.6", languages => loaded = languages);
        assert.equal(loaded?.siyuanNote, "SiYuan");
    } finally {
        globalThis.fetch = originalFetch;
    }
});

it("routes failed language loading to the independent startup error view", async () => {
    const originalFetch = globalThis.fetch;
    const originalError = console.error;
    globalThis.fetch = async () => new Response("", {status: 404});
    console.error = () => {};
    let failure: unknown;
    try {
        await loadLanguages("ar", "test", () => assert.fail("unexpected startup"), error => failure = error);
        assert.match(String(failure), /HTTP 404/);
    } finally {
        globalThis.fetch = originalFetch;
        console.error = originalError;
    }
});