fix: block invalid config startup

Co-authored-by: Muhammed Mukhthar CM <mukhtharcm@gmail.com>
This commit is contained in:
Peter Steinberger
2026-01-17 10:25:24 +00:00
parent ad360b4d18
commit f8e673cdbc
7 changed files with 164 additions and 17 deletions

View File

@@ -0,0 +1,36 @@
import fs from "node:fs/promises";
import { describe, expect, it } from "vitest";
import { withTempHome } from "./test-helpers.js";
import type { ClawdbotConfig } from "./types.js";
describe("config backup rotation", () => {
it("keeps a 5-deep backup ring for config writes", async () => {
await withTempHome(async () => {
const { resolveConfigPath, writeConfigFile } = await import("./config.js");
const configPath = resolveConfigPath();
const buildConfig = (version: number): ClawdbotConfig =>
({
identity: { name: `v${version}` },
}) as ClawdbotConfig;
for (let version = 0; version <= 6; version += 1) {
await writeConfigFile(buildConfig(version));
}
const readName = async (suffix = "") => {
const raw = await fs.readFile(`${configPath}${suffix}`, "utf-8");
return (JSON.parse(raw) as { identity?: { name?: string } }).identity?.name ?? null;
};
await expect(readName()).resolves.toBe("v6");
await expect(readName(".bak")).resolves.toBe("v5");
await expect(readName(".bak.1")).resolves.toBe("v4");
await expect(readName(".bak.2")).resolves.toBe("v3");
await expect(readName(".bak.3")).resolves.toBe("v2");
await expect(readName(".bak.4")).resolves.toBe("v1");
await expect(fs.stat(`${configPath}.bak.5`)).rejects.toThrow();
});
});
});