|
| 1 | +import { describe, expect, test } from "vite-plus/test"; |
| 2 | +import { BailianError, ExitCode } from "bailian-cli-core"; |
| 3 | +import { mcpMarketplaceDetailPage } from "bailian-cli-runtime"; |
| 4 | +import { |
| 5 | + isMcpNotActivated, |
| 6 | + mcpActivateHint, |
| 7 | + rethrowWithMcpActivateHint, |
| 8 | +} from "../src/commands/mcp/activate-hint.ts"; |
| 9 | + |
| 10 | +describe("mcp-activate-hint", () => { |
| 11 | + test("识别 404 + 未开通 / MCP不存在 / MCP_IS_INVALID", () => { |
| 12 | + expect( |
| 13 | + isMcpNotActivated(new BailianError("MCP request failed: 404 Not Found - MCP不存在或未开通")), |
| 14 | + ).toBe(true); |
| 15 | + expect( |
| 16 | + isMcpNotActivated(new BailianError("MCP request failed: 404 - MCP不存在或未开通")), |
| 17 | + ).toBe(true); |
| 18 | + expect( |
| 19 | + isMcpNotActivated(new BailianError("MCP request failed: 404 Not Found - MCP_IS_INVALID")), |
| 20 | + ).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + test("裸 404 或非 MCP 错误不加开通判定", () => { |
| 24 | + expect(isMcpNotActivated(new BailianError("MCP request failed: 404 Not Found"))).toBe(false); |
| 25 | + expect(isMcpNotActivated(new BailianError("MCP request failed: 405 Method Not Allowed"))).toBe( |
| 26 | + false, |
| 27 | + ); |
| 28 | + expect(isMcpNotActivated(new Error("MCP不存在或未开通"))).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + test("hint 含对应 server 的 MCP 广场深链", () => { |
| 32 | + const serverCode = "market-cmapi00073529"; |
| 33 | + expect(mcpActivateHint(serverCode)).toContain(mcpMarketplaceDetailPage(serverCode)); |
| 34 | + expect(mcpActivateHint(serverCode)).toMatch(/Activate|re-activate/i); |
| 35 | + }); |
| 36 | + |
| 37 | + test("WebSearch hint 含 SSE 升级说明", () => { |
| 38 | + expect(mcpActivateHint("WebSearch")).toMatch(/SSE|Streamable HTTP/i); |
| 39 | + }); |
| 40 | + |
| 41 | + test("rethrow 保留原 message,补 hint", () => { |
| 42 | + const serverCode = "market-cmapi00073529"; |
| 43 | + const original = new BailianError( |
| 44 | + "MCP request failed: 404 Not Found - MCP不存在或未开通", |
| 45 | + ExitCode.GENERAL, |
| 46 | + ); |
| 47 | + try { |
| 48 | + rethrowWithMcpActivateHint(original, serverCode); |
| 49 | + expect.unreachable("should throw"); |
| 50 | + } catch (error) { |
| 51 | + expect(error).toBeInstanceOf(BailianError); |
| 52 | + const wrapped = error as BailianError; |
| 53 | + expect(wrapped.message).toBe(original.message); |
| 54 | + expect(wrapped.exitCode).toBe(ExitCode.GENERAL); |
| 55 | + expect(wrapped.hint).toContain(mcpMarketplaceDetailPage(serverCode)); |
| 56 | + expect(wrapped.cause).toBe(original); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + test("已有 hint 或非未开通错误原样抛出", () => { |
| 61 | + const withHint = new BailianError( |
| 62 | + "MCP request failed: 404 Not Found - MCP不存在或未开通", |
| 63 | + ExitCode.GENERAL, |
| 64 | + "already hinted", |
| 65 | + ); |
| 66 | + try { |
| 67 | + rethrowWithMcpActivateHint(withHint, "WebSearch"); |
| 68 | + expect.unreachable("should throw"); |
| 69 | + } catch (error) { |
| 70 | + expect(error).toBe(withHint); |
| 71 | + } |
| 72 | + |
| 73 | + const other = new BailianError("MCP request failed: 401 Unauthorized"); |
| 74 | + try { |
| 75 | + rethrowWithMcpActivateHint(other, "WebSearch"); |
| 76 | + expect.unreachable("should throw"); |
| 77 | + } catch (error) { |
| 78 | + expect(error).toBe(other); |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
0 commit comments