feat(core): generalize path correction for use across tools (#10612)

This commit is contained in:
anthony bushong
2025-10-09 13:51:16 -07:00
committed by GitHub
parent 433ca84ce0
commit ae02236c63
4 changed files with 174 additions and 126 deletions
@@ -274,77 +274,6 @@ describe('SmartEditTool', () => {
expect(result.occurrences).toBe(1);
});
});
describe('correctPath', () => {
it('should correct a relative path if it is unambiguous', () => {
const testFile = 'unique.txt';
fs.writeFileSync(path.join(rootDir, testFile), 'content');
const params: EditToolParams = {
file_path: testFile,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toBeNull();
expect(params.file_path).toBe(path.join(rootDir, testFile));
});
it('should correct a partial relative path if it is unambiguous', () => {
const subDir = path.join(rootDir, 'sub');
fs.mkdirSync(subDir);
const testFile = 'file.txt';
const partialPath = path.join('sub', testFile);
const fullPath = path.join(subDir, testFile);
fs.writeFileSync(fullPath, 'content');
const params: EditToolParams = {
file_path: partialPath,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toBeNull();
expect(params.file_path).toBe(fullPath);
});
it('should return an error for a relative path that does not exist', () => {
const params: EditToolParams = {
file_path: 'test.txt',
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const result = (tool as any).correctPath(params);
expect(result).toMatch(/File not found for 'test.txt'/);
});
it('should return an error for an ambiguous path', () => {
const subDir1 = path.join(rootDir, 'module1');
const subDir2 = path.join(rootDir, 'module2');
fs.mkdirSync(subDir1, { recursive: true });
fs.mkdirSync(subDir2, { recursive: true });
const ambiguousFile = 'component.ts';
fs.writeFileSync(path.join(subDir1, ambiguousFile), 'content 1');
fs.writeFileSync(path.join(subDir2, ambiguousFile), 'content 2');
const params: EditToolParams = {
file_path: ambiguousFile,
instruction: 'An instruction',
old_string: 'old',
new_string: 'new',
};
const validationResult = (tool as any).correctPath(params);
expect(validationResult).toMatch(/ambiguous and matches multiple files/);
});
});
describe('validateToolParams', () => {
it('should return null for valid params', () => {