From 9d8690d6b30853d6ba8bf46ea8a5aebbb7b915a2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 23 Apr 2026 18:38:26 +0100 Subject: [PATCH] test: share temp plugin extension fixtures --- .../codex-app-server.extensions.test.ts | 58 +++--------------- .../pi-embedded-runner.extensions.test.ts | 58 +++--------------- .../temp-plugin-extension-fixtures.ts | 61 +++++++++++++++++++ 3 files changed, 81 insertions(+), 96 deletions(-) create mode 100644 src/agents/test-helpers/temp-plugin-extension-fixtures.ts diff --git a/src/agents/codex-app-server.extensions.test.ts b/src/agents/codex-app-server.extensions.test.ts index 9ab9cb15fa4..fe6af762163 100644 --- a/src/agents/codex-app-server.extensions.test.ts +++ b/src/agents/codex-app-server.extensions.test.ts @@ -1,61 +1,23 @@ -import fs from "node:fs"; -import os from "node:os"; -import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { createCodexAppServerToolResultExtensionRunner } from "../plugin-sdk/agent-harness.js"; import { listCodexAppServerExtensionFactories } from "../plugins/codex-app-server-extension-factory.js"; -import { clearPluginLoaderCache, loadOpenClawPlugins } from "../plugins/loader.js"; -import { createEmptyPluginRegistry } from "../plugins/registry.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; +import { loadOpenClawPlugins } from "../plugins/loader.js"; +import { + cleanupTempPluginTestEnvironment, + createTempPluginDir, + resetActivePluginRegistryForTest, + writeTempPlugin, +} from "./test-helpers/temp-plugin-extension-fixtures.js"; -const EMPTY_PLUGIN_SCHEMA = { type: "object", additionalProperties: false, properties: {} }; const originalBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; const tempDirs: string[] = []; function createTempDir(): string { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-codex-ext-")); - tempDirs.push(dir); - return dir; -} - -function writeTempPlugin(params: { - dir: string; - id: string; - body: string; - manifest?: Record; - filename?: string; -}): string { - const pluginDir = path.join(params.dir, params.id); - fs.mkdirSync(pluginDir, { recursive: true }); - const file = path.join(pluginDir, params.filename ?? `${params.id}.mjs`); - fs.writeFileSync(file, params.body, "utf-8"); - fs.writeFileSync( - path.join(pluginDir, "openclaw.plugin.json"), - JSON.stringify( - { - id: params.id, - ...params.manifest, - configSchema: EMPTY_PLUGIN_SCHEMA, - }, - null, - 2, - ), - "utf-8", - ); - return file; + return createTempPluginDir(tempDirs, "openclaw-codex-ext-"); } afterEach(() => { - for (const dir of tempDirs.splice(0)) { - fs.rmSync(dir, { recursive: true, force: true }); - } - clearPluginLoaderCache(); - setActivePluginRegistry(createEmptyPluginRegistry()); - if (originalBundledPluginsDir === undefined) { - delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; - } else { - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = originalBundledPluginsDir; - } + cleanupTempPluginTestEnvironment(tempDirs, originalBundledPluginsDir); }); describe("Codex app-server extension factories", () => { @@ -96,7 +58,7 @@ describe("Codex app-server extension factories", () => { loadOpenClawPlugins(options); expect(listCodexAppServerExtensionFactories()).toHaveLength(1); - setActivePluginRegistry(createEmptyPluginRegistry()); + resetActivePluginRegistryForTest(); expect(listCodexAppServerExtensionFactories()).toHaveLength(0); loadOpenClawPlugins(options); diff --git a/src/agents/pi-embedded-runner.extensions.test.ts b/src/agents/pi-embedded-runner.extensions.test.ts index d0ed99175d3..710e3cbd3b1 100644 --- a/src/agents/pi-embedded-runner.extensions.test.ts +++ b/src/agents/pi-embedded-runner.extensions.test.ts @@ -1,62 +1,24 @@ -import fs from "node:fs"; -import os from "node:os"; -import path from "node:path"; import { SessionManager } from "@mariozechner/pi-coding-agent"; import { afterEach, describe, expect, it } from "vitest"; import { listEmbeddedExtensionFactories } from "../plugins/embedded-extension-factory.js"; -import { clearPluginLoaderCache, loadOpenClawPlugins } from "../plugins/loader.js"; -import { createEmptyPluginRegistry } from "../plugins/registry.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; +import { loadOpenClawPlugins } from "../plugins/loader.js"; import { buildEmbeddedExtensionFactories } from "./pi-embedded-runner/extensions.js"; +import { + cleanupTempPluginTestEnvironment, + createTempPluginDir, + resetActivePluginRegistryForTest, + writeTempPlugin, +} from "./test-helpers/temp-plugin-extension-fixtures.js"; -const EMPTY_PLUGIN_SCHEMA = { type: "object", additionalProperties: false, properties: {} }; const originalBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; const tempDirs: string[] = []; function createTempDir(): string { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-embedded-ext-")); - tempDirs.push(dir); - return dir; -} - -function writeTempPlugin(params: { - dir: string; - id: string; - body: string; - manifest?: Record; - filename?: string; -}): string { - const pluginDir = path.join(params.dir, params.id); - fs.mkdirSync(pluginDir, { recursive: true }); - const file = path.join(pluginDir, params.filename ?? `${params.id}.mjs`); - fs.writeFileSync(file, params.body, "utf-8"); - fs.writeFileSync( - path.join(pluginDir, "openclaw.plugin.json"), - JSON.stringify( - { - id: params.id, - ...params.manifest, - configSchema: EMPTY_PLUGIN_SCHEMA, - }, - null, - 2, - ), - "utf-8", - ); - return file; + return createTempPluginDir(tempDirs, "openclaw-embedded-ext-"); } afterEach(() => { - for (const dir of tempDirs.splice(0)) { - fs.rmSync(dir, { recursive: true, force: true }); - } - clearPluginLoaderCache(); - setActivePluginRegistry(createEmptyPluginRegistry()); - if (originalBundledPluginsDir === undefined) { - delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; - } else { - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = originalBundledPluginsDir; - } + cleanupTempPluginTestEnvironment(tempDirs, originalBundledPluginsDir); }); describe("buildEmbeddedExtensionFactories", () => { @@ -104,7 +66,7 @@ describe("buildEmbeddedExtensionFactories", () => { expect(firstFactories).toHaveLength(1); expect(listEmbeddedExtensionFactories()).toHaveLength(1); - setActivePluginRegistry(createEmptyPluginRegistry()); + resetActivePluginRegistryForTest(); expect(listEmbeddedExtensionFactories()).toHaveLength(0); loadOpenClawPlugins(options); diff --git a/src/agents/test-helpers/temp-plugin-extension-fixtures.ts b/src/agents/test-helpers/temp-plugin-extension-fixtures.ts new file mode 100644 index 00000000000..326a053634c --- /dev/null +++ b/src/agents/test-helpers/temp-plugin-extension-fixtures.ts @@ -0,0 +1,61 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { clearPluginLoaderCache } from "../../plugins/loader.js"; +import { createEmptyPluginRegistry } from "../../plugins/registry.js"; +import { setActivePluginRegistry } from "../../plugins/runtime.js"; + +const EMPTY_PLUGIN_SCHEMA = { type: "object", additionalProperties: false, properties: {} }; + +export function createTempPluginDir(tempDirs: string[], prefix: string): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + tempDirs.push(dir); + return dir; +} + +export function writeTempPlugin(params: { + dir: string; + id: string; + body: string; + manifest?: Record; + filename?: string; +}): string { + const pluginDir = path.join(params.dir, params.id); + fs.mkdirSync(pluginDir, { recursive: true }); + const file = path.join(pluginDir, params.filename ?? `${params.id}.mjs`); + fs.writeFileSync(file, params.body, "utf-8"); + fs.writeFileSync( + path.join(pluginDir, "openclaw.plugin.json"), + JSON.stringify( + { + id: params.id, + ...params.manifest, + configSchema: EMPTY_PLUGIN_SCHEMA, + }, + null, + 2, + ), + "utf-8", + ); + return file; +} + +export function cleanupTempPluginTestEnvironment( + tempDirs: string[], + originalBundledPluginsDir: string | undefined, +) { + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } + clearPluginLoaderCache(); + setActivePluginRegistry(createEmptyPluginRegistry()); + if (originalBundledPluginsDir === undefined) { + delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; + } else { + process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = originalBundledPluginsDir; + } +} + +export function resetActivePluginRegistryForTest() { + setActivePluginRegistry(createEmptyPluginRegistry()); +}