|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
2 | 2 | import { serversApi } from "./servers"; |
| 3 | +import type { GatewayTestRequest } from "@/generated/types"; |
3 | 4 |
|
4 | 5 | describe("serversApi", () => { |
5 | 6 | const mockFetch = vi.fn(); |
@@ -255,6 +256,31 @@ describe("serversApi", () => { |
255 | 256 | vi.useRealTimers(); |
256 | 257 | }); |
257 | 258 |
|
| 259 | + it("ignores non-oauth_callback messages from the popup", async () => { |
| 260 | + vi.useFakeTimers(); |
| 261 | + const mockAuthWindow = { closed: false } as unknown as Window; |
| 262 | + vi.spyOn(window, "open").mockReturnValue(mockAuthWindow); |
| 263 | + |
| 264 | + const promise = serversApi.triggerOAuthAuthorization("server-123"); |
| 265 | + |
| 266 | + // Correct source, but payloads that fail the null / type guard are ignored. |
| 267 | + const nullEvent = new MessageEvent("message", { data: null }); |
| 268 | + Object.defineProperty(nullEvent, "source", { value: mockAuthWindow, writable: false }); |
| 269 | + window.dispatchEvent(nullEvent); |
| 270 | + |
| 271 | + const wrongTypeEvent = new MessageEvent("message", { data: { type: "something_else" } }); |
| 272 | + Object.defineProperty(wrongTypeEvent, "source", { value: mockAuthWindow, writable: false }); |
| 273 | + window.dispatchEvent(wrongTypeEvent); |
| 274 | + |
| 275 | + // Neither settled the promise; closing the popup does. |
| 276 | + (mockAuthWindow as { closed: boolean }).closed = true; |
| 277 | + vi.advanceTimersByTime(1000); |
| 278 | + |
| 279 | + await expect(promise).rejects.toThrow("OAuth authorization was cancelled"); |
| 280 | + |
| 281 | + vi.useRealTimers(); |
| 282 | + }); |
| 283 | + |
258 | 284 | it("rejects with cancellation message when user closes the popup", async () => { |
259 | 285 | vi.useFakeTimers(); |
260 | 286 | const mockAuthWindow = { closed: false } as unknown as Window; |
@@ -299,4 +325,193 @@ describe("serversApi", () => { |
299 | 325 | ); |
300 | 326 | }); |
301 | 327 | }); |
| 328 | + |
| 329 | + describe("validateServerId (via get)", () => { |
| 330 | + it("throws 'Invalid server ID' for an empty id", () => { |
| 331 | + expect(() => serversApi.get("")).toThrow(/^Invalid server ID$/); |
| 332 | + }); |
| 333 | + |
| 334 | + it("throws 'Invalid server ID format' for an id with illegal characters", () => { |
| 335 | + expect(() => serversApi.get("bad/id")).toThrow("Invalid server ID format"); |
| 336 | + }); |
| 337 | + }); |
| 338 | + |
| 339 | + describe("list", () => { |
| 340 | + const jsonResponse = (body: unknown) => |
| 341 | + new Response(JSON.stringify(body), { |
| 342 | + status: 200, |
| 343 | + headers: { "Content-Type": "application/json" }, |
| 344 | + }); |
| 345 | + |
| 346 | + it("requests /gateways with only include_pagination by default", async () => { |
| 347 | + const body = { servers: [], pagination: { nextCursor: null } }; |
| 348 | + mockFetch.mockResolvedValueOnce(jsonResponse(body)); |
| 349 | + |
| 350 | + const result = await serversApi.list(); |
| 351 | + |
| 352 | + expect(result).toEqual(body); |
| 353 | + const url = mockFetch.mock.calls[0][0] as string; |
| 354 | + expect(url).toContain("/gateways?"); |
| 355 | + expect(url).toContain("include_pagination=true"); |
| 356 | + expect(url).not.toContain("cursor="); |
| 357 | + expect(url).not.toContain("limit="); |
| 358 | + expect(url).not.toContain("include_inactive="); |
| 359 | + }); |
| 360 | + |
| 361 | + it("includes cursor, clamped limit, and include_inactive when provided", async () => { |
| 362 | + mockFetch.mockResolvedValueOnce(jsonResponse({ servers: [] })); |
| 363 | + |
| 364 | + await serversApi.list({ cursor: "next-page", limit: 500, include_inactive: true }); |
| 365 | + |
| 366 | + const url = mockFetch.mock.calls[0][0] as string; |
| 367 | + expect(url).toContain("cursor=next-page"); |
| 368 | + expect(url).toContain("limit=100"); // clamped to the max of 100 |
| 369 | + expect(url).toContain("include_inactive=true"); |
| 370 | + }); |
| 371 | + |
| 372 | + it("clamps a limit below 1 up to 1", async () => { |
| 373 | + mockFetch.mockResolvedValueOnce(jsonResponse({ servers: [] })); |
| 374 | + |
| 375 | + await serversApi.list({ limit: 0 }); |
| 376 | + |
| 377 | + expect(mockFetch.mock.calls[0][0]).toContain("limit=1"); |
| 378 | + }); |
| 379 | + |
| 380 | + it("falls back to a limit of 25 for a non-finite limit", async () => { |
| 381 | + mockFetch.mockResolvedValueOnce(jsonResponse({ servers: [] })); |
| 382 | + |
| 383 | + await serversApi.list({ limit: Number.POSITIVE_INFINITY }); |
| 384 | + |
| 385 | + expect(mockFetch.mock.calls[0][0]).toContain("limit=25"); |
| 386 | + }); |
| 387 | + |
| 388 | + it("resolves when passed an AbortSignal", async () => { |
| 389 | + mockFetch.mockResolvedValueOnce(jsonResponse({ servers: [] })); |
| 390 | + const controller = new AbortController(); |
| 391 | + |
| 392 | + await expect(serversApi.list({ signal: controller.signal })).resolves.toEqual({ |
| 393 | + servers: [], |
| 394 | + }); |
| 395 | + }); |
| 396 | + }); |
| 397 | + |
| 398 | + describe("get", () => { |
| 399 | + const jsonResponse = (body: unknown) => |
| 400 | + new Response(JSON.stringify(body), { |
| 401 | + status: 200, |
| 402 | + headers: { "Content-Type": "application/json" }, |
| 403 | + }); |
| 404 | + |
| 405 | + it("fetches /gateways/:id and returns the server", async () => { |
| 406 | + const server = { id: "get-basic", name: "Basic" }; |
| 407 | + mockFetch.mockResolvedValueOnce(jsonResponse(server)); |
| 408 | + |
| 409 | + const result = await serversApi.get("get-basic"); |
| 410 | + |
| 411 | + expect(result).toEqual(server); |
| 412 | + expect(mockFetch).toHaveBeenCalledWith( |
| 413 | + expect.stringContaining("/gateways/get-basic"), |
| 414 | + expect.objectContaining({ method: "GET" }), |
| 415 | + ); |
| 416 | + }); |
| 417 | + |
| 418 | + it("returns the same in-flight promise for concurrent gets (request cache)", async () => { |
| 419 | + mockFetch.mockResolvedValueOnce(jsonResponse({ id: "get-cache" })); |
| 420 | + |
| 421 | + const first = serversApi.get("get-cache"); |
| 422 | + const second = serversApi.get("get-cache"); |
| 423 | + |
| 424 | + expect(first).toBe(second); |
| 425 | + await first; |
| 426 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 427 | + }); |
| 428 | + |
| 429 | + it("evicts the cache on failure so a later call retries", async () => { |
| 430 | + mockFetch.mockResolvedValueOnce( |
| 431 | + new Response(JSON.stringify({ detail: "boom" }), { |
| 432 | + status: 500, |
| 433 | + headers: { "Content-Type": "application/json" }, |
| 434 | + }), |
| 435 | + ); |
| 436 | + |
| 437 | + await expect(serversApi.get("get-evict")).rejects.toThrow("HTTP 500"); |
| 438 | + |
| 439 | + // The failed request was removed from the cache, so this refetches. |
| 440 | + mockFetch.mockResolvedValueOnce(jsonResponse({ id: "get-evict" })); |
| 441 | + const result = await serversApi.get("get-evict"); |
| 442 | + |
| 443 | + expect(result).toEqual({ id: "get-evict" }); |
| 444 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 445 | + }); |
| 446 | + }); |
| 447 | + |
| 448 | + describe("testConnection", () => { |
| 449 | + it("POSTs /gateways/:id/test and returns the result", async () => { |
| 450 | + mockFetch.mockResolvedValueOnce( |
| 451 | + new Response(JSON.stringify({ success: true, message: "Reachable" }), { |
| 452 | + status: 200, |
| 453 | + headers: { "Content-Type": "application/json" }, |
| 454 | + }), |
| 455 | + ); |
| 456 | + |
| 457 | + const result = await serversApi.testConnection("server-123"); |
| 458 | + |
| 459 | + expect(result).toEqual({ success: true, message: "Reachable" }); |
| 460 | + expect(mockFetch).toHaveBeenCalledWith( |
| 461 | + expect.stringContaining("/gateways/server-123/test"), |
| 462 | + expect.objectContaining({ method: "POST" }), |
| 463 | + ); |
| 464 | + }); |
| 465 | + |
| 466 | + it("throws synchronously for an invalid server ID", () => { |
| 467 | + expect(() => serversApi.testConnection("../etc/passwd")).toThrow("Invalid server ID format"); |
| 468 | + }); |
| 469 | + }); |
| 470 | + |
| 471 | + describe("delete", () => { |
| 472 | + it("DELETEs /gateways/:id", async () => { |
| 473 | + mockFetch.mockResolvedValueOnce( |
| 474 | + new Response(JSON.stringify({}), { |
| 475 | + status: 200, |
| 476 | + headers: { "Content-Type": "application/json" }, |
| 477 | + }), |
| 478 | + ); |
| 479 | + |
| 480 | + await serversApi.delete("server-123"); |
| 481 | + |
| 482 | + expect(mockFetch).toHaveBeenCalledWith( |
| 483 | + expect.stringContaining("/gateways/server-123"), |
| 484 | + expect.objectContaining({ method: "DELETE" }), |
| 485 | + ); |
| 486 | + }); |
| 487 | + |
| 488 | + it("throws synchronously for an invalid server ID", () => { |
| 489 | + expect(() => serversApi.delete("../etc/passwd")).toThrow("Invalid server ID format"); |
| 490 | + }); |
| 491 | + }); |
| 492 | + |
| 493 | + describe("testConnectivity", () => { |
| 494 | + it("POSTs the request to /v1/mcp-servers/test and returns the response", async () => { |
| 495 | + const upstream = { statusCode: 200, body: "ok" }; |
| 496 | + mockFetch.mockResolvedValueOnce( |
| 497 | + new Response(JSON.stringify(upstream), { |
| 498 | + status: 200, |
| 499 | + headers: { "Content-Type": "application/json" }, |
| 500 | + }), |
| 501 | + ); |
| 502 | + |
| 503 | + const request: GatewayTestRequest = { |
| 504 | + method: "GET", |
| 505 | + baseUrl: "https://example.com", |
| 506 | + path: "/health", |
| 507 | + }; |
| 508 | + const result = await serversApi.testConnectivity(request); |
| 509 | + |
| 510 | + expect(result).toEqual(upstream); |
| 511 | + expect(mockFetch).toHaveBeenCalledWith( |
| 512 | + expect.stringContaining("/v1/mcp-servers/test"), |
| 513 | + expect.objectContaining({ method: "POST", body: JSON.stringify(request) }), |
| 514 | + ); |
| 515 | + }); |
| 516 | + }); |
302 | 517 | }); |
0 commit comments