Skip to content

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

5.4 KB
Raw Blame
import * as assert from "node:assert/strict";
import {readFileSync} from "node:fs";
import {describe, it} from "node:test";
import {runInNewContext} from "node:vm";
import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
import {isTableLikeView} from "./viewType";
import {getAVCellEditorOwner} from "./cellEditor";

const source = readFileSync("src/protyle/render/av/cell.ts", "utf8");
const compiled = transpileModule(source.slice(source.indexOf("const updateCellValueByInput ="),
    source.indexOf("export const updateCellsValue =")), {
    compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
}).outputText;

const createEditor = (changed: boolean, viewType = "table") => {
    const saved: unknown[][] = [];
    let focusCount = 0;
    let removed = false;
    const input = {value: "updated value", dataset: {changed: changed ? "true" : undefined}};
    const mask = {
        querySelector: () => input,
        remove: () => {
            removed = true;
        },
    };
    const protyle = {};
    const block = {
        getAttribute: (name: string) => name === "data-av-type" ? viewType : "database-id",
    };
    const cells = [{dataset: {}, classList: {add: () => {}}}];
    const context = {
        isTableLikeView,
        document: {
            querySelector: (selector: string) => selector === ".av__mask" ? mask : null,
            querySelectorAll: () => [mask],
        },
        hasClosestByClassName: (_element: unknown, className: string) =>
            className === "av__row" ? {dataset: {id: "row-id"}} : null,
        updateCellsValue: (...args: unknown[]) => saved.push(args),
        addDragFill: () => {},
        focusBlock: () => focusCount++,
    };
    const submit = runInNewContext(`${compiled}\nupdateCellValueByInput;`, context);
    return {
        close: (restoreFocus = false) => submit(protyle, "block", block, cells, restoreFocus),
        saved,
        protyle,
        block,
        cells,
        isRemoved: () => removed,
        focusCount: () => focusCount,
    };
};

describe("database cell editor navigation", () => {
    it("owns ordinary Attributes dialogs, row panels and inline databases by their actual container", () => {
        for (const container of [{kind: "attributes-dialog"}, {kind: "row-panel"}, {kind: "custom-attributes"}, null]) {
            const block = {closest: (selector: string) => {
                assert.equal(selector, ".protyle-db-row, .b3-dialog__container, .custom-attr");
                return container;
            }} as unknown as HTMLElement;
            assert.equal(getAVCellEditorOwner(block), container || block);
        }
    });
    it("dispatches cancel-only lifecycle events for owned location dialogs on save and discard closes", () => {
        const api = {} as typeof import("./cellEditor");
        let closed = 0;
        const dialog = {
            dataset: {avLocationEditor: "true", avBlockId: "database-block"},
            dispatchEvent: (event: Event) => {
                assert.equal(event.type, "siyuan-av-cell-editor-close");
                closed++;
            },
            remove: () => assert.fail("the Dialog must clean up its own session and listeners"),
        };
        runInNewContext(transpileModule(readFileSync("src/protyle/render/av/cellEditor.ts", "utf8"), {
            compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
        }).outputText, {
            exports: api,
            CustomEvent,
            document: {querySelectorAll: (selector: string) => {
                assert.match(selector, /\[data-av-location-editor\]/);
                return [dialog];
            }},
        });
        const owner = {querySelectorAll: () => [{dataset: {nodeId: "database-block"}}]} as unknown as HTMLElement;
        api.closeAVCellEditor(owner, true);
        api.closeAVCellEditor(owner, false);
        api.closeAVCellEditor({querySelectorAll: (): HTMLElement[] => []} as unknown as HTMLElement);
        assert.equal(closed, 2);
    });
    it("submits list property edits using the row identity and restores focus", () => {
        const editor = createEditor(true, "list");
        editor.close(true);
        assert.deepEqual(editor.saved[0], [editor.protyle, editor.block, "updated value", editor.cells]);
        assert.equal(editor.focusCount(), 1);
        assert.equal(editor.isRemoved(), true);
    });
    it("submits edited content to the original cell without focusing the departing document", () => {
        const editor = createEditor(true);
        editor.close();
        assert.equal(editor.saved.length, 1);
        assert.deepEqual(editor.saved[0], [editor.protyle, editor.block, "updated value", editor.cells]);
        assert.equal(editor.isRemoved(), true);
        assert.equal(editor.focusCount(), 0);
    });

    it("closes unchanged content without submitting or restoring focus", () => {
        const editor = createEditor(false);
        editor.close();
        assert.equal(editor.saved.length, 0);
        assert.equal(editor.isRemoved(), true);
        assert.equal(editor.focusCount(), 0);
    });

    it("preserves focus restoration when finishing editing inside the document", () => {
        for (const changed of [false, true]) {
            const editor = createEditor(changed);
            editor.close(true);
            assert.equal(editor.focusCount(), 1);
            assert.equal(editor.isRemoved(), true);
        }
    });
});