test: share json file symlink helper

This commit is contained in:
Peter Steinberger
2026-04-19 00:40:26 +01:00
parent 2172bf1cdd
commit 58a3527e17

View File

@@ -23,6 +23,31 @@ async function withJsonPath<T>(
);
}
async function withJsonSymlink<T>(
run: (params: {
root: string;
targetDir: string;
targetPath: string;
linkPath: string;
}) => Promise<T> | T,
): Promise<T> {
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);
});
},
);