Skip to content

Commit 95c0469

Browse files
committed
test: consolidate model and metadata fixtures
1 parent ee0d376 commit 95c0469

4 files changed

Lines changed: 103 additions & 193 deletions

‎src/agents/model-selection.plugin-runtime.test.ts‎

Lines changed: 35 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
33

44
const normalizeProviderModelIdWithPluginMock = vi.fn();
5+
6+
function normalizeLegacyFixtureModel({
7+
provider,
8+
context,
9+
}: {
10+
provider: string;
11+
context: { modelId?: string };
12+
}) {
13+
return provider === "custom-provider" && context.modelId === "custom-legacy-model"
14+
? "custom-modern-model"
15+
: undefined;
16+
}
17+
518
const emptyPluginMetadataSnapshot = vi.hoisted(() => ({
619
configFingerprint: "model-selection-plugin-runtime-test-empty-plugin-metadata",
720
plugins: [
@@ -57,15 +70,7 @@ describe("model-selection plugin runtime normalization", () => {
5770
});
5871

5972
it("delegates provider-owned model id normalization to plugin runtime hooks", async () => {
60-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) => {
61-
if (
62-
provider === "custom-provider" &&
63-
(context as { modelId?: string }).modelId === "custom-legacy-model"
64-
) {
65-
return "custom-modern-model";
66-
}
67-
return undefined;
68-
});
73+
normalizeProviderModelIdWithPluginMock.mockImplementation(normalizeLegacyFixtureModel);
6974

7075
const { parseModelRef } = await import("./model-selection.js");
7176

@@ -97,15 +102,7 @@ describe("model-selection plugin runtime normalization", () => {
97102
});
98103

99104
it("keeps provider plugin normalization when inferring provider for bare defaults", async () => {
100-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) => {
101-
if (
102-
provider === "custom-provider" &&
103-
(context as { modelId?: string }).modelId === "custom-legacy-model"
104-
) {
105-
return "custom-modern-model";
106-
}
107-
return undefined;
108-
});
105+
normalizeProviderModelIdWithPluginMock.mockImplementation(normalizeLegacyFixtureModel);
109106

110107
const { resolveConfiguredModelRef } = await import("./model-selection.js");
111108

@@ -130,86 +127,39 @@ describe("model-selection plugin runtime normalization", () => {
130127
});
131128
});
132129

133-
it("keeps model visibility policy construction off plugin runtime hooks by default", async () => {
134-
// Visibility policy is a hot/static path. It preserves configured keys
135-
// unless callers explicitly opt into runtime plugin normalization.
136-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) => {
137-
if (
138-
provider === "custom-provider" &&
139-
(context as { modelId?: string }).modelId === "custom-legacy-model"
140-
) {
141-
return "custom-modern-model";
142-
}
143-
return undefined;
144-
});
145-
130+
it.each([
131+
["keeps model visibility policy construction off plugin runtime hooks by default", undefined],
132+
[
133+
"propagates explicit plugin runtime normalization opt-in through model visibility policy",
134+
true,
135+
],
136+
] as const)("%s", async (_name, allowPluginNormalization) => {
137+
normalizeProviderModelIdWithPluginMock.mockImplementation(normalizeLegacyFixtureModel);
146138
const { createModelVisibilityPolicy } = await import("./model-visibility-policy.js");
147-
148139
const policy = createModelVisibilityPolicy({
149140
cfg: {
150-
agents: {
151-
defaults: {
152-
models: {
153-
"custom-provider/custom-legacy-model": {},
154-
},
155-
},
156-
},
141+
agents: { defaults: { models: { "custom-provider/custom-legacy-model": {} } } },
157142
},
158143
catalog: [],
159144
defaultProvider: "custom-provider",
160145
defaultModel: "custom-legacy-model",
146+
...(allowPluginNormalization ? { allowPluginNormalization } : {}),
161147
});
162148

163-
expect(policy.allowedKeys.has("custom-provider/custom-legacy-model")).toBe(true);
164-
expect(policy.allowedKeys.has("custom-provider/custom-modern-model")).toBe(false);
165-
expect(normalizeProviderModelIdWithPluginMock).not.toHaveBeenCalled();
166-
});
167-
168-
it("propagates explicit plugin runtime normalization opt-in through model visibility policy", async () => {
169-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) => {
170-
if (
171-
provider === "custom-provider" &&
172-
(context as { modelId?: string }).modelId === "custom-legacy-model"
173-
) {
174-
return "custom-modern-model";
175-
}
176-
return undefined;
177-
});
178-
179-
const { createModelVisibilityPolicy } = await import("./model-visibility-policy.js");
180-
181-
const policy = createModelVisibilityPolicy({
182-
cfg: {
183-
agents: {
184-
defaults: {
185-
models: {
186-
"custom-provider/custom-legacy-model": {},
187-
},
188-
},
189-
},
190-
},
191-
catalog: [],
192-
defaultProvider: "custom-provider",
193-
defaultModel: "custom-legacy-model",
194-
allowPluginNormalization: true,
195-
});
196-
197-
expect(policy.allowedKeys.has("custom-provider/custom-modern-model")).toBe(true);
198-
expect(normalizeProviderModelIdWithPluginMock).toHaveBeenCalled();
149+
if (allowPluginNormalization) {
150+
expect(policy.allowedKeys.has("custom-provider/custom-modern-model")).toBe(true);
151+
expect(normalizeProviderModelIdWithPluginMock).toHaveBeenCalled();
152+
} else {
153+
expect(policy.allowedKeys.has("custom-provider/custom-legacy-model")).toBe(true);
154+
expect(policy.allowedKeys.has("custom-provider/custom-modern-model")).toBe(false);
155+
expect(normalizeProviderModelIdWithPluginMock).not.toHaveBeenCalled();
156+
}
199157
});
200158

201159
it("keeps plugin-normalized stored overrides allowed in auto-reply runtime selection", async () => {
202160
// Stored session overrides are runtime inputs, so provider-owned
203161
// normalization keeps old persisted ids usable without resetting them.
204-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) => {
205-
if (
206-
provider === "custom-provider" &&
207-
(context as { modelId?: string }).modelId === "custom-legacy-model"
208-
) {
209-
return "custom-modern-model";
210-
}
211-
return undefined;
212-
});
162+
normalizeProviderModelIdWithPluginMock.mockImplementation(normalizeLegacyFixtureModel);
213163

214164
const cfg = {
215165
agents: {
@@ -268,12 +218,7 @@ describe("model-selection plugin runtime normalization", () => {
268218
});
269219

270220
it("normalizes raw persisted overrides through plugin runtime hooks", () => {
271-
normalizeProviderModelIdWithPluginMock.mockImplementation(({ provider, context }) =>
272-
provider === "custom-provider" &&
273-
(context as { modelId?: string }).modelId === "custom-legacy-model"
274-
? "custom-modern-model"
275-
: undefined,
276-
);
221+
normalizeProviderModelIdWithPluginMock.mockImplementation(normalizeLegacyFixtureModel);
277222

278223
expect(
279224
resolveSessionModelRef(

‎src/plugins/current-plugin-metadata.test-support.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { adoptCurrentPluginMetadataSnapshotIfAbsent } from "./current-plugin-metadata-snapshot.js";
22
import { clearCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-state.js";
3+
import type { InstalledPluginIndex } from "./installed-plugin-index.js";
4+
import type { PluginManifestRecord, PluginManifestRegistry } from "./manifest-registry.js";
35
import type { PluginMetadataSnapshot } from "./plugin-metadata-snapshot.types.js";
46

57
/** Replaces a test fixture through the operation lifecycle's clear and adopt boundaries. */
@@ -12,3 +14,51 @@ export function setCurrentPluginMetadataSnapshot(
1214
adoptCurrentPluginMetadataSnapshotIfAbsent(snapshot, options);
1315
}
1416
}
17+
18+
export function makePluginMetadataIndex(pluginId = "demo"): InstalledPluginIndex {
19+
const rootDir = `/plugins/${pluginId}`;
20+
return {
21+
version: 1,
22+
hostContractVersion: "test",
23+
compatRegistryVersion: "test",
24+
migrationVersion: 1,
25+
policyHash: "test",
26+
generatedAtMs: 1,
27+
installRecords: {},
28+
diagnostics: [],
29+
plugins: [
30+
{
31+
pluginId,
32+
manifestPath: `${rootDir}/openclaw.plugin.json`,
33+
manifestHash: `${pluginId}-manifest`,
34+
rootDir,
35+
origin: "global",
36+
enabled: true,
37+
startup: {
38+
sidecar: false,
39+
memory: false,
40+
agentHarnesses: [],
41+
},
42+
compat: [],
43+
},
44+
],
45+
};
46+
}
47+
48+
export function makePluginMetadataManifestRegistry(pluginId = "demo"): PluginManifestRegistry {
49+
const plugin: PluginManifestRecord = {
50+
id: pluginId,
51+
name: pluginId,
52+
channels: [],
53+
providers: [pluginId],
54+
cliBackends: [],
55+
skills: [],
56+
hooks: [],
57+
commandAliases: [{ name: `${pluginId}-command` }],
58+
rootDir: `/plugins/${pluginId}`,
59+
source: `/plugins/${pluginId}/index.js`,
60+
manifestPath: `/plugins/${pluginId}/openclaw.plugin.json`,
61+
origin: "global",
62+
};
63+
return { plugins: [plugin], diagnostics: [] };
64+
}

‎src/plugins/plugin-metadata-snapshot.test.ts‎

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import {
77
withPluginMetadataSnapshotScope,
88
} from "./current-plugin-metadata-snapshot.js";
99
import { getCurrentPluginMetadataSnapshotState } from "./current-plugin-metadata-state.js";
10-
import { setCurrentPluginMetadataSnapshot } from "./current-plugin-metadata.test-support.js";
10+
import {
11+
makePluginMetadataIndex as makeIndex,
12+
makePluginMetadataManifestRegistry as makeManifestRegistry,
13+
setCurrentPluginMetadataSnapshot,
14+
} from "./current-plugin-metadata.test-support.js";
1115
import type { PluginDiscoveryResult } from "./discovery.js";
1216
import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js";
13-
import type { InstalledPluginIndex } from "./installed-plugin-index.js";
14-
import {
15-
loadPluginManifestRegistryCore,
16-
type PluginManifestRecord,
17-
type PluginManifestRegistry,
18-
} from "./manifest-registry.js";
17+
import { loadPluginManifestRegistryCore } from "./manifest-registry.js";
1918
import {
2019
createPluginCache,
2120
getPluginCache,
@@ -58,54 +57,6 @@ vi.mock("./manifest-registry-installed.js", async (importOriginal) => {
5857
};
5958
});
6059

61-
function makeIndex(pluginId = "demo"): InstalledPluginIndex {
62-
const rootDir = `/plugins/${pluginId}`;
63-
return {
64-
version: 1,
65-
hostContractVersion: "test",
66-
compatRegistryVersion: "test",
67-
migrationVersion: 1,
68-
policyHash: "test",
69-
generatedAtMs: 1,
70-
installRecords: {},
71-
diagnostics: [],
72-
plugins: [
73-
{
74-
pluginId,
75-
manifestPath: `${rootDir}/openclaw.plugin.json`,
76-
manifestHash: `${pluginId}-manifest`,
77-
rootDir,
78-
origin: "global",
79-
enabled: true,
80-
startup: {
81-
sidecar: false,
82-
memory: false,
83-
agentHarnesses: [],
84-
},
85-
compat: [],
86-
},
87-
],
88-
};
89-
}
90-
91-
function makeManifestRegistry(pluginId = "demo"): PluginManifestRegistry {
92-
const plugin: PluginManifestRecord = {
93-
id: pluginId,
94-
name: pluginId,
95-
channels: [],
96-
providers: [pluginId],
97-
cliBackends: [],
98-
skills: [],
99-
hooks: [],
100-
commandAliases: [{ name: `${pluginId}-command` }],
101-
rootDir: `/plugins/${pluginId}`,
102-
source: `/plugins/${pluginId}/index.js`,
103-
manifestPath: `/plugins/${pluginId}/openclaw.plugin.json`,
104-
origin: "global",
105-
};
106-
return { plugins: [plugin], diagnostics: [] };
107-
}
108-
10960
describe("plugin metadata snapshot", () => {
11061
beforeEach(() => {
11162
loadPluginRegistrySnapshotWithMetadata.mockReset();

‎src/plugins/providers.runtime.consult-current-snapshot.test.ts‎

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Verifies provider runtime uses current plugin metadata snapshots.
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import type { OpenClawConfig } from "../config/types.openclaw.js";
4-
import { setCurrentPluginMetadataSnapshot } from "./current-plugin-metadata.test-support.js";
4+
import {
5+
makePluginMetadataIndex as makeIndex,
6+
makePluginMetadataManifestRegistry,
7+
setCurrentPluginMetadataSnapshot,
8+
} from "./current-plugin-metadata.test-support.js";
59
import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js";
6-
import type { InstalledPluginIndex } from "./installed-plugin-index.js";
7-
import type { PluginManifestRecord, PluginManifestRegistry } from "./manifest-registry.js";
10+
import type { PluginManifestRegistry } from "./manifest-registry.js";
811
import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js";
912
import { loadPluginMetadataSnapshot } from "./plugin-metadata-snapshot.js";
1013
import { resetPluginRuntimeStateForTest } from "./runtime.js";
@@ -38,52 +41,13 @@ import { isPluginProvidersLoadInFlight, resolvePluginProvidersCore } from "./pro
3841

3942
const WORKSPACE = "/workspace/a";
4043

41-
function makeIndex(pluginId = "demo"): InstalledPluginIndex {
42-
const rootDir = `/plugins/${pluginId}`;
43-
return {
44-
version: 1,
45-
hostContractVersion: "test",
46-
compatRegistryVersion: "test",
47-
migrationVersion: 1,
48-
policyHash: "test",
49-
generatedAtMs: 1,
50-
installRecords: {},
51-
diagnostics: [],
52-
plugins: [
53-
{
54-
pluginId,
55-
manifestPath: `${rootDir}/openclaw.plugin.json`,
56-
manifestHash: `${pluginId}-manifest`,
57-
rootDir,
58-
origin: "global",
59-
enabled: true,
60-
startup: {
61-
sidecar: false,
62-
memory: false,
63-
agentHarnesses: [],
64-
},
65-
compat: [],
66-
},
67-
],
68-
};
69-
}
70-
7144
function makeManifestRegistry(pluginId = "demo"): PluginManifestRegistry {
72-
const plugin: PluginManifestRecord = {
73-
id: pluginId,
74-
name: pluginId,
75-
channels: [],
76-
providers: [pluginId],
77-
cliBackends: [],
78-
skills: [],
79-
hooks: [],
80-
commandAliases: [],
81-
rootDir: `/plugins/${pluginId}`,
82-
source: `/plugins/${pluginId}/index.js`,
83-
manifestPath: `/plugins/${pluginId}/openclaw.plugin.json`,
84-
origin: "global",
85-
};
86-
return { plugins: [plugin], diagnostics: [] };
45+
const registry = makePluginMetadataManifestRegistry(pluginId);
46+
// Provider fixtures intentionally declare no command aliases.
47+
for (const plugin of registry.plugins) {
48+
plugin.commandAliases = [];
49+
}
50+
return registry;
8751
}
8852

8953
// Build a snapshot from a provided index (no disk) and register it as the

0 commit comments

Comments
 (0)