From e9de2c424cc5efc9461a0b2225be94713aa223f4 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 18 Aug 2025 22:14:36 -0700 Subject: [PATCH] address feedback --- src/services/FileIndexService.test.ts | 1 + src/services/FileIndexService.ts | 25 ++++-- src/ui/TUIChat.tsx | 4 +- src/ui/__tests__/TUIChat.testHelper.ts | 55 ++++---------- src/util/filePatterns.ts | 2 +- src/util/fileWatcher.test.ts | 101 ++++++++++++++++++++++++- src/util/fileWatcher.ts | 8 +- 7 files changed, 142 insertions(+), 54 deletions(-) diff --git a/src/services/FileIndexService.test.ts b/src/services/FileIndexService.test.ts index d672e41ba..52552856d 100644 --- a/src/services/FileIndexService.test.ts +++ b/src/services/FileIndexService.test.ts @@ -9,6 +9,7 @@ import { FileIndexService } from './FileIndexService.js'; vi.mock('../util/fileWatcher.js', () => ({ getFileWatcher: () => ({ startWatching: vi.fn(), + stopWatching: vi.fn(), onChange: vi.fn(() => () => {}), // Return unsubscribe function }), })); diff --git a/src/services/FileIndexService.ts b/src/services/FileIndexService.ts index cbee0287a..12dca0f09 100644 --- a/src/services/FileIndexService.ts +++ b/src/services/FileIndexService.ts @@ -42,6 +42,13 @@ export class FileIndexService extends BaseService { this.fileWatcherUnsubscribe(); this.fileWatcherUnsubscribe = null; } + + // Stop the file watcher to prevent resource leaks + if (this.fileWatcherInitialized) { + const watcher = getFileWatcher(); + watcher.stopWatching(); + } + this.fileWatcherInitialized = false; } @@ -159,6 +166,7 @@ export class FileIndexService extends BaseService { return new Promise((resolve, reject) => { const allMatches: string[] = []; let processed = 0; + let streamEnded = false; const globStream = glob.stream(patterns, options); @@ -167,13 +175,13 @@ export class FileIndexService extends BaseService { processed++; if (processed % batchSize === 0) { - if (globStream.emittedEnd) { + if (streamEnded) { return; } globStream.pause(); setTimeout(() => { - if (globStream.emittedEnd) { + if (streamEnded) { return; } globStream.resume(); @@ -181,8 +189,15 @@ export class FileIndexService extends BaseService { } }); - globStream.on("end", () => resolve(allMatches)); - globStream.on("error", (err) => reject(err)); + globStream.on("end", () => { + streamEnded = true; + resolve(allMatches); + }); + + globStream.on("error", (err) => { + streamEnded = true; + reject(err); + }); }); } @@ -208,7 +223,7 @@ export class FileIndexService extends BaseService { if (filterText.length === 0) { // Show files sorted alphabetically when no filter - return files + return [...files] .sort((a, b) => { const aFileName = a.path.split("/").pop() || a.path; const bFileName = b.path.split("/").pop() || b.path; diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index 2cfd6cd7d..75403e8b2 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -6,7 +6,6 @@ import { ApiClientServiceState, AuthServiceState, ConfigServiceState, - FileIndexServiceState, MCPServiceState, ModelServiceState, } from "../services/types.js"; @@ -60,8 +59,7 @@ const TUIChat: React.FC = ({ model: ModelServiceState; mcp: MCPServiceState; apiClient: ApiClientServiceState; - fileIndex: FileIndexServiceState; - }>(["auth", "config", "model", "mcp", "apiClient", "fileIndex"]); + }>(["auth", "config", "model", "mcp", "apiClient"]); // Use navigation context const { diff --git a/src/ui/__tests__/TUIChat.testHelper.ts b/src/ui/__tests__/TUIChat.testHelper.ts index ade91012e..e450b809a 100644 --- a/src/ui/__tests__/TUIChat.testHelper.ts +++ b/src/ui/__tests__/TUIChat.testHelper.ts @@ -12,6 +12,17 @@ import { MockMCPService, } from "./TUIChat.setup.js"; +// Shared mock FileIndex to avoid duplication +const MOCK_FILE_INDEX = { + files: [ + { path: "README.md", displayName: "README.md" }, + { path: "src/index.ts", displayName: "src/index.ts" }, + { path: "package.json", displayName: "package.json" }, + ], + isIndexing: false, + error: null, +}; + // Define RenderResult type to match ink-testing-library's Instance interface RenderResult { stdin: { @@ -118,15 +129,6 @@ export function runTest( }, mcp: { mcpService: new MockMCPService() }, apiClient: { apiClient: new MockApiClient() }, - fileIndex: { - files: [ - { path: "README.md", displayName: "README.md" }, - { path: "src/index.ts", displayName: "src/index.ts" }, - { path: "package.json", displayName: "package.json" }, - ], - isIndexing: false, - error: null, - }, }, loading: false, error: null, @@ -152,15 +154,7 @@ export function runTest( case "apiClient": return { apiClient: new MockApiClient() }; case "fileIndex": - return { - files: [ - { path: "README.md", displayName: "README.md" }, - { path: "src/index.ts", displayName: "src/index.ts" }, - { path: "package.json", displayName: "package.json" }, - ], - isIndexing: false, - error: null, - }; + return MOCK_FILE_INDEX; default: return null; } @@ -292,15 +286,6 @@ export function runTestSuite( }, mcp: { mcpService: new MockMCPService() }, apiClient: { apiClient: new MockApiClient() }, - fileIndex: { - files: [ - { path: "README.md", displayName: "README.md" }, - { path: "src/index.ts", displayName: "src/index.ts" }, - { path: "package.json", displayName: "package.json" }, - ], - isIndexing: false, - error: null, - }, }, loading: false, error: null, @@ -326,21 +311,7 @@ export function runTestSuite( case "apiClient": return { apiClient: new MockApiClient() }; case "fileIndex": - return { - files: [ - { path: "README.md", displayName: "README.md" }, - { - path: "src/index.ts", - displayName: "src/index.ts", - }, - { - path: "package.json", - displayName: "package.json", - }, - ], - isIndexing: false, - error: null, - }; + return MOCK_FILE_INDEX; default: return null; } diff --git a/src/util/filePatterns.ts b/src/util/filePatterns.ts index 471d66f97..4b8fc4777 100644 --- a/src/util/filePatterns.ts +++ b/src/util/filePatterns.ts @@ -45,7 +45,7 @@ export const FILE_IGNORE_PATTERNS = [ "**/*.tgz", "**/*.rar", "**/*.7z", - "**/*.next", + "**/.next/**", ]; export const FILE_PATTERNS = [ diff --git a/src/util/fileWatcher.test.ts b/src/util/fileWatcher.test.ts index 9998d888e..9a31e8d93 100644 --- a/src/util/fileWatcher.test.ts +++ b/src/util/fileWatcher.test.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as os from 'os'; import * as path from 'path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -11,7 +12,7 @@ describe('FileWatcher', () => { beforeEach(() => { // Create a temporary directory for testing - tempDir = fs.mkdtempSync(path.join(process.cwd(), 'test-')); + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filewatcher-test-')); watcher = new FileWatcher({ debounceMs: 100, // Shorter debounce for tests maxDepth: 2 @@ -85,6 +86,74 @@ describe('FileWatcher', () => { expect(callback).not.toHaveBeenCalled(); }); + it('should ignore files matching wildcard patterns like **/*.tmp', async () => { + const callback = vi.fn(); + watcher.onChange(callback); + watcher.startWatching(tempDir); + + // Wait for watcher to initialize + await new Promise(resolve => setTimeout(resolve, 100)); + callback.mockClear(); + + // Create a .tmp file (should be ignored by **/*.tmp pattern) + const tmpFile = path.join(tempDir, 'test.tmp'); + fs.writeFileSync(tmpFile, 'temporary content'); + + // Create a .log file (should be ignored by **/*.log pattern) + const logFile = path.join(tempDir, 'debug.log'); + fs.writeFileSync(logFile, 'log content'); + + // Create a .min.js file (should be ignored by **/*.min.js pattern) + const minJsFile = path.join(tempDir, 'bundle.min.js'); + fs.writeFileSync(minJsFile, 'minified js'); + + // Wait for debounce + await new Promise(resolve => setTimeout(resolve, 300)); + + // Should not have been called because all files match ignore patterns + expect(callback).not.toHaveBeenCalled(); + }); + + it('should ignore specific files like .DS_Store', async () => { + const callback = vi.fn(); + watcher.onChange(callback); + watcher.startWatching(tempDir); + + // Wait for watcher to initialize + await new Promise(resolve => setTimeout(resolve, 100)); + callback.mockClear(); + + // Create .DS_Store file (should be ignored by **/.DS_Store pattern) + const dsStoreFile = path.join(tempDir, '.DS_Store'); + fs.writeFileSync(dsStoreFile, 'ds store content'); + + // Wait for debounce + await new Promise(resolve => setTimeout(resolve, 300)); + + // Should not have been called because .DS_Store matches ignore pattern + expect(callback).not.toHaveBeenCalled(); + }); + + it('should detect files that do not match ignore patterns', async () => { + const callback = vi.fn(); + watcher.onChange(callback); + watcher.startWatching(tempDir); + + // Wait for watcher to initialize + await new Promise(resolve => setTimeout(resolve, 100)); + callback.mockClear(); + + // Create a .js file (should NOT be ignored) + const jsFile = path.join(tempDir, 'test.js'); + fs.writeFileSync(jsFile, 'console.log("hello");'); + + // Wait for debounce + await new Promise(resolve => setTimeout(resolve, 300)); + + // Should have been called because .js files are not in ignore patterns + expect(callback).toHaveBeenCalled(); + }); + it('should allow unsubscribing from callbacks', async () => { const callback = vi.fn(); const unsubscribe = watcher.onChange(callback); @@ -103,4 +172,34 @@ describe('FileWatcher', () => { // Should not have been called because we unsubscribed expect(callback).not.toHaveBeenCalled(); }); + + describe('shouldIgnorePath method', () => { + it('should correctly ignore wildcard file patterns', () => { + const watcher = new FileWatcher(); + + // Test the private method by accessing it through prototype + const shouldIgnorePath = (watcher as any).shouldIgnorePath.bind(watcher); + + // Test wildcard file patterns + expect(shouldIgnorePath('/some/path/file.tmp')).toBe(true); + expect(shouldIgnorePath('/some/path/file.log')).toBe(true); + expect(shouldIgnorePath('/some/path/bundle.min.js')).toBe(true); + expect(shouldIgnorePath('/some/path/styles.min.css')).toBe(true); + + // Test files that should NOT be ignored + expect(shouldIgnorePath('/some/path/file.js')).toBe(false); + expect(shouldIgnorePath('/some/path/file.ts')).toBe(false); + expect(shouldIgnorePath('/some/path/README.md')).toBe(false); + + // Test specific files + expect(shouldIgnorePath('/some/path/.DS_Store')).toBe(true); + expect(shouldIgnorePath('/some/path/Thumbs.db')).toBe(true); + + // Test directory patterns + expect(shouldIgnorePath('/some/path/node_modules')).toBe(true); + expect(shouldIgnorePath('/some/path/node_modules/package')).toBe(true); + expect(shouldIgnorePath('/path/dist')).toBe(true); + expect(shouldIgnorePath('/path/.git')).toBe(true); + }); + }); }); diff --git a/src/util/fileWatcher.ts b/src/util/fileWatcher.ts index 5d1ae386c..7359fdc0f 100644 --- a/src/util/fileWatcher.ts +++ b/src/util/fileWatcher.ts @@ -38,10 +38,14 @@ export class FileWatcher { normalizedPath.endsWith(`/${dirName}`) || normalizedPath === dirName ); - } else { + } else if (suffix.startsWith("*")) { // File pattern like **/*.tmp + const extension = suffix.slice(1); // Remove the '*' to get the extension + return normalizedPath.endsWith(extension); + } else { + // File pattern like **/package.json return ( - normalizedPath.includes(suffix.slice(0, -1)) || + normalizedPath.endsWith(`/${suffix}`) || normalizedPath.endsWith(suffix) ); }