|
| 1 | +import { describe, expect, test } from "vite-plus/test"; |
| 2 | +import { BailianError, ExitCode } from "bailian-cli-core"; |
| 3 | +import { MCP_WEBSEARCH_PAGE } from "bailian-cli-runtime"; |
| 4 | +import { |
| 5 | + isWebSearchMcpNotActivated, |
| 6 | + rethrowWithWebSearchActivateHint, |
| 7 | + webSearchActivateHint, |
| 8 | +} from "../src/commands/search/web-activate-hint.ts"; |
| 9 | + |
| 10 | +describe("web-activate-hint", () => { |
| 11 | + test("识别 404 + 未开通 / MCP不存在 / MCP_IS_INVALID", () => { |
| 12 | + expect( |
| 13 | + isWebSearchMcpNotActivated( |
| 14 | + new BailianError("MCP request failed: 404 Not Found - MCP不存在或未开通"), |
| 15 | + ), |
| 16 | + ).toBe(true); |
| 17 | + expect( |
| 18 | + isWebSearchMcpNotActivated(new BailianError("MCP request failed: 404 - MCP不存在或未开通")), |
| 19 | + ).toBe(true); |
| 20 | + expect( |
| 21 | + isWebSearchMcpNotActivated( |
| 22 | + new BailianError("MCP request failed: 404 Not Found - MCP_IS_INVALID"), |
| 23 | + ), |
| 24 | + ).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + test("裸 404 或非 MCP 错误不加开通判定", () => { |
| 28 | + expect(isWebSearchMcpNotActivated(new BailianError("MCP request failed: 404 Not Found"))).toBe( |
| 29 | + false, |
| 30 | + ); |
| 31 | + expect( |
| 32 | + isWebSearchMcpNotActivated(new BailianError("MCP request failed: 405 Method Not Allowed")), |
| 33 | + ).toBe(false); |
| 34 | + expect(isWebSearchMcpNotActivated(new Error("MCP不存在或未开通"))).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + test("hint 含 MCP 广场 WebSearch 深链", () => { |
| 38 | + expect(webSearchActivateHint()).toContain(MCP_WEBSEARCH_PAGE); |
| 39 | + expect(webSearchActivateHint()).toMatch(/Activate|re-activate/i); |
| 40 | + }); |
| 41 | + |
| 42 | + test("rethrow 保留原 message,补 Hint", () => { |
| 43 | + const original = new BailianError( |
| 44 | + "MCP request failed: 404 Not Found - MCP不存在或未开通", |
| 45 | + ExitCode.GENERAL, |
| 46 | + ); |
| 47 | + try { |
| 48 | + rethrowWithWebSearchActivateHint(original); |
| 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(MCP_WEBSEARCH_PAGE); |
| 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 | + rethrowWithWebSearchActivateHint(withHint); |
| 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 | + rethrowWithWebSearchActivateHint(other); |
| 76 | + expect.unreachable("should throw"); |
| 77 | + } catch (error) { |
| 78 | + expect(error).toBe(other); |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
0 commit comments