| | |
| | |
| | |
| | |
| | |
| |
|
| | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| | import { renderHook, waitFor } from '@testing-library/react'; |
| | import type { |
| | Config, |
| | GeminiClient, |
| | ContentGenerator, |
| | } from '@google/gemini-cli-core'; |
| | import { |
| | CodeAssistServer, |
| | LoggingContentGenerator, |
| | UserTierId, |
| | } from '@google/gemini-cli-core'; |
| | import type { OAuth2Client } from 'google-auth-library'; |
| | import { usePrivacySettings } from './usePrivacySettings.js'; |
| |
|
| | |
| | vi.mock('@google/gemini-cli-core', () => { |
| | |
| | class MockCodeAssistServer { |
| | projectId = 'test-project-id'; |
| | loadCodeAssist = vi.fn(); |
| | getCodeAssistGlobalUserSetting = vi.fn(); |
| | setCodeAssistGlobalUserSetting = vi.fn(); |
| |
|
| | constructor( |
| | _client?: GeminiClient, |
| | _projectId?: string, |
| | _httpOptions?: Record<string, unknown>, |
| | _sessionId?: string, |
| | _userTier?: UserTierId, |
| | ) {} |
| | } |
| |
|
| | class MockLoggingContentGenerator { |
| | getWrapped = vi.fn(); |
| |
|
| | constructor( |
| | _wrapped?: ContentGenerator, |
| | _config?: Record<string, unknown>, |
| | ) {} |
| | } |
| |
|
| | return { |
| | Config: vi.fn(), |
| | CodeAssistServer: MockCodeAssistServer, |
| | LoggingContentGenerator: MockLoggingContentGenerator, |
| | GeminiClient: vi.fn(), |
| | UserTierId: { |
| | FREE: 'free-tier', |
| | LEGACY: 'legacy-tier', |
| | STANDARD: 'standard-tier', |
| | }, |
| | }; |
| | }); |
| |
|
| | describe('usePrivacySettings', () => { |
| | let mockConfig: Config; |
| | let mockClient: GeminiClient; |
| | let mockCodeAssistServer: CodeAssistServer; |
| | let mockLoggingContentGenerator: LoggingContentGenerator; |
| |
|
| | beforeEach(() => { |
| | vi.clearAllMocks(); |
| |
|
| | |
| | mockCodeAssistServer = new CodeAssistServer( |
| | null as unknown as OAuth2Client, |
| | 'test-project-id', |
| | ) as unknown as CodeAssistServer; |
| | ( |
| | mockCodeAssistServer.loadCodeAssist as ReturnType<typeof vi.fn> |
| | ).mockResolvedValue({ |
| | currentTier: { id: UserTierId.FREE }, |
| | }); |
| | ( |
| | mockCodeAssistServer.getCodeAssistGlobalUserSetting as ReturnType< |
| | typeof vi.fn |
| | > |
| | ).mockResolvedValue({ |
| | freeTierDataCollectionOptin: true, |
| | }); |
| | ( |
| | mockCodeAssistServer.setCodeAssistGlobalUserSetting as ReturnType< |
| | typeof vi.fn |
| | > |
| | ).mockResolvedValue({ |
| | freeTierDataCollectionOptin: false, |
| | }); |
| |
|
| | |
| | mockLoggingContentGenerator = new LoggingContentGenerator( |
| | mockCodeAssistServer, |
| | null as unknown as Config, |
| | ) as unknown as LoggingContentGenerator; |
| | ( |
| | mockLoggingContentGenerator.getWrapped as ReturnType<typeof vi.fn> |
| | ).mockReturnValue(mockCodeAssistServer); |
| |
|
| | |
| | mockClient = { |
| | getContentGenerator: vi.fn().mockReturnValue(mockLoggingContentGenerator), |
| | } as unknown as GeminiClient; |
| |
|
| | |
| | mockConfig = { |
| | getGeminiClient: vi.fn().mockReturnValue(mockClient), |
| | } as unknown as Config; |
| | }); |
| |
|
| | it('should handle LoggingContentGenerator wrapper correctly and not throw "Oauth not being used" error', async () => { |
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | |
| | expect(result.current.privacyState.isLoading).toBe(true); |
| | expect(result.current.privacyState.error).toBeUndefined(); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | |
| | expect(result.current.privacyState.error).toBeUndefined(); |
| | expect(result.current.privacyState.isFreeTier).toBe(true); |
| | expect(result.current.privacyState.dataCollectionOptIn).toBe(true); |
| |
|
| | |
| | expect(mockLoggingContentGenerator.getWrapped).toHaveBeenCalled(); |
| | }); |
| |
|
| | it('should work with direct CodeAssistServer (no wrapper)', async () => { |
| | |
| | const directServer = new CodeAssistServer( |
| | null as unknown as OAuth2Client, |
| | 'test-project-id', |
| | ) as unknown as CodeAssistServer; |
| | (directServer.loadCodeAssist as ReturnType<typeof vi.fn>).mockResolvedValue( |
| | { |
| | currentTier: { id: UserTierId.FREE }, |
| | }, |
| | ); |
| | ( |
| | directServer.getCodeAssistGlobalUserSetting as ReturnType<typeof vi.fn> |
| | ).mockResolvedValue({ |
| | freeTierDataCollectionOptin: true, |
| | }); |
| |
|
| | mockClient.getContentGenerator = vi.fn().mockReturnValue(directServer); |
| |
|
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | expect(result.current.privacyState.error).toBeUndefined(); |
| | expect(result.current.privacyState.isFreeTier).toBe(true); |
| | expect(result.current.privacyState.dataCollectionOptIn).toBe(true); |
| | }); |
| |
|
| | it('should handle paid tier users correctly', async () => { |
| | |
| | ( |
| | mockCodeAssistServer.loadCodeAssist as ReturnType<typeof vi.fn> |
| | ).mockResolvedValue({ |
| | currentTier: { id: UserTierId.STANDARD }, |
| | }); |
| |
|
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | expect(result.current.privacyState.error).toBeUndefined(); |
| | expect(result.current.privacyState.isFreeTier).toBe(false); |
| | expect(result.current.privacyState.dataCollectionOptIn).toBeUndefined(); |
| | }); |
| |
|
| | it('should throw error when content generator is not a CodeAssistServer', async () => { |
| | |
| | const mockOtherGenerator = { someOtherMethod: vi.fn() }; |
| | ( |
| | mockLoggingContentGenerator.getWrapped as ReturnType<typeof vi.fn> |
| | ).mockReturnValue(mockOtherGenerator); |
| |
|
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | expect(result.current.privacyState.error).toBe('Oauth not being used'); |
| | }); |
| |
|
| | it('should throw error when CodeAssistServer has no projectId', async () => { |
| | |
| | const mockServerNoProject = { |
| | ...mockCodeAssistServer, |
| | projectId: undefined, |
| | }; |
| | ( |
| | mockLoggingContentGenerator.getWrapped as ReturnType<typeof vi.fn> |
| | ).mockReturnValue(mockServerNoProject); |
| |
|
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | expect(result.current.privacyState.error).toBe('Oauth not being used'); |
| | }); |
| |
|
| | it('should update data collection opt-in setting', async () => { |
| | const { result } = renderHook(() => usePrivacySettings(mockConfig)); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.privacyState.isLoading).toBe(false); |
| | }); |
| |
|
| | |
| | await result.current.updateDataCollectionOptIn(false); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.privacyState.dataCollectionOptIn).toBe(false); |
| | }); |
| |
|
| | expect( |
| | mockCodeAssistServer.setCodeAssistGlobalUserSetting, |
| | ).toHaveBeenCalledWith({ |
| | cloudaicompanionProject: 'test-project-id', |
| | freeTierDataCollectionOptin: false, |
| | }); |
| | }); |
| | }); |
| |
|