|
1 | 1 | // npx vitest services/marketplace/__tests__/RemoteConfigLoader.spec.ts |
2 | 2 |
|
3 | 3 | import axios from "axios" |
| 4 | +import * as fs from "fs" |
| 5 | +import * as path from "path" |
4 | 6 | import { RemoteConfigLoader } from "../RemoteConfigLoader" |
5 | 7 | import type { MarketplaceItemType } from "@roo-code/types" |
6 | 8 |
|
7 | 9 | // Mock axios |
8 | 10 | vi.mock("axios") |
9 | 11 | const mockedAxios = axios as any |
10 | 12 |
|
| 13 | +// Mock fs |
| 14 | +vi.mock("fs") |
| 15 | +const mockedFs = fs as any |
| 16 | + |
| 17 | +// Mock path |
| 18 | +vi.mock("path") |
| 19 | +const mockedPath = path as any |
| 20 | + |
11 | 21 | // Mock the cloud config |
12 | 22 | vi.mock("@roo-code/cloud", () => ({ |
13 | 23 | getRooCodeApiUrl: () => "https://test.api.com", |
@@ -332,4 +342,202 @@ describe("RemoteConfigLoader", () => { |
332 | 342 | Date.now = originalDateNow |
333 | 343 | }) |
334 | 344 | }) |
| 345 | + |
| 346 | + describe("local fallback functionality", () => { |
| 347 | + beforeEach(() => { |
| 348 | + // Reset mocks |
| 349 | + vi.clearAllMocks() |
| 350 | + loader.clearCache() |
| 351 | + // Mock path.join to return a predictable path |
| 352 | + mockedPath.join.mockReturnValue("/test/path/data/mcps.yaml") |
| 353 | + }) |
| 354 | + |
| 355 | + it("should fallback to local data when remote API fails", async () => { |
| 356 | + const localMcpsYaml = `items: |
| 357 | + - id: "test-mcp" |
| 358 | + name: "Test MCP" |
| 359 | + description: "A test MCP" |
| 360 | + url: "https://github.com/test/test-mcp" |
| 361 | + content: |
| 362 | + - name: "Installation" |
| 363 | + content: '{"command": "test"}'` |
| 364 | + |
| 365 | + // Mock remote API failure |
| 366 | + mockedAxios.get.mockImplementation((url: string) => { |
| 367 | + if (url.includes("/modes")) { |
| 368 | + return Promise.resolve({ data: "items: []" }) |
| 369 | + } |
| 370 | + if (url.includes("/mcps")) { |
| 371 | + return Promise.reject(new Error("Network error")) |
| 372 | + } |
| 373 | + return Promise.reject(new Error("Unknown URL")) |
| 374 | + }) |
| 375 | + |
| 376 | + // Mock local file system |
| 377 | + mockedFs.existsSync.mockReturnValue(true) |
| 378 | + mockedFs.readFileSync.mockReturnValue(localMcpsYaml) |
| 379 | + |
| 380 | + const items = await loader.loadAllItems() |
| 381 | + |
| 382 | + // Should have attempted remote call first |
| 383 | + expect(mockedAxios.get).toHaveBeenCalledWith( |
| 384 | + "https://test.api.com/api/marketplace/mcps", |
| 385 | + expect.any(Object), |
| 386 | + ) |
| 387 | + |
| 388 | + // Should have fallen back to local file |
| 389 | + expect(mockedFs.existsSync).toHaveBeenCalled() |
| 390 | + expect(mockedFs.readFileSync).toHaveBeenCalled() |
| 391 | + |
| 392 | + // Should contain the test MCP |
| 393 | + expect(items).toHaveLength(1) |
| 394 | + expect(items[0]).toEqual({ |
| 395 | + type: "mcp", |
| 396 | + id: "test-mcp", |
| 397 | + name: "Test MCP", |
| 398 | + description: "A test MCP", |
| 399 | + url: "https://github.com/test/test-mcp", |
| 400 | + content: [ |
| 401 | + { |
| 402 | + name: "Installation", |
| 403 | + content: '{"command": "test"}', |
| 404 | + }, |
| 405 | + ], |
| 406 | + }) |
| 407 | + }) |
| 408 | + |
| 409 | + it("should return empty array when local file doesn't exist", async () => { |
| 410 | + // Mock remote API failure |
| 411 | + mockedAxios.get.mockImplementation((url: string) => { |
| 412 | + if (url.includes("/modes")) { |
| 413 | + return Promise.resolve({ data: "items: []" }) |
| 414 | + } |
| 415 | + if (url.includes("/mcps")) { |
| 416 | + return Promise.reject(new Error("Network error")) |
| 417 | + } |
| 418 | + return Promise.reject(new Error("Unknown URL")) |
| 419 | + }) |
| 420 | + |
| 421 | + // Mock local file not existing |
| 422 | + mockedFs.existsSync.mockReturnValue(false) |
| 423 | + |
| 424 | + const items = await loader.loadAllItems() |
| 425 | + |
| 426 | + // Should have attempted remote call first |
| 427 | + expect(mockedAxios.get).toHaveBeenCalledWith( |
| 428 | + "https://test.api.com/api/marketplace/mcps", |
| 429 | + expect.any(Object), |
| 430 | + ) |
| 431 | + |
| 432 | + // Should have checked for local file |
| 433 | + expect(mockedFs.existsSync).toHaveBeenCalledWith(expect.stringContaining("data/mcps.yaml")) |
| 434 | + |
| 435 | + // Should not have tried to read the file |
| 436 | + expect(mockedFs.readFileSync).not.toHaveBeenCalled() |
| 437 | + |
| 438 | + // Should return empty array (only modes, no MCPs) |
| 439 | + expect(items).toHaveLength(0) |
| 440 | + }) |
| 441 | + |
| 442 | + it("should handle local file read errors gracefully", async () => { |
| 443 | + // Mock remote API failure |
| 444 | + mockedAxios.get.mockImplementation((url: string) => { |
| 445 | + if (url.includes("/modes")) { |
| 446 | + return Promise.resolve({ data: "items: []" }) |
| 447 | + } |
| 448 | + if (url.includes("/mcps")) { |
| 449 | + return Promise.reject(new Error("Network error")) |
| 450 | + } |
| 451 | + return Promise.reject(new Error("Unknown URL")) |
| 452 | + }) |
| 453 | + |
| 454 | + // Mock local file exists but read fails |
| 455 | + mockedFs.existsSync.mockReturnValue(true) |
| 456 | + mockedFs.readFileSync.mockImplementation(() => { |
| 457 | + throw new Error("File read error") |
| 458 | + }) |
| 459 | + |
| 460 | + const items = await loader.loadAllItems() |
| 461 | + |
| 462 | + // Should have attempted to read local file |
| 463 | + expect(mockedFs.readFileSync).toHaveBeenCalledWith(expect.stringContaining("data/mcps.yaml"), "utf-8") |
| 464 | + |
| 465 | + // Should return empty array when local fallback fails |
| 466 | + expect(items).toHaveLength(0) |
| 467 | + }) |
| 468 | + |
| 469 | + it("should prefer remote data over local when remote is available", async () => { |
| 470 | + const remoteMcpsYaml = `items: |
| 471 | + - id: "remote-mcp" |
| 472 | + name: "Remote MCP" |
| 473 | + description: "From remote API" |
| 474 | + url: "https://github.com/remote/mcp" |
| 475 | + content: |
| 476 | + - name: "Installation" |
| 477 | + content: '{"command": "remote"}'` |
| 478 | + |
| 479 | + // Mock successful remote API |
| 480 | + mockedAxios.get.mockImplementation((url: string) => { |
| 481 | + if (url.includes("/modes")) { |
| 482 | + return Promise.resolve({ data: "items: []" }) |
| 483 | + } |
| 484 | + if (url.includes("/mcps")) { |
| 485 | + return Promise.resolve({ data: remoteMcpsYaml }) |
| 486 | + } |
| 487 | + return Promise.reject(new Error("Unknown URL")) |
| 488 | + }) |
| 489 | + |
| 490 | + const items = await loader.loadAllItems() |
| 491 | + |
| 492 | + // Should have used remote data |
| 493 | + expect(items).toHaveLength(1) |
| 494 | + expect(items[0].id).toBe("remote-mcp") |
| 495 | + expect(items[0].name).toBe("Remote MCP") |
| 496 | + |
| 497 | + // Should not have accessed local file system |
| 498 | + expect(mockedFs.existsSync).not.toHaveBeenCalled() |
| 499 | + expect(mockedFs.readFileSync).not.toHaveBeenCalled() |
| 500 | + }) |
| 501 | + }) |
| 502 | + |
| 503 | + describe("Google Researcher MCP Server integration", () => { |
| 504 | + it("should find Google Researcher MCP by ID when loaded from local data", async () => { |
| 505 | + const localMcpsYaml = `items: |
| 506 | + - id: "google-researcher-mcp" |
| 507 | + name: "Google Researcher MCP Server" |
| 508 | + description: "Power your AI agents with Google Search–enhanced research" |
| 509 | + author: "Zohar Babin" |
| 510 | + url: "https://github.com/zoharbabin/google-research-mcp" |
| 511 | + content: |
| 512 | + - name: "STDIO Installation" |
| 513 | + content: '{"google-researcher": {"command": "npx", "args": ["google-researcher-mcp@latest"]}}'` |
| 514 | + |
| 515 | + // Mock remote API failure to trigger local fallback |
| 516 | + mockedAxios.get.mockImplementation((url: string) => { |
| 517 | + if (url.includes("/modes")) { |
| 518 | + return Promise.resolve({ data: "items: []" }) |
| 519 | + } |
| 520 | + if (url.includes("/mcps")) { |
| 521 | + return Promise.reject(new Error("Network error")) |
| 522 | + } |
| 523 | + return Promise.reject(new Error("Unknown URL")) |
| 524 | + }) |
| 525 | + |
| 526 | + // Mock local file system |
| 527 | + mockedFs.existsSync.mockReturnValue(true) |
| 528 | + mockedFs.readFileSync.mockReturnValue(localMcpsYaml) |
| 529 | + |
| 530 | + const item = await loader.getItem("google-researcher-mcp", "mcp" as MarketplaceItemType) |
| 531 | + |
| 532 | + expect(item).not.toBeNull() |
| 533 | + expect(item?.id).toBe("google-researcher-mcp") |
| 534 | + expect(item?.name).toBe("Google Researcher MCP Server") |
| 535 | + expect(item?.author).toBe("Zohar Babin") |
| 536 | + |
| 537 | + // Type guard to ensure we have an MCP item |
| 538 | + if (item?.type === "mcp") { |
| 539 | + expect(item.url).toBe("https://github.com/zoharbabin/google-research-mcp") |
| 540 | + } |
| 541 | + }) |
| 542 | + }) |
335 | 543 | }) |
0 commit comments