perf: speed contract test imports

This commit is contained in:
Peter Steinberger
2026-04-29 05:37:05 +01:00
parent 3aadeba93f
commit 02c4249632
6 changed files with 59 additions and 48 deletions

View File

@@ -1,9 +1,9 @@
import { readdirSync, readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { basename, dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { GUARDED_EXTENSION_PUBLIC_SURFACE_BASENAMES } from "openclaw/plugin-sdk/plugin-test-contracts";
import { describe, expect, it } from "vitest";
import { classifyBundledExtensionSourcePath } from "../../../../scripts/lib/extension-source-classifier.mjs";
import { GUARDED_EXTENSION_PUBLIC_SURFACE_BASENAMES } from "../../../plugin-sdk/test-helpers/public-artifacts.js";
import { loadPluginManifestRegistry } from "../../../plugins/manifest-registry.js";
const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
@@ -15,7 +15,9 @@ const bundledPluginRecords = loadPluginManifestRegistry({
config: {},
}).plugins.filter((plugin) => plugin.origin === "bundled");
const bundledPluginRoots = new Map(
bundledPluginRecords.map((plugin) => [plugin.id, plugin.rootDir] as const),
bundledPluginRecords.map(
(plugin) => [plugin.id, resolveBundledPluginSourceRoot(plugin.rootDir)] as const,
),
);
const BUNDLED_EXTENSION_IDS = [...bundledPluginRoots.keys()].toSorted(
(left, right) => right.length - left.length,
@@ -44,6 +46,12 @@ const GUARDED_CHANNEL_EXTENSIONS = new Set([
"zalo",
"zalouser",
]);
function resolveBundledPluginSourceRoot(rootDir: string): string {
const sourceRoot = resolve(REPO_ROOT, BUNDLED_PLUGIN_ROOT_DIR, basename(rootDir));
return existsSync(sourceRoot) ? sourceRoot : rootDir;
}
function bundledPluginFile(pluginId: string, relativePath: string): string {
const rootDir = bundledPluginRoots.get(pluginId);
if (!rootDir) {
@@ -343,7 +351,7 @@ function collectExtensionSourceFiles(): string[] {
}
extensionSourceFilesCache = bundledPluginRecords.flatMap((plugin) =>
collectSourceFiles(undefined, {
rootDir: plugin.rootDir,
rootDir: resolveBundledPluginSourceRoot(plugin.rootDir),
shouldSkipEntry: ({ entryName, normalizedFullPath }) =>
classifyBundledExtensionSourcePath(normalizedFullPath).isTestLike ||
entryName === "api.ts" ||

View File

@@ -8,50 +8,11 @@ import {
type PluginRecord,
type PluginRuntime,
} from "../testing.js";
export { assertNoImportTimeSideEffects } from "./import-side-effects.js";
import { uniqueSortedStrings } from "./string-utils.js";
export { registerProviders, requireProvider, uniqueSortedStrings };
function formatImportSideEffectCall(args: readonly unknown[]): string {
if (args.length === 0) {
return "(no args)";
}
return args
.map((arg) => {
try {
return JSON.stringify(arg);
} catch {
return String(arg);
}
})
.join(", ");
}
export function assertNoImportTimeSideEffects(params: {
moduleId: string;
forbiddenSeam: string;
calls: readonly (readonly unknown[])[];
why: string;
fixHint: string;
}) {
if (params.calls.length === 0) {
return;
}
const observedCalls = params.calls
.slice(0, 3)
.map((call, index) => ` ${index + 1}. ${formatImportSideEffectCall(call)}`)
.join("\n");
throw new Error(
[
`[runtime contract] ${params.moduleId} touched ${params.forbiddenSeam} during module import.`,
`why this is banned: ${params.why}`,
`expected fix: ${params.fixHint}`,
`observed calls (${params.calls.length}):`,
observedCalls,
].join("\n"),
);
}
export function createPluginRegistryFixture(config = {} as OpenClawConfig) {
return {
config,

View File

@@ -0,0 +1,39 @@
function formatImportSideEffectCall(args: readonly unknown[]): string {
if (args.length === 0) {
return "(no args)";
}
return args
.map((arg) => {
try {
return JSON.stringify(arg);
} catch {
return String(arg);
}
})
.join(", ");
}
export function assertNoImportTimeSideEffects(params: {
moduleId: string;
forbiddenSeam: string;
calls: readonly (readonly unknown[])[];
why: string;
fixHint: string;
}) {
if (params.calls.length === 0) {
return;
}
const observedCalls = params.calls
.slice(0, 3)
.map((call, index) => ` ${index + 1}. ${formatImportSideEffectCall(call)}`)
.join("\n");
throw new Error(
[
`[runtime contract] ${params.moduleId} touched ${params.forbiddenSeam} during module import.`,
`why this is banned: ${params.why}`,
`expected fix: ${params.fixHint}`,
`observed calls (${params.calls.length}):`,
observedCalls,
].join("\n"),
);
}

View File

@@ -1,4 +1,7 @@
import { assertUniqueValues, BUNDLED_RUNTIME_SIDECAR_PATHS } from "../testing.js";
import {
assertUniqueValues,
BUNDLED_RUNTIME_SIDECAR_PATHS,
} from "../../plugins/runtime-sidecar-paths.js";
export function getPublicArtifactBasename(relativePath: string): string {
return relativePath.split("/").at(-1) ?? relativePath;

View File

@@ -1,5 +1,5 @@
import { assertNoImportTimeSideEffects } from "openclaw/plugin-sdk/plugin-test-contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { assertNoImportTimeSideEffects } from "../../plugin-sdk/test-helpers/import-side-effects.js";
const listChannelPlugins = vi.hoisted(() =>
vi.fn(() => [

View File

@@ -1,8 +1,8 @@
import fs from "node:fs";
import path from "node:path";
import { GUARDED_EXTENSION_PUBLIC_SURFACE_BASENAMES } from "openclaw/plugin-sdk/plugin-test-contracts";
import { BUNDLED_PLUGIN_PATH_PREFIX } from "openclaw/plugin-sdk/test-fixtures";
import { describe, expect, it } from "vitest";
import { GUARDED_EXTENSION_PUBLIC_SURFACE_BASENAMES } from "../src/plugin-sdk/test-helpers/public-artifacts.js";
const repoRoot = path.resolve(import.meta.dirname, "..");
const ALLOWED_EXTENSION_PUBLIC_SURFACE_BASENAMES = new Set(