| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| | import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; |
| | import { renderHook, waitFor, act } from '@testing-library/react'; |
| | import { useAtCompletion } from './useAtCompletion.js'; |
| | import type { Config, FileSearch } from '@google/gemini-cli-core'; |
| | import { FileSearchFactory } from '@google/gemini-cli-core'; |
| | import type { FileSystemStructure } from '@google/gemini-cli-test-utils'; |
| | import { createTmpDir, cleanupTmpDir } from '@google/gemini-cli-test-utils'; |
| | import { useState } from 'react'; |
| | import type { Suggestion } from '../components/SuggestionsDisplay.js'; |
| |
|
| | |
| | function useTestHarnessForAtCompletion( |
| | enabled: boolean, |
| | pattern: string, |
| | config: Config | undefined, |
| | cwd: string, |
| | ) { |
| | const [suggestions, setSuggestions] = useState<Suggestion[]>([]); |
| | const [isLoadingSuggestions, setIsLoadingSuggestions] = useState(false); |
| |
|
| | useAtCompletion({ |
| | enabled, |
| | pattern, |
| | config, |
| | cwd, |
| | setSuggestions, |
| | setIsLoadingSuggestions, |
| | }); |
| |
|
| | return { suggestions, isLoadingSuggestions }; |
| | } |
| |
|
| | describe('useAtCompletion', () => { |
| | let testRootDir: string; |
| | let mockConfig: Config; |
| |
|
| | beforeEach(() => { |
| | mockConfig = { |
| | getFileFilteringOptions: vi.fn(() => ({ |
| | respectGitIgnore: true, |
| | respectGeminiIgnore: true, |
| | })), |
| | getEnableRecursiveFileSearch: () => true, |
| | getFileFilteringDisableFuzzySearch: () => false, |
| | } as unknown as Config; |
| | vi.clearAllMocks(); |
| | }); |
| |
|
| | afterEach(async () => { |
| | if (testRootDir) { |
| | await cleanupTmpDir(testRootDir); |
| | } |
| | vi.restoreAllMocks(); |
| | }); |
| |
|
| | describe('File Search Logic', () => { |
| | it('should perform a recursive search for an empty pattern', async () => { |
| | const structure: FileSystemStructure = { |
| | 'file.txt': '', |
| | src: { |
| | 'index.js': '', |
| | components: ['Button.tsx', 'Button with spaces.tsx'], |
| | }, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'src/', |
| | 'src/components/', |
| | 'file.txt', |
| | 'src/components/Button\\ with\\ spaces.tsx', |
| | 'src/components/Button.tsx', |
| | 'src/index.js', |
| | ]); |
| | }); |
| |
|
| | it('should correctly filter the recursive list based on a pattern', async () => { |
| | const structure: FileSystemStructure = { |
| | 'file.txt': '', |
| | src: { |
| | 'index.js': '', |
| | components: { |
| | 'Button.tsx': '', |
| | }, |
| | }, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, 'src/', mockConfig, testRootDir), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'src/', |
| | 'src/components/', |
| | 'src/index.js', |
| | 'src/components/Button.tsx', |
| | ]); |
| | }); |
| |
|
| | it('should append a trailing slash to directory paths in suggestions', async () => { |
| | const structure: FileSystemStructure = { |
| | 'file.txt': '', |
| | dir: {}, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'dir/', |
| | 'file.txt', |
| | ]); |
| | }); |
| | }); |
| |
|
| | describe('UI State and Loading Behavior', () => { |
| | it('should be in a loading state during initial file system crawl', async () => { |
| | testRootDir = await createTmpDir({}); |
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir), |
| | ); |
| |
|
| | |
| | expect(result.current.isLoadingSuggestions).toBe(true); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| | }); |
| | }); |
| |
|
| | it('should NOT show a loading indicator for subsequent searches that complete under 200ms', async () => { |
| | const structure: FileSystemStructure = { 'a.txt': '', 'b.txt': '' }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ pattern }) => |
| | useTestHarnessForAtCompletion(true, pattern, mockConfig, testRootDir), |
| | { initialProps: { pattern: 'a' } }, |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'a.txt', |
| | ]); |
| | }); |
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| |
|
| | rerender({ pattern: 'b' }); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'b.txt', |
| | ]); |
| | }); |
| |
|
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| | }); |
| |
|
| | it('should show a loading indicator and clear old suggestions for subsequent searches that take longer than 200ms', async () => { |
| | const structure: FileSystemStructure = { 'a.txt': '', 'b.txt': '' }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const realFileSearch = FileSearchFactory.create({ |
| | projectRoot: testRootDir, |
| | ignoreDirs: [], |
| | useGitignore: true, |
| | useGeminiignore: true, |
| | cache: false, |
| | cacheTtl: 0, |
| | enableRecursiveFileSearch: true, |
| | disableFuzzySearch: false, |
| | }); |
| | await realFileSearch.initialize(); |
| |
|
| | |
| | const mockFileSearch: FileSearch = { |
| | initialize: vi.fn().mockResolvedValue(undefined), |
| | search: vi |
| | .fn() |
| | .mockImplementation(async (...args) => |
| | realFileSearch.search(...args), |
| | ), |
| | }; |
| | vi.spyOn(FileSearchFactory, 'create').mockReturnValue(mockFileSearch); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ pattern }) => |
| | useTestHarnessForAtCompletion(true, pattern, mockConfig, testRootDir), |
| | { initialProps: { pattern: 'a' } }, |
| | ); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'a.txt', |
| | ]); |
| | }); |
| |
|
| | |
| | vi.useFakeTimers(); |
| |
|
| | |
| | act(() => { |
| | rerender({ pattern: 'b' }); |
| | }); |
| |
|
| | |
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| |
|
| | |
| | act(() => { |
| | vi.advanceTimersByTime(200); |
| | }); |
| |
|
| | |
| | expect(result.current.isLoadingSuggestions).toBe(true); |
| | expect(result.current.suggestions).toEqual([]); |
| |
|
| | |
| | vi.useRealTimers(); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'b.txt', |
| | ]); |
| | }); |
| |
|
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| | }); |
| |
|
| | it('should abort the previous search when a new one starts', async () => { |
| | const structure: FileSystemStructure = { 'a.txt': '', 'b.txt': '' }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const abortSpy = vi.spyOn(AbortController.prototype, 'abort'); |
| | const mockFileSearch: FileSearch = { |
| | initialize: vi.fn().mockResolvedValue(undefined), |
| | search: vi.fn().mockImplementation(async (pattern: string) => { |
| | const delay = pattern === 'a' ? 500 : 50; |
| | await new Promise((resolve) => setTimeout(resolve, delay)); |
| | return [pattern]; |
| | }), |
| | }; |
| | vi.spyOn(FileSearchFactory, 'create').mockReturnValue(mockFileSearch); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ pattern }) => |
| | useTestHarnessForAtCompletion(true, pattern, mockConfig, testRootDir), |
| | { initialProps: { pattern: 'a' } }, |
| | ); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(mockFileSearch.search).toHaveBeenCalledWith( |
| | 'a', |
| | expect.any(Object), |
| | ); |
| | }); |
| |
|
| | |
| | act(() => { |
| | rerender({ pattern: 'b' }); |
| | }); |
| |
|
| | |
| | expect(abortSpy).toHaveBeenCalledTimes(1); |
| |
|
| | |
| | await waitFor( |
| | () => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual(['b']); |
| | }, |
| | { timeout: 1000 }, |
| | ); |
| |
|
| | |
| | expect(mockFileSearch.search).toHaveBeenCalledWith( |
| | 'b', |
| | expect.any(Object), |
| | ); |
| | }); |
| | }); |
| |
|
| | describe('State Management', () => { |
| | it('should reset the state when disabled after being in a READY state', async () => { |
| | const structure: FileSystemStructure = { 'a.txt': '' }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ enabled }) => |
| | useTestHarnessForAtCompletion(enabled, 'a', mockConfig, testRootDir), |
| | { initialProps: { enabled: true } }, |
| | ); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'a.txt', |
| | ]); |
| | }); |
| |
|
| | |
| | rerender({ enabled: false }); |
| |
|
| | |
| | expect(result.current.suggestions).toEqual([]); |
| | }); |
| |
|
| | it('should reset the state when disabled after being in an ERROR state', async () => { |
| | testRootDir = await createTmpDir({}); |
| |
|
| | |
| | const mockFileSearch: FileSearch = { |
| | initialize: vi |
| | .fn() |
| | .mockRejectedValue(new Error('Initialization failed')), |
| | search: vi.fn(), |
| | }; |
| | vi.spyOn(FileSearchFactory, 'create').mockReturnValue(mockFileSearch); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ enabled }) => |
| | useTestHarnessForAtCompletion(enabled, '', mockConfig, testRootDir), |
| | { initialProps: { enabled: true } }, |
| | ); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| | }); |
| | expect(result.current.suggestions).toEqual([]); |
| |
|
| | |
| | rerender({ enabled: false }); |
| |
|
| | |
| | |
| | |
| | expect(result.current.suggestions).toEqual([]); |
| | }); |
| | }); |
| |
|
| | describe('Filtering and Configuration', () => { |
| | it('should respect .gitignore files', async () => { |
| | const gitignoreContent = ['dist/', '*.log'].join('\n'); |
| | const structure: FileSystemStructure = { |
| | '.git': {}, |
| | '.gitignore': gitignoreContent, |
| | dist: {}, |
| | 'test.log': '', |
| | src: {}, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'src/', |
| | '.gitignore', |
| | ]); |
| | }); |
| |
|
| | it('should work correctly when config is undefined', async () => { |
| | const structure: FileSystemStructure = { |
| | node_modules: {}, |
| | src: {}, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion(true, '', undefined, testRootDir), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'node_modules/', |
| | 'src/', |
| | ]); |
| | }); |
| |
|
| | it('should reset and re-initialize when the cwd changes', async () => { |
| | const structure1: FileSystemStructure = { 'file1.txt': '' }; |
| | const rootDir1 = await createTmpDir(structure1); |
| | const structure2: FileSystemStructure = { 'file2.txt': '' }; |
| | const rootDir2 = await createTmpDir(structure2); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ cwd, pattern }) => |
| | useTestHarnessForAtCompletion(true, pattern, mockConfig, cwd), |
| | { |
| | initialProps: { |
| | cwd: rootDir1, |
| | pattern: 'file', |
| | }, |
| | }, |
| | ); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'file1.txt', |
| | ]); |
| | }); |
| |
|
| | |
| | act(() => { |
| | rerender({ cwd: rootDir2, pattern: 'file' }); |
| | }); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.isLoadingSuggestions).toBe(true); |
| | expect(result.current.suggestions).toEqual([]); |
| | }); |
| |
|
| | |
| | await waitFor(() => { |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'file2.txt', |
| | ]); |
| | }); |
| | expect(result.current.isLoadingSuggestions).toBe(false); |
| |
|
| | await cleanupTmpDir(rootDir1); |
| | await cleanupTmpDir(rootDir2); |
| | }); |
| |
|
| | it('should perform a non-recursive search when enableRecursiveFileSearch is false', async () => { |
| | const structure: FileSystemStructure = { |
| | 'file.txt': '', |
| | src: { |
| | 'index.js': '', |
| | }, |
| | }; |
| | testRootDir = await createTmpDir(structure); |
| |
|
| | const nonRecursiveConfig = { |
| | getEnableRecursiveFileSearch: () => false, |
| | getFileFilteringOptions: vi.fn(() => ({ |
| | respectGitIgnore: true, |
| | respectGeminiIgnore: true, |
| | })), |
| | getFileFilteringDisableFuzzySearch: () => false, |
| | } as unknown as Config; |
| |
|
| | const { result } = renderHook(() => |
| | useTestHarnessForAtCompletion( |
| | true, |
| | '', |
| | nonRecursiveConfig, |
| | testRootDir, |
| | ), |
| | ); |
| |
|
| | await waitFor(() => { |
| | expect(result.current.suggestions.length).toBeGreaterThan(0); |
| | }); |
| |
|
| | |
| | expect(result.current.suggestions.map((s) => s.value)).toEqual([ |
| | 'src/', |
| | 'file.txt', |
| | ]); |
| | }); |
| | }); |
| | }); |
| |
|