diff options
| author | Bryan Morgan <[email protected]> | 2025-06-01 17:49:48 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-01 17:49:48 -0400 |
| commit | f7a2442faca5e3c51bab753672130968456a4c23 (patch) | |
| tree | 0024439ae480c7626f4e3232dc5cff0ff475d51f /packages/core/src/tools/edit.test.ts | |
| parent | f2a8d39f42ae88c1b7a9a5a75854363a53444ca2 (diff) | |
Added replace tool ability to replace more than 1 occurrence (#669)
Co-authored-by: N. Taylor Mullen <[email protected]>
Diffstat (limited to 'packages/core/src/tools/edit.test.ts')
| -rw-r--r-- | packages/core/src/tools/edit.test.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/packages/core/src/tools/edit.test.ts b/packages/core/src/tools/edit.test.ts index e0de98d5..c6c2ba63 100644 --- a/packages/core/src/tools/edit.test.ts +++ b/packages/core/src/tools/edit.test.ts @@ -484,6 +484,49 @@ describe('EditTool', () => { ); }); + it('should successfully replace multiple occurrences when expected_replacements specified', async () => { + fs.writeFileSync(filePath, 'old text old text old text', 'utf8'); + const params: EditToolParams = { + file_path: filePath, + old_string: 'old', + new_string: 'new', + expected_replacements: 3, + }; + + // Simulate confirmation by setting shouldAlwaysEdit + (tool as any).shouldAlwaysEdit = true; + + const result = await tool.execute(params, new AbortController().signal); + + (tool as any).shouldAlwaysEdit = false; // Reset for other tests + + expect(result.llmContent).toMatch(/Successfully modified file/); + expect(fs.readFileSync(filePath, 'utf8')).toBe( + 'new text new text new text', + ); + const display = result.returnDisplay as FileDiff; + expect(display.fileDiff).toMatch(/old text old text old text/); + expect(display.fileDiff).toMatch(/new text new text new text/); + expect(display.fileName).toBe(testFile); + }); + + it('should return error if expected_replacements does not match actual occurrences', async () => { + fs.writeFileSync(filePath, 'old text old text', 'utf8'); + const params: EditToolParams = { + file_path: filePath, + old_string: 'old', + new_string: 'new', + expected_replacements: 3, // Expecting 3 but only 2 exist + }; + const result = await tool.execute(params, new AbortController().signal); + expect(result.llmContent).toMatch( + /Expected 3 occurrences but found 2 for old_string in file/, + ); + expect(result.returnDisplay).toMatch( + /Failed to edit, expected 3 occurrence\(s\) but found 2/, + ); + }); + it('should return error if trying to create a file that already exists (empty old_string)', async () => { fs.writeFileSync(filePath, 'Existing content', 'utf8'); const params: EditToolParams = { |
