|
| 1 | +import { describe, it, expect, beforeEach, mock, spyOn } from "bun:test" |
| 2 | +import { createSkillTool } from "./tools" |
| 3 | +import { SkillMcpManager } from "../../features/skill-mcp-manager" |
| 4 | +import type { LoadedSkill } from "../../features/opencode-skill-loader/types" |
| 5 | +import type { Tool as McpTool } from "@modelcontextprotocol/sdk/types.js" |
| 6 | + |
| 7 | +mock.module("node:fs", () => ({ |
| 8 | + readFileSync: () => `--- |
| 9 | +description: Test skill description |
| 10 | +--- |
| 11 | +Test skill body content`, |
| 12 | +})) |
| 13 | + |
| 14 | +function createMockSkillWithMcp(name: string, mcpServers: Record<string, unknown>): LoadedSkill { |
| 15 | + return { |
| 16 | + name, |
| 17 | + path: `/test/skills/${name}/SKILL.md`, |
| 18 | + resolvedPath: `/test/skills/${name}`, |
| 19 | + definition: { |
| 20 | + name, |
| 21 | + description: `Test skill ${name}`, |
| 22 | + template: "Test template", |
| 23 | + }, |
| 24 | + scope: "opencode-project", |
| 25 | + mcpConfig: mcpServers as LoadedSkill["mcpConfig"], |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const mockContext = { |
| 30 | + sessionID: "test-session", |
| 31 | + messageID: "msg-1", |
| 32 | + agent: "test-agent", |
| 33 | + abort: new AbortController().signal, |
| 34 | +} |
| 35 | + |
| 36 | +describe("skill tool - MCP schema display", () => { |
| 37 | + let manager: SkillMcpManager |
| 38 | + let loadedSkills: LoadedSkill[] |
| 39 | + let sessionID: string |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + manager = new SkillMcpManager() |
| 43 | + loadedSkills = [] |
| 44 | + sessionID = "test-session-1" |
| 45 | + }) |
| 46 | + |
| 47 | + describe("formatMcpCapabilities with inputSchema", () => { |
| 48 | + it("displays tool inputSchema when available", async () => { |
| 49 | + // #given |
| 50 | + const mockToolsWithSchema: McpTool[] = [ |
| 51 | + { |
| 52 | + name: "browser_type", |
| 53 | + description: "Type text into an element", |
| 54 | + inputSchema: { |
| 55 | + type: "object", |
| 56 | + properties: { |
| 57 | + element: { type: "string", description: "Human-readable element description" }, |
| 58 | + ref: { type: "string", description: "Element reference from page snapshot" }, |
| 59 | + text: { type: "string", description: "Text to type into the element" }, |
| 60 | + submit: { type: "boolean", description: "Submit form after typing" }, |
| 61 | + }, |
| 62 | + required: ["element", "ref", "text"], |
| 63 | + }, |
| 64 | + }, |
| 65 | + ] |
| 66 | + |
| 67 | + loadedSkills = [ |
| 68 | + createMockSkillWithMcp("test-skill", { |
| 69 | + playwright: { command: "npx", args: ["-y", "@anthropic-ai/mcp-playwright"] }, |
| 70 | + }), |
| 71 | + ] |
| 72 | + |
| 73 | + // Mock manager.listTools to return our mock tools |
| 74 | + spyOn(manager, "listTools").mockResolvedValue(mockToolsWithSchema) |
| 75 | + spyOn(manager, "listResources").mockResolvedValue([]) |
| 76 | + spyOn(manager, "listPrompts").mockResolvedValue([]) |
| 77 | + |
| 78 | + const tool = createSkillTool({ |
| 79 | + skills: loadedSkills, |
| 80 | + mcpManager: manager, |
| 81 | + getSessionID: () => sessionID, |
| 82 | + }) |
| 83 | + |
| 84 | + // #when |
| 85 | + const result = await tool.execute({ name: "test-skill" }, mockContext) |
| 86 | + |
| 87 | + // #then |
| 88 | + // Should include inputSchema details |
| 89 | + expect(result).toContain("browser_type") |
| 90 | + expect(result).toContain("inputSchema") |
| 91 | + expect(result).toContain("element") |
| 92 | + expect(result).toContain("ref") |
| 93 | + expect(result).toContain("text") |
| 94 | + expect(result).toContain("submit") |
| 95 | + expect(result).toContain("required") |
| 96 | + }) |
| 97 | + |
| 98 | + it("displays multiple tools with their schemas", async () => { |
| 99 | + // #given |
| 100 | + const mockToolsWithSchema: McpTool[] = [ |
| 101 | + { |
| 102 | + name: "browser_navigate", |
| 103 | + description: "Navigate to a URL", |
| 104 | + inputSchema: { |
| 105 | + type: "object", |
| 106 | + properties: { |
| 107 | + url: { type: "string", description: "URL to navigate to" }, |
| 108 | + }, |
| 109 | + required: ["url"], |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "browser_click", |
| 114 | + description: "Click an element", |
| 115 | + inputSchema: { |
| 116 | + type: "object", |
| 117 | + properties: { |
| 118 | + element: { type: "string" }, |
| 119 | + ref: { type: "string" }, |
| 120 | + }, |
| 121 | + required: ["element", "ref"], |
| 122 | + }, |
| 123 | + }, |
| 124 | + ] |
| 125 | + |
| 126 | + loadedSkills = [ |
| 127 | + createMockSkillWithMcp("playwright-skill", { |
| 128 | + playwright: { command: "npx", args: ["-y", "@anthropic-ai/mcp-playwright"] }, |
| 129 | + }), |
| 130 | + ] |
| 131 | + |
| 132 | + spyOn(manager, "listTools").mockResolvedValue(mockToolsWithSchema) |
| 133 | + spyOn(manager, "listResources").mockResolvedValue([]) |
| 134 | + spyOn(manager, "listPrompts").mockResolvedValue([]) |
| 135 | + |
| 136 | + const tool = createSkillTool({ |
| 137 | + skills: loadedSkills, |
| 138 | + mcpManager: manager, |
| 139 | + getSessionID: () => sessionID, |
| 140 | + }) |
| 141 | + |
| 142 | + // #when |
| 143 | + const result = await tool.execute({ name: "playwright-skill" }, mockContext) |
| 144 | + |
| 145 | + // #then |
| 146 | + expect(result).toContain("browser_navigate") |
| 147 | + expect(result).toContain("browser_click") |
| 148 | + expect(result).toContain("url") |
| 149 | + expect(result).toContain("Navigate to a URL") |
| 150 | + }) |
| 151 | + |
| 152 | + it("handles tools without inputSchema gracefully", async () => { |
| 153 | + // #given |
| 154 | + const mockToolsMinimal: McpTool[] = [ |
| 155 | + { |
| 156 | + name: "simple_tool", |
| 157 | + inputSchema: { type: "object" }, |
| 158 | + }, |
| 159 | + ] |
| 160 | + |
| 161 | + loadedSkills = [ |
| 162 | + createMockSkillWithMcp("simple-skill", { |
| 163 | + simple: { command: "echo", args: ["test"] }, |
| 164 | + }), |
| 165 | + ] |
| 166 | + |
| 167 | + spyOn(manager, "listTools").mockResolvedValue(mockToolsMinimal) |
| 168 | + spyOn(manager, "listResources").mockResolvedValue([]) |
| 169 | + spyOn(manager, "listPrompts").mockResolvedValue([]) |
| 170 | + |
| 171 | + const tool = createSkillTool({ |
| 172 | + skills: loadedSkills, |
| 173 | + mcpManager: manager, |
| 174 | + getSessionID: () => sessionID, |
| 175 | + }) |
| 176 | + |
| 177 | + // #when |
| 178 | + const result = await tool.execute({ name: "simple-skill" }, mockContext) |
| 179 | + |
| 180 | + // #then |
| 181 | + expect(result).toContain("simple_tool") |
| 182 | + // Should not throw, should handle gracefully |
| 183 | + }) |
| 184 | + |
| 185 | + it("formats schema in a way LLM can understand for skill_mcp calls", async () => { |
| 186 | + // #given |
| 187 | + const mockTools: McpTool[] = [ |
| 188 | + { |
| 189 | + name: "query", |
| 190 | + description: "Execute SQL query", |
| 191 | + inputSchema: { |
| 192 | + type: "object", |
| 193 | + properties: { |
| 194 | + sql: { type: "string", description: "SQL query to execute" }, |
| 195 | + params: { type: "array", description: "Query parameters" }, |
| 196 | + }, |
| 197 | + required: ["sql"], |
| 198 | + }, |
| 199 | + }, |
| 200 | + ] |
| 201 | + |
| 202 | + loadedSkills = [ |
| 203 | + createMockSkillWithMcp("db-skill", { |
| 204 | + sqlite: { command: "uvx", args: ["mcp-server-sqlite"] }, |
| 205 | + }), |
| 206 | + ] |
| 207 | + |
| 208 | + spyOn(manager, "listTools").mockResolvedValue(mockTools) |
| 209 | + spyOn(manager, "listResources").mockResolvedValue([]) |
| 210 | + spyOn(manager, "listPrompts").mockResolvedValue([]) |
| 211 | + |
| 212 | + const tool = createSkillTool({ |
| 213 | + skills: loadedSkills, |
| 214 | + mcpManager: manager, |
| 215 | + getSessionID: () => sessionID, |
| 216 | + }) |
| 217 | + |
| 218 | + // #when |
| 219 | + const result = await tool.execute({ name: "db-skill" }, mockContext) |
| 220 | + |
| 221 | + // #then |
| 222 | + // Should provide enough info for LLM to construct valid skill_mcp call |
| 223 | + expect(result).toContain("sqlite") |
| 224 | + expect(result).toContain("query") |
| 225 | + expect(result).toContain("sql") |
| 226 | + expect(result).toContain("required") |
| 227 | + expect(result).toMatch(/sql[\s\S]*string/i) |
| 228 | + }) |
| 229 | + }) |
| 230 | +}) |
0 commit comments