From fa50b635800f20b0732d4f34c6da404db4dbc95f Mon Sep 17 00:00:00 2001 From: Josh Lehman Date: Fri, 20 Mar 2026 06:56:11 -0700 Subject: [PATCH] plugins: stabilize loader git path regression test --- .../loader.git-path-regression.test.ts | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/plugins/loader.git-path-regression.test.ts b/src/plugins/loader.git-path-regression.test.ts index 23ab4f4243d..fac5e22657c 100644 --- a/src/plugins/loader.git-path-regression.test.ts +++ b/src/plugins/loader.git-path-regression.test.ts @@ -1,18 +1,8 @@ +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 { afterEach, describe, expect, it } from "vitest"; -import { __testing } from "./loader.js"; - -type CreateJiti = typeof import("jiti").createJiti; - -let createJitiPromise: Promise | undefined; - -async function getCreateJiti() { - createJitiPromise ??= import("jiti").then(({ createJiti }) => createJiti); - return createJitiPromise; -} const tempRoots: string[] = []; @@ -39,7 +29,6 @@ describe("plugin loader git path regression", () => { 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( @@ -69,29 +58,46 @@ export const copiedRuntimeMarker = { `, "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, - }); - // The production loader uses sync Jiti evaluation, so this regression test - // should exercise the same seam instead of Jiti's async import helper. - expect(() => withoutAlias(copiedChannelRuntime)).toThrow(); - - const withAlias = createJiti(jitiBaseUrl, { - ...__testing.buildPluginLoaderJitiOptions({ - "openclaw/plugin-sdk/channel-runtime": copiedChannelRuntimeShim, - }), - tryNative: false, - }); - expect(withAlias(copiedChannelRuntime)).toMatchObject({ - copiedRuntimeMarker: { - PAIRING_APPROVED_MESSAGE: "paired", - resolveOutboundSendDep: expect.any(Function), - }, + const script = ` + import { createJiti } from "jiti"; + const withoutAlias = createJiti(${JSON.stringify(jitiBaseFile)}, { + interopDefault: true, + tryNative: false, + extensions: [".ts", ".tsx", ".mts", ".cts", ".mtsx", ".ctsx", ".js", ".mjs", ".cjs", ".json"], + }); + let withoutAliasThrew = false; + try { + withoutAlias(${JSON.stringify(copiedChannelRuntime)}); + } catch { + withoutAliasThrew = true; + } + const withAlias = createJiti(${JSON.stringify(jitiBaseFile)}, { + interopDefault: true, + tryNative: false, + extensions: [".ts", ".tsx", ".mts", ".cts", ".mtsx", ".ctsx", ".js", ".mjs", ".cjs", ".json"], + alias: { + "openclaw/plugin-sdk/channel-runtime": ${JSON.stringify(copiedChannelRuntimeShim)}, + }, + }); + const mod = withAlias(${JSON.stringify(copiedChannelRuntime)}); + console.log(JSON.stringify({ + withoutAliasThrew, + marker: mod.copiedRuntimeMarker?.PAIRING_APPROVED_MESSAGE, + dep: mod.copiedRuntimeMarker?.resolveOutboundSendDep?.(), + })); + `; + const raw = execFileSync(process.execPath, ["--input-type=module", "--eval", script], { + cwd: process.cwd(), + encoding: "utf-8", }); + const result = JSON.parse(raw) as { + withoutAliasThrew: boolean; + marker?: string; + dep?: string; + }; + expect(result.withoutAliasThrew).toBe(true); + expect(result.marker).toBe("paired"); + expect(result.dep).toBe("shimmed"); }); });