|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { AnyAgentTool } from "../agents/tools/common.js"; |
| 3 | +import { setPluginToolMeta } from "../plugins/tools.js"; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + createBundleMcpToolRuntime: vi.fn(), |
| 7 | + createOpenClawCodingTools: vi.fn(), |
| 8 | + disposeBundleRuntime: vi.fn(), |
| 9 | + loadModelCatalog: vi.fn(async (): Promise<Array<Record<string, unknown>>> => []), |
| 10 | + normalizeProviderToolSchemasWithPlugin: vi.fn(), |
| 11 | + resolveDefaultModelForAgent: vi.fn(() => ({ provider: "openai", model: "gpt-5.5" })), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../agents/model-catalog.js", () => ({ |
| 15 | + findModelInCatalog: ( |
| 16 | + catalog: Array<{ provider?: string; id?: string }>, |
| 17 | + provider: string, |
| 18 | + modelId: string, |
| 19 | + ) => catalog.find((entry) => entry.provider === provider && entry.id === modelId), |
| 20 | + loadModelCatalog: mocks.loadModelCatalog, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("../agents/model-selection.js", async (importOriginal) => ({ |
| 24 | + ...(await importOriginal<typeof import("../agents/model-selection.js")>()), |
| 25 | + resolveDefaultModelForAgent: mocks.resolveDefaultModelForAgent, |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("../agents/agent-bundle-mcp-tools.js", () => ({ |
| 29 | + createBundleMcpToolRuntime: mocks.createBundleMcpToolRuntime, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock("../agents/agent-tools.js", () => ({ |
| 33 | + createOpenClawCodingTools: mocks.createOpenClawCodingTools, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("../plugins/provider-runtime.js", () => ({ |
| 37 | + inspectProviderToolSchemasWithPlugin: () => [], |
| 38 | + normalizeProviderToolSchemasWithPlugin: mocks.normalizeProviderToolSchemasWithPlugin, |
| 39 | +})); |
| 40 | + |
| 41 | +const { collectRuntimeToolSchemaFindings } = await import("./doctor-core-checks.runtime.js"); |
| 42 | + |
| 43 | +function tool(name: string, parameters: unknown): AnyAgentTool { |
| 44 | + return { |
| 45 | + name, |
| 46 | + label: name, |
| 47 | + description: name, |
| 48 | + parameters, |
| 49 | + execute: async () => ({ text: "ok" }), |
| 50 | + } as unknown as AnyAgentTool; |
| 51 | +} |
| 52 | + |
| 53 | +function bundleMcpTool(name: string, parameters: unknown): AnyAgentTool { |
| 54 | + const entry = tool(name, parameters); |
| 55 | + setPluginToolMeta(entry, { pluginId: "bundle-mcp", optional: false }); |
| 56 | + return entry; |
| 57 | +} |
| 58 | + |
| 59 | +describe("doctor runtime tool schema error handling", () => { |
| 60 | + beforeEach(() => { |
| 61 | + mocks.createOpenClawCodingTools.mockReset().mockReturnValue([]); |
| 62 | + mocks.createBundleMcpToolRuntime.mockReset().mockResolvedValue({ |
| 63 | + tools: [], |
| 64 | + dispose: mocks.disposeBundleRuntime, |
| 65 | + }); |
| 66 | + mocks.disposeBundleRuntime.mockReset().mockResolvedValue(undefined); |
| 67 | + mocks.loadModelCatalog.mockClear(); |
| 68 | + mocks.normalizeProviderToolSchemasWithPlugin |
| 69 | + .mockReset() |
| 70 | + .mockImplementation(({ context }) => context.tools); |
| 71 | + mocks.resolveDefaultModelForAgent.mockClear(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("reports agent runtime tool construction failures without aborting schema checks", async () => { |
| 75 | + mocks.createOpenClawCodingTools.mockImplementationOnce(() => { |
| 76 | + throw new Error("fuzzplugin startup failed"); |
| 77 | + }); |
| 78 | + |
| 79 | + await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({ |
| 80 | + checkId: "core/doctor/runtime-tool-schemas", |
| 81 | + severity: "error", |
| 82 | + message: "Agent main runtime tool schema validation could not load the runtime tool set.", |
| 83 | + path: "agents.main.tools", |
| 84 | + requirement: "fuzzplugin startup failed", |
| 85 | + fixHint: |
| 86 | + "Fix provider/plugin tool loading errors, then rerun doctor before relying on assistant tool startup.", |
| 87 | + }); |
| 88 | + expect(mocks.createBundleMcpToolRuntime).toHaveBeenCalledTimes(1); |
| 89 | + expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("reports agent runtime tool normalization failures without aborting doctor", async () => { |
| 93 | + mocks.createOpenClawCodingTools.mockReturnValueOnce([ |
| 94 | + tool("fuzzplugin_move_angles", { type: "object", properties: {} }), |
| 95 | + ]); |
| 96 | + mocks.normalizeProviderToolSchemasWithPlugin.mockImplementation(({ context }) => { |
| 97 | + const tools = context.tools as AnyAgentTool[]; |
| 98 | + if (tools.some((entry) => entry.name === "fuzzplugin_move_angles")) { |
| 99 | + throw new Error("fuzzplugin schema normalization failed"); |
| 100 | + } |
| 101 | + return tools; |
| 102 | + }); |
| 103 | + |
| 104 | + await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({ |
| 105 | + checkId: "core/doctor/runtime-tool-schemas", |
| 106 | + severity: "error", |
| 107 | + message: |
| 108 | + "Agent main runtime tool schema validation could not normalize the runtime tool set.", |
| 109 | + path: "agents.main.tools", |
| 110 | + requirement: "fuzzplugin schema normalization failed", |
| 111 | + fixHint: |
| 112 | + "Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.", |
| 113 | + }); |
| 114 | + expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1); |
| 115 | + }); |
| 116 | + |
| 117 | + it("reports bundle MCP runtime tool normalization failures without aborting doctor", async () => { |
| 118 | + mocks.createBundleMcpToolRuntime.mockResolvedValueOnce({ |
| 119 | + tools: [bundleMcpTool("fuzzplugin__move_angles", { type: "object", properties: {} })], |
| 120 | + dispose: mocks.disposeBundleRuntime, |
| 121 | + }); |
| 122 | + mocks.normalizeProviderToolSchemasWithPlugin.mockImplementation(({ context }) => { |
| 123 | + const tools = context.tools as AnyAgentTool[]; |
| 124 | + if (tools.some((entry) => entry.name === "fuzzplugin__move_angles")) { |
| 125 | + throw new Error("fuzzplugin MCP schema normalization failed"); |
| 126 | + } |
| 127 | + return tools; |
| 128 | + }); |
| 129 | + |
| 130 | + await expect(collectRuntimeToolSchemaFindings({})).resolves.toContainEqual({ |
| 131 | + checkId: "core/doctor/runtime-tool-schemas", |
| 132 | + severity: "error", |
| 133 | + message: "Configured MCP tool schema validation could not normalize the runtime tool set.", |
| 134 | + path: "mcp.servers", |
| 135 | + requirement: "fuzzplugin MCP schema normalization failed", |
| 136 | + fixHint: |
| 137 | + "Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup.", |
| 138 | + }); |
| 139 | + expect(mocks.disposeBundleRuntime).toHaveBeenCalledTimes(1); |
| 140 | + }); |
| 141 | +}); |
0 commit comments