File size: 2,449 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import {
deletePathStrict,
getPath,
setPathCreateStrict,
setPathExistingStrict,
} from "./path-utils.js";
function asConfig(value: unknown): OpenClawConfig {
return value as OpenClawConfig;
}
function createAgentListConfig(): OpenClawConfig {
return asConfig({
agents: {
list: [{ id: "a" }],
},
});
}
describe("secrets path utils", () => {
it("deletePathStrict compacts arrays via splice", () => {
const config = asConfig({});
setPathCreateStrict(config, ["agents", "list"], [{ id: "a" }, { id: "b" }, { id: "c" }]);
const changed = deletePathStrict(config, ["agents", "list", "1"]);
expect(changed).toBe(true);
expect(getPath(config, ["agents", "list"])).toEqual([{ id: "a" }, { id: "c" }]);
});
it("getPath returns undefined for invalid array path segment", () => {
const config = asConfig({
agents: {
list: [{ id: "a" }],
},
});
expect(getPath(config, ["agents", "list", "foo"])).toBeUndefined();
});
it("setPathExistingStrict throws when path does not already exist", () => {
const config = createAgentListConfig();
expect(() =>
setPathExistingStrict(
config,
["agents", "list", "0", "memorySearch", "remote", "apiKey"],
"x",
),
).toThrow(/Path segment does not exist/);
});
it("setPathExistingStrict updates an existing leaf", () => {
const config = asConfig({
talk: {
apiKey: "old", // pragma: allowlist secret
},
});
const changed = setPathExistingStrict(config, ["talk", "apiKey"], "new");
expect(changed).toBe(true);
expect(getPath(config, ["talk", "apiKey"])).toBe("new");
});
it("setPathCreateStrict creates missing container segments", () => {
const config = asConfig({});
const changed = setPathCreateStrict(config, ["talk", "provider", "apiKey"], "x");
expect(changed).toBe(true);
expect(getPath(config, ["talk", "provider", "apiKey"])).toBe("x");
});
it("setPathCreateStrict leaves value unchanged when equal", () => {
const config = asConfig({
talk: {
apiKey: "same", // pragma: allowlist secret
},
});
const changed = setPathCreateStrict(config, ["talk", "apiKey"], "same");
expect(changed).toBe(false);
expect(getPath(config, ["talk", "apiKey"])).toBe("same");
});
});
|