Spaces:
Paused
Paused
File size: 9,332 Bytes
9284ad7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | import { jest, expect, describe, it } from '@jest/globals'
import {
splitPatch,
patchStartEndLine,
parsePatch,
checkIfDocumentationOnly,
parseReview,
extractPatternType,
HunkInfo,
} from '../utils/patch-utils'
describe('patch-utils', () => {
describe('splitPatch', () => {
it('should split patch with proper @@ headers into hunks', () => {
const patch = `@@ -10,5 +10,6 @@
line1
line2
-old line
+new line
+added line
@@ -20,3 +20,4 @@
context
-modified
+added
another`
const result = splitPatch(patch)
expect(result).toHaveLength(2)
expect(result[0]).toContain('line1')
expect(result[1]).toContain('context')
})
it('should return entire patch when no @@ headers found (new files)', () => {
const newFilePatch = `+function updateSettings(user, newTheme) {
+ const updatedUser = user;
+ updatedUser.settings.theme = newTheme;
+ return updatedUser;
+}`
const result = splitPatch(newFilePatch)
expect(result).toHaveLength(1)
expect(result[0]).toBe(newFilePatch)
})
it('should return empty array for null/undefined patch', () => {
expect(splitPatch(null)).toEqual([])
expect(splitPatch(undefined)).toEqual([])
})
it('should handle single hunk patch', () => {
const patch = `@@ -1,3 +1,4 @@
const x = 1;
-old
+new
+added`
const result = splitPatch(patch)
expect(result).toHaveLength(1)
})
it('should handle patch with only context lines', () => {
const patch = `@@ -5,3 +5,3 @@
return result;
}
-remove
+add`
const result = splitPatch(patch)
expect(result).toHaveLength(1)
})
})
describe('patchStartEndLine', () => {
it('should parse standard @@ header correctly', () => {
const patch = `@@ -10,5 +10,6 @@
content`
const result = patchStartEndLine(patch)
expect(result).toEqual({
oldStart: 10,
oldLength: 5,
newStart: 10,
newLength: 6,
oldHunk: { startLine: 10, endLine: 14 },
newHunk: { startLine: 10, endLine: 15 },
})
})
it('should handle new file without @@ header (all + lines)', () => {
const newFilePatch = `+function test() {
+ return true;
+}`
const result = patchStartEndLine(newFilePatch)
expect(result).not.toBeNull()
expect(result?.newStart).toBe(1)
expect(result?.newLength).toBe(3)
expect(result?.oldStart).toBe(0)
expect(result?.oldLength).toBe(0)
expect(result?.oldHunk.startLine).toBe(0)
expect(result?.oldHunk.endLine).toBe(0)
expect(result?.newHunk.startLine).toBe(1)
expect(result?.newHunk.endLine).toBe(3)
})
it('should handle single line new file', () => {
const singleLinePatch = `+const x = 1;`
const result = patchStartEndLine(singleLinePatch)
expect(result?.newLength).toBe(1)
expect(result?.newHunk.endLine).toBe(1)
})
it('should return null for patch with no + lines and no header', () => {
const invalidPatch = ` context line
another line`
const result = patchStartEndLine(invalidPatch)
expect(result).toBeNull()
})
it('should handle diff with whitespace before +', () => {
const patch = `@@ -1,2 +1,3 @@
line1
+added line`
const result = patchStartEndLine(patch)
expect(result?.newLength).toBe(3)
expect(result?.oldLength).toBe(2)
})
})
describe('parsePatch', () => {
it('should parse standard diff correctly', () => {
const patch = `@@ -1,4 +1,5 @@
const x = 1;
-old line
+new line
+added line
context line`
const result = parsePatch(patch)
expect(result).not.toBeNull()
expect(result?.oldStart).toBe(1)
expect(result?.newStart).toBe(1)
expect(result?.oldHunk).toContain('old line')
expect(result?.newHunk).toContain('new line')
expect(result?.newHunk).toContain('added line')
})
it('should handle new file (no old lines)', () => {
const newFilePatch = `+function test() {
+ return 1;
+}`
const result = parsePatch(newFilePatch)
expect(result).not.toBeNull()
expect(result?.oldHunk).toBe('')
expect(result?.newHunk).toContain('function test()')
expect(result?.newStart).toBe(1)
})
it('should handle deleted file (no new lines)', () => {
const deletedPatch = `@@ -1,3 +1,0 @@
-old line
-old line 2
-old line 3`
const result = parsePatch(deletedPatch)
expect(result).not.toBeNull()
expect(result?.oldHunk).toContain('old line')
expect(result?.newHunk).toBe('')
})
it('should return null for invalid patch without header and no + lines', () => {
const invalidPatch = `just some text without diff markers`
const result = parsePatch(invalidPatch)
expect(result).toBeNull()
})
})
describe('checkIfDocumentationOnly', () => {
it('should return true for pure comment marker changes (no text content)', () => {
const docOnlyDiff = `-//
+////`
expect(checkIfDocumentationOnly(docOnlyDiff)).toBe(true)
})
it('should return false for comment text changes', () => {
const codeDiff = `-// Old comment
+// New comment`
expect(checkIfDocumentationOnly(codeDiff)).toBe(false)
})
it('should return false for code changes', () => {
const codeDiff = `-const x = 1;
+const x = 2;`
expect(checkIfDocumentationOnly(codeDiff)).toBe(false)
})
it('should return false for Python docstrings', () => {
const pythonDocDiff = `-"""Old doc"""
+"""New doc"""`
expect(checkIfDocumentationOnly(pythonDocDiff)).toBe(false)
})
})
describe('parseReview', () => {
it('should parse review with line numbers', () => {
const response = `[10] | major | bug: Missing null check
Null pointer risk here`
const patches = [
{
newStart: 1,
newLength: 20,
},
]
const result = parseReview(response, patches, 'test.js')
expect(result).toHaveLength(1)
expect(result[0].startLine).toBe(10)
expect(result[0].endLine).toBe(10)
expect(result[0].severity).toBe('major')
})
it('should parse review with line range', () => {
const response = `[10-15] | critical | security: SQL injection risk`
const patches = [
{
newStart: 1,
newLength: 20,
},
]
const result = parseReview(response, patches, 'test.js')
expect(result).toHaveLength(1)
expect(result[0].startLine).toBe(10)
expect(result[0].endLine).toBe(15)
expect(result[0].severity).toBe('critical')
})
it('should skip reviews outside valid line range', () => {
const response = `[100] | major | bug: Out of range`
const patches = [
{
newStart: 1,
newLength: 20,
},
]
const result = parseReview(response, patches, 'test.js')
expect(result).toHaveLength(0)
})
it('should extract remedy from code blocks', () => {
const response = `[10] | major | bug: Fix this
\`\`\`
const fixed = value || defaultValue;
\`\`\``
const patches = [
{
newStart: 1,
newLength: 20,
},
]
const result = parseReview(response, patches, 'test.js')
expect(result).toHaveLength(1)
expect(result[0].remedy).toContain('fixed')
})
})
describe('extractPatternType', () => {
it('should extract Security pattern', () => {
expect(extractPatternType('Security issue found')).toBe('security')
})
it('should extract Bug pattern', () => {
expect(extractPatternType('Bug in loop')).toBe('bug')
})
it('should extract Performance pattern', () => {
expect(extractPatternType('Performance bottleneck detected')).toBe('performance')
})
it('should return general for unknown patterns', () => {
expect(extractPatternType('Some random message')).toBe('general')
})
})
describe('integration: real-world patch scenarios', () => {
it('should handle the actual e.js patch from PR #52', () => {
const actualPatch = `+function updateSettings(user, newTheme) {
+
+ const updatedUser = user;
+
+ updatedUser.settings.theme = newTheme;
+
+ return updatedUser;
+}
+
+
+const account = { id: 1, settings: { theme: 'dark' } };
+const newAccount = updateSettings(account, 'light');
+console.log(account.settings.theme);`
const split = splitPatch(actualPatch)
expect(split).toHaveLength(1)
const hunkInfo = patchStartEndLine(split[0])
expect(hunkInfo).not.toBeNull()
expect(hunkInfo?.newLength).toBe(13)
const parsed = parsePatch(split[0])
expect(parsed).not.toBeNull()
expect(parsed?.newHunk).toContain('updateSettings')
})
it('should handle multi-file diff with multiple hunks', () => {
const multiFileDiff = `diff --git a/calc.py b/calc.py
@@ -1,5 +1,6 @@
if __name__ == "__main__":
print(f"Addition: {add(10, 5)}")
- print(f"Division: {divide(10, 5)}")
+ print(f"Division: {divide(10, 5)}")
+ ded
\\ No newline at end of file`
const split = splitPatch(multiFileDiff)
expect(split.length).toBeGreaterThanOrEqual(1)
const hunkInfo = patchStartEndLine(split[0])
expect(hunkInfo).not.toBeNull()
})
})
}) |