mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 00:31:37 +00:00
test(utils): share temp-dir helper across cli and web tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import * as fs from "node:fs/promises";
|
import * as fs from "node:fs/promises";
|
||||||
import * as os from "node:os";
|
|
||||||
import * as path from "node:path";
|
import * as path from "node:path";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { withTempDir } from "../test-utils/temp-dir.js";
|
||||||
import {
|
import {
|
||||||
cameraTempPath,
|
cameraTempPath,
|
||||||
parseCameraClipPayload,
|
parseCameraClipPayload,
|
||||||
@@ -12,15 +12,6 @@ import {
|
|||||||
} from "./nodes-camera.js";
|
} from "./nodes-camera.js";
|
||||||
import { parseScreenRecordPayload, screenRecordTempPath } from "./nodes-screen.js";
|
import { parseScreenRecordPayload, screenRecordTempPath } from "./nodes-screen.js";
|
||||||
|
|
||||||
async function withTempDir<T>(prefix: string, run: (dir: string) => Promise<T>): Promise<T> {
|
|
||||||
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<T>(run: (dir: string) => Promise<T>): Promise<T> {
|
async function withCameraTempDir<T>(run: (dir: string) => Promise<T>): Promise<T> {
|
||||||
return await withTempDir("openclaw-test-", run);
|
return await withTempDir("openclaw-test-", run);
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/test-utils/temp-dir.ts
Normal file
12
src/test-utils/temp-dir.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
export async function withTempDir<T>(prefix: string, run: (dir: string) => Promise<T>): Promise<T> {
|
||||||
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||||
|
try {
|
||||||
|
return await run(dir);
|
||||||
|
} finally {
|
||||||
|
await fs.rm(dir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { saveSessionStore } from "../../config/sessions.js";
|
import { saveSessionStore } from "../../config/sessions.js";
|
||||||
|
import { withTempDir } from "../../test-utils/temp-dir.js";
|
||||||
import {
|
import {
|
||||||
debugMention,
|
debugMention,
|
||||||
isBotMentionedFromTargets,
|
isBotMentionedFromTargets,
|
||||||
@@ -29,15 +29,6 @@ const makeMsg = (overrides: Partial<WebInboundMsg>): WebInboundMsg =>
|
|||||||
...overrides,
|
...overrides,
|
||||||
}) as WebInboundMsg;
|
}) as WebInboundMsg;
|
||||||
|
|
||||||
async function withTempDir<T>(prefix: string, run: (dir: string) => Promise<T>): Promise<T> {
|
|
||||||
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", () => {
|
describe("isBotMentionedFromTargets", () => {
|
||||||
const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] };
|
const mentionCfg = { mentionRegexes: [/\bopenclaw\b/i] };
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user