mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 21:21:10 +00:00
test(plugin-sdk): share temp dir test helper
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { hasAnthropicVertexAvailableAuth } from "./anthropic-vertex-auth-presence.js";
|
||||
import { createPluginSdkTestHarness } from "./test-helpers.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
tempDirs.splice(0, tempDirs.length).map((dir) => fs.rm(dir, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
const { createTempDir } = createPluginSdkTestHarness();
|
||||
|
||||
describe("hasAnthropicVertexAvailableAuth", () => {
|
||||
it("preserves unicode GOOGLE_APPLICATION_CREDENTIALS paths", async () => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-vertex-auth-"));
|
||||
tempDirs.push(root);
|
||||
const root = await createTempDir("openclaw-vertex-auth-");
|
||||
const unicodeDir = path.join(root, "認証情報");
|
||||
await fs.mkdir(unicodeDir, { recursive: true });
|
||||
const credentialsPath = path.join(unicodeDir, "application_default_credentials.json");
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
appendMemoryHostEvent,
|
||||
readMemoryHostEvents,
|
||||
resolveMemoryHostEventLogPath,
|
||||
} from "./memory-host-events.js";
|
||||
import { createPluginSdkTestHarness } from "./test-helpers.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
const { createTempDir } = createPluginSdkTestHarness();
|
||||
|
||||
describe("memory host event journal helpers", () => {
|
||||
it("appends and reads typed workspace events", async () => {
|
||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-host-events-"));
|
||||
tempDirs.push(workspaceDir);
|
||||
const workspaceDir = await createTempDir("memory-host-events-");
|
||||
|
||||
await appendMemoryHostEvent(workspaceDir, {
|
||||
type: "memory.recall.recorded",
|
||||
|
||||
32
src/plugin-sdk/test-helpers.ts
Normal file
32
src/plugin-sdk/test-helpers.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { mkdtemp, rm, type RmOptions } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach } from "vitest";
|
||||
|
||||
export function createPluginSdkTestHarness(options?: { cleanup?: RmOptions }) {
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
while (tempDirs.length > 0) {
|
||||
const dir = tempDirs.pop();
|
||||
if (!dir) {
|
||||
continue;
|
||||
}
|
||||
await rm(dir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
...options?.cleanup,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
async function createTempDir(prefix: string): Promise<string> {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), prefix));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
return {
|
||||
createTempDir,
|
||||
};
|
||||
}
|
||||
@@ -1,35 +1,19 @@
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createPluginSdkTestHarness } from "./test-helpers.js";
|
||||
import { materializeWindowsSpawnProgram, resolveWindowsSpawnProgram } from "./windows-spawn.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function createTempDir(): Promise<string> {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "openclaw-windows-spawn-test-"));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
while (tempDirs.length > 0) {
|
||||
const dir = tempDirs.pop();
|
||||
if (!dir) {
|
||||
continue;
|
||||
}
|
||||
await rm(dir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 8,
|
||||
retryDelay: 8,
|
||||
});
|
||||
}
|
||||
const { createTempDir } = createPluginSdkTestHarness({
|
||||
cleanup: {
|
||||
maxRetries: 8,
|
||||
retryDelay: 8,
|
||||
},
|
||||
});
|
||||
|
||||
describe("resolveWindowsSpawnProgram", () => {
|
||||
it("fails closed by default for unresolved windows wrappers", async () => {
|
||||
const dir = await createTempDir();
|
||||
const dir = await createTempDir("openclaw-windows-spawn-test-");
|
||||
const shimPath = path.join(dir, "wrapper.cmd");
|
||||
await writeFile(shimPath, "@ECHO off\r\necho wrapper\r\n", "utf8");
|
||||
|
||||
@@ -44,7 +28,7 @@ describe("resolveWindowsSpawnProgram", () => {
|
||||
});
|
||||
|
||||
it("only returns shell fallback when explicitly opted in", async () => {
|
||||
const dir = await createTempDir();
|
||||
const dir = await createTempDir("openclaw-windows-spawn-test-");
|
||||
const shimPath = path.join(dir, "wrapper.cmd");
|
||||
await writeFile(shimPath, "@ECHO off\r\necho wrapper\r\n", "utf8");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user