Skip to content

Commit cf25af4

Browse files
authored
test(plugins): drive plugin tool registry reuse through the real seam (#133656)
#133584 repointed the four failing call-count assertions at `getLoadedRuntimePluginRegistry`, but left the rest of the `./loader.js` scaffolding in place. Every remaining `resolveCompatibleRuntimePluginRegistryMock` setup in this file was inert: #133518 moved `resolveCompatibleRuntimePluginRegistry` into `active-runtime-registry.ts`, where `getLoadedRuntimePluginRegistry` reaches it intra-module, so the `vi.mock("./loader.js")` seam observes nothing. A `mockImplementation` probe on that mock takes zero calls in this file. Six `mockReturnValue` sites therefore claimed to steer the reuse-vs-rebuild decision while steering nothing. They passed only because each test also called `setActivePluginRegistry`, so the real resolver quietly did the work: the tests were green for a reason none of them asserted. Delete the dead scaffolding and drive the same states through the boundary `tools.ts` actually crosses. The counting mock becomes the delegate rather than a side-channel counter, so the resolver's return value is observable: the partial- and stale-root tests now assert that the active registry really was consulted and still rebuilt, and the disabled-owner test pins `requiredPluginIds` to the owner that may enter the runtime scope. `./loader.js` keeps every binding its consumers import, but the registry-resolution ones now delegate to their real owners instead of to a double no production path reaches. Behavior is unchanged; production is untouched. Each revised test was verified to fail when the invariant it names is broken: skipping the active-registry consult in `resolvePluginToolRegistry` (22 red), scoping on plugin records instead of tool entries (the 4 tests whose overrides were removed), dropping the disabled-entry guards in `resolvePluginToolRuntimePluginIds` and `manifest-owner-policy.ts`, and re-consulting the seam on either the reuse or the cold-load branch.
1 parent fbfc46e commit cf25af4

1 file changed

Lines changed: 56 additions & 38 deletions

File tree

‎src/plugins/tools.optional.test.ts‎

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { SecretRef } from "../config/types.secrets.js";
77
import { resetLogger, setLoggerOverride } from "../logging/logger.js";
88
import { loggingState } from "../logging/state.js";
99
import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js";
10+
import type { PluginLoadOptions } from "./loader-types.js";
1011
import { createEmptyPluginRegistry } from "./registry-empty.js";
1112
import { appendRuntimePluginToolGrant } from "./tool-grant-allowlist.js";
1213

@@ -21,39 +22,51 @@ type MockRegistryToolEntry = {
2122
};
2223

2324
const loadOpenClawPluginsMock = vi.fn();
24-
const resolveCompatibleRuntimePluginRegistryMock = vi.fn();
25-
const getLoadedRuntimePluginRegistryMock = vi.fn();
2625
const applyPluginAutoEnableMock = vi.fn();
2726
const loadContextMocks = vi.hoisted(() => ({
2827
actualResolve: undefined as
2928
| typeof import("./runtime/load-context.resolve.js").resolvePluginRuntimeLoadContext
3029
| undefined,
3130
resolve: vi.fn(),
3231
}));
33-
34-
vi.mock("./loader.js", () => ({
35-
loadOpenClawPlugins: (params: unknown) => loadOpenClawPluginsMock(params),
36-
loadPluginRegistryHandle: (params: unknown) => loadOpenClawPluginsMock(params),
37-
resolveCompatibleRuntimePluginRegistry: (params: unknown) =>
38-
resolveCompatibleRuntimePluginRegistryMock(params),
39-
resolvePluginRegistryLoadCacheKey: (params: unknown) => JSON.stringify(params),
40-
resolveRuntimePluginRegistry: (params: unknown) =>
41-
resolveCompatibleRuntimePluginRegistryMock(params),
32+
const activeRegistryMocks = vi.hoisted(() => ({
33+
actualGetLoadedRegistry: undefined as
34+
| typeof import("./active-runtime-registry.js").getLoadedRuntimePluginRegistry
35+
| undefined,
36+
getLoadedRegistry: vi.fn(),
4237
}));
4338

44-
// resolveCompatibleRuntimePluginRegistry now lives in this module, so the
45-
// ./loader.js seam no longer observes the intra-module call that tool resolution
46-
// triggers. Count the boundary tools.ts actually crosses, keeping real behavior.
39+
// Only the load entry points are intercepted. The registry-resolution bindings
40+
// stay wired to their real owners so a consumer that crosses them observes
41+
// production behavior instead of a double that no production path reaches.
42+
vi.mock("./loader.js", async () => {
43+
const [activeRuntimeRegistry, loaderCache] = await Promise.all([
44+
import("./active-runtime-registry.js"),
45+
import("./loader-cache.js"),
46+
]);
47+
const loadPluginRegistryHandle = (params: unknown) => loadOpenClawPluginsMock(params);
48+
return {
49+
loadOpenClawPlugins: loadPluginRegistryHandle,
50+
loadPluginRegistryHandle,
51+
resolveCompatibleRuntimePluginRegistry:
52+
activeRuntimeRegistry.resolveCompatibleRuntimePluginRegistry,
53+
resolvePluginRegistryLoadCacheKey: loaderCache.resolvePluginRegistryLoadCacheKey,
54+
resolveRuntimePluginRegistry: (options?: PluginLoadOptions) =>
55+
activeRuntimeRegistry.resolveCompatibleRuntimePluginRegistry(options) ??
56+
loadPluginRegistryHandle({ ...options, activate: false }),
57+
};
58+
});
59+
60+
// resolvePluginToolRegistry reads the active registry through this module, so
61+
// this is the seam that decides reuse-vs-rebuild. It delegates to the real
62+
// implementation; tests drive it by installing an active registry.
4763
vi.mock("./active-runtime-registry.js", async (importOriginal) => {
4864
const actual = await importOriginal<typeof import("./active-runtime-registry.js")>();
65+
activeRegistryMocks.actualGetLoadedRegistry = actual.getLoadedRuntimePluginRegistry;
4966
return {
5067
...actual,
51-
getLoadedRuntimePluginRegistry: (
52-
params?: Parameters<typeof actual.getLoadedRuntimePluginRegistry>[0],
53-
) => {
54-
getLoadedRuntimePluginRegistryMock(params);
55-
return actual.getLoadedRuntimePluginRegistry(params);
56-
},
68+
getLoadedRuntimePluginRegistry: (...args: unknown[]) =>
69+
activeRegistryMocks.getLoadedRegistry(...args),
5770
};
5871
});
5972

@@ -524,6 +537,10 @@ function mockCallParams(
524537
return call[0] as Record<string, unknown>;
525538
}
526539

540+
function activeRegistryRequiredPluginIds(index = 0): unknown {
541+
return mockCallParams(activeRegistryMocks.getLoadedRegistry, index).requiredPluginIds;
542+
}
543+
527544
function expectLoaderSelectedOnlyPluginIds(expectedPluginIds: readonly string[]) {
528545
const selectedPluginIds = loadOpenClawPluginsMock.mock.calls.map(
529546
([params]) => (params as { onlyPluginIds?: string[] }).onlyPluginIds,
@@ -572,11 +589,13 @@ describe("resolvePluginTools optional tools", () => {
572589

573590
beforeEach(() => {
574591
loadOpenClawPluginsMock.mockReset();
575-
getLoadedRuntimePluginRegistryMock.mockReset();
576-
resolveCompatibleRuntimePluginRegistryMock.mockReset();
577-
resolveCompatibleRuntimePluginRegistryMock.mockImplementation(() =>
578-
getActivePluginRegistry?.(),
579-
);
592+
activeRegistryMocks.getLoadedRegistry.mockReset();
593+
activeRegistryMocks.getLoadedRegistry.mockImplementation((...args: unknown[]) => {
594+
if (!activeRegistryMocks.actualGetLoadedRegistry) {
595+
throw new Error("active-runtime-registry mock was not initialized");
596+
}
597+
return activeRegistryMocks.actualGetLoadedRegistry(...(args as [never]));
598+
});
580599
applyPluginAutoEnableMock.mockReset();
581600
applyPluginAutoEnableMock.mockImplementation(({ config }: { config: unknown }) => ({
582601
config,
@@ -1157,7 +1176,6 @@ describe("resolvePluginTools optional tools", () => {
11571176
"gateway-bindable",
11581177
"/tmp",
11591178
);
1160-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(partialRegistry);
11611179
loadOpenClawPluginsMock.mockReturnValue(fullRegistry);
11621180

11631181
const tools = resolvePluginTools(
@@ -1168,6 +1186,7 @@ describe("resolvePluginTools optional tools", () => {
11681186
);
11691187

11701188
expectResolvedToolNames(tools, ["other_tool", "optional_tool"]);
1189+
expect(activeRegistryMocks.getLoadedRegistry).toHaveReturnedWith(partialRegistry);
11711190
const loaderParams = mockCallParams(loadOpenClawPluginsMock) as {
11721191
activate?: unknown;
11731192
cache?: unknown;
@@ -1245,7 +1264,6 @@ describe("resolvePluginTools optional tools", () => {
12451264
"gateway-bindable",
12461265
"/tmp",
12471266
);
1248-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(staleRegistry);
12491267
loadOpenClawPluginsMock.mockReturnValue(freshRegistry);
12501268

12511269
const tools = resolvePluginTools(
@@ -1256,6 +1274,7 @@ describe("resolvePluginTools optional tools", () => {
12561274
);
12571275

12581276
expectResolvedToolNames(tools, ["other_tool", "optional_tool"]);
1277+
expect(activeRegistryMocks.getLoadedRegistry).toHaveReturnedWith(staleRegistry);
12591278
expect(getActivePluginRegistry?.()).toBe(staleRegistry);
12601279
expectLoaderSelectedOnlyPluginIds(["multi", "optional-demo"]);
12611280
expect(freshRegistry.diagnostics).toStrictEqual([]);
@@ -2855,7 +2874,6 @@ describe("resolvePluginTools optional tools", () => {
28552874
status: "loaded",
28562875
});
28572876
setActivePluginRegistry?.(replacementRegistry as never, "provider-runtime", "default", "/tmp");
2858-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(undefined);
28592877
loadOpenClawPluginsMock.mockReset();
28602878
loadOpenClawPluginsMock
28612879
.mockReturnValueOnce(gatewayRegistry)
@@ -3086,7 +3104,6 @@ describe("resolvePluginTools optional tools", () => {
30863104

30873105
it("reuses a compatible active registry instead of loading again", () => {
30883106
const activeRegistry = createOptionalDemoActiveRegistry();
3089-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(activeRegistry);
30903107

30913108
const tools = resolvePluginTools(
30923109
createResolveToolsParams({
@@ -3095,13 +3112,13 @@ describe("resolvePluginTools optional tools", () => {
30953112
);
30963113

30973114
expectResolvedToolNames(tools, ["optional_tool"]);
3115+
expect(activeRegistryMocks.getLoadedRegistry).toHaveReturnedWith(activeRegistry);
30983116
expect(loadOpenClawPluginsMock).not.toHaveBeenCalled();
30993117
});
31003118

31013119
it("reuses the gateway-bindable registry when it covers the tool runtime scope", () => {
31023120
const activeRegistry = createOptionalDemoActiveRegistry();
31033121
setActivePluginRegistry(activeRegistry as never, "gateway-startup", "gateway-bindable", "/tmp");
3104-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(activeRegistry);
31053122

31063123
const tools = resolvePluginTools(
31073124
createResolveToolsParams({
@@ -3111,7 +3128,8 @@ describe("resolvePluginTools optional tools", () => {
31113128
);
31123129

31133130
expectResolvedToolNames(tools, ["optional_tool"]);
3114-
expect(getLoadedRuntimePluginRegistryMock).toHaveBeenCalledOnce();
3131+
expect(activeRegistryMocks.getLoadedRegistry).toHaveBeenCalledOnce();
3132+
expect(activeRegistryMocks.getLoadedRegistry).toHaveReturnedWith(activeRegistry);
31153133
expect(loadOpenClawPluginsMock).not.toHaveBeenCalled();
31163134
});
31173135

@@ -3139,7 +3157,6 @@ describe("resolvePluginTools optional tools", () => {
31393157
diagnostics: [],
31403158
};
31413159
setActivePluginRegistry(activeRegistry as never, "gateway-startup", "gateway-bindable", "/tmp");
3142-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(undefined);
31433160
loadOpenClawPluginsMock.mockReturnValue(activeRegistry);
31443161

31453162
const tools = resolvePluginTools(
@@ -3151,7 +3168,8 @@ describe("resolvePluginTools optional tools", () => {
31513168

31523169
expectResolvedToolNames(tools, ["optional_tool"]);
31533170
expect(heavyFactory).not.toHaveBeenCalled();
3154-
expect(getLoadedRuntimePluginRegistryMock).toHaveBeenCalledOnce();
3171+
expect(activeRegistryMocks.getLoadedRegistry).toHaveBeenCalledOnce();
3172+
expect(activeRegistryRequiredPluginIds()).toEqual(["optional-demo"]);
31553173
expect(loadOpenClawPluginsMock).not.toHaveBeenCalled();
31563174
});
31573175

@@ -3198,7 +3216,6 @@ describe("resolvePluginTools optional tools", () => {
31983216
diagnostics: [],
31993217
};
32003218
setActivePluginRegistry(activeRegistry as never, "gateway-startup", "gateway-bindable", "/tmp");
3201-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(undefined);
32023219
loadOpenClawPluginsMock.mockReturnValue(activeRegistry);
32033220

32043221
const tools = resolvePluginTools(
@@ -3211,7 +3228,10 @@ describe("resolvePluginTools optional tools", () => {
32113228

32123229
expectResolvedToolNames(tools, ["memory_search", "memory_get"]);
32133230
expect(memorySearchFactory).toHaveBeenCalledTimes(1);
3214-
expect(getLoadedRuntimePluginRegistryMock).toHaveBeenCalledOnce();
3231+
expect(activeRegistryMocks.getLoadedRegistry).toHaveBeenCalledOnce();
3232+
// The disabled owner must never enter the runtime scope: asking the active
3233+
// registry for it would miss and force a pointless cold load.
3234+
expect(activeRegistryRequiredPluginIds()).toEqual(["memory-core"]);
32153235
expect(loadOpenClawPluginsMock).not.toHaveBeenCalled();
32163236
});
32173237

@@ -3268,7 +3288,6 @@ describe("resolvePluginTools optional tools", () => {
32683288
"gateway-bindable",
32693289
"/tmp",
32703290
);
3271-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(undefined);
32723291
loadOpenClawPluginsMock.mockReturnValue(loadedRegistry);
32733292

32743293
const tools = resolvePluginTools(
@@ -3316,7 +3335,6 @@ describe("resolvePluginTools optional tools", () => {
33163335
],
33173336
});
33183337
setActivePluginRegistry(activeRegistry as never, "gateway-startup", "gateway-bindable", "/tmp");
3319-
resolveCompatibleRuntimePluginRegistryMock.mockReturnValue(activeRegistry);
33203338
loadOpenClawPluginsMock.mockReturnValue(createToolRegistry([]));
33213339

33223340
resolvePluginTools({
@@ -3327,7 +3345,7 @@ describe("resolvePluginTools optional tools", () => {
33273345
toolAllowlist: ["*", "tavily"],
33283346
allowGatewaySubagentBinding: true,
33293347
});
3330-
expect(getLoadedRuntimePluginRegistryMock).toHaveBeenCalledOnce();
3348+
expect(activeRegistryMocks.getLoadedRegistry).toHaveBeenCalledOnce();
33313349
const loaderParams = mockCallParams(loadOpenClawPluginsMock) as {
33323350
onlyPluginIds?: string[];
33333351
toolDiscovery?: unknown;

0 commit comments

Comments
 (0)