address feedback
This commit is contained in:
parent
179f7a3e82
commit
e9de2c424c
7 changed files with 142 additions and 54 deletions
|
|
@ -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
|
||||
}),
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -42,6 +42,13 @@ export class FileIndexService extends BaseService<FileIndexServiceState> {
|
|||
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<FileIndexServiceState> {
|
|||
return new Promise<string[]>((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<FileIndexServiceState> {
|
|||
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<FileIndexServiceState> {
|
|||
}
|
||||
});
|
||||
|
||||
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<FileIndexServiceState> {
|
|||
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import {
|
|||
ApiClientServiceState,
|
||||
AuthServiceState,
|
||||
ConfigServiceState,
|
||||
FileIndexServiceState,
|
||||
MCPServiceState,
|
||||
ModelServiceState,
|
||||
} from "../services/types.js";
|
||||
|
|
@ -60,8 +59,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
model: ModelServiceState;
|
||||
mcp: MCPServiceState;
|
||||
apiClient: ApiClientServiceState;
|
||||
fileIndex: FileIndexServiceState;
|
||||
}>(["auth", "config", "model", "mcp", "apiClient", "fileIndex"]);
|
||||
}>(["auth", "config", "model", "mcp", "apiClient"]);
|
||||
|
||||
// Use navigation context
|
||||
const {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ export const FILE_IGNORE_PATTERNS = [
|
|||
"**/*.tgz",
|
||||
"**/*.rar",
|
||||
"**/*.7z",
|
||||
"**/*.next",
|
||||
"**/.next/**",
|
||||
];
|
||||
|
||||
export const FILE_PATTERNS = [
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue