From 4f7032fbd9fa7049caea49ff2a0e220a5b2e9665 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 23:26:41 +0000 Subject: [PATCH] test(utils): share temp-dir helper across cli and web tests --- src/cli/nodes-camera.test.ts | 11 +---------- src/test-utils/temp-dir.ts | 12 ++++++++++++ src/web/auto-reply/web-auto-reply-utils.test.ts | 11 +---------- 3 files changed, 14 insertions(+), 20 deletions(-) create mode 100644 src/test-utils/temp-dir.ts diff --git a/src/cli/nodes-camera.test.ts b/src/cli/nodes-camera.test.ts index e6f11ff0e..bd78480fd 100644 --- a/src/cli/nodes-camera.test.ts +++ b/src/cli/nodes-camera.test.ts @@ -1,7 +1,7 @@ import * as fs from "node:fs/promises"; -import * as os from "node:os"; import * as path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { withTempDir } from "../test-utils/temp-dir.js"; import { cameraTempPath, parseCameraClipPayload, @@ -12,15 +12,6 @@ import { } from "./nodes-camera.js"; import { parseScreenRecordPayload, screenRecordTempPath } from "./nodes-screen.js"; -async function withTempDir(prefix: string, run: (dir: string) => Promise): Promise { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); - try { - return await run(dir); - } finally { - await fs.rm(dir, { recursive: true, force: true }); - } -} - async function withCameraTempDir(run: (dir: string) => Promise): Promise { return await withTempDir("openclaw-test-", run); } diff --git a/src/test-utils/temp-dir.ts b/src/test-utils/temp-dir.ts new file mode 100644 index 000000000..0efe486af --- /dev/null +++ b/src/test-utils/temp-dir.ts @@ -0,0 +1,12 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; + +export async function withTempDir(prefix: string, run: (dir: string) => Promise): Promise { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); + try { + return await run(dir); + } finally { + await fs.rm(dir, { recursive: true, force: true }); + } +} diff --git a/src/web/auto-reply/web-auto-reply-utils.test.ts b/src/web/auto-reply/web-auto-reply-utils.test.ts index ec4d67b59..bb7f27f3a 100644 --- a/src/web/auto-reply/web-auto-reply-utils.test.ts +++ b/src/web/auto-reply/web-auto-reply-utils.test.ts @@ -1,8 +1,8 @@ import fs from "node:fs/promises"; -import os from "node:os"; import path from "node:path"; import { describe, expect, it, vi } from "vitest"; import { saveSessionStore } from "../../config/sessions.js"; +import { withTempDir } from "../../test-utils/temp-dir.js"; import { debugMention, isBotMentionedFromTargets, @@ -29,15 +29,6 @@ const makeMsg = (overrides: Partial): WebInboundMsg => ...overrides, }) as WebInboundMsg; -async function withTempDir(prefix: string, run: (dir: string) => Promise): Promise { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); - try { - return await run(dir); - } finally { - await fs.rm(dir, { recursive: true, force: true }); - } -} - describe("isBotMentionedFromTargets", () => { const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] };