| 1 | import { describe, expect, test, beforeEach, vi } from "vitest"; |
| 2 | |
| 3 | // Mock the config module |
| 4 | vi.mock("../config.js"); |
| 5 | import * as config from "../config.js"; |
| 6 | |
| 7 | import { ApiClientService } from "./ApiClientService.js"; |
| 8 | |
| 9 | describe("ApiClientService", () => { |
| 10 | let service: ApiClientService; |
| 11 | const mockApiClient = { |
| 12 | get: vi.fn(), |
| 13 | post: vi.fn(), |
| 14 | put: vi.fn(), |
| 15 | delete: vi.fn(), |
| 16 | }; |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | vi.resetAllMocks(); |
| 20 | service = new ApiClientService(); |
| 21 | }); |
| 22 | |
| 23 | describe("State Management", () => { |
| 24 | test("should initialize with null api client", async () => { |
| 25 | vi.mocked(config.getApiClient).mockReturnValue(null as any); |
| 26 | |
| 27 | const state = await service.initialize({ accessToken: null } as any); |
| 28 | |
| 29 | expect(state).toEqual({ |
| 30 | apiClient: null, |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | test("should initialize with api client when token provided", async () => { |
| 35 | vi.mocked(config.getApiClient).mockReturnValue(mockApiClient as any); |
| 36 | |
| 37 | const state = await service.initialize({ |
| 38 | accessToken: "test-token", |
| 39 | } as any); |
| 40 | |
| 41 | expect(state).toEqual({ |
| 42 | apiClient: mockApiClient, |
| 43 | }); |
| 44 | expect(vi.mocked(config.getApiClient)).toHaveBeenCalledWith("test-token"); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | describe("update()", () => { |
| 49 | test("should update api client with new auth config", async () => { |
| 50 | // Initialize first |
| 51 | vi.mocked(config.getApiClient).mockReturnValue(null as any); |
| 52 | await service.initialize({ accessToken: null } as any); |
| 53 | |
| 54 | // Update with new token |
| 55 | vi.mocked(config.getApiClient).mockReturnValue(mockApiClient as any); |
| 56 | const state = await service.update({ accessToken: "new-token" } as any); |
| 57 | |
| 58 | expect(state).toEqual({ |
| 59 | apiClient: mockApiClient, |
| 60 | }); |
| 61 | expect(service.getState()).toEqual(state); |
| 62 | }); |
| 63 | |
| 64 | test("should handle update errors", async () => { |
| 65 | await service.initialize({ accessToken: null } as any); |
| 66 | |
| 67 | vi.mocked(config.getApiClient).mockImplementation(() => { |
| 68 | throw new Error("Failed to create client"); |
| 69 | }); |
| 70 | |
| 71 | await expect( |
| 72 | service.update({ accessToken: "bad-token" } as any), |
| 73 | ).rejects.toThrow("Failed to create client"); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | describe("isReady()", () => { |
| 78 | test("should return false when not initialized", () => { |
| 79 | expect(service.isReady()).toBe(false); |
| 80 | }); |
| 81 | |
| 82 | test("should return false when api client is null", async () => { |
| 83 | vi.mocked(config.getApiClient).mockReturnValue(null as any); |
| 84 | await service.initialize({ accessToken: null } as any); |
| 85 | |
| 86 | expect(service.isReady()).toBe(false); |
| 87 | }); |
| 88 | |
| 89 | test("should return true when api client exists", async () => { |
| 90 | vi.mocked(config.getApiClient).mockReturnValue(mockApiClient as any); |
| 91 | await service.initialize({ accessToken: "token" } as any); |
| 92 | |
| 93 | expect(service.isReady()).toBe(true); |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | describe("getDependencies()", () => { |
| 98 | test("should declare auth dependency", () => { |
| 99 | expect(service.getDependencies()).toEqual(["auth"]); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("Event Emission", () => { |
| 104 | test("should emit stateChanged on update", async () => { |
| 105 | vi.mocked(config.getApiClient).mockReturnValue(null as any); |
| 106 | await service.initialize({ accessToken: null } as any); |
| 107 | |
| 108 | const listener = vi.fn(); |
| 109 | service.on("stateChanged", listener); |
| 110 | |
| 111 | vi.mocked(config.getApiClient).mockReturnValue(mockApiClient as any); |
| 112 | await service.update({ accessToken: "new-token" } as any); |
| 113 | |
| 114 | expect(listener).toHaveBeenCalledWith( |
| 115 | expect.objectContaining({ apiClient: mockApiClient }), |
| 116 | expect.objectContaining({ apiClient: null }), |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | test("should emit error on update failure", async () => { |
| 121 | await service.initialize({ accessToken: null } as any); |
| 122 | |
| 123 | const errorListener = vi.fn(); |
| 124 | service.on("error", errorListener); |
| 125 | |
| 126 | const error = new Error("Update failed"); |
| 127 | vi.mocked(config.getApiClient).mockImplementation(() => { |
| 128 | throw error; |
| 129 | }); |
| 130 | |
| 131 | await expect( |
| 132 | service.update({ accessToken: "bad" } as any), |
| 133 | ).rejects.toThrow(); |
| 134 | expect(errorListener).toHaveBeenCalledWith(error); |
| 135 | }); |
| 136 | }); |
| 137 | }); |