Skip to content

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

4.3 KB
Raw Blame
import * as assert from "node:assert/strict";
import {test} from "node:test";
import {ScriptTarget} from "typescript";
import {loadModule} from "../../testSupport/loadModule";
import {queueTransaction, queueTransactionBatch, waitForPendingTransactions} from "../util/transactionQueue";
import {getEditorTransaction} from "../util/transactionContract";

const fixture = () => {
    const rendered: Element[] = [];
    const carriers = [
        {isConnected: true, closest: (): Element => null, getAttribute: (name: string) => name === "data-av-type" ? "table" : "NodeAttributeView"},
        {isConnected: true, closest: (): Element => null, getAttribute: (name: string) => name === "data-av-type" ? "table" : "NodeAttributeView"},
    ];
    let finish: (success: boolean) => void;
    const dependencies = {
        getEditorTransaction,
        Constants: {},
        queueTransaction,
        queueTransactionBatch,
        prepareViewFoldTransaction: (_protyle: IProtyle, doOperations: IOperation[], undoOperations: IOperation[]) =>
            ({doOperations, undoOperations}),
        cleanBlockSelectionModeHTML: (html: string) => html,
        cleanTableCellRichHTML: (html: string) => html,
        cleanTableVirtualizationHTML: (html: string) => html,
        cleanListMindmapHTML: (html: string) => html,
        restoreInlineElementBoundaryHTML: (html: string) => html,
        normalizeHTMLAssetIFrameBlockDOM: (html: string) => html,
        cleanHeadingNumberOperations: (): void => undefined,
        needSubscribe: () => true,
        isInEmbedBlock: (): void => undefined,
        syncTrackedRanges: (): void => undefined,
        avRender: (element: Element) => rendered.push(element),
        invalidateViewFoldRequests: (): void => undefined,
        getBlockSelectionStatusIDs: (): string[] => [],
        countBlockWord: (): void => undefined,
        handleViewFoldSourceOperation: () => false,
        queueHeadingNumberRefresh: (): void => undefined,
        refreshHeadingFoldIndicators: (): void => undefined,
        applyViewFoldStates: (): void => undefined,
        fetchPost: (_url: string, data: {transactions: unknown[]}, callback: (response: unknown) => void) =>
            new Promise<void>(resolve => {
                finish = success => {
                    if (success) {
                        callback({data: data.transactions});
                    }
                    resolve();
                };
            }),
    };
    const exports = loadModule<any>("src/protyle/wysiwyg/transaction.ts", {
        require: () => dependencies,
        window: {siyuan: {config: {sync: {provider: 0}}}},
        getSelection: () => ({rangeCount: 0}),
    }, ScriptTarget.ES2021);
    const element = {
        childElementCount: 1,
        querySelectorAll: (selector: string) => selector === '[data-node-id="carrier"]' ?
            carriers.filter(item => item.isConnected) : [],
        querySelector: () => carriers[0],
    };
    const protyle = {wysiwyg: {element}, options: {}} as unknown as IProtyle;
    const insert = () => exports.transaction(protyle, [{
        action: "insert", id: "carrier", previousID: "paragraph", data: '<div data-type="NodeAttributeView"></div>',
    }]);
    return {protyle, carriers, rendered, insert, finish: (success: boolean) => finish(success)};
};

test("本地模板数据库及其副本在插入事务成功后才渲染", async () => {
    const f = fixture();
    f.insert();
    assert.equal(f.rendered.length, 0);
    await new Promise(resolve => setImmediate(resolve));
    assert.equal(f.rendered.length, 0);
    f.finish(true);
    await waitForPendingTransactions(f.protyle);
    assert.deepEqual(f.rendered, f.carriers);
});

test("插入事务失败时不渲染尚未保存的数据库", async () => {
    const f = fixture();
    f.insert();
    await new Promise(resolve => setImmediate(resolve));
    f.finish(false);
    await waitForPendingTransactions(f.protyle);
    assert.equal(f.rendered.length, 0);
});

test("等待插入事务期间移除的数据库副本不再渲染", async () => {
    const f = fixture();
    f.insert();
    await new Promise(resolve => setImmediate(resolve));
    f.carriers[0].isConnected = false;
    f.finish(true);
    await waitForPendingTransactions(f.protyle);
    assert.deepEqual(f.rendered, [f.carriers[1]]);
});