Skip to content

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

2.6 KB
Raw Blame
import {strict as assert} from "node:assert";
import test from "node:test";
import type {Transaction} from "../../types/api";
import {getEditorTransaction} from "./transactionContract";
Object.assign(globalThis, {SIYUAN_VERSION: "test", NODE_ENV: "test"});

const {operationsMayChangeHeadingNumbers, operationsMayChangeOutline} = require("./headingNumberCore") as typeof import("./headingNumberCore");

test("block reference swaps retain affected roots and invalidate the outline", () => {
    const response: Transaction = JSON.parse(`{"doOperations":[{
        "action":"swapBlockRef","id":"ref","blockID":"def",
        "data":{"includeChildren":true,"originalToEmbed":true},"retData":["source","target"]
    }]}`);
    const operations = getEditorTransaction(response).doOperations;
    assert.equal(operations.length, 1);
    assert.deepEqual(operations[0].retData, ["source", "target"]);
    assert.equal(operationsMayChangeOutline(operations), true);
    assert.equal(operationsMayChangeHeadingNumbers(operations), true);
});

test("editor transaction selection preserves the original extension response", () => {
    const response: Transaction = JSON.parse(`{
        "timestamp": 123,
        "templateDocTreePlanID": "plan",
        "doOperations": [
            {"action": "update", "data": "content"},
            {"action": "plugin-custom", "data": {"extension": [1, null, true]}},
            {"action": "updateAttrs", "data": {"old": {}, "new": {"fold": "1"}}}
        ],
        "undoOperations": null
    }`);
    const selected = getEditorTransaction(response);
    assert.equal(selected.timestamp, 123);
    assert.equal(selected.templateDocTreePlanID, "plan");
    assert.deepEqual(selected.doOperations.map(operation => operation.action), ["update", "updateAttrs"]);
    assert.equal(selected.doOperations[0], response.doOperations[0]);
    assert.equal(response.doOperations.length, 3);
    assert.deepEqual(response.doOperations[1].data, {extension: [1, null, true]});
    assert.equal(selected.undoOperations, null);
});

test("authoritative list conversion invalidates heading numbers and the outline", () => {
    const response: Transaction = JSON.parse(`{"doOperations":[{
        "action":"convertList","id":"document","blockIDs":["list"],
        "data":{"type":"heading","level":5,"recursively":false},
        "retData":{"rootIDs":["document"],"removedIDs":["list"],"focusID":"paragraph"}
    }]}`);
    const operations = getEditorTransaction(response).doOperations;
    assert.equal(operations.length, 1);
    assert.equal(operationsMayChangeOutline(operations), true);
    assert.equal(operationsMayChangeHeadingNumbers(operations), true);
});