|
| 1 | +import { api } from "@/controllers/API/api"; |
| 2 | +import { useUtilityStore } from "@/stores/utilityStore"; |
| 3 | +import { customGetMCPUrl } from "../custom-mcp-url"; |
| 4 | + |
| 5 | +describe("customGetMCPUrl", () => { |
| 6 | + const originalBaseURL = api.defaults.baseURL; |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + api.defaults.baseURL = originalBaseURL; |
| 10 | + useUtilityStore.setState({ mcpBaseUrl: "" }); |
| 11 | + }); |
| 12 | + |
| 13 | + it("uses mcpBaseUrl from store when set", () => { |
| 14 | + api.defaults.baseURL = ""; |
| 15 | + useUtilityStore.setState({ mcpBaseUrl: "https://custom.example.com" }); |
| 16 | + |
| 17 | + const url = customGetMCPUrl("proj-1"); |
| 18 | + |
| 19 | + expect(url).toBe( |
| 20 | + "https://custom.example.com/api/v1/mcp/project/proj-1/streamable", |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("mcpBaseUrl takes priority over api.defaults.baseURL", () => { |
| 25 | + api.defaults.baseURL = "https://api-default.example.com"; |
| 26 | + useUtilityStore.setState({ mcpBaseUrl: "https://override.example.com" }); |
| 27 | + |
| 28 | + const url = customGetMCPUrl("proj-1"); |
| 29 | + |
| 30 | + expect(url).toBe( |
| 31 | + "https://override.example.com/api/v1/mcp/project/proj-1/streamable", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("falls back to api.defaults.baseURL when mcpBaseUrl is empty", () => { |
| 36 | + api.defaults.baseURL = "https://api-default.example.com"; |
| 37 | + useUtilityStore.setState({ mcpBaseUrl: "" }); |
| 38 | + |
| 39 | + const url = customGetMCPUrl("proj-1"); |
| 40 | + |
| 41 | + expect(url).toBe( |
| 42 | + "https://api-default.example.com/api/v1/mcp/project/proj-1/streamable", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it("falls back to window.location.origin when both are empty", () => { |
| 47 | + api.defaults.baseURL = ""; |
| 48 | + useUtilityStore.setState({ mcpBaseUrl: "" }); |
| 49 | + |
| 50 | + const url = customGetMCPUrl("proj-1"); |
| 51 | + |
| 52 | + expect(url).toBe( |
| 53 | + `${window.location.origin}/api/v1/mcp/project/proj-1/streamable`, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("strips trailing slashes from mcpBaseUrl", () => { |
| 58 | + useUtilityStore.setState({ mcpBaseUrl: "https://example.com/" }); |
| 59 | + |
| 60 | + const url = customGetMCPUrl("proj-1"); |
| 61 | + |
| 62 | + expect(url).toBe( |
| 63 | + "https://example.com/api/v1/mcp/project/proj-1/streamable", |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("strips multiple trailing slashes", () => { |
| 68 | + useUtilityStore.setState({ mcpBaseUrl: "https://example.com///" }); |
| 69 | + |
| 70 | + const url = customGetMCPUrl("proj-1"); |
| 71 | + |
| 72 | + expect(url).toBe( |
| 73 | + "https://example.com/api/v1/mcp/project/proj-1/streamable", |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("returns SSE URL when transport is sse", () => { |
| 78 | + useUtilityStore.setState({ mcpBaseUrl: "https://example.com" }); |
| 79 | + |
| 80 | + const url = customGetMCPUrl("proj-1", {}, "sse"); |
| 81 | + |
| 82 | + expect(url).toBe("https://example.com/api/v1/mcp/project/proj-1/sse"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("returns composer URL when useComposer is true and streamableHttpUrl is set", () => { |
| 86 | + useUtilityStore.setState({ mcpBaseUrl: "https://should-not-use.com" }); |
| 87 | + |
| 88 | + const url = customGetMCPUrl( |
| 89 | + "proj-1", |
| 90 | + { |
| 91 | + useComposer: true, |
| 92 | + streamableHttpUrl: "https://composer.example.com/streamable", |
| 93 | + }, |
| 94 | + "streamablehttp", |
| 95 | + ); |
| 96 | + |
| 97 | + expect(url).toBe("https://composer.example.com/streamable"); |
| 98 | + }); |
| 99 | +}); |
0 commit comments