Skip to content

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

4.1 KB
Raw Blame
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";

const buildGlobals = ["SIYUAN_VERSION", "NODE_ENV"].map(name => ({
    name, descriptor: Object.getOwnPropertyDescriptor(globalThis, name),
}));
buildGlobals.forEach(({name}) => Object.defineProperty(globalThis, name, {value: "test", configurable: true}));
const {isContainerGutterBridge} = require("./container") as typeof import("./container");
buildGlobals.forEach(({name, descriptor}) => {
    if (descriptor) {
        Object.defineProperty(globalThis, name, descriptor);
    } else {
        Reflect.deleteProperty(globalThis, name);
    }
});

const setup = (type = "NodeBlockquote") => {
    const block = {
        getBoundingClientRect: () => ({left: 140, top: 100, bottom: 150}),
    } as Element;
    const container = {
        dataset: {type},
        contains: (element: Element) => element === block,
    } as unknown as HTMLElement;
    const button = {dataset: {type: "NodeParagraph"}} as unknown as HTMLElement;
    const buttons = [button];
    const gutter = {
        classList: {contains: () => false},
        getBoundingClientRect: () => ({left: 100, top: 110, bottom: 134}),
        querySelectorAll: () => buttons,
    } as unknown as HTMLElement;
    const target = {closest: (): HTMLElement => null} as unknown as HTMLElement;
    const hit = (x = 130, y = 120, currentBlock = block) =>
        isContainerGutterBridge(gutter, container, target, x, y, () => currentBlock);
    return {block, container, button, buttons, gutter, target, hit};
};

describe("isContainerGutterBridge", () => {
    it("preserves a child gutter while crossing nested container padding", () => {
        ["NodeBlockquote", "NodeCallout", "NodeSuperBlock", "NodeList", "NodeListItem"].forEach(type => {
            assert.equal(setup(type).hit(), true);
        });
    });

    it("preserves embedded content gutters across embed and editor padding", () => {
        const {container, buttons, gutter, target, block} = setup("NodeBlockQueryEmbed");
        const embedButton = {dataset: {type: "NodeBlockQueryEmbed"}} as unknown as HTMLElement;
        buttons.unshift(embedButton);
        const getBlock = (item: HTMLElement) => item === embedButton ? container : block;
        [139, 130, 110, 100].forEach(x => {
            assert.equal(isContainerGutterBridge(gutter, container, target, x, 120, getBlock), true);
        });
        [[99, 120], [141, 120], [130, 90], [130, 151]].forEach(([x, y]) => {
            assert.equal(isContainerGutterBridge(gutter, container, target, x, y, getBlock), false);
        });
        assert.equal(isContainerGutterBridge(gutter, container, target, 130, 120, () => container), false);
        assert.equal(isContainerGutterBridge(gutter, container, target, 130, 120, () => ({} as Element)), false);
    });

    it("does not retain a gutter outside the path to its block", () => {
        const {hit} = setup();
        [[99, 120], [141, 120], [130, 99], [130, 151]].forEach(([x, y]) => {
            assert.equal(hit(x, y), false);
        });
    });

    it("allows switching to unrelated blocks and the container itself", () => {
        const {hit, container} = setup();
        assert.equal(hit(130, 120, {} as Element), false);
        assert.equal(hit(130, 120, container), false);
        assert.equal(hit(130, 120, null), false);
        assert.equal(setup("NodeParagraph").hit(), false);
    });

    it("does not restore hidden gutters or use folding controls as anchors", () => {
        const hidden = setup();
        hidden.gutter.classList.contains = () => true;
        assert.equal(hidden.hit(), false);
        const fold = setup();
        fold.button.dataset.type = "fold";
        assert.equal(fold.hit(), false);
        const row = setup("NodeListItem");
        row.button.dataset.rowId = "row";
        assert.equal(row.hit(), false, "database row actions must not become the list marker's block actions");
    });

    it("keeps callout titles directly accessible", () => {
        const {target, hit} = setup("NodeCallout");
        target.closest = () => ({} as HTMLElement);
        assert.equal(hit(), false);
    });
});