mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-03 10:01:34 +00:00
* fix: bound misc unbounded fs.readFile calls; remove unused fs import
* fix: decode buffer to string before passing to string consumers
readRegularFile and readRegularFileSync return { buffer, stat },
not a string. All 4 new call sites passed the raw object to functions
expecting a string (JSON.parse, RegExp.test, template literals, etc.),
causing TS2345 type errors and runtime failures.
Fix each call by extracting .buffer and calling .toString('utf8')
before passing the result to string consumers.
* style: fix oxfmt formatting in config-set-input.ts
* fix: bound config and trajectory metadata reads
Co-authored-by: 陈宪彪0668000387 <chen.xianbiao@xydigit.com>
* refactor: isolate bounded read ownership
Co-authored-by: 陈宪彪0668000387 <chen.xianbiao@xydigit.com>
---------
Co-authored-by: Peter Steinberger <steipete@gmail.com>
129 lines
3.8 KiB
TypeScript
129 lines
3.8 KiB
TypeScript
// Config set input tests cover config value parsing from CLI input and files.
|
|
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 --batch-file when the file does not exist", () => {
|
|
expect(() =>
|
|
parseBatchSource({
|
|
batchFile: "/nonexistent/path/batch.json5",
|
|
}),
|
|
).toThrow("--batch-file not found: /nonexistent/path/batch.json5");
|
|
});
|
|
|
|
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.");
|
|
});
|
|
});
|
|
|
|
it("rejects --batch-file payloads above the config mutation limit", () => {
|
|
withBatchFile(
|
|
"openclaw-config-set-input-oversized-",
|
|
" ".repeat(8 * 1024 * 1024 + 1),
|
|
(batchPath) => {
|
|
expect(() => parseBatchSource({ batchFile: batchPath })).toThrow(
|
|
"File exceeds 8388608 bytes",
|
|
);
|
|
},
|
|
);
|
|
});
|
|
});
|