mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
Enable "modify" in write tool (#1044)
This commit is contained in:
@@ -605,141 +605,4 @@ describe('EditTool', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
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,
|
||||
old_string: originalContent,
|
||||
new_string: 'modified content',
|
||||
};
|
||||
|
||||
fs.writeFileSync(filePath, originalContent, 'utf8');
|
||||
|
||||
const result = await tool.onModify(
|
||||
params,
|
||||
new AbortController().signal,
|
||||
'vscode',
|
||||
);
|
||||
|
||||
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,
|
||||
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,
|
||||
old_string: '',
|
||||
new_string: 'new file content',
|
||||
};
|
||||
|
||||
const result = await tool.onModify(
|
||||
params,
|
||||
new AbortController().signal,
|
||||
'vscode',
|
||||
);
|
||||
|
||||
expect(mockOpenDiff).toHaveBeenCalledTimes(1);
|
||||
|
||||
const [oldPath, newPath] = mockOpenDiff.mock.calls[0];
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result!.updatedParams).toEqual({
|
||||
file_path: filePath,
|
||||
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,
|
||||
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,
|
||||
'vscode',
|
||||
);
|
||||
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,
|
||||
'vscode',
|
||||
);
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user