test(core): reuse shared temp dir helpers in utils tests

This commit is contained in:
Vincent Koc
2026-04-06 06:24:01 +01:00
parent 691aa7e052
commit 908a96e242

View File

@@ -1,7 +1,7 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { withTempDir, withTempDirSync } from "./test-helpers/temp-dir.js";
import {
ensureDir,
resolveConfigDir,
@@ -12,21 +12,9 @@ import {
sleep,
} from "./utils.js";
async function withTempDir<T>(
prefix: string,
run: (dir: string) => T | Promise<T>,
): Promise<Awaited<T>> {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
try {
return await run(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
describe("ensureDir", () => {
it("creates nested directory", async () => {
await withTempDir("openclaw-test-", async (tmp) => {
await withTempDir({ prefix: "openclaw-test-" }, async (tmp) => {
const target = path.join(tmp, "nested", "dir");
await ensureDir(target);
expect(fs.existsSync(target)).toBe(true);
@@ -46,15 +34,12 @@ describe("sleep", () => {
describe("resolveConfigDir", () => {
it("prefers ~/.openclaw when legacy dir is missing", async () => {
const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-config-dir-"));
try {
await withTempDirSync({ prefix: "openclaw-config-dir-" }, async (root) => {
const newDir = path.join(root, ".openclaw");
await fs.promises.mkdir(newDir, { recursive: true });
const resolved = resolveConfigDir({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(newDir);
} finally {
await fs.promises.rm(root, { recursive: true, force: true });
}
});
});
it("expands OPENCLAW_STATE_DIR using the provided env", () => {