mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 14:50:21 +00:00
test: guard loader fixtures against broad sdk imports
This commit is contained in:
45
src/plugins/loader-sdk-import-guardrails.test.ts
Normal file
45
src/plugins/loader-sdk-import-guardrails.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const ALLOWED_PLUGIN_SDK_FIXTURE_IMPORTS = new Set([
|
||||
// Intentional legacy SDK-root compatibility smoke tests.
|
||||
'src/plugins/loader.test.ts:configSchema: (require("openclaw/plugin-sdk").emptyPluginConfigSchema)(),',
|
||||
'src/plugins/loader.test.ts:const { onDiagnosticEvent } = require("openclaw/plugin-sdk");',
|
||||
// Intentional jiti alias regression test.
|
||||
'src/plugins/loader.git-path-regression.test.ts:`import { resolveOutboundSendDep } from "openclaw/plugin-sdk/infra-runtime";',
|
||||
'src/plugins/loader.git-path-regression.test.ts: "openclaw/plugin-sdk/infra-runtime": ${JSON.stringify(copiedChannelRuntimeShim)},',
|
||||
]);
|
||||
|
||||
const LOADER_FIXTURE_TEST_FILES = [
|
||||
"src/plugins/loader.cli-metadata.test.ts",
|
||||
"src/plugins/loader.git-path-regression.test.ts",
|
||||
"src/plugins/loader.test.ts",
|
||||
];
|
||||
|
||||
function findLoaderFixtureSdkImports(): string[] {
|
||||
const repoRoot = process.cwd();
|
||||
const matches: string[] = [];
|
||||
for (const file of LOADER_FIXTURE_TEST_FILES) {
|
||||
const source = fs.readFileSync(path.join(repoRoot, file), "utf-8");
|
||||
for (const line of source.split("\n")) {
|
||||
if (
|
||||
line.includes('require("openclaw/plugin-sdk') ||
|
||||
(line.includes("import ") && line.includes('"openclaw/plugin-sdk'))
|
||||
) {
|
||||
matches.push(`${file}:${line.trim()}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
describe("plugin loader fixture SDK imports", () => {
|
||||
it("keeps generated jiti plugin fixtures off the SDK except explicit compatibility smokes", () => {
|
||||
const unexpected = findLoaderFixtureSdkImports().filter(
|
||||
(entry) => !ALLOWED_PLUGIN_SDK_FIXTURE_IMPORTS.has(entry),
|
||||
);
|
||||
|
||||
expect(unexpected).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { loadOpenClawPluginCliRegistry, loadOpenClawPlugins } from "./loader.js"
|
||||
import {
|
||||
cleanupPluginLoaderFixturesForTest,
|
||||
EMPTY_PLUGIN_SCHEMA,
|
||||
inlineChannelPluginEntryFactorySource,
|
||||
makeTempDir,
|
||||
resetPluginLoaderTestStateForTest,
|
||||
useNoBundledPlugins,
|
||||
@@ -19,33 +20,6 @@ afterAll(() => {
|
||||
cleanupPluginLoaderFixturesForTest();
|
||||
});
|
||||
|
||||
function inlineChannelPluginEntryFactorySource(): string {
|
||||
return `function defineChannelPluginEntry(options) {
|
||||
return {
|
||||
id: options.id,
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
configSchema: { schema: { type: "object" } },
|
||||
channelPlugin: options.plugin,
|
||||
setChannelRuntime: options.setRuntime,
|
||||
register(api) {
|
||||
if (api.registrationMode === "cli-metadata") {
|
||||
options.registerCliMetadata?.(api);
|
||||
return;
|
||||
}
|
||||
options.setRuntime?.(api.runtime);
|
||||
api.registerChannel({ plugin: options.plugin });
|
||||
if (api.registrationMode !== "full") {
|
||||
return;
|
||||
}
|
||||
options.registerCliMetadata?.(api);
|
||||
options.registerFull?.(api);
|
||||
},
|
||||
};
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
describe("plugin loader CLI metadata", () => {
|
||||
it("suppresses trust warning logs during CLI metadata loads", async () => {
|
||||
useNoBundledPlugins();
|
||||
|
||||
@@ -40,6 +40,33 @@ export const EMPTY_PLUGIN_SCHEMA = {
|
||||
properties: {},
|
||||
};
|
||||
|
||||
export function inlineChannelPluginEntryFactorySource(): string {
|
||||
return `function defineChannelPluginEntry(options) {
|
||||
return {
|
||||
id: options.id,
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
configSchema: { schema: { type: "object" } },
|
||||
channelPlugin: options.plugin,
|
||||
setChannelRuntime: options.setRuntime,
|
||||
register(api) {
|
||||
if (api.registrationMode === "cli-metadata") {
|
||||
options.registerCliMetadata?.(api);
|
||||
return;
|
||||
}
|
||||
options.setRuntime?.(api.runtime);
|
||||
api.registerChannel({ plugin: options.plugin });
|
||||
if (api.registrationMode !== "full") {
|
||||
return;
|
||||
}
|
||||
options.registerCliMetadata?.(api);
|
||||
options.registerFull?.(api);
|
||||
},
|
||||
};
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
export function makeTempDir() {
|
||||
const dir = path.join(fixtureRoot, `case-${tempDirIndex++}`);
|
||||
mkdirSafe(dir);
|
||||
|
||||
Reference in New Issue
Block a user