|
| 1 | +import child_process from "child_process"; |
| 2 | +import { EventEmitter } from "events"; |
| 3 | +import fs = require("fs"); |
| 4 | +import * as path from "path"; |
| 5 | +import yargs from "yargs"; |
| 6 | +import mcp from "../../packages/cli/lib/commands/mcp"; |
| 7 | + |
| 8 | +function createFakeChildProcess(): EventEmitter { |
| 9 | + return new EventEmitter(); |
| 10 | +} |
| 11 | + |
| 12 | +describe("Unit - MCP CLI command", () => { |
| 13 | + const mcpPackageJson = path.join(process.cwd(), "node_modules", "@igniteui", "mcp-server", "package.json"); |
| 14 | + const mcpEntry = path.resolve(path.dirname(mcpPackageJson), "dist", "index.js"); |
| 15 | + |
| 16 | + let stderrWriteSpy: jasmine.Spy; |
| 17 | + let spawnSpy: jasmine.Spy; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + process.exitCode = undefined; |
| 21 | + stderrWriteSpy = spyOn(process.stderr, "write").and.returnValue(true); |
| 22 | + spawnSpy = spyOn(child_process, "spawn"); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + process.exitCode = undefined; |
| 27 | + }); |
| 28 | + |
| 29 | + function mockMcpPackageResolution(resolvedPath?: string, shouldThrow = false): void { |
| 30 | + const moduleApi = require("module"); |
| 31 | + const originalResolveFilename = moduleApi._resolveFilename; |
| 32 | + |
| 33 | + spyOn(moduleApi, "_resolveFilename").and.callFake((request: string, ...args: any[]) => { |
| 34 | + if (request === "@igniteui/mcp-server/package.json") { |
| 35 | + if (shouldThrow) { |
| 36 | + throw new Error("Cannot find module"); |
| 37 | + } |
| 38 | + return resolvedPath; |
| 39 | + } |
| 40 | + |
| 41 | + return originalResolveFilename.call(moduleApi, request, ...args); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + function mockInstalledMcp(entryExists: boolean, child?: EventEmitter): EventEmitter { |
| 46 | + const spawnedChild = child ?? createFakeChildProcess(); |
| 47 | + mockMcpPackageResolution(mcpPackageJson); |
| 48 | + spyOn(fs, "existsSync").and.returnValue(entryExists); |
| 49 | + spawnSpy.and.returnValue(spawnedChild as any); |
| 50 | + return spawnedChild; |
| 51 | + } |
| 52 | + |
| 53 | + describe("metadata", () => { |
| 54 | + it("registers the MCP command with the expected description", () => { |
| 55 | + expect(mcp.command).toBe("mcp"); |
| 56 | + expect(mcp.describe).toBe("Starts the Ignite UI MCP server for AI assistant integration"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("configures the debug and remote options", () => { |
| 60 | + const buildParser = mcp.builder as any; |
| 61 | + const parser = buildParser(yargs([])); |
| 62 | + const argv = parser.parseSync(["--remote", "https://docs.example.test", "--debug"]); |
| 63 | + const defaults = buildParser(yargs([])).parseSync([]); |
| 64 | + |
| 65 | + expect(argv.remote).toBe("https://docs.example.test"); |
| 66 | + expect(argv.debug).toBeTrue(); |
| 67 | + expect(defaults.debug).toBeFalse(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("preflight checks", () => { |
| 72 | + it("shows an install message when the MCP server package cannot be resolved", async () => { |
| 73 | + const existsSyncSpy = spyOn(fs, "existsSync"); |
| 74 | + mockMcpPackageResolution(undefined, true); |
| 75 | + |
| 76 | + await mcp.handler({ _: ["mcp"], $0: "ig" } as any); |
| 77 | + |
| 78 | + expect(process.exitCode).toBe(1); |
| 79 | + expect(existsSyncSpy).not.toHaveBeenCalled(); |
| 80 | + expect(spawnSpy).not.toHaveBeenCalled(); |
| 81 | + |
| 82 | + expect(stderrWriteSpy).toHaveBeenCalled(); |
| 83 | + const message = stderrWriteSpy.calls.allArgs().map(args => args[0]).join(""); |
| 84 | + expect(message).toContain("MCP server package not found"); |
| 85 | + expect(message).toContain("yarn install"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("shows a build message when the MCP server entry does not exist", async () => { |
| 89 | + mockMcpPackageResolution(mcpPackageJson); |
| 90 | + spyOn(fs, "existsSync").and.returnValue(false); |
| 91 | + |
| 92 | + await mcp.handler({ _: ["mcp"], $0: "ig" } as any); |
| 93 | + |
| 94 | + expect(fs.existsSync).toHaveBeenCalledWith(mcpEntry); |
| 95 | + expect(process.exitCode).toBe(1); |
| 96 | + expect(spawnSpy).not.toHaveBeenCalled(); |
| 97 | + |
| 98 | + expect(stderrWriteSpy).toHaveBeenCalled(); |
| 99 | + const message = stderrWriteSpy.calls.allArgs().map(args => args[0]).join(""); |
| 100 | + expect(message).toContain("MCP server not built"); |
| 101 | + expect(message).toContain("build:mcp"); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("runtime behavior", () => { |
| 106 | + it("starts the installed MCP server with stdio inheritance", async () => { |
| 107 | + const child = mockInstalledMcp(true); |
| 108 | + const result = mcp.handler({ _: ["mcp"], $0: "ig" } as any) as Promise<void>; |
| 109 | + |
| 110 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 111 | + process.execPath, |
| 112 | + [mcpEntry], |
| 113 | + { stdio: "inherit" } |
| 114 | + ); |
| 115 | + |
| 116 | + child.emit("exit", 0); |
| 117 | + await result; |
| 118 | + }); |
| 119 | + |
| 120 | + it("forwards remote and debug flags to the installed MCP server", async () => { |
| 121 | + const remoteUrl = "https://docs.example.test"; |
| 122 | + const child = mockInstalledMcp(true); |
| 123 | + const result = mcp.handler({ |
| 124 | + remote: remoteUrl, |
| 125 | + debug: true, |
| 126 | + _: ["mcp"], |
| 127 | + $0: "ig" |
| 128 | + } as any) as Promise<void>; |
| 129 | + |
| 130 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 131 | + process.execPath, |
| 132 | + [mcpEntry, "--remote", remoteUrl, "--debug"], |
| 133 | + { stdio: "inherit" } |
| 134 | + ); |
| 135 | + |
| 136 | + child.emit("exit", 0); |
| 137 | + await result; |
| 138 | + }); |
| 139 | + |
| 140 | + it("forwards only the debug flag when remote mode is not used", async () => { |
| 141 | + const child = mockInstalledMcp(true); |
| 142 | + const result = mcp.handler({ |
| 143 | + debug: true, |
| 144 | + _: ["mcp"], |
| 145 | + $0: "ig" |
| 146 | + } as any) as Promise<void>; |
| 147 | + |
| 148 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 149 | + process.execPath, |
| 150 | + [mcpEntry, "--debug"], |
| 151 | + { stdio: "inherit" } |
| 152 | + ); |
| 153 | + |
| 154 | + child.emit("exit", 0); |
| 155 | + await result; |
| 156 | + }); |
| 157 | + |
| 158 | + it("propagates the child process exit code", async () => { |
| 159 | + const child = mockInstalledMcp(true); |
| 160 | + const result = mcp.handler({ _: ["mcp"], $0: "ig" } as any) as Promise<void>; |
| 161 | + |
| 162 | + child.emit("exit", 7); |
| 163 | + await result; |
| 164 | + |
| 165 | + expect(process.exitCode).toBe(7); |
| 166 | + }); |
| 167 | + |
| 168 | + it("defaults the process exit code to 0 when the child exits without one", async () => { |
| 169 | + const child = mockInstalledMcp(true); |
| 170 | + const result = mcp.handler({ _: ["mcp"], $0: "ig" } as any) as Promise<void>; |
| 171 | + |
| 172 | + child.emit("exit", null); |
| 173 | + await result; |
| 174 | + |
| 175 | + expect(process.exitCode).toBe(0); |
| 176 | + }); |
| 177 | + |
| 178 | + it("reports child process startup errors", async () => { |
| 179 | + const child = mockInstalledMcp(true); |
| 180 | + const error = new Error("boom"); |
| 181 | + const result = mcp.handler({ _: ["mcp"], $0: "ig" } as any) as Promise<void>; |
| 182 | + |
| 183 | + child.emit("error", error); |
| 184 | + |
| 185 | + await expectAsync(result).toBeRejectedWith(error); |
| 186 | + |
| 187 | + expect(stderrWriteSpy).toHaveBeenCalled(); |
| 188 | + const message = stderrWriteSpy.calls.allArgs().map(args => args[0]).join(""); |
| 189 | + expect(message).toContain("Failed to start MCP server"); |
| 190 | + expect(message).toContain("boom"); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments