|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | + |
| 4 | +import { StaticTokenAuthService } from "../../auth/StaticTokenAuthService" |
| 5 | + |
| 6 | +// Mock vscode |
| 7 | +vi.mock("vscode", () => ({ |
| 8 | + window: { |
| 9 | + showInformationMessage: vi.fn(), |
| 10 | + }, |
| 11 | + env: { |
| 12 | + openExternal: vi.fn(), |
| 13 | + uriScheme: "vscode", |
| 14 | + }, |
| 15 | + Uri: { |
| 16 | + parse: vi.fn(), |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +describe("StaticTokenAuthService", () => { |
| 21 | + let authService: StaticTokenAuthService |
| 22 | + let mockContext: vscode.ExtensionContext |
| 23 | + let mockLog: (...args: unknown[]) => void |
| 24 | + const testToken = "test-static-token" |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockLog = vi.fn() |
| 28 | + |
| 29 | + // Create a minimal mock that satisfies the constructor requirements |
| 30 | + const mockContextPartial = { |
| 31 | + extension: { |
| 32 | + packageJSON: { |
| 33 | + publisher: "TestPublisher", |
| 34 | + name: "test-extension", |
| 35 | + }, |
| 36 | + }, |
| 37 | + globalState: { |
| 38 | + get: vi.fn(), |
| 39 | + update: vi.fn(), |
| 40 | + }, |
| 41 | + secrets: { |
| 42 | + get: vi.fn(), |
| 43 | + store: vi.fn(), |
| 44 | + delete: vi.fn(), |
| 45 | + onDidChange: vi.fn(), |
| 46 | + }, |
| 47 | + subscriptions: [], |
| 48 | + } |
| 49 | + |
| 50 | + // Use type assertion for test mocking |
| 51 | + mockContext = mockContextPartial as unknown as vscode.ExtensionContext |
| 52 | + |
| 53 | + authService = new StaticTokenAuthService(mockContext, testToken, mockLog) |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + vi.clearAllMocks() |
| 58 | + }) |
| 59 | + |
| 60 | + describe("constructor", () => { |
| 61 | + it("should create instance and log static token mode", () => { |
| 62 | + expect(authService).toBeInstanceOf(StaticTokenAuthService) |
| 63 | + expect(mockLog).toHaveBeenCalledWith("[auth] Using static token authentication mode") |
| 64 | + }) |
| 65 | + |
| 66 | + it("should use console.log as default logger", () => { |
| 67 | + const serviceWithoutLog = new StaticTokenAuthService( |
| 68 | + mockContext as unknown as vscode.ExtensionContext, |
| 69 | + testToken, |
| 70 | + ) |
| 71 | + // Can't directly test console.log usage, but constructor should not throw |
| 72 | + expect(serviceWithoutLog).toBeInstanceOf(StaticTokenAuthService) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe("initialize", () => { |
| 77 | + it("should start in active-session state", async () => { |
| 78 | + await authService.initialize() |
| 79 | + expect(authService.getState()).toBe("active-session") |
| 80 | + }) |
| 81 | + |
| 82 | + it("should emit active-session event on initialize", async () => { |
| 83 | + const spy = vi.fn() |
| 84 | + authService.on("active-session", spy) |
| 85 | + |
| 86 | + await authService.initialize() |
| 87 | + |
| 88 | + expect(spy).toHaveBeenCalledWith({ previousState: "initializing" }) |
| 89 | + }) |
| 90 | + |
| 91 | + it("should log successful initialization", async () => { |
| 92 | + await authService.initialize() |
| 93 | + expect(mockLog).toHaveBeenCalledWith("[auth] Static token auth service initialized in active-session state") |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe("getSessionToken", () => { |
| 98 | + it("should return the provided token", () => { |
| 99 | + expect(authService.getSessionToken()).toBe(testToken) |
| 100 | + }) |
| 101 | + |
| 102 | + it("should return different token when constructed with different token", () => { |
| 103 | + const differentToken = "different-token" |
| 104 | + const differentService = new StaticTokenAuthService(mockContext, differentToken, mockLog) |
| 105 | + expect(differentService.getSessionToken()).toBe(differentToken) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe("getUserInfo", () => { |
| 110 | + it("should return empty object", () => { |
| 111 | + expect(authService.getUserInfo()).toEqual({}) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe("getStoredOrganizationId", () => { |
| 116 | + it("should return null", () => { |
| 117 | + expect(authService.getStoredOrganizationId()).toBeNull() |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + describe("authentication state methods", () => { |
| 122 | + it("should always return true for isAuthenticated", () => { |
| 123 | + expect(authService.isAuthenticated()).toBe(true) |
| 124 | + }) |
| 125 | + |
| 126 | + it("should always return true for hasActiveSession", () => { |
| 127 | + expect(authService.hasActiveSession()).toBe(true) |
| 128 | + }) |
| 129 | + |
| 130 | + it("should always return true for hasOrIsAcquiringActiveSession", () => { |
| 131 | + expect(authService.hasOrIsAcquiringActiveSession()).toBe(true) |
| 132 | + }) |
| 133 | + |
| 134 | + it("should return active-session for getState", () => { |
| 135 | + expect(authService.getState()).toBe("active-session") |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + describe("disabled authentication methods", () => { |
| 140 | + const expectedErrorMessage = "Authentication methods are disabled in StaticTokenAuthService" |
| 141 | + |
| 142 | + it("should throw error for login", async () => { |
| 143 | + await expect(authService.login()).rejects.toThrow(expectedErrorMessage) |
| 144 | + }) |
| 145 | + |
| 146 | + it("should throw error for logout", async () => { |
| 147 | + await expect(authService.logout()).rejects.toThrow(expectedErrorMessage) |
| 148 | + }) |
| 149 | + |
| 150 | + it("should throw error for handleCallback", async () => { |
| 151 | + await expect(authService.handleCallback("code", "state")).rejects.toThrow(expectedErrorMessage) |
| 152 | + }) |
| 153 | + |
| 154 | + it("should throw error for handleCallback with organization", async () => { |
| 155 | + await expect(authService.handleCallback("code", "state", "org_123")).rejects.toThrow(expectedErrorMessage) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe("event emission", () => { |
| 160 | + it("should be able to register and emit events", async () => { |
| 161 | + const activeSessionSpy = vi.fn() |
| 162 | + const userInfoSpy = vi.fn() |
| 163 | + |
| 164 | + authService.on("active-session", activeSessionSpy) |
| 165 | + authService.on("user-info", userInfoSpy) |
| 166 | + |
| 167 | + await authService.initialize() |
| 168 | + |
| 169 | + expect(activeSessionSpy).toHaveBeenCalledWith({ previousState: "initializing" }) |
| 170 | + // user-info event is not emitted in static token mode |
| 171 | + expect(userInfoSpy).not.toHaveBeenCalled() |
| 172 | + }) |
| 173 | + }) |
| 174 | +}) |
0 commit comments