test: smoke packed bundled channel entries

This commit is contained in:
Peter Steinberger
2026-04-08 05:55:29 +01:00
parent 6c0d25cea4
commit 75fe554db7
4 changed files with 293 additions and 38 deletions

View File

@@ -14,6 +14,7 @@ afterEach(() => {
}
vi.resetModules();
vi.doUnmock("jiti");
vi.unstubAllEnvs();
});
describe("loadBundledEntryExportSync", () => {
@@ -84,4 +85,39 @@ describe("loadBundledEntryExportSync", () => {
platformSpy.mockRestore();
}
});
it("can disable source-tree fallback for dist bundled entry checks", () => {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-entry-contract-"));
tempDirs.push(tempRoot);
fs.writeFileSync(path.join(tempRoot, "package.json"), '{"name":"openclaw"}\n', "utf8");
const pluginRoot = path.join(tempRoot, "dist", "extensions", "telegram");
const sourceRoot = path.join(tempRoot, "extensions", "telegram", "src");
fs.mkdirSync(pluginRoot, { recursive: true });
fs.mkdirSync(sourceRoot, { recursive: true });
const importerPath = path.join(pluginRoot, "index.js");
fs.writeFileSync(importerPath, "export default {};\n", "utf8");
fs.writeFileSync(
path.join(sourceRoot, "secret-contract.ts"),
"export const sentinel = 42;\n",
"utf8",
);
expect(
loadBundledEntryExportSync<number>(pathToFileURL(importerPath).href, {
specifier: "./src/secret-contract.js",
exportName: "sentinel",
}),
).toBe(42);
vi.stubEnv("OPENCLAW_DISABLE_BUNDLED_ENTRY_SOURCE_FALLBACK", "1");
expect(() =>
loadBundledEntryExportSync<number>(pathToFileURL(importerPath).href, {
specifier: "./src/secret-contract.js",
exportName: "sentinel",
}),
).toThrow(`resolved "${path.join(pluginRoot, "src", "secret-contract.js")}"`);
});
});

View File

@@ -67,6 +67,11 @@ export type BundledChannelSetupEntryContract<TPlugin = ChannelPlugin> = {
const nodeRequire = createRequire(import.meta.url);
const jitiLoaders = new Map<string, ReturnType<typeof createJiti>>();
const loadedModuleExports = new Map<string, unknown>();
const disableBundledEntrySourceFallbackEnv = "OPENCLAW_DISABLE_BUNDLED_ENTRY_SOURCE_FALLBACK";
function isTruthyEnvFlag(value: string | undefined): boolean {
return value !== undefined && !/^(?:0|false)$/iu.test(value.trim());
}
function resolveSpecifierCandidates(modulePath: string): string[] {
const ext = normalizeLowercaseStringOrEmpty(path.extname(modulePath));
@@ -140,6 +145,9 @@ function resolveBundledEntryModuleCandidates(
if (!importerPath.startsWith(distExtensionsRoot)) {
return candidates;
}
if (isTruthyEnvFlag(process.env[disableBundledEntrySourceFallbackEnv])) {
return candidates;
}
const pluginDirName = path.basename(importerDir);
const sourcePluginRoot = path.join(packageRoot, "extensions", pluginDirName);