mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:40:43 +00:00
perf: speed contract test imports
This commit is contained in:
@@ -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" ||
|
||||
|
||||
@@ -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,
|
||||
|
||||
39
src/plugin-sdk/test-helpers/import-side-effects.ts
Normal file
39
src/plugin-sdk/test-helpers/import-side-effects.ts
Normal 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"),
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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(() => [
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user