diff --git a/src/plugins/loader.test.ts b/src/plugins/loader.test.ts index 489ab3ce294..e0399526e7a 100644 --- a/src/plugins/loader.test.ts +++ b/src/plugins/loader.test.ts @@ -1,11 +1,18 @@ -import { execFileSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { pathToFileURL } from "node:url"; -import { createJiti } from "jiti"; import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; import { withEnv } from "../test-utils/env.js"; +type CreateJiti = typeof import("jiti").createJiti; + +let createJitiPromise: Promise | undefined; + +async function getCreateJiti() { + createJitiPromise ??= import("jiti").then(({ createJiti }) => createJiti); + return createJitiPromise; +} + async function importFreshPluginTestModules() { vi.resetModules(); vi.doUnmock("node:fs"); @@ -3244,42 +3251,24 @@ module.exports = { body: `module.exports = { id: "legacy-root-import", configSchema: (require("openclaw/plugin-sdk").emptyPluginConfigSchema)(), - register() {}, -};`, + register() {}, + };`, }); - const loaderModuleUrl = pathToFileURL( - path.join(process.cwd(), "src", "plugins", "loader.ts"), - ).href; - const script = ` - import { loadOpenClawPlugins } from ${JSON.stringify(loaderModuleUrl)}; - const registry = loadOpenClawPlugins({ + const registry = withEnv({ OPENCLAW_BUNDLED_PLUGINS_DIR: "/nonexistent/bundled/plugins" }, () => + loadOpenClawPlugins({ cache: false, - workspaceDir: ${JSON.stringify(plugin.dir)}, + workspaceDir: plugin.dir, config: { plugins: { - load: { paths: [${JSON.stringify(plugin.file)}] }, + load: { paths: [plugin.file] }, allow: ["legacy-root-import"], }, }, - }); - const record = registry.plugins.find((entry) => entry.id === "legacy-root-import"); - if (!record || record.status !== "loaded") { - console.error(record?.error ?? "legacy-root-import missing"); - process.exit(1); - } - `; - - execFileSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", script], { - cwd: process.cwd(), - env: { - ...process.env, - OPENCLAW_HOME: undefined, - OPENCLAW_BUNDLED_PLUGINS_DIR: "/nonexistent/bundled/plugins", - }, - encoding: "utf-8", - stdio: "pipe", - }); + }), + ); + const record = registry.plugins.find((entry) => entry.id === "legacy-root-import"); + expect(record?.status).toBe("loaded"); }); it.each([ @@ -3572,20 +3561,54 @@ module.exports = { }); it("loads source runtime shims through the non-native Jiti boundary", async () => { - const jiti = createJiti(import.meta.url, { - ...__testing.buildPluginLoaderJitiOptions(__testing.resolvePluginSdkScopedAliasMap()), + const copiedExtensionRoot = path.join(makeTempDir(), "extensions", "discord"); + const copiedSourceDir = path.join(copiedExtensionRoot, "src"); + const copiedPluginSdkDir = path.join(copiedExtensionRoot, "plugin-sdk"); + mkdirSafe(copiedSourceDir); + mkdirSafe(copiedPluginSdkDir); + const jitiBaseFile = path.join(copiedSourceDir, "__jiti-base__.mjs"); + fs.writeFileSync(jitiBaseFile, "export {};\n", "utf-8"); + fs.writeFileSync( + path.join(copiedSourceDir, "channel.runtime.ts"), + `import { resolveOutboundSendDep } from "openclaw/plugin-sdk/channel-runtime"; + +export const syntheticRuntimeMarker = { + resolveOutboundSendDep, +}; +`, + "utf-8", + ); + const copiedChannelRuntimeShim = path.join(copiedPluginSdkDir, "channel-runtime.ts"); + fs.writeFileSync( + copiedChannelRuntimeShim, + `export function resolveOutboundSendDep() { + return "shimmed"; +} +`, + "utf-8", + ); + const copiedChannelRuntime = path.join(copiedExtensionRoot, "src", "channel.runtime.ts"); + const jitiBaseUrl = pathToFileURL(jitiBaseFile).href; + + const createJiti = await getCreateJiti(); + const withoutAlias = createJiti(jitiBaseUrl, { + ...__testing.buildPluginLoaderJitiOptions({}), tryNative: false, }); - const discordChannelRuntime = path.join( - process.cwd(), - "extensions", - "discord", - "src", - "channel.runtime.ts", + await expect(withoutAlias.import(copiedChannelRuntime)).rejects.toThrow( + /plugin-sdk\/channel-runtime/, ); - await expect(jiti.import(discordChannelRuntime)).resolves.toMatchObject({ - discordSetupWizard: expect.any(Object), + const withAlias = createJiti(jitiBaseUrl, { + ...__testing.buildPluginLoaderJitiOptions({ + "openclaw/plugin-sdk/channel-runtime": copiedChannelRuntimeShim, + }), + tryNative: false, + }); + await expect(withAlias.import(copiedChannelRuntime)).resolves.toMatchObject({ + syntheticRuntimeMarker: { + resolveOutboundSendDep: expect.any(Function), + }, }); }, 240_000); @@ -3627,6 +3650,7 @@ export const copiedRuntimeMarker = { const copiedChannelRuntime = path.join(copiedExtensionRoot, "src", "channel.runtime.ts"); const jitiBaseUrl = pathToFileURL(jitiBaseFile).href; + const createJiti = await getCreateJiti(); const withoutAlias = createJiti(jitiBaseUrl, { ...__testing.buildPluginLoaderJitiOptions({}), tryNative: false,