Files
openclaw-zero-token/cli/config-set-input.test.ts
sjhu 571e14a236 feat: upgrade to upstream v2026.3.28
Major upgrade from e26988a38 to upstream v2026.3.28 (f9b107928).
Key changes:
- Upstream src/, ui/, extensions/ (89 bundled extensions)
- Zero-token web providers preserved in src/zero-token/
- AskOnce plugin restored and registered as CLI command
- Added missing packages: @anthropic-ai/vertex-sdk, @modelcontextprotocol/sdk
- Fixed tsconfig rootDir, skipLibCheck for plugin-sdk DTS build
- Added askonce to bundled plugin metadata and package.json exports
- Fixed AskOnce CLI command registration (missing commands metadata)
- Restored AskOnce adapter imports (correct 5-level relative paths)
- Removed stale migration artifacts from root directory
2026-03-30 17:58:12 +08:00

108 lines
3.2 KiB
TypeScript

import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { parseBatchSource } from "./config-set-input.js";
function withBatchFile<T>(prefix: string, contents: string, run: (batchPath: string) => T): T {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
const batchPath = path.join(tempDir, "batch.json");
fs.writeFileSync(batchPath, contents, "utf8");
try {
return run(batchPath);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
describe("config set input parsing", () => {
it("returns null when no batch options are provided", () => {
expect(parseBatchSource({})).toBeNull();
});
it("rejects using both --batch-json and --batch-file", () => {
expect(() =>
parseBatchSource({
batchJson: "[]",
batchFile: "/tmp/batch.json",
}),
).toThrow("Use either --batch-json or --batch-file, not both.");
});
it("parses valid --batch-json payloads", () => {
const parsed = parseBatchSource({
batchJson:
'[{"path":"gateway.auth.mode","value":"token"},{"path":"channels.discord.token","ref":{"source":"env","provider":"default","id":"DISCORD_BOT_TOKEN"}},{"path":"secrets.providers.default","provider":{"source":"env"}}]',
});
expect(parsed).toEqual([
{
path: "gateway.auth.mode",
value: "token",
},
{
path: "channels.discord.token",
ref: {
source: "env",
provider: "default",
id: "DISCORD_BOT_TOKEN",
},
},
{
path: "secrets.providers.default",
provider: {
source: "env",
},
},
]);
});
it.each([
{ name: "malformed payload", batchJson: "{", message: "Failed to parse --batch-json:" },
{
name: "non-array payload",
batchJson: '{"path":"gateway.auth.mode","value":"token"}',
message: "--batch-json must be a JSON array.",
},
{
name: "entry without path",
batchJson: '[{"value":"token"}]',
message: "--batch-json[0].path is required.",
},
{
name: "entry with multiple mode keys",
batchJson: '[{"path":"gateway.auth.mode","value":"token","provider":{"source":"env"}}]',
message: "--batch-json[0] must include exactly one of: value, ref, provider.",
},
] as const)("rejects $name", ({ batchJson, message }) => {
expect(() => parseBatchSource({ batchJson })).toThrow(message);
});
it("parses valid --batch-file payloads", () => {
withBatchFile(
"openclaw-config-set-input-",
'[{"path":"gateway.auth.mode","value":"token"}]',
(batchPath) => {
const parsed = parseBatchSource({
batchFile: batchPath,
});
expect(parsed).toEqual([
{
path: "gateway.auth.mode",
value: "token",
},
]);
},
);
});
it("rejects malformed --batch-file payloads", () => {
withBatchFile("openclaw-config-set-input-invalid-", "{}", (batchPath) => {
expect(() =>
parseBatchSource({
batchFile: batchPath,
}),
).toThrow("--batch-file must be a JSON array.");
});
});
});