2025-05-15 23:51:53 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
|
2025-05-25 14:16:08 -07:00
|
|
|
const mockEnsureCorrectEdit = vi.hoisted(() => vi.fn());
|
|
|
|
|
const mockGenerateJson = vi.hoisted(() => vi.fn());
|
2025-06-08 18:56:58 +01:00
|
|
|
const mockOpenDiff = vi.hoisted(() => vi.fn());
|
2025-05-25 14:16:08 -07:00
|
|
|
|
|
|
|
|
vi.mock('../utils/editCorrector.js', () => ({
|
|
|
|
|
ensureCorrectEdit: mockEnsureCorrectEdit,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock('../core/client.js', () => ({
|
|
|
|
|
GeminiClient: vi.fn().mockImplementation(() => ({
|
|
|
|
|
generateJson: mockGenerateJson,
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-06-08 18:56:58 +01:00
|
|
|
vi.mock('../utils/editor.js', () => ({
|
|
|
|
|
openDiff: mockOpenDiff,
|
|
|
|
|
}));
|
|
|
|
|
|
2025-05-16 23:33:12 -07:00
|
|
|
import { describe, it, expect, beforeEach, afterEach, vi, Mock } from 'vitest';
|
2025-05-15 23:51:53 -07:00
|
|
|
import { EditTool, EditToolParams } from './edit.js';
|
|
|
|
|
import { FileDiff } from './tools.js';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import os from 'os';
|
2025-06-02 22:05:45 +02:00
|
|
|
import { ApprovalMode, Config } from '../config/config.js';
|
2025-05-16 16:39:28 -07:00
|
|
|
import { Content, Part, SchemaUnion } from '@google/genai';
|
2025-06-08 18:56:58 +01:00
|
|
|
import { ToolConfirmationOutcome } from './tools.js';
|
2025-05-15 23:51:53 -07:00
|
|
|
|
|
|
|
|
describe('EditTool', () => {
|
|
|
|
|
let tool: EditTool;
|
|
|
|
|
let tempDir: string;
|
|
|
|
|
let rootDir: string;
|
|
|
|
|
let mockConfig: Config;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'edit-tool-test-'));
|
|
|
|
|
rootDir = path.join(tempDir, 'root');
|
|
|
|
|
fs.mkdirSync(rootDir);
|
|
|
|
|
|
2025-06-02 22:30:52 -07:00
|
|
|
// The client instance that EditTool will use
|
|
|
|
|
const mockClientInstanceWithGenerateJson = {
|
|
|
|
|
generateJson: mockGenerateJson, // mockGenerateJson is already defined and hoisted
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
mockConfig = {
|
2025-06-02 22:30:52 -07:00
|
|
|
getGeminiClient: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockReturnValue(mockClientInstanceWithGenerateJson),
|
2025-05-15 23:51:53 -07:00
|
|
|
getTargetDir: () => rootDir,
|
2025-06-02 22:05:45 +02:00
|
|
|
getApprovalMode: vi.fn(() => false),
|
|
|
|
|
setApprovalMode: vi.fn(),
|
2025-05-16 23:33:12 -07:00
|
|
|
// getGeminiConfig: () => ({ apiKey: 'test-api-key' }), // This was not a real Config method
|
2025-05-15 23:51:53 -07:00
|
|
|
// Add other properties/methods of Config if EditTool uses them
|
2025-05-16 23:33:12 -07:00
|
|
|
// Minimal other methods to satisfy Config type if needed by EditTool constructor or other direct uses:
|
|
|
|
|
getApiKey: () => 'test-api-key',
|
|
|
|
|
getModel: () => 'test-model',
|
|
|
|
|
getSandbox: () => false,
|
|
|
|
|
getDebugMode: () => false,
|
|
|
|
|
getQuestion: () => undefined,
|
|
|
|
|
getFullContext: () => false,
|
|
|
|
|
getToolDiscoveryCommand: () => undefined,
|
|
|
|
|
getToolCallCommand: () => undefined,
|
|
|
|
|
getMcpServerCommand: () => undefined,
|
|
|
|
|
getMcpServers: () => undefined,
|
|
|
|
|
getUserAgent: () => 'test-agent',
|
|
|
|
|
getUserMemory: () => '',
|
|
|
|
|
setUserMemory: vi.fn(),
|
|
|
|
|
getGeminiMdFileCount: () => 0,
|
|
|
|
|
setGeminiMdFileCount: vi.fn(),
|
|
|
|
|
getToolRegistry: () => ({}) as any, // Minimal mock for ToolRegistry
|
2025-05-15 23:51:53 -07:00
|
|
|
} as unknown as Config;
|
|
|
|
|
|
2025-05-16 23:33:12 -07:00
|
|
|
// Reset mocks before each test
|
2025-06-02 22:05:45 +02:00
|
|
|
(mockConfig.getApprovalMode as Mock).mockClear();
|
|
|
|
|
(mockConfig.getApprovalMode as Mock).mockClear();
|
2025-05-16 23:33:12 -07:00
|
|
|
// Default to not skipping confirmation
|
2025-06-02 22:05:45 +02:00
|
|
|
(mockConfig.getApprovalMode as Mock).mockReturnValue(ApprovalMode.DEFAULT);
|
2025-05-16 23:33:12 -07:00
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
// Reset mocks and set default implementation for ensureCorrectEdit
|
|
|
|
|
mockEnsureCorrectEdit.mockReset();
|
|
|
|
|
mockEnsureCorrectEdit.mockImplementation(async (currentContent, params) => {
|
|
|
|
|
let occurrences = 0;
|
|
|
|
|
if (params.old_string && currentContent) {
|
|
|
|
|
// Simple string counting for the mock
|
|
|
|
|
let index = currentContent.indexOf(params.old_string);
|
|
|
|
|
while (index !== -1) {
|
|
|
|
|
occurrences++;
|
|
|
|
|
index = currentContent.indexOf(params.old_string, index + 1);
|
|
|
|
|
}
|
|
|
|
|
} else if (params.old_string === '') {
|
|
|
|
|
occurrences = 0; // Creating a new file
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve({ params, occurrences });
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-16 16:39:28 -07:00
|
|
|
// Default mock for generateJson to return the snippet unchanged
|
|
|
|
|
mockGenerateJson.mockReset();
|
|
|
|
|
mockGenerateJson.mockImplementation(
|
|
|
|
|
async (contents: Content[], schema: SchemaUnion) => {
|
|
|
|
|
// The problematic_snippet is the last part of the user's content
|
|
|
|
|
const userContent = contents.find((c: Content) => c.role === 'user');
|
|
|
|
|
let promptText = '';
|
|
|
|
|
if (userContent && userContent.parts) {
|
|
|
|
|
promptText = userContent.parts
|
|
|
|
|
.filter((p: Part) => typeof (p as any).text === 'string')
|
|
|
|
|
.map((p: Part) => (p as any).text)
|
|
|
|
|
.join('\n');
|
|
|
|
|
}
|
|
|
|
|
const snippetMatch = promptText.match(
|
|
|
|
|
/Problematic target snippet:\n```\n([\s\S]*?)\n```/,
|
|
|
|
|
);
|
|
|
|
|
const problematicSnippet =
|
|
|
|
|
snippetMatch && snippetMatch[1] ? snippetMatch[1] : '';
|
|
|
|
|
|
2025-05-26 14:07:22 -07:00
|
|
|
if (((schema as any).properties as any)?.corrected_target_snippet) {
|
2025-05-16 16:39:28 -07:00
|
|
|
return Promise.resolve({
|
|
|
|
|
corrected_target_snippet: problematicSnippet,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-05-26 14:07:22 -07:00
|
|
|
if (((schema as any).properties as any)?.corrected_new_string) {
|
2025-05-16 16:39:28 -07:00
|
|
|
// For new_string correction, we might need more sophisticated logic,
|
|
|
|
|
// but for now, returning original is a safe default if not specified by a test.
|
|
|
|
|
const originalNewStringMatch = promptText.match(
|
|
|
|
|
/original_new_string \(what was intended to replace original_old_string\):\n```\n([\s\S]*?)\n```/,
|
|
|
|
|
);
|
|
|
|
|
const originalNewString =
|
|
|
|
|
originalNewStringMatch && originalNewStringMatch[1]
|
|
|
|
|
? originalNewStringMatch[1]
|
|
|
|
|
: '';
|
|
|
|
|
return Promise.resolve({ corrected_new_string: originalNewString });
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve({}); // Default empty object if schema doesn't match
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
tool = new EditTool(mockConfig);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-25 14:16:08 -07:00
|
|
|
describe('_applyReplacement', () => {
|
|
|
|
|
// Access private method for testing
|
|
|
|
|
// Note: `tool` is initialized in `beforeEach` of the parent describe block
|
|
|
|
|
it('should return newString if isNewFile is true', () => {
|
|
|
|
|
expect((tool as any)._applyReplacement(null, 'old', 'new', true)).toBe(
|
|
|
|
|
'new',
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
(tool as any)._applyReplacement('existing', 'old', 'new', true),
|
|
|
|
|
).toBe('new');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return newString if currentContent is null and oldString is empty (defensive)', () => {
|
|
|
|
|
expect((tool as any)._applyReplacement(null, '', 'new', false)).toBe(
|
|
|
|
|
'new',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return empty string if currentContent is null and oldString is not empty (defensive)', () => {
|
|
|
|
|
expect((tool as any)._applyReplacement(null, 'old', 'new', false)).toBe(
|
|
|
|
|
'',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should replace oldString with newString in currentContent', () => {
|
|
|
|
|
expect(
|
|
|
|
|
(tool as any)._applyReplacement(
|
|
|
|
|
'hello old world old',
|
|
|
|
|
'old',
|
|
|
|
|
'new',
|
|
|
|
|
false,
|
|
|
|
|
),
|
|
|
|
|
).toBe('hello new world new');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return currentContent if oldString is empty and not a new file', () => {
|
|
|
|
|
expect(
|
|
|
|
|
(tool as any)._applyReplacement('hello world', '', 'new', false),
|
|
|
|
|
).toBe('hello world');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-01 19:18:43 +01:00
|
|
|
describe('validateToolParams', () => {
|
2025-05-15 23:51:53 -07:00
|
|
|
it('should return null for valid params', () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(rootDir, 'test.txt'),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-01 19:18:43 +01:00
|
|
|
expect(tool.validateToolParams(params)).toBeNull();
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error for relative path', () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: 'test.txt',
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-01 19:18:43 +01:00
|
|
|
expect(tool.validateToolParams(params)).toMatch(
|
|
|
|
|
/File path must be absolute/,
|
|
|
|
|
);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error for path outside root', () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(tempDir, 'outside-root.txt'),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-01 19:18:43 +01:00
|
|
|
expect(tool.validateToolParams(params)).toMatch(
|
2025-05-15 23:51:53 -07:00
|
|
|
/File path must be within the root directory/,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('shouldConfirmExecute', () => {
|
|
|
|
|
const testFile = 'edit_me.txt';
|
|
|
|
|
let filePath: string;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
filePath = path.join(rootDir, testFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return false if params are invalid', async () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: 'relative.txt',
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-05-27 23:40:25 -07:00
|
|
|
expect(
|
|
|
|
|
await tool.shouldConfirmExecute(params, new AbortController().signal),
|
|
|
|
|
).toBe(false);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should request confirmation for valid edit', async () => {
|
|
|
|
|
fs.writeFileSync(filePath, 'some old content here');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
|
|
|
|
// ensureCorrectEdit will be called by shouldConfirmExecute
|
|
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({ params, occurrences: 1 });
|
2025-05-27 23:40:25 -07:00
|
|
|
const confirmation = await tool.shouldConfirmExecute(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
);
|
2025-05-15 23:51:53 -07:00
|
|
|
expect(confirmation).toEqual(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
title: `Confirm Edit: ${testFile}`,
|
|
|
|
|
fileName: testFile,
|
|
|
|
|
fileDiff: expect.any(String),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return false if old_string is not found (ensureCorrectEdit returns 0)', async () => {
|
|
|
|
|
fs.writeFileSync(filePath, 'some content here');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'not_found', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-06 22:54:37 -07:00
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({
|
|
|
|
|
params: {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
old_string: 'not_found',
|
|
|
|
|
new_string: 'new',
|
|
|
|
|
},
|
|
|
|
|
occurrences: 0,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Our new implementation shows confirmation but with no changes,
|
|
|
|
|
// which should still return false due to no edits applied
|
|
|
|
|
const result = await tool.shouldConfirmExecute(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
);
|
|
|
|
|
// If no edits would be applied, confirmation should be false
|
|
|
|
|
expect(result).toBe(false);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return false if multiple occurrences of old_string are found (ensureCorrectEdit returns > 1)', async () => {
|
|
|
|
|
fs.writeFileSync(filePath, 'old old content here');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-06 22:54:37 -07:00
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({
|
|
|
|
|
params: {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
old_string: 'old',
|
|
|
|
|
new_string: 'new',
|
|
|
|
|
},
|
|
|
|
|
occurrences: 2,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Multiple occurrences should result in failed edit, no confirmation
|
|
|
|
|
const result = await tool.shouldConfirmExecute(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
);
|
|
|
|
|
expect(result).toBe(false);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should request confirmation for creating a new file (empty old_string)', async () => {
|
|
|
|
|
const newFileName = 'new_file.txt';
|
|
|
|
|
const newFilePath = path.join(rootDir, newFileName);
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: newFilePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: '', new_string: 'new file content' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-05-27 23:40:25 -07:00
|
|
|
const confirmation = await tool.shouldConfirmExecute(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
);
|
2025-05-15 23:51:53 -07:00
|
|
|
expect(confirmation).toEqual(
|
|
|
|
|
expect.objectContaining({
|
2025-06-06 22:54:37 -07:00
|
|
|
title: expect.stringContaining(newFileName),
|
2025-05-15 23:51:53 -07:00
|
|
|
fileName: newFileName,
|
|
|
|
|
fileDiff: expect.any(String),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
2025-05-25 14:16:08 -07:00
|
|
|
|
2025-06-06 22:54:37 -07:00
|
|
|
it('should not use AI correction and provide clear feedback for non-matching text', async () => {
|
2025-05-25 14:16:08 -07:00
|
|
|
const originalContent = 'This is the original string to be replaced.';
|
2025-06-06 22:54:37 -07:00
|
|
|
const nonMatchingOldString = 'completely different text'; // This won't match at all
|
|
|
|
|
const newString = 'new string';
|
2025-05-25 14:16:08 -07:00
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, originalContent);
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: nonMatchingOldString, new_string: newString }],
|
2025-05-25 14:16:08 -07:00
|
|
|
};
|
|
|
|
|
|
2025-06-06 22:54:37 -07:00
|
|
|
// With deterministic approach, this should return false (no confirmation)
|
|
|
|
|
// because the old_string doesn't match exactly
|
|
|
|
|
const confirmation = await tool.shouldConfirmExecute(
|
2025-05-25 14:16:08 -07:00
|
|
|
params,
|
2025-05-27 23:40:25 -07:00
|
|
|
new AbortController().signal,
|
2025-05-25 14:16:08 -07:00
|
|
|
);
|
2025-06-06 22:54:37 -07:00
|
|
|
|
|
|
|
|
// Should return false because edit will fail (no exact match)
|
|
|
|
|
expect(confirmation).toBe(false);
|
2025-05-25 14:16:08 -07:00
|
|
|
});
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('execute', () => {
|
|
|
|
|
const testFile = 'execute_me.txt';
|
|
|
|
|
let filePath: string;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
filePath = path.join(rootDir, testFile);
|
|
|
|
|
// Default for execute tests, can be overridden
|
|
|
|
|
mockEnsureCorrectEdit.mockImplementation(async (content, params) => {
|
|
|
|
|
let occurrences = 0;
|
|
|
|
|
if (params.old_string && content) {
|
|
|
|
|
let index = content.indexOf(params.old_string);
|
|
|
|
|
while (index !== -1) {
|
|
|
|
|
occurrences++;
|
|
|
|
|
index = content.indexOf(params.old_string, index + 1);
|
|
|
|
|
}
|
|
|
|
|
} else if (params.old_string === '') {
|
|
|
|
|
occurrences = 0;
|
|
|
|
|
}
|
|
|
|
|
return { params, occurrences };
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error if params are invalid', async () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: 'relative.txt',
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
expect(result.llmContent).toMatch(/Error: Invalid parameters provided/);
|
|
|
|
|
expect(result.returnDisplay).toMatch(/Error: File path must be absolute/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should edit an existing file and return diff with fileName', async () => {
|
|
|
|
|
const initialContent = 'This is some old text.';
|
|
|
|
|
const newContent = 'This is some new text.'; // old -> new
|
|
|
|
|
fs.writeFileSync(filePath, initialContent, 'utf8');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
|
|
|
|
|
2025-06-06 22:54:37 -07:00
|
|
|
// Mock ensureCorrectEdit to return the expected params and occurrences
|
|
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({
|
|
|
|
|
params: {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
old_string: 'old',
|
|
|
|
|
new_string: 'new',
|
|
|
|
|
},
|
|
|
|
|
occurrences: 1,
|
|
|
|
|
});
|
2025-05-15 23:51:53 -07:00
|
|
|
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.llmContent).toMatch(/Successfully applied 1\/1 edits/);
|
|
|
|
|
expect(result.editsApplied).toBe(1);
|
|
|
|
|
expect(result.editsAttempted).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(0);
|
2025-05-15 23:51:53 -07:00
|
|
|
expect(fs.readFileSync(filePath, 'utf8')).toBe(newContent);
|
|
|
|
|
const display = result.returnDisplay as FileDiff;
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(display.fileDiff).toContain('-This is some old text.');
|
|
|
|
|
expect(display.fileDiff).toContain('+This is some new text.');
|
2025-05-15 23:51:53 -07:00
|
|
|
expect(display.fileName).toBe(testFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should create a new file if old_string is empty and file does not exist, and return created message', async () => {
|
|
|
|
|
const newFileName = 'brand_new_file.txt';
|
|
|
|
|
const newFilePath = path.join(rootDir, newFileName);
|
|
|
|
|
const fileContent = 'Content for the new file.';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: newFilePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: '', new_string: fileContent }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
|
|
|
|
|
2025-06-02 22:05:45 +02:00
|
|
|
(mockConfig.getApprovalMode as Mock).mockReturnValueOnce(
|
|
|
|
|
ApprovalMode.AUTO_EDIT,
|
|
|
|
|
);
|
2025-05-15 23:51:53 -07:00
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
|
|
|
|
|
expect(result.llmContent).toMatch(/Created new file/);
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.editsApplied).toBe(1);
|
|
|
|
|
expect(result.editsAttempted).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(0);
|
2025-05-15 23:51:53 -07:00
|
|
|
expect(fs.readFileSync(newFilePath, 'utf8')).toBe(fileContent);
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.returnDisplay).toContain('Created');
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error if old_string is not found in file', async () => {
|
|
|
|
|
fs.writeFileSync(filePath, 'Some content.', 'utf8');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'nonexistent', new_string: 'replacement' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-06 22:54:37 -07:00
|
|
|
// Mock ensureCorrectEdit to return 0 occurrences
|
|
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({
|
|
|
|
|
params: {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
old_string: 'not_found',
|
|
|
|
|
new_string: 'replacement',
|
|
|
|
|
},
|
|
|
|
|
occurrences: 0,
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.llmContent).toMatch(/Failed to apply any edits/);
|
|
|
|
|
expect(result.editsApplied).toBe(0);
|
|
|
|
|
expect(result.editsAttempted).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(1);
|
|
|
|
|
expect(result.failedEdits).toHaveLength(1);
|
|
|
|
|
expect(result.failedEdits![0].error).toMatch(/String not found/);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error if multiple occurrences of old_string are found', async () => {
|
2025-06-06 22:54:37 -07:00
|
|
|
const initialContent = 'old old content here';
|
|
|
|
|
fs.writeFileSync(filePath, initialContent, 'utf8');
|
2025-05-15 23:51:53 -07:00
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-06 22:54:37 -07:00
|
|
|
|
|
|
|
|
// Mock ensureCorrectEdit to return multiple occurrences
|
|
|
|
|
mockEnsureCorrectEdit.mockResolvedValueOnce({
|
|
|
|
|
params: {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
old_string: 'old',
|
|
|
|
|
new_string: 'new',
|
|
|
|
|
},
|
|
|
|
|
occurrences: 2,
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
2025-06-06 22:54:37 -07:00
|
|
|
|
|
|
|
|
expect(result.llmContent).toMatch(/Failed to apply any edits/);
|
|
|
|
|
expect(result.editsApplied).toBe(0);
|
|
|
|
|
expect(result.editsAttempted).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(1);
|
|
|
|
|
expect(result.failedEdits).toHaveLength(1);
|
|
|
|
|
expect(result.failedEdits![0].error).toMatch(
|
|
|
|
|
/Expected 1 occurrences but found 2/,
|
2025-05-15 23:51:53 -07:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-01 17:49:48 -04:00
|
|
|
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,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-06-01 17:49:48 -04:00
|
|
|
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
|
|
|
|
|
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.llmContent).toMatch(/Successfully applied 1\/1 edits/);
|
2025-06-01 17:49:48 -04:00
|
|
|
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,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-06-01 17:49:48 -04:00
|
|
|
expected_replacements: 3, // Expecting 3 but only 2 exist
|
|
|
|
|
};
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
expect(result.llmContent).toMatch(
|
2025-06-06 22:54:37 -07:00
|
|
|
/Failed to apply any edits.*Expected 3 occurrences but found 2/,
|
2025-06-01 17:49:48 -04:00
|
|
|
);
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.returnDisplay).toMatch(/No edits applied/);
|
2025-06-01 17:49:48 -04:00
|
|
|
});
|
|
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
it('should return error if trying to create a file that already exists (empty old_string)', async () => {
|
2025-06-06 22:54:37 -07:00
|
|
|
const existingContent = 'File already exists.';
|
|
|
|
|
fs.writeFileSync(filePath, existingContent, 'utf8');
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [{ old_string: '', new_string: 'new content' }],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
|
|
|
|
|
expect(result.llmContent).toMatch(/File already exists/);
|
|
|
|
|
expect(result.editsApplied).toBe(0);
|
|
|
|
|
expect(result.editsAttempted).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject multiple edits with mixed file creation and editing on non-existent file', async () => {
|
|
|
|
|
// Ensure file doesn't exist
|
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
|
fs.unlinkSync(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [
|
|
|
|
|
{ old_string: '', new_string: 'new content' },
|
|
|
|
|
{ old_string: 'some text', new_string: 'replacement' },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
|
|
|
|
|
// File should be created with first edit, but second edit should fail
|
|
|
|
|
expect(result.llmContent).toMatch(/Created new file.*Failed edits/);
|
|
|
|
|
expect(result.editsApplied).toBe(1);
|
|
|
|
|
expect(result.editsFailed).toBe(1);
|
|
|
|
|
expect(result.failedEdits![0].error).toMatch(/String not found/);
|
|
|
|
|
|
|
|
|
|
// File should now exist with content from first edit
|
|
|
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
|
|
|
expect(fs.readFileSync(filePath, 'utf8')).toBe('new content');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should demonstrate deterministic position-based edit behavior', async () => {
|
|
|
|
|
// Demonstrates that position-based processor is strict about exact matches
|
|
|
|
|
const originalContent = `function processUser(userData) {
|
|
|
|
|
const userName = userData.name;
|
|
|
|
|
console.log('Processing user:', userName);
|
|
|
|
|
return { user: userName, processed: true };
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, originalContent);
|
|
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [
|
|
|
|
|
// This edit will succeed - userData appears exactly once
|
|
|
|
|
{ old_string: 'userData', new_string: 'userInfo' },
|
|
|
|
|
// This edit will fail - after first edit, this exact string no longer exists
|
|
|
|
|
{
|
|
|
|
|
old_string: 'const userName = userData.name;',
|
|
|
|
|
new_string: 'const displayName = userInfo.name;',
|
|
|
|
|
},
|
|
|
|
|
// These demonstrate that dependent edits fail when context changes
|
|
|
|
|
{
|
|
|
|
|
old_string: "console.log('Processing user:', userName);",
|
|
|
|
|
new_string: "console.log('Processing user:', displayName);",
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-05-15 23:51:53 -07:00
|
|
|
};
|
2025-06-06 22:54:37 -07:00
|
|
|
|
2025-05-15 23:51:53 -07:00
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
2025-06-06 22:54:37 -07:00
|
|
|
expect(result.llmContent).toMatch(/Successfully applied 2\/3 edits/);
|
|
|
|
|
expect(result.llmContent).toMatch(
|
|
|
|
|
/Failed edits.*Expected 1 occurrences but found 2/,
|
2025-05-15 23:51:53 -07:00
|
|
|
);
|
2025-06-06 22:54:37 -07:00
|
|
|
|
|
|
|
|
// Verify what edits were actually applied (based on position-based processing)
|
|
|
|
|
const finalContent = fs.readFileSync(filePath, 'utf8');
|
|
|
|
|
// Check that the content changed in some way (deterministic behavior test)
|
|
|
|
|
expect(finalContent).not.toBe(originalContent);
|
|
|
|
|
// The exact result depends on position-based processing order
|
|
|
|
|
expect(finalContent).toContain('userInfo');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle non-conflicting edits efficiently', async () => {
|
|
|
|
|
// Demonstrates successful position-based processing with non-conflicting edits
|
|
|
|
|
const originalContent = `const config = {
|
|
|
|
|
apiUrl: 'https://api.old.com',
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
retries: 3
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function makeRequest() {
|
|
|
|
|
return fetch(config.apiUrl);
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, originalContent);
|
|
|
|
|
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [
|
|
|
|
|
// These edits don't interfere with each other
|
|
|
|
|
{
|
|
|
|
|
old_string: "apiUrl: 'https://api.old.com'",
|
|
|
|
|
new_string: "apiUrl: 'https://api.new.com'",
|
|
|
|
|
},
|
|
|
|
|
{ old_string: 'timeout: 5000', new_string: 'timeout: 10000' },
|
|
|
|
|
{ old_string: 'retries: 3', new_string: 'retries: 5' },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = await tool.execute(params, new AbortController().signal);
|
|
|
|
|
expect(result.llmContent).toMatch(/Successfully applied 3\/3 edits/);
|
|
|
|
|
|
|
|
|
|
// All edits should succeed because they don't conflict
|
|
|
|
|
const finalContent = fs.readFileSync(filePath, 'utf8');
|
|
|
|
|
const expectedContent = `const config = {
|
|
|
|
|
apiUrl: 'https://api.new.com',
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
retries: 5
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function makeRequest() {
|
|
|
|
|
return fetch(config.apiUrl);
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
expect(finalContent).toBe(expectedContent);
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|
|
|
|
|
});
|
2025-06-05 06:48:03 -07:00
|
|
|
|
|
|
|
|
describe('getDescription', () => {
|
2025-06-06 22:54:37 -07:00
|
|
|
it('should return consistent format even if old_string and new_string are the same', () => {
|
2025-06-05 06:48:03 -07:00
|
|
|
const testFileName = 'test.txt';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(rootDir, testFileName),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [
|
|
|
|
|
{ old_string: 'identical_string', new_string: 'identical_string' },
|
|
|
|
|
],
|
2025-06-05 06:48:03 -07:00
|
|
|
};
|
|
|
|
|
// shortenPath will be called internally, resulting in just the file name
|
|
|
|
|
expect(tool.getDescription(params)).toBe(
|
2025-06-06 22:54:37 -07:00
|
|
|
`${testFileName}: identical_string => identical_string`,
|
2025-06-05 06:48:03 -07:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a snippet of old and new strings if they are different', () => {
|
|
|
|
|
const testFileName = 'test.txt';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(rootDir, testFileName),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [
|
|
|
|
|
{
|
|
|
|
|
old_string: 'this is the old string value',
|
|
|
|
|
new_string: 'this is the new string value',
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-06-05 06:48:03 -07:00
|
|
|
};
|
|
|
|
|
// shortenPath will be called internally, resulting in just the file name
|
|
|
|
|
// The snippets are truncated at 30 chars + '...'
|
|
|
|
|
expect(tool.getDescription(params)).toBe(
|
|
|
|
|
`${testFileName}: this is the old string value => this is the new string value`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle very short strings correctly in the description', () => {
|
|
|
|
|
const testFileName = 'short.txt';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(rootDir, testFileName),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
2025-06-05 06:48:03 -07:00
|
|
|
};
|
|
|
|
|
expect(tool.getDescription(params)).toBe(`${testFileName}: old => new`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should truncate long strings in the description', () => {
|
|
|
|
|
const testFileName = 'long.txt';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: path.join(rootDir, testFileName),
|
2025-06-06 22:54:37 -07:00
|
|
|
edits: [
|
|
|
|
|
{
|
|
|
|
|
old_string:
|
|
|
|
|
'this is a very long old string that will definitely be truncated',
|
|
|
|
|
new_string:
|
|
|
|
|
'this is a very long new string that will also be truncated',
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-06-05 06:48:03 -07:00
|
|
|
};
|
|
|
|
|
expect(tool.getDescription(params)).toBe(
|
|
|
|
|
`${testFileName}: this is a very long old string... => this is a very long new string...`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-06-08 18:56:58 +01:00
|
|
|
|
|
|
|
|
describe('onModify', () => {
|
|
|
|
|
const testFile = 'some_file.txt';
|
|
|
|
|
let filePath: string;
|
|
|
|
|
const diffDir = path.join(os.tmpdir(), 'gemini-cli-edit-tool-diffs');
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
filePath = path.join(rootDir, testFile);
|
|
|
|
|
mockOpenDiff.mockClear();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fs.rmSync(diffDir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should create temporary files, call openDiff, and return updated params with diff', async () => {
|
|
|
|
|
const originalContent = 'original content';
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [
|
|
|
|
|
{ old_string: originalContent, new_string: 'modified content' },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, originalContent, 'utf8');
|
|
|
|
|
|
|
|
|
|
const result = await tool.onModify(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
ToolConfirmationOutcome.ModifyVSCode,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(mockOpenDiff).toHaveBeenCalledTimes(1);
|
|
|
|
|
const [oldPath, newPath] = mockOpenDiff.mock.calls[0];
|
|
|
|
|
expect(oldPath).toMatch(
|
|
|
|
|
/gemini-cli-edit-tool-diffs[/\\]gemini-cli-edit-some_file\.txt-old-\d+/,
|
|
|
|
|
);
|
|
|
|
|
expect(newPath).toMatch(
|
|
|
|
|
/gemini-cli-edit-tool-diffs[/\\]gemini-cli-edit-some_file\.txt-new-\d+/,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
|
expect(result!.updatedParams).toEqual({
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [
|
|
|
|
|
{ old_string: originalContent, new_string: 'modified content' },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.updatedDiff).toEqual(`Index: some_file.txt
|
|
|
|
|
===================================================================
|
|
|
|
|
--- some_file.txt\tCurrent
|
|
|
|
|
+++ some_file.txt\tProposed
|
|
|
|
|
@@ -1,1 +1,1 @@
|
|
|
|
|
-original content
|
|
|
|
|
\\ No newline at end of file
|
|
|
|
|
+modified content
|
|
|
|
|
\\ No newline at end of file
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
// Verify temp files are cleaned up
|
|
|
|
|
expect(fs.existsSync(oldPath)).toBe(false);
|
|
|
|
|
expect(fs.existsSync(newPath)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle non-existent files and return updated params', async () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [{ old_string: '', new_string: 'new file content' }],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const result = await tool.onModify(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
ToolConfirmationOutcome.ModifyVSCode,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(mockOpenDiff).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
|
|
const [oldPath, newPath] = mockOpenDiff.mock.calls[0];
|
|
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
|
expect(result!.updatedParams).toEqual({
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [{ old_string: '', new_string: 'new file content' }],
|
|
|
|
|
});
|
|
|
|
|
expect(result!.updatedDiff).toContain('new file content');
|
|
|
|
|
|
|
|
|
|
// Verify temp files are cleaned up
|
|
|
|
|
expect(fs.existsSync(oldPath)).toBe(false);
|
|
|
|
|
expect(fs.existsSync(newPath)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should clean up previous temp files before creating new ones', async () => {
|
|
|
|
|
const params: EditToolParams = {
|
|
|
|
|
file_path: filePath,
|
|
|
|
|
edits: [{ old_string: 'old', new_string: 'new' }],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, 'some old content', 'utf8');
|
|
|
|
|
|
|
|
|
|
// Call onModify first time
|
|
|
|
|
const result1 = await tool.onModify(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
ToolConfirmationOutcome.ModifyVSCode,
|
|
|
|
|
);
|
|
|
|
|
const firstCall = mockOpenDiff.mock.calls[0];
|
|
|
|
|
const firstOldPath = firstCall[0];
|
|
|
|
|
const firstNewPath = firstCall[1];
|
|
|
|
|
|
|
|
|
|
expect(result1).toBeDefined();
|
|
|
|
|
expect(fs.existsSync(firstOldPath)).toBe(false);
|
|
|
|
|
expect(fs.existsSync(firstNewPath)).toBe(false);
|
|
|
|
|
|
|
|
|
|
// Ensure different timestamps so that the file names are different for testing.
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
|
|
|
|
|
|
|
|
const result2 = await tool.onModify(
|
|
|
|
|
params,
|
|
|
|
|
new AbortController().signal,
|
|
|
|
|
ToolConfirmationOutcome.ModifyVSCode,
|
|
|
|
|
);
|
|
|
|
|
const secondCall = mockOpenDiff.mock.calls[1];
|
|
|
|
|
const secondOldPath = secondCall[0];
|
|
|
|
|
const secondNewPath = secondCall[1];
|
|
|
|
|
|
|
|
|
|
// Call onModify second time
|
|
|
|
|
expect(result2).toBeDefined();
|
|
|
|
|
expect(fs.existsSync(secondOldPath)).toBe(false);
|
|
|
|
|
expect(fs.existsSync(secondNewPath)).toBe(false);
|
|
|
|
|
|
|
|
|
|
// Verify different file names were used
|
|
|
|
|
expect(firstOldPath).not.toBe(secondOldPath);
|
|
|
|
|
expect(firstNewPath).not.toBe(secondNewPath);
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-05-15 23:51:53 -07:00
|
|
|
});
|