test: tighten json file helper coverage

This commit is contained in:
Peter Steinberger
2026-03-14 00:44:12 +00:00
parent 958a2f31da
commit 17eaa59a7a

View File

@@ -15,6 +15,15 @@ describe("json-file helpers", () => {
});
});
it("returns undefined when the target path is a directory", async () => {
await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
const pathname = path.join(root, "config-dir");
fs.mkdirSync(pathname);
expect(loadJsonFile(pathname)).toBeUndefined();
});
});
it("creates parent dirs, writes a trailing newline, and loads the saved object", async () => {
await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
const pathname = path.join(root, "nested", "config.json");
@@ -30,4 +39,15 @@ describe("json-file helpers", () => {
expect(dirMode).toBe(0o700);
});
});
it("overwrites existing JSON files with the latest payload", async () => {
await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
const pathname = path.join(root, "config.json");
fs.writeFileSync(pathname, '{"enabled":false}\n', "utf8");
saveJsonFile(pathname, { enabled: true, count: 2 });
expect(loadJsonFile(pathname)).toEqual({ enabled: true, count: 2 });
});
});
});