Skip to content

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

6.0 KB
Raw Blame
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {callMobileAppShowKeyboard, canInput} from "./mobileAppUtil";

const createElement = (options: {
    tagName?: string,
    classNames?: string[],
    attributes?: Record<string, string>,
    parentElement?: HTMLElement,
}) => {
    const attributes = options.attributes || {};
    return {
        nodeType: 1,
        tagName: options.tagName || "DIV",
        parentElement: options.parentElement || null,
        classList: {
            contains: (className: string) => options.classNames?.includes(className) || false,
        },
        getAttribute: (name: string) => attributes[name] ?? null,
        hasAttribute: (name: string) => Object.prototype.hasOwnProperty.call(attributes, name),
    } as unknown as HTMLElement;
};

describe("mobile input detection", () => {
    it("rejects readonly and disabled text fields without requesting input", () => {
        for (const tagName of ["INPUT", "TEXTAREA"]) {
            for (const attributes of [{readonly: ""}, {readonly: "readonly"}, {disabled: ""}]) {
                assert.equal(canInput(createElement({tagName, attributes})), false);
            }
            const input = createElement({tagName});
            assert.equal(canInput(input), input);
        }
    });

    const createEditableRoot = () => createElement({
        classNames: ["protyle-wysiwyg"],
        attributes: {contenteditable: "true", "data-readonly": "false"},
        parentElement: createElement({tagName: "BODY"}),
    });

    it("rejects non-editable render areas and their descendants", () => {
        const renderElement = createElement({
            attributes: {contenteditable: "false"},
            parentElement: createEditableRoot(),
        });
        const canvasElement = createElement({tagName: "CANVAS", parentElement: renderElement});

        assert.equal(canInput(renderElement), false);
        assert.equal(canInput(canvasElement), false);
    });

    it("accepts ordinary text inside the editable root", () => {
        const wysiwygElement = createEditableRoot();
        const textElement = createElement({tagName: "SPAN", parentElement: wysiwygElement});

        assert.equal(canInput(textElement), wysiwygElement);
    });

    it("accepts explicitly editable content inside a non-editable database", () => {
        const databaseElement = createElement({
            attributes: {contenteditable: "false"},
            parentElement: createEditableRoot(),
        });
        const editableElement = createElement({
            attributes: {contenteditable: "true"},
            parentElement: databaseElement,
        });
        const textElement = createElement({tagName: "SPAN", parentElement: editableElement});

        assert.equal(canInput(editableElement), editableElement);
        assert.equal(canInput(textElement), editableElement);
        for (const tagName of ["INPUT", "TEXTAREA"]) {
            const inputElement = createElement({tagName, parentElement: databaseElement});
            assert.equal(canInput(inputElement), inputElement);
        }
    });

    it("accepts a plaintext database search field and its descendants", () => {
        const databaseElement = createElement({
            attributes: {contenteditable: "false"},
            parentElement: createEditableRoot(),
        });
        const searchElement = createElement({
            attributes: {contenteditable: "plaintext-only", "data-type": "av-search"},
            parentElement: databaseElement,
        });
        const textElement = createElement({tagName: "SPAN", parentElement: searchElement});
        const nonEditableElement = createElement({
            attributes: {contenteditable: "false"},
            parentElement: searchElement,
        });

        assert.equal(canInput(searchElement), searchElement);
        assert.equal(canInput(textElement), searchElement);
        assert.equal(canInput(nonEditableElement), false);
        assert.equal(canInput(databaseElement), false);
    });

    it("accepts the editable Protyle root as the active element", () => {
        const bodyElement = createElement({tagName: "BODY"});
        const wysiwygElement = createElement({
            classNames: ["protyle-wysiwyg"],
            attributes: {
                contenteditable: "true",
                "data-readonly": "false",
            },
            parentElement: bodyElement,
        });

        assert.equal(canInput(wysiwygElement), wysiwygElement);
    });

    it("rejects a readonly Protyle root", () => {
        const bodyElement = createElement({tagName: "BODY"});
        const wysiwygElement = createElement({
            classNames: ["protyle-wysiwyg"],
            attributes: {
                contenteditable: "false",
                "data-readonly": "true",
            },
            parentElement: bodyElement,
        });

        assert.equal(canInput(wysiwygElement), false);
    });
});

describe("mobile app keyboard", () => {
    it("notifies listeners before requesting the Android keyboard", () => {
        const originalWindow = globalThis.window;
        const calls: string[] = [];
        const testWindow = new EventTarget() as Window & typeof globalThis;
        Object.assign(testWindow, {
            JSAndroid: {
                showKeyboard() {
                    calls.push("keyboard");
                },
            },
        });
        testWindow.addEventListener("siyuan-mobile-keyboard-change", ((event: CustomEvent<boolean>) => {
            assert.equal(event.detail, true);
            calls.push("change");
        }) as EventListener);
        Object.defineProperty(globalThis, "window", {
            configurable: true,
            value: testWindow,
        });

        try {
            callMobileAppShowKeyboard();
            assert.deepEqual(calls, ["change", "keyboard"]);
        } finally {
            Object.defineProperty(globalThis, "window", {
                configurable: true,
                value: originalWindow,
            });
        }
    });
});