mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:40:44 +00:00
test: force channel loader jiti fallback path
This commit is contained in:
committed by
Peter Steinberger
parent
eeff1f7cb6
commit
83037720d9
@@ -1,12 +1,17 @@
|
||||
import fs from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { isJavaScriptModulePath } from "../../plugins/native-module-require.js";
|
||||
import type { PluginModuleLoaderFactory } from "../../plugins/plugin-module-loader-cache.js";
|
||||
import { resolveExistingPluginModulePath } from "./module-loader.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
const pluginModuleLoaderJitiFactoryOverrideKey = Symbol.for(
|
||||
"openclaw.pluginModuleLoaderJitiFactoryOverride",
|
||||
);
|
||||
|
||||
afterEach(() => {
|
||||
for (const tempDir of tempDirs.splice(0)) {
|
||||
@@ -15,8 +20,21 @@ afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.resetModules();
|
||||
vi.doUnmock("jiti");
|
||||
delete (
|
||||
globalThis as typeof globalThis & {
|
||||
[pluginModuleLoaderJitiFactoryOverrideKey]?: PluginModuleLoaderFactory;
|
||||
}
|
||||
)[pluginModuleLoaderJitiFactoryOverrideKey];
|
||||
});
|
||||
|
||||
function stubPluginModuleLoaderJitiFactory(createJiti: PluginModuleLoaderFactory): void {
|
||||
(
|
||||
globalThis as typeof globalThis & {
|
||||
[pluginModuleLoaderJitiFactoryOverrideKey]?: PluginModuleLoaderFactory;
|
||||
}
|
||||
)[pluginModuleLoaderJitiFactoryOverrideKey] = createJiti;
|
||||
}
|
||||
|
||||
function createTempDir(): string {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-module-loader-"));
|
||||
tempDirs.push(tempDir);
|
||||
@@ -70,9 +88,7 @@ describe("channel plugin module loader helpers", () => {
|
||||
}));
|
||||
const createJiti = vi.fn(() => loadWithJiti);
|
||||
vi.resetModules();
|
||||
vi.doMock("jiti", () => ({
|
||||
createJiti,
|
||||
}));
|
||||
stubPluginModuleLoaderJitiFactory(createJiti as unknown as PluginModuleLoaderFactory);
|
||||
const loaderModule = await importFreshModule<typeof import("./module-loader.js")>(
|
||||
import.meta.url,
|
||||
"./module-loader.js?scope=source-ts-jiti-fallback",
|
||||
@@ -82,15 +98,24 @@ describe("channel plugin module loader helpers", () => {
|
||||
fs.mkdirSync(path.dirname(modulePath), { recursive: true });
|
||||
fs.writeFileSync(modulePath, "export const ok = true;\n", "utf8");
|
||||
|
||||
expect(
|
||||
loaderModule.loadChannelPluginModule({
|
||||
modulePath,
|
||||
rootDir,
|
||||
}),
|
||||
).toEqual({
|
||||
loadedBy: "jiti",
|
||||
target: fs.realpathSync.native(modulePath),
|
||||
});
|
||||
const testRequire = createRequire(import.meta.url);
|
||||
const originalTsHook = testRequire.extensions[".ts"];
|
||||
delete testRequire.extensions[".ts"];
|
||||
try {
|
||||
expect(
|
||||
loaderModule.loadChannelPluginModule({
|
||||
modulePath,
|
||||
rootDir,
|
||||
}),
|
||||
).toEqual({
|
||||
loadedBy: "jiti",
|
||||
target: fs.realpathSync.native(modulePath),
|
||||
});
|
||||
} finally {
|
||||
if (originalTsHook) {
|
||||
testRequire.extensions[".ts"] = originalTsHook;
|
||||
}
|
||||
}
|
||||
expect(createJiti).toHaveBeenCalledOnce();
|
||||
expect(createJiti).toHaveBeenCalledWith(
|
||||
expect.stringContaining("module-loader.ts"),
|
||||
|
||||
Reference in New Issue
Block a user