mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:40:44 +00:00
test: share temp plugin extension fixtures
This commit is contained in:
@@ -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<string, unknown>;
|
||||
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);
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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);
|
||||
|
||||
61
src/agents/test-helpers/temp-plugin-extension-fixtures.ts
Normal file
61
src/agents/test-helpers/temp-plugin-extension-fixtures.ts
Normal file
@@ -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<string, unknown>;
|
||||
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());
|
||||
}
|
||||
Reference in New Issue
Block a user