From 58a3527e17bf56dd94d82e647bb4c8a02fded75d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 19 Apr 2026 00:40:26 +0100 Subject: [PATCH] test: share json file symlink helper --- src/infra/json-file.test.ts | 43 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/infra/json-file.test.ts b/src/infra/json-file.test.ts index d26fd94a2e9..7d2de812987 100644 --- a/src/infra/json-file.test.ts +++ b/src/infra/json-file.test.ts @@ -23,6 +23,31 @@ async function withJsonPath( ); } +async function withJsonSymlink( + run: (params: { + root: string; + targetDir: string; + targetPath: string; + linkPath: string; + }) => Promise | T, +): Promise { + return withTempDir({ prefix: "openclaw-json-file-" }, async (root) => { + const targetDir = path.join(root, "target"); + return run({ + root, + targetDir, + targetPath: path.join(targetDir, "config.json"), + linkPath: path.join(root, "config-link.json"), + }); + }); +} + +function expectSavedPayloadThroughSymlink(linkPath: string, targetPath: string) { + expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true); + expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD); + expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD); +} + describe("json-file helpers", () => { afterEach(() => { vi.restoreAllMocks(); @@ -106,19 +131,14 @@ describe("json-file helpers", () => { it.runIf(process.platform !== "win32")( "preserves symlink destinations when replacing existing JSON files", async () => { - await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => { - const targetDir = path.join(root, "target"); - const targetPath = path.join(targetDir, "config.json"); - const linkPath = path.join(root, "config-link.json"); + await withJsonSymlink(({ targetDir, targetPath, linkPath }) => { fs.mkdirSync(targetDir, { recursive: true }); writeExistingJson(targetPath); fs.symlinkSync(targetPath, linkPath); saveJsonFile(linkPath, SAVED_PAYLOAD); - expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true); - expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD); - expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD); + expectSavedPayloadThroughSymlink(linkPath, targetPath); }); }, ); @@ -126,18 +146,13 @@ describe("json-file helpers", () => { it.runIf(process.platform !== "win32")( "creates a missing target file through an existing symlink", async () => { - await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => { - const targetDir = path.join(root, "target"); - const targetPath = path.join(targetDir, "config.json"); - const linkPath = path.join(root, "config-link.json"); + await withJsonSymlink(({ targetDir, targetPath, linkPath }) => { fs.mkdirSync(targetDir, { recursive: true }); fs.symlinkSync(targetPath, linkPath); saveJsonFile(linkPath, SAVED_PAYLOAD); - expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true); - expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD); - expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD); + expectSavedPayloadThroughSymlink(linkPath, targetPath); }); }, );