mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-09 11:13:58 +00:00
132 lines
4.4 KiB
TypeScript
132 lines
4.4 KiB
TypeScript
// Provides fixtures for plugin auto-enable config tests.
|
|
import path from "node:path";
|
|
import { clearCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js";
|
|
import { resolveInstalledPluginIndexPolicyHash } from "../plugins/installed-plugin-index-policy.js";
|
|
import type { PluginManifestRegistry } from "../plugins/manifest-registry.js";
|
|
import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
|
|
import type { PluginOrigin } from "../plugins/plugin-origin.types.js";
|
|
import { clearPluginSetupRegistryCache } from "../plugins/setup-registry.js";
|
|
import { cleanupTrackedTempDirs, makeTrackedTempDir } from "../plugins/test-helpers/fs-fixtures.js";
|
|
import type { OpenClawConfig } from "./types.openclaw.js";
|
|
|
|
const tempDirs: string[] = [];
|
|
|
|
/** Clears auto-enable plugin caches and temp dirs between tests. */
|
|
export function resetPluginAutoEnableTestState(): void {
|
|
clearCurrentPluginMetadataSnapshot();
|
|
clearPluginSetupRegistryCache();
|
|
cleanupTrackedTempDirs(tempDirs);
|
|
}
|
|
|
|
export function makeTempDir(): string {
|
|
return makeTrackedTempDir("openclaw-plugin-auto-enable", tempDirs);
|
|
}
|
|
|
|
export function makeIsolatedEnv(overrides: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv {
|
|
const rootDir = makeTempDir();
|
|
return {
|
|
OPENCLAW_STATE_DIR: path.join(rootDir, "state"),
|
|
OPENCLAW_BUNDLED_PLUGINS_DIR: path.join(process.cwd(), "extensions"),
|
|
OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1",
|
|
VITEST: "true",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
export function makeRegistry(
|
|
plugins: Array<{
|
|
id: string;
|
|
channels: string[];
|
|
activation?: { onAgentHarnesses?: string[] };
|
|
autoEnableWhenConfiguredProviders?: string[];
|
|
modelSupport?: { modelPrefixes?: string[]; modelPatterns?: string[] };
|
|
contracts?: {
|
|
speechProviders?: string[];
|
|
webSearchProviders?: string[];
|
|
webFetchProviders?: string[];
|
|
tools?: string[];
|
|
};
|
|
providers?: string[];
|
|
cliBackends?: string[];
|
|
origin?: PluginOrigin;
|
|
configSchema?: Record<string, unknown>;
|
|
channelConfigs?: Record<
|
|
string,
|
|
{ schema: Record<string, unknown>; label?: string; preferOver?: string[] }
|
|
>;
|
|
}>,
|
|
): PluginManifestRegistry {
|
|
return {
|
|
plugins: plugins.map((plugin) => ({
|
|
id: plugin.id,
|
|
channels: plugin.channels,
|
|
activation: plugin.activation,
|
|
autoEnableWhenConfiguredProviders: plugin.autoEnableWhenConfiguredProviders,
|
|
modelSupport: plugin.modelSupport,
|
|
contracts: plugin.contracts,
|
|
configSchema: plugin.configSchema,
|
|
channelConfigs: plugin.channelConfigs,
|
|
providers: plugin.providers ?? [],
|
|
cliBackends: plugin.cliBackends ?? [],
|
|
skills: [],
|
|
hooks: [],
|
|
origin: plugin.origin ?? "config",
|
|
rootDir: `/fake/${plugin.id}`,
|
|
source: `/fake/${plugin.id}/index.js`,
|
|
manifestPath: `/fake/${plugin.id}/openclaw.plugin.json`,
|
|
})),
|
|
diagnostics: [],
|
|
};
|
|
}
|
|
|
|
export function createPluginMetadataSnapshot(params: {
|
|
config?: OpenClawConfig;
|
|
manifestRegistry: PluginManifestRegistry;
|
|
workspaceDir?: string;
|
|
}): PluginMetadataSnapshot {
|
|
const policyHash = resolveInstalledPluginIndexPolicyHash(params.config);
|
|
return {
|
|
policyHash,
|
|
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
|
|
index: {
|
|
version: 1,
|
|
hostContractVersion: "test",
|
|
compatRegistryVersion: "test",
|
|
migrationVersion: 1,
|
|
policyHash,
|
|
generatedAtMs: 1,
|
|
installRecords: {},
|
|
plugins: [],
|
|
diagnostics: [],
|
|
},
|
|
registryDiagnostics: [],
|
|
manifestRegistry: params.manifestRegistry,
|
|
plugins: params.manifestRegistry.plugins,
|
|
diagnostics: params.manifestRegistry.diagnostics,
|
|
byPluginId: new Map(params.manifestRegistry.plugins.map((plugin) => [plugin.id, plugin])),
|
|
normalizePluginId: (pluginId) => pluginId,
|
|
owners: {
|
|
channels: new Map(),
|
|
channelConfigs: new Map(),
|
|
providers: new Map(),
|
|
modelCatalogProviders: new Map(),
|
|
cliBackends: new Map(),
|
|
setupProviders: new Map(),
|
|
commandAliases: new Map(),
|
|
contracts: new Map(),
|
|
},
|
|
metrics: {
|
|
registrySnapshotMs: 0,
|
|
manifestRegistryMs: 0,
|
|
ownerMapsMs: 0,
|
|
totalMs: 0,
|
|
indexPluginCount: 0,
|
|
manifestPluginCount: params.manifestRegistry.plugins.length,
|
|
},
|
|
};
|
|
}
|
|
|
|
export function makeApnChannelConfig() {
|
|
return { channels: { apn: { someKey: "value" } } };
|
|
}
|