From ba331014beb02a875026d75a9747cd2622656620 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 21:41:23 +0100 Subject: [PATCH] test: share plugin sdk facade fixtures --- src/plugin-sdk/facade-loader.test.ts | 24 ++++++--------------- src/plugin-sdk/facade-runtime.test.ts | 24 ++++++--------------- src/plugin-sdk/test-helpers.ts | 31 ++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/plugin-sdk/facade-loader.test.ts b/src/plugin-sdk/facade-loader.test.ts index 7d7b235531c..ffc4b21bd3a 100644 --- a/src/plugin-sdk/facade-loader.test.ts +++ b/src/plugin-sdk/facade-loader.test.ts @@ -8,7 +8,11 @@ import { setFacadeLoaderJitiFactoryForTest, } from "./facade-loader.js"; import { listImportedBundledPluginFacadeIds as listImportedFacadeRuntimeIds } from "./facade-runtime.js"; -import { createPluginSdkTestHarness } from "./test-helpers.js"; +import { + createBundledPluginPublicSurfaceFixture, + createPluginSdkTestHarness, + createThrowingBundledPluginPublicSurfaceFixture, +} from "./test-helpers.js"; const { createTempDirSync } = createPluginSdkTestHarness(); const originalBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; @@ -16,25 +20,11 @@ const FACADE_LOADER_GLOBAL = "__openclawTestLoadBundledPluginPublicSurfaceModule type FacadeLoaderJitiFactory = NonNullable[0]>; function createBundledPluginDir(prefix: string, marker: string): string { - const rootDir = createTempDirSync(prefix); - fs.mkdirSync(path.join(rootDir, "demo"), { recursive: true }); - fs.writeFileSync( - path.join(rootDir, "demo", "api.js"), - `export const marker = ${JSON.stringify(marker)};\n`, - "utf8", - ); - return rootDir; + return createBundledPluginPublicSurfaceFixture({ createTempDirSync, marker, prefix }); } function createThrowingPluginDir(prefix: string): string { - const rootDir = createTempDirSync(prefix); - fs.mkdirSync(path.join(rootDir, "bad"), { recursive: true }); - fs.writeFileSync( - path.join(rootDir, "bad", "api.js"), - `throw new Error("plugin load failure");\n`, - "utf8", - ); - return rootDir; + return createThrowingBundledPluginPublicSurfaceFixture({ createTempDirSync, prefix }); } function createCircularPluginDir(prefix: string): string { diff --git a/src/plugin-sdk/facade-runtime.test.ts b/src/plugin-sdk/facade-runtime.test.ts index 9b1a7080b1b..73020f6a6d4 100644 --- a/src/plugin-sdk/facade-runtime.test.ts +++ b/src/plugin-sdk/facade-runtime.test.ts @@ -12,32 +12,22 @@ import { loadBundledPluginPublicSurfaceModuleSync, resetFacadeRuntimeStateForTest, } from "./facade-runtime.js"; -import { createPluginSdkTestHarness } from "./test-helpers.js"; +import { + createBundledPluginPublicSurfaceFixture, + createPluginSdkTestHarness, + createThrowingBundledPluginPublicSurfaceFixture, +} from "./test-helpers.js"; const { createTempDirSync } = createPluginSdkTestHarness(); const originalBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; const originalStateDir = process.env.OPENCLAW_STATE_DIR; function createBundledPluginDir(prefix: string, marker: string): string { - const rootDir = createTempDirSync(prefix); - fs.mkdirSync(path.join(rootDir, "demo"), { recursive: true }); - fs.writeFileSync( - path.join(rootDir, "demo", "api.js"), - `export const marker = ${JSON.stringify(marker)};\n`, - "utf8", - ); - return rootDir; + return createBundledPluginPublicSurfaceFixture({ createTempDirSync, marker, prefix }); } function createThrowingPluginDir(prefix: string): string { - const rootDir = createTempDirSync(prefix); - fs.mkdirSync(path.join(rootDir, "bad"), { recursive: true }); - fs.writeFileSync( - path.join(rootDir, "bad", "api.js"), - `throw new Error("plugin load failure");\n`, - "utf8", - ); - return rootDir; + return createThrowingBundledPluginPublicSurfaceFixture({ createTempDirSync, prefix }); } afterEach(() => { diff --git a/src/plugin-sdk/test-helpers.ts b/src/plugin-sdk/test-helpers.ts index b4ca0418079..1062e915cd2 100644 --- a/src/plugin-sdk/test-helpers.ts +++ b/src/plugin-sdk/test-helpers.ts @@ -1,4 +1,4 @@ -import { mkdirSync, type RmOptions } from "node:fs"; +import { mkdirSync, writeFileSync, type RmOptions } from "node:fs"; import { mkdir, mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -44,3 +44,32 @@ export function createPluginSdkTestHarness(options?: { cleanup?: RmOptions }) { createTempDirSync, }; } + +export function createBundledPluginPublicSurfaceFixture(params: { + createTempDirSync: (prefix: string) => string; + marker: string; + prefix: string; +}) { + const rootDir = params.createTempDirSync(params.prefix); + mkdirSync(path.join(rootDir, "demo"), { recursive: true }); + writeFileSync( + path.join(rootDir, "demo", "api.js"), + `export const marker = ${JSON.stringify(params.marker)};\n`, + "utf8", + ); + return rootDir; +} + +export function createThrowingBundledPluginPublicSurfaceFixture(params: { + createTempDirSync: (prefix: string) => string; + prefix: string; +}) { + const rootDir = params.createTempDirSync(params.prefix); + mkdirSync(path.join(rootDir, "bad"), { recursive: true }); + writeFileSync( + path.join(rootDir, "bad", "api.js"), + `throw new Error("plugin load failure");\n`, + "utf8", + ); + return rootDir; +}