mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 12:21:13 -05:00
test: add comprehensive tests for NewUnifiedDiffStrategy error handling and edge cases
This commit is contained in:
@@ -35,11 +35,13 @@ line3`;
|
|||||||
+modified line3`;
|
+modified line3`;
|
||||||
|
|
||||||
const result = await strategy.applyDiff(original, diff);
|
const result = await strategy.applyDiff(original, diff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
expect(result).toBe(`line1
|
if(result.success) {
|
||||||
|
expect(result.content).toBe(`line1
|
||||||
new line
|
new line
|
||||||
line2
|
line2
|
||||||
modified line3`);
|
modified line3`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle multiple hunks', async () => {
|
it('should handle multiple hunks', async () => {
|
||||||
@@ -309,4 +311,223 @@ console.log(primeArray);
|
|||||||
expect(result.content).toBe(expected);
|
expect(result.content).toBe(expected);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('error handling and edge cases', () => {
|
||||||
|
it('should reject completely invalid diff format', async () => {
|
||||||
|
const original = 'line1\nline2\nline3';
|
||||||
|
const invalidDiff = 'this is not a diff at all';
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, invalidDiff);
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reject diff with invalid hunk format', async () => {
|
||||||
|
const original = 'line1\nline2\nline3';
|
||||||
|
const invalidHunkDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
invalid hunk header
|
||||||
|
line1
|
||||||
|
-line2
|
||||||
|
+new line`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, invalidHunkDiff);
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail when diff tries to modify non-existent content', async () => {
|
||||||
|
const original = 'line1\nline2\nline3';
|
||||||
|
const nonMatchingDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
line1
|
||||||
|
-nonexistent line
|
||||||
|
+new line
|
||||||
|
line3`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, nonMatchingDiff);
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle overlapping hunks', async () => {
|
||||||
|
const original = `line1
|
||||||
|
line2
|
||||||
|
line3
|
||||||
|
line4
|
||||||
|
line5`;
|
||||||
|
const overlappingDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
line1
|
||||||
|
line2
|
||||||
|
-line3
|
||||||
|
+modified3
|
||||||
|
line4
|
||||||
|
@@ ... @@
|
||||||
|
line2
|
||||||
|
-line3
|
||||||
|
-line4
|
||||||
|
+modified3and4
|
||||||
|
line5`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, overlappingDiff);
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty lines modifications', async () => {
|
||||||
|
const original = `line1
|
||||||
|
|
||||||
|
line3
|
||||||
|
|
||||||
|
line5`;
|
||||||
|
const emptyLinesDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
line1
|
||||||
|
|
||||||
|
-line3
|
||||||
|
+line3modified
|
||||||
|
|
||||||
|
line5`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, emptyLinesDiff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe(`line1
|
||||||
|
|
||||||
|
line3modified
|
||||||
|
|
||||||
|
line5`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mixed line endings in diff', async () => {
|
||||||
|
const original = 'line1\r\nline2\nline3\r\n';
|
||||||
|
const mixedEndingsDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
line1\r
|
||||||
|
-line2
|
||||||
|
+modified2\r
|
||||||
|
line3`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, mixedEndingsDiff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe('line1\r\nmodified2\r\nline3\r\n');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle partial line modifications', async () => {
|
||||||
|
const original = 'const value = oldValue + 123;';
|
||||||
|
const partialDiff = `--- a/file.txt
|
||||||
|
+++ b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
-const value = oldValue + 123;
|
||||||
|
+const value = newValue + 123;`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, partialDiff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe('const value = newValue + 123;');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle slightly malformed but recoverable diff', async () => {
|
||||||
|
const original = 'line1\nline2\nline3';
|
||||||
|
// Missing space after --- and +++
|
||||||
|
const slightlyBadDiff = `---a/file.txt
|
||||||
|
+++b/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
line1
|
||||||
|
-line2
|
||||||
|
+new line
|
||||||
|
line3`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, slightlyBadDiff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe('line1\nnew line\nline3');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('similar code sections', () => {
|
||||||
|
it('should correctly modify the right section when similar code exists', async () => {
|
||||||
|
const original = `function add(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function subtract(a, b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function multiply(a, b) {
|
||||||
|
return a + b; // Bug here
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const diff = `--- a/math.js
|
||||||
|
+++ b/math.js
|
||||||
|
@@ ... @@
|
||||||
|
function multiply(a, b) {
|
||||||
|
- return a + b; // Bug here
|
||||||
|
+ return a * b;
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, diff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe(`function add(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function subtract(a, b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function multiply(a, b) {
|
||||||
|
return a * b;
|
||||||
|
}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle multiple similar sections with correct context', async () => {
|
||||||
|
const original = `if (condition) {
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (otherCondition) {
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const diff = `--- a/file.js
|
||||||
|
+++ b/file.js
|
||||||
|
@@ ... @@
|
||||||
|
if (otherCondition) {
|
||||||
|
doSomething();
|
||||||
|
- doSomething();
|
||||||
|
+ doSomethingElse();
|
||||||
|
doSomething();
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const result = await strategy.applyDiff(original, diff);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
if (result.success) {
|
||||||
|
expect(result.content).toBe(`if (condition) {
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
doSomething();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (otherCondition) {
|
||||||
|
doSomething();
|
||||||
|
doSomethingElse();
|
||||||
|
doSomething();
|
||||||
|
}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user