|
| 1 | +/** |
| 2 | + * Complete useAuthQuery tests using QueryObserver |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test' |
| 6 | +import { QueryClient, QueryObserver } from '@tanstack/react-query' |
| 7 | + |
| 8 | +import { authQueryKeys } from '../../hooks/use-auth-query' |
| 9 | +import type { User } from '../../utils/auth' |
| 10 | + |
| 11 | +describe('useAuthQuery - Complete Tests', () => { |
| 12 | + let queryClient: QueryClient |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + queryClient = new QueryClient({ |
| 16 | + defaultOptions: { |
| 17 | + queries: { retry: false }, |
| 18 | + mutations: { retry: false }, |
| 19 | + }, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + queryClient.clear() |
| 25 | + mock.restore() |
| 26 | + }) |
| 27 | + |
| 28 | + describe('P1: Query States', () => { |
| 29 | + test('should return success state with user data when API key is valid', async () => { |
| 30 | + const mockGetUserInfo = mock(async () => ({ id: 'test-id', email: 'test@example.com' })) |
| 31 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 32 | + |
| 33 | + const apiKey = 'valid-key' |
| 34 | + |
| 35 | + const queryFn = async () => { |
| 36 | + const authResult = await mockGetUserInfo({ |
| 37 | + apiKey, |
| 38 | + fields: ['id', 'email'], |
| 39 | + logger: mockLogger as any, |
| 40 | + }) |
| 41 | + if (!authResult) { |
| 42 | + throw new Error('Invalid API key') |
| 43 | + } |
| 44 | + return authResult |
| 45 | + } |
| 46 | + |
| 47 | + const observer = new QueryObserver(queryClient, { |
| 48 | + queryKey: authQueryKeys.validation(apiKey), |
| 49 | + queryFn, |
| 50 | + staleTime: 5 * 60 * 1000, |
| 51 | + gcTime: 10 * 60 * 1000, |
| 52 | + retry: false, |
| 53 | + }) |
| 54 | + |
| 55 | + let currentResult: any = null |
| 56 | + const unsubscribe = observer.subscribe((result) => { |
| 57 | + currentResult = result |
| 58 | + }) |
| 59 | + |
| 60 | + try { |
| 61 | + // Wait for query to settle |
| 62 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 63 | + |
| 64 | + expect(currentResult.isSuccess).toBe(true) |
| 65 | + expect(currentResult.data).toEqual({ id: 'test-id', email: 'test@example.com' }) |
| 66 | + } finally { |
| 67 | + unsubscribe() |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + test('should return error state when API key is invalid', async () => { |
| 72 | + const mockGetUserInfo = mock(async () => null) |
| 73 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 74 | + |
| 75 | + const apiKey = 'invalid-key' |
| 76 | + |
| 77 | + const queryFn = async () => { |
| 78 | + const authResult = await mockGetUserInfo({ |
| 79 | + apiKey, |
| 80 | + fields: ['id', 'email'], |
| 81 | + logger: mockLogger as any, |
| 82 | + }) |
| 83 | + if (!authResult) { |
| 84 | + throw new Error('Invalid API key') |
| 85 | + } |
| 86 | + return authResult |
| 87 | + } |
| 88 | + |
| 89 | + const observer = new QueryObserver(queryClient, { |
| 90 | + queryKey: authQueryKeys.validation(apiKey), |
| 91 | + queryFn, |
| 92 | + retry: false, |
| 93 | + }) |
| 94 | + |
| 95 | + let currentResult: any = null |
| 96 | + const unsubscribe = observer.subscribe((result) => { |
| 97 | + currentResult = result |
| 98 | + }) |
| 99 | + |
| 100 | + try { |
| 101 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 102 | + |
| 103 | + expect(currentResult.isError).toBe(true) |
| 104 | + } finally { |
| 105 | + unsubscribe() |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + test('should disable query when no API key is available', () => { |
| 110 | + const mockGetUserInfo = mock(async () => ({ id: 'test', email: 'test@example.com' })) |
| 111 | + const apiKey = '' |
| 112 | + |
| 113 | + const queryFn = async () => { |
| 114 | + return await mockGetUserInfo({ apiKey, fields: ['id', 'email'], logger: {} as any }) |
| 115 | + } |
| 116 | + |
| 117 | + const observer = new QueryObserver(queryClient, { |
| 118 | + queryKey: authQueryKeys.validation(apiKey), |
| 119 | + queryFn, |
| 120 | + enabled: !!apiKey, // Should be false |
| 121 | + }) |
| 122 | + |
| 123 | + let currentResult: any = null |
| 124 | + const unsubscribe = observer.subscribe((result) => { |
| 125 | + currentResult = result |
| 126 | + }) |
| 127 | + |
| 128 | + // Query should not execute |
| 129 | + expect(mockGetUserInfo).not.toHaveBeenCalled() |
| 130 | + |
| 131 | + unsubscribe() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + describe('P1: Caching Behavior', () => { |
| 136 | + test('should not retry on auth failure', async () => { |
| 137 | + const mockGetUserInfo = mock(async () => { throw new Error('Auth failed') }) |
| 138 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 139 | + |
| 140 | + const apiKey = 'test-key' |
| 141 | + |
| 142 | + const queryFn = async () => { |
| 143 | + return await mockGetUserInfo({ |
| 144 | + apiKey, |
| 145 | + fields: ['id', 'email'], |
| 146 | + logger: mockLogger as any, |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + const observer = new QueryObserver(queryClient, { |
| 151 | + queryKey: authQueryKeys.validation(apiKey), |
| 152 | + queryFn, |
| 153 | + retry: false, |
| 154 | + }) |
| 155 | + |
| 156 | + const unsubscribe = observer.subscribe(() => {}) |
| 157 | + |
| 158 | + try { |
| 159 | + await new Promise(resolve => setTimeout(resolve, 200)) |
| 160 | + |
| 161 | + // Verify getUserInfoFromApiKey was called exactly once (no retries) |
| 162 | + expect(mockGetUserInfo).toHaveBeenCalledTimes(1) |
| 163 | + } finally { |
| 164 | + unsubscribe() |
| 165 | + } |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + describe('P1: Credential Reading', () => { |
| 170 | + test('should read API key from credentials file correctly', async () => { |
| 171 | + const mockGetUserCredentials = mock(() => ({ authToken: 'file-api-key' } as User)) |
| 172 | + const mockGetUserInfo = mock(async () => ({ id: 'test-id', email: 'test@example.com' })) |
| 173 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 174 | + |
| 175 | + // Simulate useAuthQuery's logic of getting API key |
| 176 | + const userCredentials = mockGetUserCredentials() |
| 177 | + const apiKey = userCredentials?.authToken || process.env.CODEBUFF_API_KEY || '' |
| 178 | + |
| 179 | + const queryFn = async () => { |
| 180 | + return await mockGetUserInfo({ |
| 181 | + apiKey, |
| 182 | + fields: ['id', 'email'], |
| 183 | + logger: mockLogger as any, |
| 184 | + }) |
| 185 | + } |
| 186 | + |
| 187 | + const observer = new QueryObserver(queryClient, { |
| 188 | + queryKey: authQueryKeys.validation(apiKey), |
| 189 | + queryFn, |
| 190 | + enabled: !!apiKey, |
| 191 | + }) |
| 192 | + |
| 193 | + const unsubscribe = observer.subscribe(() => {}) |
| 194 | + |
| 195 | + try { |
| 196 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 197 | + |
| 198 | + // Verify getUserInfoFromApiKey was called with the file API key |
| 199 | + expect(mockGetUserInfo).toHaveBeenCalledWith({ |
| 200 | + apiKey: 'file-api-key', |
| 201 | + fields: ['id', 'email'], |
| 202 | + logger: mockLogger, |
| 203 | + }) |
| 204 | + } finally { |
| 205 | + unsubscribe() |
| 206 | + } |
| 207 | + }) |
| 208 | + |
| 209 | + test('should fall back to CODEBUFF_API_KEY environment variable when no credentials file', async () => { |
| 210 | + const mockGetUserCredentials = mock(() => null) |
| 211 | + const mockGetUserInfo = mock(async () => ({ id: 'test-id', email: 'test@example.com' })) |
| 212 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 213 | + |
| 214 | + // Set environment variable |
| 215 | + const originalEnv = process.env.CODEBUFF_API_KEY |
| 216 | + process.env.CODEBUFF_API_KEY = 'env-api-key' |
| 217 | + |
| 218 | + try { |
| 219 | + // Simulate useAuthQuery's logic |
| 220 | + const userCredentials = mockGetUserCredentials() |
| 221 | + const apiKey = userCredentials?.authToken || process.env.CODEBUFF_API_KEY || '' |
| 222 | + |
| 223 | + const queryFn = async () => { |
| 224 | + return await mockGetUserInfo({ |
| 225 | + apiKey, |
| 226 | + fields: ['id', 'email'], |
| 227 | + logger: mockLogger as any, |
| 228 | + }) |
| 229 | + } |
| 230 | + |
| 231 | + const observer = new QueryObserver(queryClient, { |
| 232 | + queryKey: authQueryKeys.validation(apiKey), |
| 233 | + queryFn, |
| 234 | + enabled: !!apiKey, |
| 235 | + }) |
| 236 | + |
| 237 | + const unsubscribe = observer.subscribe(() => {}) |
| 238 | + |
| 239 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 240 | + |
| 241 | + // Verify getUserInfoFromApiKey was called with env API key |
| 242 | + expect(mockGetUserInfo).toHaveBeenCalledWith({ |
| 243 | + apiKey: 'env-api-key', |
| 244 | + fields: ['id', 'email'], |
| 245 | + logger: mockLogger, |
| 246 | + }) |
| 247 | + |
| 248 | + unsubscribe() |
| 249 | + } finally { |
| 250 | + process.env.CODEBUFF_API_KEY = originalEnv |
| 251 | + } |
| 252 | + }) |
| 253 | + |
| 254 | + test('should handle missing credentials gracefully', () => { |
| 255 | + const mockGetUserCredentials = mock(() => null) |
| 256 | + const mockGetUserInfo = mock(async () => ({ id: 'test', email: 'test@example.com' })) |
| 257 | + const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}) } |
| 258 | + |
| 259 | + // Clear environment variable |
| 260 | + const originalEnv = process.env.CODEBUFF_API_KEY |
| 261 | + delete process.env.CODEBUFF_API_KEY |
| 262 | + |
| 263 | + try { |
| 264 | + const userCredentials = mockGetUserCredentials() |
| 265 | + const apiKey = userCredentials?.authToken || process.env.CODEBUFF_API_KEY || '' |
| 266 | + |
| 267 | + const queryFn = async () => { |
| 268 | + return await mockGetUserInfo({ |
| 269 | + apiKey, |
| 270 | + fields: ['id', 'email'], |
| 271 | + logger: mockLogger as any, |
| 272 | + }) |
| 273 | + } |
| 274 | + |
| 275 | + const observer = new QueryObserver(queryClient, { |
| 276 | + queryKey: authQueryKeys.validation(apiKey), |
| 277 | + queryFn, |
| 278 | + enabled: !!apiKey, // Should be false |
| 279 | + }) |
| 280 | + |
| 281 | + const unsubscribe = observer.subscribe(() => {}) |
| 282 | + |
| 283 | + // Query should not execute |
| 284 | + expect(mockGetUserInfo).not.toHaveBeenCalled() |
| 285 | + |
| 286 | + unsubscribe() |
| 287 | + } finally { |
| 288 | + process.env.CODEBUFF_API_KEY = originalEnv |
| 289 | + } |
| 290 | + }) |
| 291 | + }) |
| 292 | +}) |
0 commit comments