mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add recentlyUsedModels
This commit is contained in:
5 files changed
+187
-7
No files matched your search
@@ -0,0 +1,134 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
import { AISettingsProvider } from './AISettingsProvider';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
|
||||
// Mock localStorage
|
||||
const localStorageMock = {
|
||||
getItem: vi.fn(),
|
||||
setItem: vi.fn(),
|
||||
removeItem: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
};
|
||||
|
||||
Object.defineProperty(window, 'localStorage', {
|
||||
value: localStorageMock,
|
||||
});
|
||||
|
||||
describe('AISettingsProvider', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
localStorageMock.getItem.mockReturnValue(null);
|
||||
});
|
||||
|
||||
it('should initialize with empty recentlyUsedModels array', () => {
|
||||
const { result } = renderHook(() => useAISettings(), {
|
||||
wrapper: ({ children }) => (
|
||||
<TestApp>
|
||||
<AISettingsProvider>{children}</AISettingsProvider>
|
||||
</TestApp>
|
||||
),
|
||||
});
|
||||
|
||||
expect(result.current.settings.recentlyUsedModels).toEqual([]);
|
||||
});
|
||||
|
||||
it('should add recently used models correctly', () => {
|
||||
const { result } = renderHook(() => useAISettings(), {
|
||||
wrapper: ({ children }) => (
|
||||
<TestApp>
|
||||
<AISettingsProvider>{children}</AISettingsProvider>
|
||||
</TestApp>
|
||||
),
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.addRecentlyUsedModel('openrouter/anthropic/claude-3.5-sonnet');
|
||||
});
|
||||
|
||||
expect(result.current.settings.recentlyUsedModels).toEqual([
|
||||
'openrouter/anthropic/claude-3.5-sonnet'
|
||||
]);
|
||||
});
|
||||
|
||||
it('should move existing model to front when used again', () => {
|
||||
const { result } = renderHook(() => useAISettings(), {
|
||||
wrapper: ({ children }) => (
|
||||
<TestApp>
|
||||
<AISettingsProvider>{children}</AISettingsProvider>
|
||||
</TestApp>
|
||||
),
|
||||
});
|
||||
|
||||
// Add multiple models
|
||||
act(() => {
|
||||
result.current.addRecentlyUsedModel('openrouter/anthropic/claude-3.5-sonnet');
|
||||
result.current.addRecentlyUsedModel('openrouter/openai/gpt-4');
|
||||
result.current.addRecentlyUsedModel('openrouter/google/gemini-pro');
|
||||
});
|
||||
|
||||
expect(result.current.settings.recentlyUsedModels).toEqual([
|
||||
'openrouter/google/gemini-pro',
|
||||
'openrouter/openai/gpt-4',
|
||||
'openrouter/anthropic/claude-3.5-sonnet'
|
||||
]);
|
||||
|
||||
// Use the first model again - it should move to front
|
||||
act(() => {
|
||||
result.current.addRecentlyUsedModel('openrouter/anthropic/claude-3.5-sonnet');
|
||||
});
|
||||
|
||||
expect(result.current.settings.recentlyUsedModels).toEqual([
|
||||
'openrouter/anthropic/claude-3.5-sonnet',
|
||||
'openrouter/google/gemini-pro',
|
||||
'openrouter/openai/gpt-4'
|
||||
]);
|
||||
});
|
||||
|
||||
it('should limit recently used models to 10 items', () => {
|
||||
const { result } = renderHook(() => useAISettings(), {
|
||||
wrapper: ({ children }) => (
|
||||
<TestApp>
|
||||
<AISettingsProvider>{children}</AISettingsProvider>
|
||||
</TestApp>
|
||||
),
|
||||
});
|
||||
|
||||
// Add 12 models
|
||||
act(() => {
|
||||
for (let i = 1; i <= 12; i++) {
|
||||
result.current.addRecentlyUsedModel(`provider/model-${i}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Should only keep the last 10
|
||||
expect(result.current.settings.recentlyUsedModels).toHaveLength(10);
|
||||
expect(result.current.settings.recentlyUsedModels[0]).toBe('provider/model-12');
|
||||
expect(result.current.settings.recentlyUsedModels[9]).toBe('provider/model-3');
|
||||
});
|
||||
|
||||
it('should migrate old settings without recentlyUsedModels', () => {
|
||||
const oldSettings = {
|
||||
providers: {
|
||||
openrouter: {
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
apiKey: 'test-key'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
localStorageMock.getItem.mockReturnValue(JSON.stringify(oldSettings));
|
||||
|
||||
const { result } = renderHook(() => useAISettings(), {
|
||||
wrapper: ({ children }) => (
|
||||
<TestApp>
|
||||
<AISettingsProvider>{children}</AISettingsProvider>
|
||||
</TestApp>
|
||||
),
|
||||
});
|
||||
|
||||
expect(result.current.settings.recentlyUsedModels).toEqual([]);
|
||||
expect(result.current.settings.providers).toEqual(oldSettings.providers);
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ interface AISettingsProviderProps {
|
||||
|
||||
const DEFAULT_SETTINGS: AISettings = {
|
||||
providers: {},
|
||||
recentlyUsedModels: [],
|
||||
};
|
||||
|
||||
const STORAGE_KEY = 'ai-settings';
|
||||
@@ -22,11 +23,16 @@ function migrateOldSettings(stored: unknown): AISettings {
|
||||
apiKey: oldSettings.apiKey || '',
|
||||
},
|
||||
},
|
||||
recentlyUsedModels: [],
|
||||
};
|
||||
}
|
||||
// If it's already the new format or invalid, return as AISettings or default
|
||||
if (stored && typeof stored === 'object' && 'providers' in stored) {
|
||||
return stored as AISettings;
|
||||
const settings = stored as Partial<AISettings>;
|
||||
return {
|
||||
providers: settings.providers || {},
|
||||
recentlyUsedModels: settings.recentlyUsedModels || [],
|
||||
};
|
||||
}
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
@@ -87,6 +93,30 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
}));
|
||||
};
|
||||
|
||||
const addRecentlyUsedModel = (modelId: string) => {
|
||||
setSettings(prev => {
|
||||
const existingIndex = prev.recentlyUsedModels.indexOf(modelId);
|
||||
if (existingIndex === 0) {
|
||||
return prev;
|
||||
} else if (existingIndex === -1) {
|
||||
// Add new model to front, keep only last 10
|
||||
return {
|
||||
...prev,
|
||||
recentlyUsedModels: [modelId, ...prev.recentlyUsedModels].slice(0, 10),
|
||||
}
|
||||
} else {
|
||||
// Move existing model to front
|
||||
return {
|
||||
...prev,
|
||||
recentlyUsedModels: [
|
||||
modelId,
|
||||
...prev.recentlyUsedModels.filter((_, index) => index !== existingIndex)
|
||||
]
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const isConfigured = Object.entries(settings.providers).length > 0;
|
||||
|
||||
const contextValue: AISettingsContextType = {
|
||||
@@ -95,6 +125,7 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
addProvider,
|
||||
removeProvider,
|
||||
updateProvider,
|
||||
addRecentlyUsedModel,
|
||||
isConfigured,
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user