




















1+import { spawnSync } from "node:child_process";
12import fs from "node:fs";
23import { createRequire } from "node:module";
34import os from "node:os";
45import path from "node:path";
6+import { pathToFileURL } from "node:url";
57import { afterEach, describe, expect, it } from "vitest";
68import {
79installOpenClawPluginSdkNativeResolver,
@@ -122,6 +124,80 @@ describe("installOpenClawPluginSdkNativeResolver", () => {
122124}
123125});
124126127+it("keeps SDK aliases available for native ESM lazy imports", () => {
128+const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sdk-native-esm-resolver-"));
129+const probePath = path.join(root, "probe.mjs");
130+const resolverModuleUrl = pathToFileURL(
131+path.join(process.cwd(), "src", "plugins", "plugin-sdk-native-resolver.ts"),
132+).href;
133+fs.writeFileSync(
134+probePath,
135+[
136+'import fs from "node:fs";',
137+'import path from "node:path";',
138+'import { pathToFileURL } from "node:url";',
139+`import { installOpenClawPluginSdkNativeResolver, resetOpenClawPluginSdkNativeResolverForTest } from ${JSON.stringify(resolverModuleUrl)};`,
140+`const root = ${JSON.stringify(root)};`,
141+"const writeJson = (targetPath, value) => {",
142+" fs.mkdirSync(path.dirname(targetPath), { recursive: true });",
143+' fs.writeFileSync(targetPath, `${JSON.stringify(value, null, 2)}\\n`, "utf8");',
144+"};",
145+'writeJson(path.join(root, "package.json"), {',
146+' name: "openclaw",',
147+' type: "module",',
148+' bin: { openclaw: "./openclaw.mjs" },',
149+" exports: {",
150+' "./plugin-sdk": "./dist/plugin-sdk/root-alias.cjs",',
151+' "./plugin-sdk/channel-outbound": "./dist/plugin-sdk/channel-outbound.js",',
152+" },",
153+"});",
154+'fs.writeFileSync(path.join(root, "openclaw.mjs"), "#!/usr/bin/env node\\n", "utf8");',
155+'fs.mkdirSync(path.join(root, "dist", "plugin-sdk"), { recursive: true });',
156+'fs.writeFileSync(path.join(root, "dist", "plugin-sdk", "root-alias.cjs"), "module.exports = {};\\n", "utf8");',
157+'fs.writeFileSync(path.join(root, "dist", "plugin-sdk", "channel-outbound.js"), "export const defineChannelMessageAdapter = () => \\"adapter\\";\\n", "utf8");',
158+'const loaderModulePath = path.join(root, "dist", "plugins", "loader.js");',
159+"fs.mkdirSync(path.dirname(loaderModulePath), { recursive: true });",
160+'fs.writeFileSync(loaderModulePath, "export default {};\\n", "utf8");',
161+'const pluginRoot = path.join(root, "external-plugin");',
162+'writeJson(path.join(pluginRoot, "package.json"), { name: "external-plugin", type: "module" });',
163+'const entryPath = path.join(pluginRoot, "dist", "runtime-api.js");',
164+'const lazyPath = path.join(pluginRoot, "dist", "lazy.js");',
165+"fs.mkdirSync(path.dirname(entryPath), { recursive: true });",
166+"fs.writeFileSync(",
167+" entryPath,",
168+' "import { defineChannelMessageAdapter } from \\"openclaw/plugin-sdk/channel-outbound\\"; export const eager = defineChannelMessageAdapter(); export const loadLazy = () => import(\\"./lazy.js\\");\\n",',
169+' "utf8",',
170+");",
171+"fs.writeFileSync(",
172+" lazyPath,",
173+' "import { defineChannelMessageAdapter } from \\"openclaw/plugin-sdk/channel-outbound\\"; export const lazy = defineChannelMessageAdapter();\\n",',
174+' "utf8",',
175+");",
176+"installOpenClawPluginSdkNativeResolver({",
177+" modulePath: loaderModulePath,",
178+" pluginModulePath: entryPath,",
179+' pluginSdkResolution: "dist",',
180+"});",
181+"const module = await import(pathToFileURL(entryPath).href);",
182+"const lazy = await module.loadLazy();",
183+"resetOpenClawPluginSdkNativeResolverForTest();",
184+"console.log(`${module.eager}:${lazy.lazy}`);",
185+"",
186+].join("\n"),
187+"utf8",
188+);
189+190+const result = spawnSync(process.execPath, ["--import", "tsx", probePath], {
191+cwd: process.cwd(),
192+encoding: "utf8",
193+});
194+fs.rmSync(root, { recursive: true, force: true });
195+196+expect(result.stderr).toBe("");
197+expect(result.status).toBe(0);
198+expect(result.stdout.trim()).toBe("adapter:adapter");
199+});
200+125201it("does not resolve SDK aliases for parents outside registered plugin roots", () => {
126202const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sdk-native-guard-"));
127203const { loaderModulePath } = writeFakeOpenClawPackage(root);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。