Skip to content

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

9.4 KB
Raw Blame
import * as assert from "node:assert/strict";
import {readFileSync} from "node:fs";
import {resolve} from "node:path";
import {test} from "node:test";
import {runInNewContext} from "node:vm";
import {ModuleKind, transpileModule} from "typescript";
import {escapeHtmlTextAndAttr} from "../../util/escape";

const {parse} = require("ifdef-loader/preprocessor");

type AccessibilitySetting = {enabled: boolean; override: boolean | null};
type Invoke = (channel: string, data: {cmd: string; enabled?: boolean}) => Promise<AccessibilitySetting>;
type Slot = {key: string; html: () => string; afterMount: (root: unknown) => Promise<void>};

const getAccessibilitySlot = (browser: boolean, mobile: boolean, invoke?: Invoke, slotKey = "accessibilitySupport",
                              switches: string[] = [], scheme = "http"): Slot | undefined => {
    const source = readFileSync(resolve(process.cwd(), "src/config/tabs/appTab.ts"), "utf8");
    const processed = parse(source, {BROWSER: browser, MOBILE: mobile}, false, true);
    const code = transpileModule(processed, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText;
    const moduleExports: {registerAppTab?: (tab: unknown) => void} = {};
    runInNewContext(code, {
        exports: moduleExports,
        process: {platform: "win32"},
        console: {warn: (): void => undefined},
        window: {siyuan: {
            languages: new Proxy({}, {get: (_, key) => String(key)}),
            config: {system: {isMicrosoftStore: true, networkProxy: {host: 'user:"<&@host', port: "7890", scheme}}},
        }},
        require: (name: string) => {
            if (name === "electron") {
                assert.equal(browser, false);
                return {ipcRenderer: {invoke}};
            }
            if (name === "../../constants") {
                return {Constants: {SIYUAN_GET: "siyuan-get"}};
            }
            if (name === "../../util/hostCapabilities") {
                return {getHostCapabilities: () => ({importExport: false, workspaces: false})};
            }
            if (name === "../../util/escape") {
                return {escapeHtmlTextAndAttr};
            }
            return new Proxy({}, {get: () => () => ""});
        },
    });
    const slots: Slot[] = [];
    const group = new Proxy({}, {
        get: (_, method) => (value: Slot) => {
            if (method === "slot") {
                slots.push(value);
            } else if (method === "switch") {
                switches.push(String(value));
            }
        },
    });
    moduleExports.registerAppTab({group: () => group});
    return slots.find(slot => slot.key === slotKey);
};

test("workspace storage is available in desktop, browser and mobile without local workspace management", () => {
    for (const [browser, mobile] of [[false, false], [true, false], [true, true]]) {
        assert.ok(getAccessibilitySlot(browser, mobile, undefined, "workspaceStorage"));
    }
});

test("settings window mode is configurable only in the Electron desktop client", () => {
    for (const [browser, mobile] of [[false, false], [true, false], [true, true], [false, true]]) {
        const switches: string[] = [];
        getAccessibilitySlot(browser, mobile, undefined, "unused", switches);
        assert.equal(switches.includes("system.settingsWindow"), !browser && !mobile);
    }
});

test("network proxy renders escaped values in every build target", () => {
    for (const [browser, mobile] of [[false, false], [true, false], [true, true], [false, true]]) {
        const slot = getAccessibilitySlot(browser, mobile, undefined, "networkProxy");
        assert.ok(slot);
        assert.ok(slot.html().includes('value="user:&quot;&lt;&amp;@host"'));
        assert.ok(slot.html().includes('value="7890"'));
    }
});

test("network proxy keeps its layout space and hides only the manual inputs", () => {
    const manual = getAccessibilitySlot(true, false, undefined, "networkProxy");
    assert.ok(manual);
    assert.ok(manual.html().includes('<div id="networkProxyManual" class="fn__flex fn__flex-1">'));
    assert.equal(manual.html().includes('class="b3-text-field fn__flex-1 fn__none"'), false);

    for (const scheme of ["system", ""]) {
        const automatic = getAccessibilitySlot(true, false, undefined, "networkProxy", [], scheme);
        assert.ok(automatic);
        assert.ok(automatic.html().includes('<div id="networkProxyManual" class="fn__flex fn__flex-1">'));
        assert.equal(automatic.html().match(/class="b3-text-field fn__flex-1 fn__none"/g)?.length, 2);
    }
});

const proxyControls = (scheme: string) => {
    let change: () => Promise<void>;
    const schemeElement = {
        value: scheme,
        addEventListener: (_: string, listener: () => Promise<void>) => { change = listener; },
    };
    const hidden: Record<string, boolean[]> = {host: [], port: []};
    const field = (name: string) => ({
        value: "",
        classList: {
            toggle: (className: string, force?: boolean): void => {
                assert.equal(className, "fn__none");
                hidden[name].push(Boolean(force));
            },
        },
    });
    const host = field("host");
    const port = field("port");
    const root = {
        querySelector: (selector: string) => {
            if (selector === "#networkProxyScheme") {
                return schemeElement;
            }
            if (selector === "#networkProxyHost") {
                return host;
            }
            if (selector === "#networkProxyPort") {
                return port;
            }
            return {addEventListener: (): void => undefined};
        },
    };
    return {root, schemeElement, hidden, change: () => change()};
};

test("network proxy hides host and port inputs unless a manual scheme is selected", async () => {
    const cases = [["http", false], ["https", false], ["socks5", false], ["system", true], ["", true]] as const;
    for (const [scheme, hidden] of cases) {
        const slot = getAccessibilitySlot(true, false, undefined, "networkProxy");
        const view = proxyControls(scheme);
        await slot.afterMount(view.root);
        assert.deepEqual(view.hidden, {host: [hidden], port: [hidden]}, `scheme ${scheme}`);
    }
    const slot = getAccessibilitySlot(true, false, undefined, "networkProxy");
    const view = proxyControls("http");
    await slot.afterMount(view.root);
    view.schemeElement.value = "system";
    await view.change();
    assert.deepEqual(view.hidden, {host: [false, true], port: [false, true]});
});

const controls = () => {
    let change: () => Promise<void>;
    const input = {
        checked: false,
        disabled: true,
        addEventListener: (_: string, listener: () => Promise<void>) => { change = listener; },
    };
    const status = {textContent: "", classList: {toggle: (): void => undefined}};
    const root = {querySelector: (selector: string) => selector === "#accessibilitySupport" ? input : status};
    return {root, input, status, change: () => change()};
};

test("accessibility preference is available in Electron but excluded from browser and mobile", () => {
    assert.ok(getAccessibilitySlot(false, false));
    assert.equal(getAccessibilitySlot(true, false), undefined);
    assert.equal(getAccessibilitySlot(true, true), undefined);
});

test("accessibility preference loads and saves through the desktop process", async () => {
    let saved = false;
    const slot = getAccessibilitySlot(false, false, async (channel, data) => {
        assert.equal(channel, "siyuan-get");
        if (data.cmd === "setAccessibilitySetting") {
            saved = data.enabled;
        }
        return {enabled: saved, override: null};
    });
    const view = controls();
    await slot.afterMount(view.root);
    assert.equal(view.input.checked, false);
    assert.equal(view.input.disabled, false);
    view.input.checked = true;
    await view.change();
    assert.equal(saved, true);
    const reopened = controls();
    await slot.afterMount(reopened.root);
    assert.equal(reopened.input.checked, true);
});

test("startup overrides display their effective state and explain the locked control", async () => {
    for (const override of [false, true]) {
        const slot = getAccessibilitySlot(false, false, async () => ({enabled: !override, override}));
        const view = controls();
        await slot.afterMount(view.root);
        assert.equal(view.input.checked, override);
        assert.equal(view.input.disabled, true);
        assert.equal(view.status.textContent, "accessibilitySupportOverrideTip");
    }
});

test("read failures keep the preference disabled and visible to the user", async () => {
    const slot = getAccessibilitySlot(false, false, async () => { throw new Error("Unavailable"); });
    const view = controls();
    await slot.afterMount(view.root);
    assert.equal(view.input.disabled, true);
    assert.equal(view.status.textContent, "accessibilitySupportError");
});

test("save failures restore the previous choice and allow retry", async () => {
    const slot = getAccessibilitySlot(false, false, async (_, data) => {
        if (data.cmd === "setAccessibilitySetting") {
            throw new Error("Unable to save");
        }
        return {enabled: false, override: null};
    });
    const view = controls();
    await slot.afterMount(view.root);
    view.input.checked = true;
    await view.change();
    assert.equal(view.input.checked, false);
    assert.equal(view.input.disabled, false);
    assert.equal(view.status.textContent, "accessibilitySupportError");
});