Skip to content

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

3.0 KB
Raw Blame
import * as assert from "node:assert/strict";
import test from "node:test";
import {createNamespacePatchQueue} from "./namespacePatchQueue";

const nextTask = () => new Promise<void>((resolve) => setTimeout(resolve, 0));

test("field submission sends only changed paths in order, without submitting stale local configuration", async () => {
    const submitted: Array<[string, unknown]> = [];
    const patch = createNamespacePatchQueue({
        namespace: "editor", getConfig: () => ({fontSize: 16, fullWidth: false}),
        submit: async () => { throw new Error("full configuration must not be submitted"); },
        submitPatch: async (path, value) => {
            submitted.push([path, value]);
            return {fontSize: 20, fullWidth: true};
        },
    });
    await Promise.all([patch("editor.fontSize", 20), patch("fullWidth", true)]);
    assert.deepEqual(submitted, [["fontSize", 20], ["fullWidth", true]]);
});

test("namespace patches are serialized against the latest server response", async () => {
    type TestConfig = {
        approval: {
            capability: string;
            action: string;
        };
    };
    let config: TestConfig = {approval: {capability: "risk", action: "risk"}};
    const submitted: TestConfig[] = [];
    const releases: Array<() => void> = [];
    const applied: string[] = [];
    const patch = createNamespacePatchQueue<TestConfig>({
        namespace: "ai",
        getConfig: () => config,
        submit: (payload) => new Promise((resolve) => {
            submitted.push(payload);
            releases.push(() => {
                config = payload;
                resolve(payload);
            });
        }),
    });

    patch("ai.approval.capability", "confirm", () => applied.push("capability"));
    patch("approval.action", "allow", () => applied.push("action"));
    await nextTask();

    assert.equal(submitted.length, 1);
    assert.deepEqual(submitted[0], {approval: {capability: "confirm", action: "risk"}});
    releases[0]();
    await nextTask();

    assert.equal(submitted.length, 2);
    assert.deepEqual(submitted[1], {approval: {capability: "confirm", action: "allow"}});
    releases[1]();
    await nextTask();

    assert.deepEqual(config, {approval: {capability: "confirm", action: "allow"}});
    assert.deepEqual(applied, ["capability", "action"]);
});

test("namespace patch queue continues after a failed submission", async () => {
    let config = {first: 0, second: 0};
    let attempts = 0;
    const patch = createNamespacePatchQueue<typeof config>({
        namespace: "settings",
        getConfig: () => config,
        submit: async (payload) => {
            attempts++;
            if (attempts === 1) {
                return undefined;
            }
            config = payload;
            return payload;
        },
    });

    patch("first", 1);
    patch("second", 2);
    await nextTask();
    await nextTask();

    assert.equal(attempts, 2);
    assert.deepEqual(config, {first: 0, second: 2});
});