|
| 1 | +import { createElement } from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { afterEach, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +const sessionState = { |
| 6 | + sourceLibrary: [] as unknown[], |
| 7 | + hydrateFromServer: vi.fn(), |
| 8 | + resetLocalData: vi.fn(), |
| 9 | +}; |
| 10 | + |
| 11 | +const settingsState = { |
| 12 | + customSystemPrompt: '', |
| 13 | + outputLanguage: 'en', |
| 14 | + hydrateFromServer: vi.fn(), |
| 15 | + resetLocalData: vi.fn(), |
| 16 | +}; |
| 17 | + |
| 18 | +vi.mock('@/lib/stores/settings-store', () => ({ |
| 19 | + useSettingsStore: Object.assign( |
| 20 | + (selector: (state: typeof settingsState) => unknown) => selector(settingsState), |
| 21 | + { getState: () => settingsState }, |
| 22 | + ), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('@/lib/stores/session-store', () => ({ |
| 26 | + useSessionStore: Object.assign( |
| 27 | + (selector: (state: typeof sessionState) => unknown) => selector(sessionState), |
| 28 | + { getState: () => sessionState }, |
| 29 | + ), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('@/lib/stores/sparring-store', () => ({ |
| 33 | + useSparringStore: (selector: (state: Record<string, unknown>) => unknown) => selector({ |
| 34 | + hydrateSavedSessions: vi.fn(), |
| 35 | + prepareForUser: vi.fn(), |
| 36 | + resetLocalData: vi.fn(), |
| 37 | + }), |
| 38 | +})); |
| 39 | + |
| 40 | +vi.mock('@/lib/observability', () => ({ |
| 41 | + captureException: vi.fn(), |
| 42 | + captureProductEvent: vi.fn(), |
| 43 | +})); |
| 44 | + |
| 45 | +import { askChat } from '@/lib/ai/client'; |
| 46 | +import { AuthProvider, useAuth } from '@/lib/supabase/AuthProvider'; |
| 47 | +import { setActiveAccessToken, supabase } from '@/lib/supabase/client'; |
| 48 | + |
| 49 | +afterEach(() => { |
| 50 | + setActiveAccessToken(null); |
| 51 | + vi.restoreAllMocks(); |
| 52 | +}); |
| 53 | + |
| 54 | +function AuthState() { |
| 55 | + const { loading, user } = useAuth(); |
| 56 | + return createElement('span', { 'data-testid': 'auth-state' }, loading ? 'loading' : user?.id ?? 'signed-out'); |
| 57 | +} |
| 58 | + |
| 59 | +it('calls the AI fetch boundary without another session lookup', async () => { |
| 60 | + const getSession = vi.spyOn(supabase.auth, 'getSession') |
| 61 | + .mockResolvedValue({ |
| 62 | + data: { session: { access_token: 'test-access-token', user: { id: 'user-1' } } }, |
| 63 | + error: null, |
| 64 | + } as Awaited<ReturnType<typeof supabase.auth.getSession>>); |
| 65 | + vi.spyOn(supabase.auth, 'onAuthStateChange').mockReturnValue({ |
| 66 | + data: { subscription: { id: 'test', callback: vi.fn(), unsubscribe: vi.fn() } }, |
| 67 | + } as ReturnType<typeof supabase.auth.onAuthStateChange>); |
| 68 | + const fetchRequest = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(JSON.stringify({ |
| 69 | + output: { |
| 70 | + response: 'Mock AI response.', |
| 71 | + followUpQuestions: [], |
| 72 | + uncertainties: [], |
| 73 | + }, |
| 74 | + }), { |
| 75 | + status: 200, |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + })); |
| 78 | + |
| 79 | + render(createElement(AuthProvider, null, createElement(AuthState))); |
| 80 | + await waitFor(() => expect(screen.getByTestId('auth-state')).toHaveTextContent('user-1')); |
| 81 | + const authInitializationLookups = getSession.mock.calls.length; |
| 82 | + |
| 83 | + const result = await askChat('Hello', [], 'general', { |
| 84 | + provider: 'openrouter', |
| 85 | + model: 'openai/gpt-5.6-luna', |
| 86 | + temperature: 0.7, |
| 87 | + reasoningEffort: 'low', |
| 88 | + }); |
| 89 | + |
| 90 | + expect(fetchRequest).toHaveBeenCalledOnce(); |
| 91 | + expect(result).toMatchObject({ response: 'Mock AI response.' }); |
| 92 | + expect(getSession).toHaveBeenCalledTimes(authInitializationLookups); |
| 93 | +}); |
| 94 | + |
| 95 | +it('maps a rejected fetch through the Edge Function error contract', async () => { |
| 96 | + setActiveAccessToken('test-access-token'); |
| 97 | + vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('Failed to fetch')); |
| 98 | + |
| 99 | + await expect(askChat('Hello', [], 'general', { |
| 100 | + provider: 'openrouter', |
| 101 | + model: 'openai/gpt-5.6-luna', |
| 102 | + temperature: 0.7, |
| 103 | + reasoningEffort: 'low', |
| 104 | + })).rejects.toMatchObject({ |
| 105 | + name: 'AIError', |
| 106 | + message: 'AI service error: Failed to send a request to the Edge Function. Please try again.', |
| 107 | + statusCode: 500, |
| 108 | + }); |
| 109 | +}); |
0 commit comments