mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 07:01:49 +00:00
test(plugin-sdk): reuse temp dir helpers in facade tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { clearRuntimeConfigSnapshot, setRuntimeConfigSnapshot } from "../config/config.js";
|
||||
@@ -13,15 +12,15 @@ import {
|
||||
resetFacadeRuntimeStateForTest,
|
||||
tryLoadActivatedBundledPluginPublicSurfaceModuleSync,
|
||||
} from "./facade-runtime.js";
|
||||
import { createPluginSdkTestHarness } from "./test-helpers.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
const { createTempDirSync } = createPluginSdkTestHarness();
|
||||
const originalBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
||||
const originalStateDir = process.env.OPENCLAW_STATE_DIR;
|
||||
const FACADE_RUNTIME_GLOBAL = "__openclawTestLoadBundledPluginPublicSurfaceModuleSync";
|
||||
|
||||
function createBundledPluginDir(prefix: string, marker: string): string {
|
||||
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(rootDir);
|
||||
const rootDir = createTempDirSync(prefix);
|
||||
fs.mkdirSync(path.join(rootDir, "demo"), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(rootDir, "demo", "api.js"),
|
||||
@@ -32,8 +31,7 @@ function createBundledPluginDir(prefix: string, marker: string): string {
|
||||
}
|
||||
|
||||
function createThrowingPluginDir(prefix: string): string {
|
||||
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(rootDir);
|
||||
const rootDir = createTempDirSync(prefix);
|
||||
fs.mkdirSync(path.join(rootDir, "bad"), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(rootDir, "bad", "api.js"),
|
||||
@@ -44,8 +42,7 @@ function createThrowingPluginDir(prefix: string): string {
|
||||
}
|
||||
|
||||
function createCircularPluginDir(prefix: string): string {
|
||||
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(rootDir);
|
||||
const rootDir = createTempDirSync(prefix);
|
||||
fs.mkdirSync(path.join(rootDir, "demo"), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(rootDir, "facade.mjs"),
|
||||
@@ -92,9 +89,6 @@ afterEach(() => {
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = originalStateDir;
|
||||
}
|
||||
for (const dir of tempDirs.splice(0, tempDirs.length)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("plugin-sdk facade runtime", () => {
|
||||
@@ -267,11 +261,9 @@ describe("plugin-sdk facade runtime", () => {
|
||||
});
|
||||
|
||||
it("resolves a globally-installed plugin whose rootDir basename matches the dirName", () => {
|
||||
const emptyBundled = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-facade-empty-bundled-"));
|
||||
tempDirs.push(emptyBundled);
|
||||
const emptyBundled = createTempDirSync("openclaw-facade-empty-bundled-");
|
||||
|
||||
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-facade-state-"));
|
||||
tempDirs.push(stateDir);
|
||||
const stateDir = createTempDirSync("openclaw-facade-state-");
|
||||
const lineDir = path.join(stateDir, "extensions", "line");
|
||||
fs.mkdirSync(lineDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
@@ -325,11 +317,9 @@ describe("plugin-sdk facade runtime", () => {
|
||||
});
|
||||
|
||||
it("resolves a globally-installed plugin with an encoded scoped rootDir basename", () => {
|
||||
const emptyBundled = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-facade-empty-bundled-"));
|
||||
tempDirs.push(emptyBundled);
|
||||
const emptyBundled = createTempDirSync("openclaw-facade-empty-bundled-");
|
||||
|
||||
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-facade-state-"));
|
||||
tempDirs.push(stateDir);
|
||||
const stateDir = createTempDirSync("openclaw-facade-state-");
|
||||
const encodedDir = path.join(stateDir, "extensions", "@openclaw+line");
|
||||
fs.mkdirSync(encodedDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
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 { createPersistentDedupe } from "./persistent-dedupe.js";
|
||||
import { createPluginSdkTestHarness } from "./test-helpers.js";
|
||||
|
||||
const tmpRoots: string[] = [];
|
||||
|
||||
async function makeTmpRoot(): Promise<string> {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-dedupe-"));
|
||||
tmpRoots.push(root);
|
||||
return root;
|
||||
}
|
||||
const { createTempDir } = createPluginSdkTestHarness();
|
||||
|
||||
function createDedupe(root: string, overrides?: { ttlMs?: number }) {
|
||||
return createPersistentDedupe({
|
||||
@@ -21,15 +14,9 @@ function createDedupe(root: string, overrides?: { ttlMs?: number }) {
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
tmpRoots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("createPersistentDedupe", () => {
|
||||
it("deduplicates keys and persists across instances", async () => {
|
||||
const root = await makeTmpRoot();
|
||||
const root = await createTempDir("openclaw-dedupe-");
|
||||
const first = createDedupe(root);
|
||||
expect(await first.checkAndRecord("m1", { namespace: "a" })).toBe(true);
|
||||
expect(await first.checkAndRecord("m1", { namespace: "a" })).toBe(false);
|
||||
@@ -40,7 +27,7 @@ describe("createPersistentDedupe", () => {
|
||||
});
|
||||
|
||||
it("guards concurrent calls for the same key", async () => {
|
||||
const root = await makeTmpRoot();
|
||||
const root = await createTempDir("openclaw-dedupe-");
|
||||
const dedupe = createDedupe(root, { ttlMs: 10_000 });
|
||||
|
||||
const [first, second] = await Promise.all([
|
||||
@@ -64,7 +51,7 @@ describe("createPersistentDedupe", () => {
|
||||
});
|
||||
|
||||
it("warmup loads persisted entries into memory", async () => {
|
||||
const root = await makeTmpRoot();
|
||||
const root = await createTempDir("openclaw-dedupe-");
|
||||
const writer = createDedupe(root);
|
||||
expect(await writer.checkAndRecord("msg-1", { namespace: "acct" })).toBe(true);
|
||||
expect(await writer.checkAndRecord("msg-2", { namespace: "acct" })).toBe(true);
|
||||
@@ -104,7 +91,7 @@ describe("createPersistentDedupe", () => {
|
||||
},
|
||||
},
|
||||
])("warmup $name", async ({ setup, namespace, expectedLoaded, verify }) => {
|
||||
const root = await makeTmpRoot();
|
||||
const root = await createTempDir("openclaw-dedupe-");
|
||||
const reader = await setup(root);
|
||||
const loaded = await reader.warmup(namespace);
|
||||
expect(loaded).toBe(expectedLoaded);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { mkdtemp, rm, type RmOptions } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
@@ -26,7 +27,14 @@ export function createPluginSdkTestHarness(options?: { cleanup?: RmOptions }) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
function createTempDirSync(prefix: string): string {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), prefix));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
return {
|
||||
createTempDir,
|
||||
createTempDirSync,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user