test(config): dedupe temp roots and cover legacy state-dir fallback

This commit is contained in:
Peter Steinberger
2026-02-21 19:32:06 +00:00
parent 5d61afb362
commit 7036352d94

View File

@@ -37,6 +37,15 @@ describe("oauth paths", () => {
});
describe("state + config path candidates", () => {
async function withTempRoot(prefix: string, run: (root: string) => Promise<void>): Promise<void> {
const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
try {
await run(root);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
}
function expectOpenClawHomeDefaults(env: NodeJS.ProcessEnv): void {
const configuredHome = env.OPENCLAW_HOME;
if (!configuredHome) {
@@ -98,20 +107,25 @@ describe("state + config path candidates", () => {
});
it("prefers ~/.openclaw when it exists and legacy dir is missing", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-state-"));
try {
await withTempRoot("openclaw-state-", async (root) => {
const newDir = path.join(root, ".openclaw");
await fs.mkdir(newDir, { recursive: true });
const resolved = resolveStateDir({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(newDir);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
});
it("falls back to existing legacy state dir when ~/.openclaw is missing", async () => {
await withTempRoot("openclaw-state-legacy-", async (root) => {
const legacyDir = path.join(root, ".clawdbot");
await fs.mkdir(legacyDir, { recursive: true });
const resolved = resolveStateDir({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(legacyDir);
});
});
it("CONFIG_PATH prefers existing config when present", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-"));
try {
await withTempRoot("openclaw-config-", async (root) => {
const legacyDir = path.join(root, ".openclaw");
await fs.mkdir(legacyDir, { recursive: true });
const legacyPath = path.join(legacyDir, "openclaw.json");
@@ -119,14 +133,11 @@ describe("state + config path candidates", () => {
const resolved = resolveConfigPathCandidate({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(legacyPath);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
});
it("respects state dir overrides when config is missing", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-override-"));
try {
await withTempRoot("openclaw-config-override-", async (root) => {
const legacyDir = path.join(root, ".openclaw");
await fs.mkdir(legacyDir, { recursive: true });
const legacyConfig = path.join(legacyDir, "openclaw.json");
@@ -136,8 +147,6 @@ describe("state + config path candidates", () => {
const env = { OPENCLAW_STATE_DIR: overrideDir } as NodeJS.ProcessEnv;
const resolved = resolveConfigPath(env, overrideDir, () => root);
expect(resolved).toBe(path.join(overrideDir, "openclaw.json"));
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
});
});