From 17eaa59a7a56b9ee10cbdbc2bbf4cba6a87128ad Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Mar 2026 00:44:12 +0000 Subject: [PATCH] test: tighten json file helper coverage --- src/infra/json-file.test.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/infra/json-file.test.ts b/src/infra/json-file.test.ts index 95def5fa54a..60dd0e3a237 100644 --- a/src/infra/json-file.test.ts +++ b/src/infra/json-file.test.ts @@ -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 }); + }); + }); });