Refresh models when providers are changed

This commit is contained in:
Alex Gleason committed 2025-09-15 12:33:24 -05:00
1 parent ab9c269240
commit dedb9c7f55
2 files changed
+68

No files matched your search

@@ -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 }) => (
<TestApp>
<MockedTestApp>{children}</MockedTestApp>
</TestApp>
),
});
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 }) => (
<TestApp>
<MockedTestApp>{children}</MockedTestApp>
</TestApp>
),
});
act(() => {
result.current.updateSettings({
recentlyUsedModels: ['test-model']
});
});
expect(mockInvalidateQueries).not.toHaveBeenCalled();
});
});
+5
View File
@@ -41,6 +41,11 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
const updateSettings = (newSettings: Partial<AISettings>) => {
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) => {