From 83037720d9c815632137adf1149b3606c0ca925e Mon Sep 17 00:00:00 2001 From: Paul Frederiksen Date: Mon, 4 May 2026 05:48:35 +0000 Subject: [PATCH] test: force channel loader jiti fallback path --- src/channels/plugins/module-loader.test.ts | 49 ++++++++++++++++------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/channels/plugins/module-loader.test.ts b/src/channels/plugins/module-loader.test.ts index ca101083d0b..4aa37e99b2e 100644 --- a/src/channels/plugins/module-loader.test.ts +++ b/src/channels/plugins/module-loader.test.ts @@ -1,12 +1,17 @@ import fs from "node:fs"; +import { createRequire } from "node:module"; import os from "node:os"; import path from "node:path"; import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures"; import { afterEach, describe, expect, it, vi } from "vitest"; import { isJavaScriptModulePath } from "../../plugins/native-module-require.js"; +import type { PluginModuleLoaderFactory } from "../../plugins/plugin-module-loader-cache.js"; import { resolveExistingPluginModulePath } from "./module-loader.js"; const tempDirs: string[] = []; +const pluginModuleLoaderJitiFactoryOverrideKey = Symbol.for( + "openclaw.pluginModuleLoaderJitiFactoryOverride", +); afterEach(() => { for (const tempDir of tempDirs.splice(0)) { @@ -15,8 +20,21 @@ afterEach(() => { vi.restoreAllMocks(); vi.resetModules(); vi.doUnmock("jiti"); + delete ( + globalThis as typeof globalThis & { + [pluginModuleLoaderJitiFactoryOverrideKey]?: PluginModuleLoaderFactory; + } + )[pluginModuleLoaderJitiFactoryOverrideKey]; }); +function stubPluginModuleLoaderJitiFactory(createJiti: PluginModuleLoaderFactory): void { + ( + globalThis as typeof globalThis & { + [pluginModuleLoaderJitiFactoryOverrideKey]?: PluginModuleLoaderFactory; + } + )[pluginModuleLoaderJitiFactoryOverrideKey] = createJiti; +} + function createTempDir(): string { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-module-loader-")); tempDirs.push(tempDir); @@ -70,9 +88,7 @@ describe("channel plugin module loader helpers", () => { })); const createJiti = vi.fn(() => loadWithJiti); vi.resetModules(); - vi.doMock("jiti", () => ({ - createJiti, - })); + stubPluginModuleLoaderJitiFactory(createJiti as unknown as PluginModuleLoaderFactory); const loaderModule = await importFreshModule( import.meta.url, "./module-loader.js?scope=source-ts-jiti-fallback", @@ -82,15 +98,24 @@ describe("channel plugin module loader helpers", () => { fs.mkdirSync(path.dirname(modulePath), { recursive: true }); fs.writeFileSync(modulePath, "export const ok = true;\n", "utf8"); - expect( - loaderModule.loadChannelPluginModule({ - modulePath, - rootDir, - }), - ).toEqual({ - loadedBy: "jiti", - target: fs.realpathSync.native(modulePath), - }); + const testRequire = createRequire(import.meta.url); + const originalTsHook = testRequire.extensions[".ts"]; + delete testRequire.extensions[".ts"]; + try { + expect( + loaderModule.loadChannelPluginModule({ + modulePath, + rootDir, + }), + ).toEqual({ + loadedBy: "jiti", + target: fs.realpathSync.native(modulePath), + }); + } finally { + if (originalTsHook) { + testRequire.extensions[".ts"] = originalTsHook; + } + } expect(createJiti).toHaveBeenCalledOnce(); expect(createJiti).toHaveBeenCalledWith( expect.stringContaining("module-loader.ts"),