mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-26 16:41:49 +00:00
test: dedupe infra and plugin-sdk utility suites
This commit is contained in:
@@ -108,6 +108,14 @@ function loadRootAliasWithStubs(options?: {
|
||||
};
|
||||
}
|
||||
|
||||
function createPackageRoot() {
|
||||
return path.dirname(path.dirname(rootAliasPath));
|
||||
}
|
||||
|
||||
function createDistAliasPath() {
|
||||
return path.join(createPackageRoot(), "dist", "plugin-sdk", "root-alias.cjs");
|
||||
}
|
||||
|
||||
describe("plugin-sdk root alias", () => {
|
||||
it("exposes the fast empty config schema helper", () => {
|
||||
const factory = rootSdk.emptyPluginConfigSchema as (() => EmptySchema) | undefined;
|
||||
@@ -149,7 +157,7 @@ describe("plugin-sdk root alias", () => {
|
||||
it("loads legacy root exports on demand and preserves reflection", () => {
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
monolithicExports: {
|
||||
slowHelper: () => "loaded",
|
||||
slowHelper: (): string => "loaded",
|
||||
},
|
||||
});
|
||||
const lazyRootSdk = lazyModule.moduleExports;
|
||||
@@ -164,40 +172,44 @@ describe("plugin-sdk root alias", () => {
|
||||
expect(Object.getOwnPropertyDescriptor(lazyRootSdk, "slowHelper")).toBeDefined();
|
||||
});
|
||||
|
||||
it("prefers native loading when compat resolves to dist", () => {
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
distExists: true,
|
||||
monolithicExports: {
|
||||
slowHelper: () => "loaded",
|
||||
it.each([
|
||||
{
|
||||
name: "prefers native loading when compat resolves to dist",
|
||||
options: {
|
||||
distExists: true,
|
||||
monolithicExports: {
|
||||
slowHelper: (): string => "loaded",
|
||||
},
|
||||
},
|
||||
});
|
||||
expectedTryNative: true,
|
||||
},
|
||||
{
|
||||
name: "prefers source loading under vitest even when compat resolves to dist",
|
||||
options: {
|
||||
distExists: true,
|
||||
env: { VITEST: "1" },
|
||||
monolithicExports: {
|
||||
slowHelper: (): string => "loaded",
|
||||
},
|
||||
},
|
||||
expectedTryNative: false,
|
||||
},
|
||||
])("$name", ({ options, expectedTryNative }) => {
|
||||
const lazyModule = loadRootAliasWithStubs(options);
|
||||
|
||||
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
|
||||
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(true);
|
||||
});
|
||||
|
||||
it("prefers source loading under vitest even when compat resolves to dist", () => {
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
distExists: true,
|
||||
env: { VITEST: "1" },
|
||||
monolithicExports: {
|
||||
slowHelper: () => "loaded",
|
||||
},
|
||||
});
|
||||
|
||||
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
|
||||
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(false);
|
||||
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(expectedTryNative);
|
||||
});
|
||||
|
||||
it("falls back to src files even when the alias itself is loaded from dist", () => {
|
||||
const packageRoot = path.dirname(path.dirname(rootAliasPath));
|
||||
const distAliasPath = path.join(packageRoot, "dist", "plugin-sdk", "root-alias.cjs");
|
||||
const packageRoot = createPackageRoot();
|
||||
const distAliasPath = createDistAliasPath();
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
aliasPath: distAliasPath,
|
||||
distExists: false,
|
||||
monolithicExports: {
|
||||
onDiagnosticEvent: () => () => undefined,
|
||||
slowHelper: () => "loaded",
|
||||
onDiagnosticEvent: (): (() => void) => () => undefined,
|
||||
slowHelper: (): string => "loaded",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -216,15 +228,15 @@ describe("plugin-sdk root alias", () => {
|
||||
});
|
||||
|
||||
it("prefers hashed dist diagnostic events chunks before falling back to src", () => {
|
||||
const packageRoot = path.dirname(path.dirname(rootAliasPath));
|
||||
const distAliasPath = path.join(packageRoot, "dist", "plugin-sdk", "root-alias.cjs");
|
||||
const packageRoot = createPackageRoot();
|
||||
const distAliasPath = createDistAliasPath();
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
aliasPath: distAliasPath,
|
||||
distExists: false,
|
||||
distEntries: ["diagnostic-events-W3Hz61fI.js"],
|
||||
monolithicExports: {
|
||||
r: () => () => undefined,
|
||||
slowHelper: () => "loaded",
|
||||
r: (): (() => void) => () => undefined,
|
||||
slowHelper: (): string => "loaded",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -241,36 +253,42 @@ describe("plugin-sdk root alias", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => {
|
||||
const delegateCompactionToRuntime = () => "delegated";
|
||||
it.each([
|
||||
{
|
||||
name: "forwards delegateCompactionToRuntime through the compat-backed root alias",
|
||||
exportName: "delegateCompactionToRuntime",
|
||||
exportValue: () => "delegated",
|
||||
expectIdentity: true,
|
||||
assertForwarded: (value: unknown) => {
|
||||
expect(typeof value).toBe("function");
|
||||
expect((value as () => string)()).toBe("delegated");
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "forwards onDiagnosticEvent through the compat-backed root alias",
|
||||
exportName: "onDiagnosticEvent",
|
||||
exportValue: () => () => undefined,
|
||||
expectIdentity: false,
|
||||
assertForwarded: (value: unknown) => {
|
||||
expect(typeof value).toBe("function");
|
||||
expect(typeof (value as (listener: () => void) => () => void)(() => undefined)).toBe(
|
||||
"function",
|
||||
);
|
||||
},
|
||||
},
|
||||
])("$name", ({ exportName, exportValue, expectIdentity, assertForwarded }) => {
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
monolithicExports: {
|
||||
delegateCompactionToRuntime,
|
||||
[exportName]: exportValue,
|
||||
},
|
||||
});
|
||||
const lazyRootSdk = lazyModule.moduleExports;
|
||||
const forwarded = lazyModule.moduleExports[exportName];
|
||||
|
||||
expect(typeof lazyRootSdk.delegateCompactionToRuntime).toBe("function");
|
||||
expect(lazyRootSdk.delegateCompactionToRuntime).toBe(delegateCompactionToRuntime);
|
||||
expect("delegateCompactionToRuntime" in lazyRootSdk).toBe(true);
|
||||
});
|
||||
|
||||
it("forwards onDiagnosticEvent through the compat-backed root alias", () => {
|
||||
const onDiagnosticEvent = () => () => undefined;
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
monolithicExports: {
|
||||
onDiagnosticEvent,
|
||||
},
|
||||
});
|
||||
const lazyRootSdk = lazyModule.moduleExports;
|
||||
|
||||
expect(typeof lazyRootSdk.onDiagnosticEvent).toBe("function");
|
||||
expect(
|
||||
typeof (lazyRootSdk.onDiagnosticEvent as (listener: () => void) => () => void)(
|
||||
() => undefined,
|
||||
),
|
||||
).toBe("function");
|
||||
expect("onDiagnosticEvent" in lazyRootSdk).toBe(true);
|
||||
assertForwarded(forwarded);
|
||||
if (expectIdentity) {
|
||||
expect(forwarded).toBe(exportValue);
|
||||
}
|
||||
expect(exportName in lazyModule.moduleExports).toBe(true);
|
||||
});
|
||||
|
||||
it("loads legacy root exports through the merged root wrapper", { timeout: 240_000 }, () => {
|
||||
|
||||
Reference in New Issue
Block a user