diff --git a/src/components/AISettingsProvider.test.tsx b/src/components/AISettingsProvider.test.tsx index 8f47aef2..783d2a4a 100644 --- a/src/components/AISettingsProvider.test.tsx +++ b/src/components/AISettingsProvider.test.tsx @@ -1,5 +1,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest'; import { renderHook, act } from '@testing-library/react'; +import { useQueryClient } from '@tanstack/react-query'; +import React from 'react'; import { useAISettings } from '@/hooks/useAISettings'; import { TestApp } from '@/test/TestApp'; @@ -110,4 +112,65 @@ describe('AISettingsProvider', () => { expect(result.current.settings.recentlyUsedModels).toEqual([]); expect(result.current.settings.providers).toEqual(oldSettings.providers); }); + + it('should invalidate provider-models query when updateSettings is called with providers', () => { + // Mock the QueryClient.invalidateQueries method + const mockInvalidateQueries = vi.fn(); + + // Create a custom TestApp that mocks the query client + const MockedTestApp = ({ children }: { children: React.ReactNode }) => { + const queryClient = useQueryClient(); + vi.spyOn(queryClient, 'invalidateQueries').mockImplementation(mockInvalidateQueries); + return <>{children}; + }; + + const { result } = renderHook(() => useAISettings(), { + wrapper: ({ children }) => ( + + {children} + + ), + }); + + act(() => { + result.current.updateSettings({ + providers: { + 'test-provider': { + baseURL: 'https://api.test.com/v1', + apiKey: 'test-key' + } + } + }); + }); + + expect(mockInvalidateQueries).toHaveBeenCalledWith({ queryKey: ['provider-models'] }); + }); + + it('should not invalidate provider-models query when updateSettings is called without providers', () => { + // Mock the QueryClient.invalidateQueries method + const mockInvalidateQueries = vi.fn(); + + // Create a custom TestApp that mocks the query client + const MockedTestApp = ({ children }: { children: React.ReactNode }) => { + const queryClient = useQueryClient(); + vi.spyOn(queryClient, 'invalidateQueries').mockImplementation(mockInvalidateQueries); + return <>{children}; + }; + + const { result } = renderHook(() => useAISettings(), { + wrapper: ({ children }) => ( + + {children} + + ), + }); + + act(() => { + result.current.updateSettings({ + recentlyUsedModels: ['test-model'] + }); + }); + + expect(mockInvalidateQueries).not.toHaveBeenCalled(); + }); }); \ No newline at end of file diff --git a/src/components/AISettingsProvider.tsx b/src/components/AISettingsProvider.tsx index e968d102..6d06a4de 100644 --- a/src/components/AISettingsProvider.tsx +++ b/src/components/AISettingsProvider.tsx @@ -41,6 +41,11 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) { const updateSettings = (newSettings: Partial) => { setSettings(prev => ({ ...prev, ...newSettings })); + + // If providers are being updated, invalidate the provider-models query + if ('providers' in newSettings) { + queryClient.invalidateQueries({ queryKey: ['provider-models'] }); + } }; const addProvider = (name: string, connection: AIConnection) => {