mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-25 12:34:38 -07:00
feat(core): replace expected_replacements with allow_multiple in replace tool (#20033)
This commit is contained in:
@@ -934,7 +934,7 @@ function doIt() {
|
||||
);
|
||||
});
|
||||
|
||||
describe('expected_replacements', () => {
|
||||
describe('allow_multiple', () => {
|
||||
const testFile = 'replacements_test.txt';
|
||||
let filePath: string;
|
||||
|
||||
@@ -944,34 +944,70 @@ function doIt() {
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: 'succeed when occurrences match expected_replacements',
|
||||
name: 'succeed when allow_multiple is true and there are multiple occurrences',
|
||||
content: 'foo foo foo',
|
||||
expected: 3,
|
||||
allow_multiple: true,
|
||||
shouldSucceed: true,
|
||||
finalContent: 'bar bar bar',
|
||||
},
|
||||
{
|
||||
name: 'fail when occurrences do not match expected_replacements',
|
||||
content: 'foo foo foo',
|
||||
expected: 2,
|
||||
shouldSucceed: false,
|
||||
name: 'succeed when allow_multiple is true and there is exactly 1 occurrence',
|
||||
content: 'foo',
|
||||
allow_multiple: true,
|
||||
shouldSucceed: true,
|
||||
finalContent: 'bar',
|
||||
},
|
||||
{
|
||||
name: 'default to 1 expected replacement if not specified',
|
||||
content: 'foo foo',
|
||||
expected: undefined,
|
||||
name: 'fail when allow_multiple is false and there are multiple occurrences',
|
||||
content: 'foo foo foo',
|
||||
allow_multiple: false,
|
||||
shouldSucceed: false,
|
||||
expectedError: ToolErrorType.EDIT_EXPECTED_OCCURRENCE_MISMATCH,
|
||||
},
|
||||
{
|
||||
name: 'default to 1 expected replacement if allow_multiple not specified',
|
||||
content: 'foo foo',
|
||||
allow_multiple: undefined,
|
||||
shouldSucceed: false,
|
||||
expectedError: ToolErrorType.EDIT_EXPECTED_OCCURRENCE_MISMATCH,
|
||||
},
|
||||
{
|
||||
name: 'succeed when allow_multiple is false and there is exactly 1 occurrence',
|
||||
content: 'foo',
|
||||
allow_multiple: false,
|
||||
shouldSucceed: true,
|
||||
finalContent: 'bar',
|
||||
},
|
||||
{
|
||||
name: 'fail when allow_multiple is true but there are 0 occurrences',
|
||||
content: 'baz',
|
||||
allow_multiple: true,
|
||||
shouldSucceed: false,
|
||||
expectedError: ToolErrorType.EDIT_NO_OCCURRENCE_FOUND,
|
||||
},
|
||||
{
|
||||
name: 'fail when allow_multiple is false but there are 0 occurrences',
|
||||
content: 'baz',
|
||||
allow_multiple: false,
|
||||
shouldSucceed: false,
|
||||
expectedError: ToolErrorType.EDIT_NO_OCCURRENCE_FOUND,
|
||||
},
|
||||
])(
|
||||
'should $name',
|
||||
async ({ content, expected, shouldSucceed, finalContent }) => {
|
||||
async ({
|
||||
content,
|
||||
allow_multiple,
|
||||
shouldSucceed,
|
||||
finalContent,
|
||||
expectedError,
|
||||
}) => {
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
const params: EditToolParams = {
|
||||
file_path: filePath,
|
||||
instruction: 'Replace all foo with bar',
|
||||
old_string: 'foo',
|
||||
new_string: 'bar',
|
||||
...(expected !== undefined && { expected_replacements: expected }),
|
||||
...(allow_multiple !== undefined && { allow_multiple }),
|
||||
};
|
||||
const invocation = tool.build(params);
|
||||
const result = await invocation.execute(new AbortController().signal);
|
||||
@@ -981,9 +1017,7 @@ function doIt() {
|
||||
if (finalContent)
|
||||
expect(fs.readFileSync(filePath, 'utf8')).toBe(finalContent);
|
||||
} else {
|
||||
expect(result.error?.type).toBe(
|
||||
ToolErrorType.EDIT_EXPECTED_OCCURRENCE_MISMATCH,
|
||||
);
|
||||
expect(result.error?.type).toBe(expectedError);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user