|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import type { ToolResult } from "../../src/utils/response-formatter.js"; |
| 3 | +import type { SchemaEntry } from "../../src/data/schemas/index.js"; |
| 4 | +import { registerSchemaTool } from "../../src/tools/harness-schema.js"; |
| 5 | + |
| 6 | +function entry(schema: Record<string, any>): SchemaEntry { |
| 7 | + return { schema, description: "test", group: "test" }; |
| 8 | +} |
| 9 | + |
| 10 | +function makeMcpServer() { |
| 11 | + const tools = new Map<string, { handler: (...args: unknown[]) => Promise<ToolResult> }>(); |
| 12 | + return { |
| 13 | + registerTool: vi.fn((name: string, _schema: unknown, handler: (...args: unknown[]) => Promise<ToolResult>) => { |
| 14 | + tools.set(name, { handler }); |
| 15 | + }), |
| 16 | + async call(name: string, args: Record<string, unknown>): Promise<ToolResult> { |
| 17 | + const tool = tools.get(name); |
| 18 | + if (!tool) throw new Error(`Tool "${name}" not registered`); |
| 19 | + const extra = { signal: new AbortController().signal, sendNotification: vi.fn(), _meta: {} }; |
| 20 | + return tool.handler(args, extra) as Promise<ToolResult>; |
| 21 | + }, |
| 22 | + } as any; |
| 23 | +} |
| 24 | + |
| 25 | +function parseResult(result: ToolResult): unknown { |
| 26 | + return JSON.parse(result.content[0]!.text); |
| 27 | +} |
| 28 | + |
| 29 | +describe("registerSchemaTool additionalSchemas", () => { |
| 30 | + it("accepts additionalSchemas without throwing", () => { |
| 31 | + const server = makeMcpServer(); |
| 32 | + expect(() => |
| 33 | + registerSchemaTool(server, { DashboardContract: entry({ type: "object", properties: { id: { type: "string" } } }) }) |
| 34 | + ).not.toThrow(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("registers without additionalSchemas (backwards compat)", () => { |
| 38 | + const server = makeMcpServer(); |
| 39 | + expect(() => registerSchemaTool(server)).not.toThrow(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("throws when additionalSchemas key collides with a built-in schema name", () => { |
| 43 | + const server = makeMcpServer(); |
| 44 | + expect(() => |
| 45 | + registerSchemaTool(server, { pipeline: entry({ type: "object" }) }), |
| 46 | + ).toThrow("conflicts with a built-in schema name"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("handler returns fields from a Harness-layout extension schema (definitions[type][type])", async () => { |
| 50 | + const server = makeMcpServer(); |
| 51 | + const dashboardSchema = entry({ |
| 52 | + definitions: { |
| 53 | + DashboardContract: { |
| 54 | + DashboardContract: { |
| 55 | + type: "object", |
| 56 | + properties: { title: { type: "string" }, widgets: { type: "array" } }, |
| 57 | + required: ["title"], |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + registerSchemaTool(server, { DashboardContract: dashboardSchema }); |
| 63 | + |
| 64 | + const result = await server.call("harness_schema", { resource_type: "DashboardContract" }); |
| 65 | + const parsed = parseResult(result) as Record<string, unknown>; |
| 66 | + |
| 67 | + expect(parsed.resource_type).toBe("DashboardContract"); |
| 68 | + expect(parsed.fields).toEqual([ |
| 69 | + { name: "title", type: "string", required: true }, |
| 70 | + { name: "widgets", type: "array", required: false }, |
| 71 | + ]); |
| 72 | + }); |
| 73 | + |
| 74 | + it("handler returns fields from a plain JSON Schema extension schema (root-level properties)", async () => { |
| 75 | + const server = makeMcpServer(); |
| 76 | + registerSchemaTool(server, { |
| 77 | + MyExtension: entry({ |
| 78 | + type: "object", |
| 79 | + properties: { name: { type: "string" }, count: { type: "number" } }, |
| 80 | + required: ["name"], |
| 81 | + }), |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await server.call("harness_schema", { resource_type: "MyExtension" }); |
| 85 | + const parsed = parseResult(result) as Record<string, unknown>; |
| 86 | + |
| 87 | + expect(parsed.resource_type).toBe("MyExtension"); |
| 88 | + expect(parsed.fields).toEqual([ |
| 89 | + { name: "name", type: "string", required: true }, |
| 90 | + { name: "count", type: "number", required: false }, |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("handler still returns built-in schema content when no additionalSchemas", async () => { |
| 95 | + const server = makeMcpServer(); |
| 96 | + registerSchemaTool(server); |
| 97 | + |
| 98 | + const result = await server.call("harness_schema", { resource_type: "pipeline" }); |
| 99 | + const parsed = parseResult(result) as Record<string, unknown>; |
| 100 | + |
| 101 | + expect(parsed.resource_type).toBe("pipeline"); |
| 102 | + expect(Array.isArray(parsed.fields)).toBe(true); |
| 103 | + }); |
| 104 | +}); |
0 commit comments