Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/filesystem/__tests__/directory-tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ interface TreeEntry {
children?: TreeEntry[];
}

function compareEntryNames(left: { name: string }, right: { name: string }): number {
return left.name.localeCompare(right.name, 'en');
}

async function buildTreeForTesting(currentPath: string, rootPath: string, excludePatterns: string[] = []): Promise<TreeEntry[]> {
const entries = await fs.readdir(currentPath, {withFileTypes: true});
const sortedEntries = [...entries].sort(compareEntryNames);
const result: TreeEntry[] = [];

for (const entry of entries) {
for (const entry of sortedEntries) {
const relativePath = path.relative(rootPath, path.join(currentPath, entry.name));
const shouldExclude = excludePatterns.some(pattern => {
if (pattern.includes('*')) {
Expand Down Expand Up @@ -137,11 +142,24 @@ describe('buildTree exclude patterns', () => {
it('should handle empty exclude patterns', async () => {
const tree = await buildTreeForTesting(testDir, testDir, []);
const entryNames = tree.map(entry => entry.name);

// All entries should be included
expect(entryNames).toContain('node_modules');
expect(entryNames).toContain('.env');
expect(entryNames).toContain('.git');
expect(entryNames).toContain('src');
});
});

it('should return root and nested entries in deterministic name order', async () => {
const tree = await buildTreeForTesting(testDir, testDir, []);
const rootNames = tree.map(entry => entry.name);
const sortedRootNames = [...rootNames].sort((left, right) => left.localeCompare(right, 'en'));
expect(rootNames).toEqual(sortedRootNames);

const nestedDir = tree.find(entry => entry.name === 'nested');
expect(nestedDir).toBeDefined();
const nestedNames = (nestedDir?.children ?? []).map(entry => entry.name);
const sortedNestedNames = [...nestedNames].sort((left, right) => left.localeCompare(right, 'en'));
expect(nestedNames).toEqual(sortedNestedNames);
});
});
7 changes: 6 additions & 1 deletion src/filesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,17 @@ server.registerTool(
}
const rootPath = args.path;

function compareEntryNames(left: { name: string }, right: { name: string }): number {
return left.name.localeCompare(right.name, 'en');
}

async function buildTree(currentPath: string, excludePatterns: string[] = []): Promise<TreeEntry[]> {
const validPath = await validatePath(currentPath);
const entries = await fs.readdir(validPath, { withFileTypes: true });
const sortedEntries = [...entries].sort(compareEntryNames);
const result: TreeEntry[] = [];

for (const entry of entries) {
for (const entry of sortedEntries) {
const relativePath = path.relative(rootPath, path.join(currentPath, entry.name));
const shouldExclude = excludePatterns.some(pattern => {
if (pattern.includes('*')) {
Expand Down
Loading