|
| 1 | +import { describe, it, expect, vi, beforeEach, type Mock } from "vitest" |
| 2 | +import { CloudAPI } from "../CloudAPI.js" |
| 3 | +import { AuthenticationError, CloudAPIError } from "../errors.js" |
| 4 | +import type { AuthService } from "@roo-code/types" |
| 5 | + |
| 6 | +// Mock the config module |
| 7 | +vi.mock("../config.js", () => ({ |
| 8 | + getRooCodeApiUrl: () => "https://api.test.com", |
| 9 | +})) |
| 10 | + |
| 11 | +// Mock the utils module |
| 12 | +vi.mock("../utils.js", () => ({ |
| 13 | + getUserAgent: () => "test-user-agent", |
| 14 | +})) |
| 15 | + |
| 16 | +describe("CloudAPI.creditBalance", () => { |
| 17 | + let mockAuthService: { |
| 18 | + getSessionToken: Mock<() => string | undefined> |
| 19 | + } |
| 20 | + let cloudAPI: CloudAPI |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + mockAuthService = { |
| 24 | + getSessionToken: vi.fn(), |
| 25 | + } |
| 26 | + cloudAPI = new CloudAPI(mockAuthService as unknown as AuthService) |
| 27 | + |
| 28 | + // Reset fetch mock |
| 29 | + global.fetch = vi.fn() |
| 30 | + }) |
| 31 | + |
| 32 | + it("should fetch credit balance successfully", async () => { |
| 33 | + const mockBalance = 12.34 |
| 34 | + mockAuthService.getSessionToken.mockReturnValue("test-session-token") |
| 35 | + |
| 36 | + global.fetch = vi.fn().mockResolvedValue({ |
| 37 | + ok: true, |
| 38 | + json: async () => ({ balance: mockBalance }), |
| 39 | + }) |
| 40 | + |
| 41 | + const balance = await cloudAPI.creditBalance() |
| 42 | + |
| 43 | + expect(balance).toBe(mockBalance) |
| 44 | + expect(global.fetch).toHaveBeenCalledWith( |
| 45 | + "https://api.test.com/api/extension/credit-balance", |
| 46 | + expect.objectContaining({ |
| 47 | + method: "GET", |
| 48 | + headers: expect.objectContaining({ |
| 49 | + Authorization: "Bearer test-session-token", |
| 50 | + "Content-Type": "application/json", |
| 51 | + "User-Agent": "test-user-agent", |
| 52 | + }), |
| 53 | + }), |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it("should throw AuthenticationError when session token is missing", async () => { |
| 58 | + mockAuthService.getSessionToken.mockReturnValue(undefined) |
| 59 | + |
| 60 | + await expect(cloudAPI.creditBalance()).rejects.toThrow(AuthenticationError) |
| 61 | + }) |
| 62 | + |
| 63 | + it("should handle API errors", async () => { |
| 64 | + mockAuthService.getSessionToken.mockReturnValue("test-session-token") |
| 65 | + |
| 66 | + global.fetch = vi.fn().mockResolvedValue({ |
| 67 | + ok: false, |
| 68 | + status: 500, |
| 69 | + statusText: "Internal Server Error", |
| 70 | + json: async () => ({ error: "Server error" }), |
| 71 | + }) |
| 72 | + |
| 73 | + await expect(cloudAPI.creditBalance()).rejects.toThrow(CloudAPIError) |
| 74 | + }) |
| 75 | + |
| 76 | + it("should handle network errors", async () => { |
| 77 | + mockAuthService.getSessionToken.mockReturnValue("test-session-token") |
| 78 | + |
| 79 | + global.fetch = vi.fn().mockRejectedValue(new TypeError("fetch failed")) |
| 80 | + |
| 81 | + await expect(cloudAPI.creditBalance()).rejects.toThrow( |
| 82 | + "Network error while calling /api/extension/credit-balance", |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it("should handle invalid response format", async () => { |
| 87 | + mockAuthService.getSessionToken.mockReturnValue("test-session-token") |
| 88 | + |
| 89 | + global.fetch = vi.fn().mockResolvedValue({ |
| 90 | + ok: true, |
| 91 | + json: async () => ({ invalid: "response" }), |
| 92 | + }) |
| 93 | + |
| 94 | + await expect(cloudAPI.creditBalance()).rejects.toThrow() |
| 95 | + }) |
| 96 | +}) |
0 commit comments