| 1 | import { describe, expect, test, beforeEach, vi } from "vitest"; |
| 2 | |
| 3 | // Mock the workos module |
| 4 | vi.mock("../auth/workos.js"); |
| 5 | |
| 6 | // Import the workos functions we need to mock |
| 7 | import { |
| 8 | loadAuthConfig, |
| 9 | isAuthenticated, |
| 10 | login, |
| 11 | logout, |
| 12 | ensureOrganization, |
| 13 | saveAuthConfig, |
| 14 | listUserOrganizations, |
| 15 | } from "../auth/workos.js"; |
| 16 | |
| 17 | import { AuthService } from "./AuthService.js"; |
| 18 | |
| 19 | describe("AuthService", () => { |
| 20 | let service: AuthService; |
| 21 | const mockAuthConfig = { |
| 22 | accessToken: "test-token", |
| 23 | organizationId: "org-123", |
| 24 | userId: "user-123", |
| 25 | userEmail: "test@example.com", |
| 26 | refreshToken: "refresh-token", |
| 27 | expiresAt: Date.now() + 3600000, |
| 28 | }; |
| 29 | |
| 30 | beforeEach(() => { |
| 31 | vi.clearAllMocks(); |
| 32 | service = new AuthService(); |
| 33 | }); |
| 34 | |
| 35 | describe("State Management", () => { |
| 36 | test("should initialize with unauthenticated state", async () => { |
| 37 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 38 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 39 | |
| 40 | const state = await service.initialize(); |
| 41 | |
| 42 | expect(state).toEqual({ |
| 43 | authConfig: null, |
| 44 | isAuthenticated: false, |
| 45 | organizationId: undefined, |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | test("should initialize with authenticated state", async () => { |
| 50 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 51 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 52 | |
| 53 | const state = await service.initialize(); |
| 54 | |
| 55 | expect(state).toEqual({ |
| 56 | authConfig: mockAuthConfig, |
| 57 | isAuthenticated: true, |
| 58 | organizationId: "org-123", |
| 59 | }); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | describe("login()", () => { |
| 64 | test("should update state after successful login", async () => { |
| 65 | // Initialize first |
| 66 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 67 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 68 | await service.initialize(); |
| 69 | |
| 70 | // Mock successful login |
| 71 | vi.mocked(login).mockResolvedValue(mockAuthConfig); |
| 72 | |
| 73 | const state = await service.login(); |
| 74 | |
| 75 | expect(state).toEqual({ |
| 76 | authConfig: mockAuthConfig, |
| 77 | isAuthenticated: true, |
| 78 | organizationId: "org-123", |
| 79 | }); |
| 80 | expect(service.getState()).toEqual(state); |
| 81 | }); |
| 82 | |
| 83 | test("should handle login failure", async () => { |
| 84 | await service.initialize(); |
| 85 | const loginError = new Error("Login failed"); |
| 86 | vi.mocked(login).mockRejectedValue(loginError); |
| 87 | |
| 88 | await expect(service.login()).rejects.toThrow("Login failed"); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | describe("logout()", () => { |
| 93 | test("should clear state after logout", async () => { |
| 94 | // Initialize with authenticated state |
| 95 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 96 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 97 | await service.initialize(); |
| 98 | |
| 99 | const state = await service.logout(); |
| 100 | |
| 101 | expect(state).toEqual({ |
| 102 | authConfig: null, |
| 103 | isAuthenticated: false, |
| 104 | organizationId: undefined, |
| 105 | }); |
| 106 | expect(logout).toHaveBeenCalled(); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe("ensureOrganization()", () => { |
| 111 | test("should update organization after ensuring", async () => { |
| 112 | // Initialize with auth but no org |
| 113 | const authWithoutOrg = { ...mockAuthConfig, organizationId: null }; |
| 114 | vi.mocked(loadAuthConfig).mockReturnValue(authWithoutOrg); |
| 115 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 116 | await service.initialize(); |
| 117 | |
| 118 | // Mock ensure organization |
| 119 | vi.mocked(ensureOrganization).mockResolvedValue(mockAuthConfig); |
| 120 | |
| 121 | const state = await service.ensureOrganization(); |
| 122 | |
| 123 | expect(state).toEqual({ |
| 124 | authConfig: mockAuthConfig, |
| 125 | isAuthenticated: true, |
| 126 | organizationId: "org-123", |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | test("should throw error if not authenticated", async () => { |
| 131 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 132 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 133 | await service.initialize(); |
| 134 | |
| 135 | await expect(service.ensureOrganization()).rejects.toThrow( |
| 136 | "Not authenticated - cannot ensure organization", |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | test("should pass organization slug to ensureOrganization in headless mode", async () => { |
| 141 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 142 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 143 | await service.initialize(); |
| 144 | |
| 145 | const updatedConfig = { ...mockAuthConfig, organizationId: "org-456" }; |
| 146 | vi.mocked(ensureOrganization).mockResolvedValue(updatedConfig); |
| 147 | |
| 148 | const state = await service.ensureOrganization(true, "org-slug"); |
| 149 | |
| 150 | expect(ensureOrganization).toHaveBeenCalledWith( |
| 151 | mockAuthConfig, |
| 152 | true, |
| 153 | "org-slug", |
| 154 | ); |
| 155 | expect(state).toEqual({ |
| 156 | authConfig: updatedConfig, |
| 157 | isAuthenticated: true, |
| 158 | organizationId: "org-456", |
| 159 | }); |
| 160 | }); |
| 161 | |
| 162 | test("should handle personal organization slug", async () => { |
| 163 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 164 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 165 | await service.initialize(); |
| 166 | |
| 167 | const personalConfig = { ...mockAuthConfig, organizationId: null }; |
| 168 | vi.mocked(ensureOrganization).mockResolvedValue(personalConfig); |
| 169 | |
| 170 | const state = await service.ensureOrganization(true, "personal"); |
| 171 | |
| 172 | expect(ensureOrganization).toHaveBeenCalledWith( |
| 173 | mockAuthConfig, |
| 174 | true, |
| 175 | "personal", |
| 176 | ); |
| 177 | expect(state).toEqual({ |
| 178 | authConfig: personalConfig, |
| 179 | isAuthenticated: true, |
| 180 | organizationId: undefined, |
| 181 | }); |
| 182 | }); |
| 183 | |
| 184 | test("should work without organization slug parameter", async () => { |
| 185 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 186 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 187 | await service.initialize(); |
| 188 | |
| 189 | vi.mocked(ensureOrganization).mockResolvedValue(mockAuthConfig); |
| 190 | |
| 191 | const state = await service.ensureOrganization(false); |
| 192 | |
| 193 | expect(ensureOrganization).toHaveBeenCalledWith( |
| 194 | mockAuthConfig, |
| 195 | false, |
| 196 | undefined, |
| 197 | ); |
| 198 | expect(state).toEqual({ |
| 199 | authConfig: mockAuthConfig, |
| 200 | isAuthenticated: true, |
| 201 | organizationId: "org-123", |
| 202 | }); |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | describe("switchOrganization()", () => { |
| 207 | test("should update state with new organization", async () => { |
| 208 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 209 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 210 | await service.initialize(); |
| 211 | |
| 212 | const state = await service.switchOrganization("org-456"); |
| 213 | |
| 214 | expect(state).toEqual({ |
| 215 | authConfig: { |
| 216 | ...mockAuthConfig, |
| 217 | organizationId: "org-456", |
| 218 | }, |
| 219 | isAuthenticated: true, |
| 220 | organizationId: "org-456", |
| 221 | }); |
| 222 | expect(saveAuthConfig).toHaveBeenCalledWith({ |
| 223 | ...mockAuthConfig, |
| 224 | organizationId: "org-456", |
| 225 | }); |
| 226 | }); |
| 227 | |
| 228 | test("should handle null organization", async () => { |
| 229 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 230 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 231 | await service.initialize(); |
| 232 | |
| 233 | const state = await service.switchOrganization(null); |
| 234 | |
| 235 | expect(state.organizationId).toBeUndefined(); |
| 236 | }); |
| 237 | |
| 238 | test("should throw error if not file-based auth", async () => { |
| 239 | const tokenOnlyAuth = { accessToken: "token" } as any; |
| 240 | vi.mocked(loadAuthConfig).mockReturnValue(tokenOnlyAuth); |
| 241 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 242 | await service.initialize(); |
| 243 | |
| 244 | await expect(service.switchOrganization("org-456")).rejects.toThrow( |
| 245 | "Not authenticated with file-based auth", |
| 246 | ); |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | describe("getAvailableOrganizations()", () => { |
| 251 | test("should return organizations when authenticated", async () => { |
| 252 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 253 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 254 | await service.initialize(); |
| 255 | |
| 256 | const mockOrgs = [ |
| 257 | { id: "org-1", name: "Org 1", slug: "org-1" }, |
| 258 | { id: "org-2", name: "Org 2", slug: "org-2" }, |
| 259 | ]; |
| 260 | vi.mocked(listUserOrganizations).mockResolvedValue(mockOrgs); |
| 261 | |
| 262 | const orgs = await service.getAvailableOrganizations(); |
| 263 | expect(orgs).toEqual(mockOrgs); |
| 264 | }); |
| 265 | |
| 266 | test("should return null when not authenticated", async () => { |
| 267 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 268 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 269 | await service.initialize(); |
| 270 | |
| 271 | const orgs = await service.getAvailableOrganizations(); |
| 272 | expect(orgs).toBeNull(); |
| 273 | }); |
| 274 | |
| 275 | test("should handle errors gracefully", async () => { |
| 276 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 277 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 278 | await service.initialize(); |
| 279 | |
| 280 | vi.mocked(listUserOrganizations).mockRejectedValue( |
| 281 | new Error("API error"), |
| 282 | ); |
| 283 | |
| 284 | // Add error event listener to prevent unhandled rejection |
| 285 | const errorHandler = vi.fn(); |
| 286 | service.on("error", errorHandler); |
| 287 | |
| 288 | const orgs = await service.getAvailableOrganizations(); |
| 289 | expect(orgs).toBeNull(); |
| 290 | expect(errorHandler).toHaveBeenCalledWith(expect.any(Error)); |
| 291 | |
| 292 | // Clean up |
| 293 | service.off("error", errorHandler); |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | describe("hasMultipleOrganizations()", () => { |
| 298 | test("should return true when multiple orgs available", async () => { |
| 299 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 300 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 301 | await service.initialize(); |
| 302 | |
| 303 | vi.mocked(listUserOrganizations).mockResolvedValue([ |
| 304 | { id: "org-1", name: "Org 1", slug: "org-1" }, |
| 305 | { id: "org-2", name: "Org 2", slug: "org-2" }, |
| 306 | ]); |
| 307 | |
| 308 | const hasMultiple = await service.hasMultipleOrganizations(); |
| 309 | expect(hasMultiple).toBe(true); |
| 310 | }); |
| 311 | |
| 312 | test("should return false when no orgs available", async () => { |
| 313 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 314 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 315 | await service.initialize(); |
| 316 | |
| 317 | vi.mocked(listUserOrganizations).mockResolvedValue([]); |
| 318 | |
| 319 | const hasMultiple = await service.hasMultipleOrganizations(); |
| 320 | expect(hasMultiple).toBe(false); |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | describe("refresh()", () => { |
| 325 | test("should reload auth state from disk", async () => { |
| 326 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 327 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 328 | await service.initialize(); |
| 329 | |
| 330 | // Update mock to return authenticated state |
| 331 | vi.mocked(loadAuthConfig).mockReturnValue(mockAuthConfig); |
| 332 | vi.mocked(isAuthenticated).mockReturnValue(true); |
| 333 | |
| 334 | const state = await service.refresh(); |
| 335 | |
| 336 | expect(state).toEqual({ |
| 337 | authConfig: mockAuthConfig, |
| 338 | isAuthenticated: true, |
| 339 | organizationId: "org-123", |
| 340 | }); |
| 341 | }); |
| 342 | }); |
| 343 | |
| 344 | describe("Event Emission", () => { |
| 345 | test("should emit stateChanged on login", async () => { |
| 346 | // Initialize with no auth config to simulate logged out state |
| 347 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 348 | vi.mocked(isAuthenticated).mockReturnValue(false); |
| 349 | await service.initialize(); |
| 350 | |
| 351 | const listener = vi.fn(); |
| 352 | service.on("stateChanged", listener); |
| 353 | |
| 354 | vi.mocked(login).mockResolvedValue(mockAuthConfig); |
| 355 | await service.login(); |
| 356 | |
| 357 | expect(listener).toHaveBeenCalledWith( |
| 358 | expect.objectContaining({ |
| 359 | isAuthenticated: true, |
| 360 | authConfig: mockAuthConfig, |
| 361 | organizationId: "org-123", |
| 362 | }), |
| 363 | expect.objectContaining({ |
| 364 | isAuthenticated: false, |
| 365 | authConfig: null, |
| 366 | }), |
| 367 | ); |
| 368 | }); |
| 369 | |
| 370 | test("should emit error on login failure", async () => { |
| 371 | await service.initialize(); |
| 372 | const errorListener = vi.fn(); |
| 373 | service.on("error", errorListener); |
| 374 | |
| 375 | const error = new Error("Login failed"); |
| 376 | vi.mocked(login).mockRejectedValue(error); |
| 377 | |
| 378 | await expect(service.login()).rejects.toThrow(); |
| 379 | expect(errorListener).toHaveBeenCalledWith(error); |
| 380 | }); |
| 381 | }); |
| 382 | }); |