|
1 | 1 | // Verifies plugin public surface loading and fallback behavior. |
2 | 2 | import fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
| 4 | +import { pathToFileURL } from "node:url"; |
4 | 5 | import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures"; |
5 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
6 | 7 | import { createTempDirTracker } from "../../test/helpers/temp-dir.js"; |
7 | 8 | import { MissingPublicSurfaceError } from "../plugin-sdk/facade-loader.js"; |
| 9 | +import { spawnNodeEvalSync } from "../test-utils/node-process.js"; |
8 | 10 | import { withMockedWindowsPlatform } from "../test-utils/vitest-spies.js"; |
9 | 11 | import { resetPluginCache } from "./plugin-cache.js"; |
10 | 12 |
|
@@ -32,7 +34,6 @@ afterEach(() => { |
32 | 34 | vi.doUnmock("./bundled-dir.js"); |
33 | 35 | vi.doUnmock("./native-module-require.js"); |
34 | 36 | vi.doUnmock("./public-surface-runtime.js"); |
35 | | - vi.doUnmock("node:module"); |
36 | 37 | if (originalBundledPluginsDir === undefined) { |
37 | 38 | delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; |
38 | 39 | } else { |
@@ -131,45 +132,74 @@ describe("bundled plugin public surface loader", () => { |
131 | 132 | }); |
132 | 133 | }); |
133 | 134 |
|
134 | | - it("prefers source require for bundled source public artifacts when a ts require hook exists", async () => { |
135 | | - const createJiti = vi.fn(() => vi.fn(() => ({ marker: "jiti-should-not-run" }))); |
136 | | - vi.doMock("jiti", () => ({ |
137 | | - createJiti, |
138 | | - })); |
139 | | - const requireLoader = Object.assign( |
140 | | - vi.fn(() => ({ marker: "source-require-ok" })), |
| 135 | + it("loads import-only dependencies under tsx and shares source artifacts for one cache generation", () => { |
| 136 | + const tempRoot = tempDirs.make("openclaw-public-surface-source-"); |
| 137 | + const bundledPluginsDir = path.join(tempRoot, "extensions"); |
| 138 | + const pluginRoot = path.join(bundledPluginsDir, "demo"); |
| 139 | + const dependencyRoot = path.join(pluginRoot, "node_modules", "import-only-fixture"); |
| 140 | + const modulePath = path.join(pluginRoot, "secret-contract-api.ts"); |
| 141 | + const dependencyPath = path.join(dependencyRoot, "index.js"); |
| 142 | + fs.mkdirSync(dependencyRoot, { recursive: true }); |
| 143 | + fs.writeFileSync( |
| 144 | + path.join(dependencyRoot, "package.json"), |
| 145 | + JSON.stringify({ type: "module", exports: { node: { import: "./index.js" } } }), |
| 146 | + ); |
| 147 | + fs.writeFileSync(dependencyPath, 'export const marker = "original";\n'); |
| 148 | + fs.writeFileSync(path.join(tempRoot, "shared.cjs"), "module.exports = {};\n"); |
| 149 | + fs.writeFileSync( |
| 150 | + modulePath, |
| 151 | + 'export { marker } from "import-only-fixture";\nexport { default as shared } from "../../shared.cjs";\n', |
| 152 | + ); |
| 153 | + |
| 154 | + const moduleUrl = (relativePath: string) => |
| 155 | + JSON.stringify(pathToFileURL(path.join(process.cwd(), relativePath)).href); |
| 156 | + const result = spawnNodeEvalSync( |
| 157 | + ` |
| 158 | + import assert from "node:assert/strict"; |
| 159 | + import fs from "node:fs"; |
| 160 | + import { createRequire } from "node:module"; |
| 161 | + import { loadBundledPluginPublicArtifactModuleSync, loadPluginPublicArtifactModuleSync } from ${moduleUrl("src/plugins/public-surface-loader.ts")}; |
| 162 | + import { loadFacadeModuleAtLocationSync } from ${moduleUrl("src/plugin-sdk/facade-loader.ts")}; |
| 163 | + import { clearPluginMetadataLifecycleCaches } from ${moduleUrl("src/plugins/plugin-metadata-lifecycle.ts")}; |
| 164 | + assert.equal(typeof createRequire(import.meta.url).extensions[".ts"], "function"); |
| 165 | + const load = () => loadBundledPluginPublicArtifactModuleSync({ dirName: "demo", artifactBasename: "secret-contract-api.js" }); |
| 166 | + const first = load(); |
| 167 | + assert.equal(first.marker, "original"); |
| 168 | + assert.equal(load(), first); |
| 169 | + assert.equal(loadPluginPublicArtifactModuleSync({ pluginRoot: ${JSON.stringify(pluginRoot)}, artifactBasename: "secret-contract-api.js" }), first); |
| 170 | + assert.equal(loadFacadeModuleAtLocationSync({ location: { modulePath: ${JSON.stringify(modulePath)}, boundaryRoot: ${JSON.stringify(pluginRoot)} }, trackedPluginId: "demo" }), first); |
| 171 | + fs.writeFileSync(${JSON.stringify(dependencyPath)}, 'export const marker = "replacement";\\n'); |
| 172 | + assert.equal(load(), first); |
| 173 | + clearPluginMetadataLifecycleCaches(); |
| 174 | + const replacement = load(); |
| 175 | + assert.notEqual(replacement, first); |
| 176 | + assert.equal(replacement.marker, "replacement"); |
| 177 | + assert.equal(replacement.shared, first.shared); |
| 178 | + console.log("source artifact import, identity, and lifecycle verified"); |
| 179 | + `, |
141 | 180 | { |
142 | | - extensions: { |
143 | | - ".ts": vi.fn(), |
| 181 | + imports: [pathToFileURL(path.join(process.cwd(), "scripts/tsx.mjs")).href], |
| 182 | + timeout: 30_000, |
| 183 | + env: { |
| 184 | + PATH: process.env.PATH, |
| 185 | + SystemRoot: process.env.SystemRoot, |
| 186 | + HOME: tempRoot, |
| 187 | + USERPROFILE: tempRoot, |
| 188 | + TMPDIR: tempRoot, |
| 189 | + TMP: tempRoot, |
| 190 | + TEMP: tempRoot, |
| 191 | + VITEST: "true", |
| 192 | + OPENCLAW_STATE_DIR: path.join(tempRoot, "state"), |
| 193 | + OPENCLAW_BUNDLED_PLUGINS_DIR: bundledPluginsDir, |
| 194 | + OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1", |
| 195 | + JITI_FS_CACHE: "0", |
144 | 196 | }, |
145 | 197 | }, |
146 | 198 | ); |
147 | | - vi.doMock("node:module", async () => { |
148 | | - const actual = await vi.importActual<typeof import("node:module")>("node:module"); |
149 | | - return Object.assign({}, actual, { |
150 | | - createRequire: vi.fn(() => requireLoader), |
151 | | - }); |
152 | | - }); |
153 | | - |
154 | | - const publicSurfaceLoader = await importFreshModule< |
155 | | - typeof import("./public-surface-loader.js") |
156 | | - >(import.meta.url, "./public-surface-loader.js?scope=source-require-fast-path"); |
157 | | - const tempRoot = tempDirs.make("openclaw-public-surface-loader-"); |
158 | | - const bundledPluginsDir = path.join(tempRoot, "extensions"); |
159 | | - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledPluginsDir; |
160 | | - |
161 | | - const modulePath = path.join(bundledPluginsDir, "demo", "secret-contract-api.ts"); |
162 | | - fs.mkdirSync(path.dirname(modulePath), { recursive: true }); |
163 | | - fs.writeFileSync(modulePath, 'export const marker = "source-require-ok";\n', "utf8"); |
164 | | - |
165 | | - expect( |
166 | | - publicSurfaceLoader.loadBundledPluginPublicArtifactModuleSync<{ marker: string }>({ |
167 | | - dirName: "demo", |
168 | | - artifactBasename: "secret-contract-api.js", |
169 | | - }).marker, |
170 | | - ).toBe("source-require-ok"); |
171 | | - expect(requireLoader).toHaveBeenCalledWith(fs.realpathSync(modulePath)); |
172 | | - expect(createJiti).not.toHaveBeenCalled(); |
| 199 | + expect(result.error).toBeUndefined(); |
| 200 | + expect(result.stderr).toBe(""); |
| 201 | + expect(result.status).toBe(0); |
| 202 | + expect(result.stdout.trim()).toBe("source artifact import, identity, and lifecycle verified"); |
173 | 203 | }); |
174 | 204 |
|
175 | 205 | it("keeps bundled dist public artifacts on the native path", async () => { |
|
0 commit comments