diff options
| author | Kumbham Ajay Goud <[email protected]> | 2025-08-04 03:33:01 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-03 22:03:01 +0000 |
| commit | a8984a9b30d153219cf517a7dc77d65ea3ef0965 (patch) | |
| tree | 2993923858e2643ab0ce3b35de64693395877c73 /packages/core/src/config/config.test.ts | |
| parent | acd48a125928cd721946713241e3236680d43507 (diff) | |
Fix: Preserve conversation history when changing auth methods via /auth (#5216)
Co-authored-by: Jacob Richman <[email protected]>
Diffstat (limited to 'packages/core/src/config/config.test.ts')
| -rw-r--r-- | packages/core/src/config/config.test.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/packages/core/src/config/config.test.ts b/packages/core/src/config/config.test.ts index 165d2882..dd50fd41 100644 --- a/packages/core/src/config/config.test.ts +++ b/packages/core/src/config/config.test.ts @@ -184,6 +184,87 @@ describe('Server Config (config.ts)', () => { // Verify that fallback mode is reset expect(config.isInFallbackMode()).toBe(false); }); + + it('should preserve conversation history when refreshing auth', async () => { + const config = new Config(baseParams); + const authType = AuthType.USE_GEMINI; + const mockContentConfig = { + model: 'gemini-pro', + apiKey: 'test-key', + }; + + (createContentGeneratorConfig as Mock).mockReturnValue(mockContentConfig); + + // Mock the existing client with some history + const mockExistingHistory = [ + { role: 'user', parts: [{ text: 'Hello' }] }, + { role: 'model', parts: [{ text: 'Hi there!' }] }, + { role: 'user', parts: [{ text: 'How are you?' }] }, + ]; + + const mockExistingClient = { + isInitialized: vi.fn().mockReturnValue(true), + getHistory: vi.fn().mockReturnValue(mockExistingHistory), + }; + + const mockNewClient = { + isInitialized: vi.fn().mockReturnValue(true), + getHistory: vi.fn().mockReturnValue([]), + setHistory: vi.fn(), + initialize: vi.fn().mockResolvedValue(undefined), + }; + + // Set the existing client + ( + config as unknown as { geminiClient: typeof mockExistingClient } + ).geminiClient = mockExistingClient; + (GeminiClient as Mock).mockImplementation(() => mockNewClient); + + await config.refreshAuth(authType); + + // Verify that existing history was retrieved + expect(mockExistingClient.getHistory).toHaveBeenCalled(); + + // Verify that new client was created and initialized + expect(GeminiClient).toHaveBeenCalledWith(config); + expect(mockNewClient.initialize).toHaveBeenCalledWith(mockContentConfig); + + // Verify that history was restored to the new client + expect(mockNewClient.setHistory).toHaveBeenCalledWith( + mockExistingHistory, + ); + }); + + it('should handle case when no existing client is initialized', async () => { + const config = new Config(baseParams); + const authType = AuthType.USE_GEMINI; + const mockContentConfig = { + model: 'gemini-pro', + apiKey: 'test-key', + }; + + (createContentGeneratorConfig as Mock).mockReturnValue(mockContentConfig); + + const mockNewClient = { + isInitialized: vi.fn().mockReturnValue(true), + getHistory: vi.fn().mockReturnValue([]), + setHistory: vi.fn(), + initialize: vi.fn().mockResolvedValue(undefined), + }; + + // No existing client + (config as unknown as { geminiClient: null }).geminiClient = null; + (GeminiClient as Mock).mockImplementation(() => mockNewClient); + + await config.refreshAuth(authType); + + // Verify that new client was created and initialized + expect(GeminiClient).toHaveBeenCalledWith(config); + expect(mockNewClient.initialize).toHaveBeenCalledWith(mockContentConfig); + + // Verify that setHistory was not called since there was no existing history + expect(mockNewClient.setHistory).not.toHaveBeenCalled(); + }); }); it('Config constructor should store userMemory correctly', () => { |
