|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { fetchInstructions } from "../instructions" |
| 3 | +import { createMCPServerInstructions } from "../create-mcp-server" |
| 4 | +import { createModeInstructions } from "../create-mode" |
| 5 | +import { McpHub } from "../../../../services/mcp/McpHub" |
| 6 | +import { DiffStrategy } from "../../../../shared/tools" |
| 7 | +import * as vscode from "vscode" |
| 8 | + |
| 9 | +// Mock the imported modules |
| 10 | +vi.mock("../create-mcp-server", () => ({ |
| 11 | + createMCPServerInstructions: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock("../create-mode", () => ({ |
| 15 | + createModeInstructions: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +describe("fetchInstructions", () => { |
| 19 | + const mockMcpHub = {} as McpHub |
| 20 | + const mockDiffStrategy = {} as DiffStrategy |
| 21 | + const mockContext = {} as vscode.ExtensionContext |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks() |
| 25 | + }) |
| 26 | + |
| 27 | + describe("create_mcp_server", () => { |
| 28 | + it("should return MCP server instructions when enableMcpServerCreation is true", async () => { |
| 29 | + const mockInstructions = "MCP server creation instructions" |
| 30 | + vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions) |
| 31 | + |
| 32 | + const result = await fetchInstructions("create_mcp_server", { |
| 33 | + mcpHub: mockMcpHub, |
| 34 | + diffStrategy: mockDiffStrategy, |
| 35 | + enableMcpServerCreation: true, |
| 36 | + }) |
| 37 | + |
| 38 | + expect(result).toBe(mockInstructions) |
| 39 | + expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should return MCP server instructions when enableMcpServerCreation is undefined (default true)", async () => { |
| 43 | + const mockInstructions = "MCP server creation instructions" |
| 44 | + vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions) |
| 45 | + |
| 46 | + const result = await fetchInstructions("create_mcp_server", { |
| 47 | + mcpHub: mockMcpHub, |
| 48 | + diffStrategy: mockDiffStrategy, |
| 49 | + // enableMcpServerCreation is undefined |
| 50 | + }) |
| 51 | + |
| 52 | + expect(result).toBe(mockInstructions) |
| 53 | + expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy) |
| 54 | + }) |
| 55 | + |
| 56 | + it("should return disabled message when enableMcpServerCreation is false", async () => { |
| 57 | + const result = await fetchInstructions("create_mcp_server", { |
| 58 | + mcpHub: mockMcpHub, |
| 59 | + diffStrategy: mockDiffStrategy, |
| 60 | + enableMcpServerCreation: false, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(result).toBe( |
| 64 | + "MCP server creation is currently disabled. This feature can be enabled in the settings.", |
| 65 | + ) |
| 66 | + expect(createMCPServerInstructions).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + describe("create_mode", () => { |
| 71 | + it("should return mode creation instructions", async () => { |
| 72 | + const mockInstructions = "Mode creation instructions" |
| 73 | + vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions) |
| 74 | + |
| 75 | + const result = await fetchInstructions("create_mode", { |
| 76 | + context: mockContext, |
| 77 | + }) |
| 78 | + |
| 79 | + expect(result).toBe(mockInstructions) |
| 80 | + expect(createModeInstructions).toHaveBeenCalledWith(mockContext) |
| 81 | + }) |
| 82 | + |
| 83 | + it("should not be affected by enableMcpServerCreation setting", async () => { |
| 84 | + const mockInstructions = "Mode creation instructions" |
| 85 | + vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions) |
| 86 | + |
| 87 | + const result = await fetchInstructions("create_mode", { |
| 88 | + context: mockContext, |
| 89 | + enableMcpServerCreation: false, |
| 90 | + }) |
| 91 | + |
| 92 | + expect(result).toBe(mockInstructions) |
| 93 | + expect(createModeInstructions).toHaveBeenCalledWith(mockContext) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe("unknown task", () => { |
| 98 | + it("should return empty string for unknown task", async () => { |
| 99 | + const result = await fetchInstructions("unknown_task", { |
| 100 | + mcpHub: mockMcpHub, |
| 101 | + diffStrategy: mockDiffStrategy, |
| 102 | + }) |
| 103 | + |
| 104 | + expect(result).toBe("") |
| 105 | + expect(createMCPServerInstructions).not.toHaveBeenCalled() |
| 106 | + expect(createModeInstructions).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + }) |
| 109 | +}) |
0 commit comments