summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/edit.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/core/src/tools/edit.test.ts')
-rw-r--r--packages/core/src/tools/edit.test.ts43
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 = {