| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | BeforeAfterDiff, |
| 4 | createBeforeAfterDiff, |
| 5 | createDiff, |
| 6 | CreateDiffArgs, |
| 7 | DiffFormatType, |
| 8 | DiffMetadata, |
| 9 | extractMetadataFromUnifiedDiff, |
| 10 | } from "./diffFormatting"; |
| 11 | |
| 12 | describe("diffFormatting", () => { |
| 13 | const sampleFilePath = "test/sample.js"; |
| 14 | const beforeContent = "const a = 1;\nconst b = 2;"; |
| 15 | const afterContent = "const a = 1;\nconst b = 3;\nconst c = 4;"; |
| 16 | |
| 17 | describe("createDiff", () => { |
| 18 | it("should create unified diff when specified", () => { |
| 19 | const args: CreateDiffArgs = { |
| 20 | beforeContent, |
| 21 | afterContent, |
| 22 | filePath: sampleFilePath, |
| 23 | diffType: DiffFormatType.Unified, |
| 24 | contextLines: 3, |
| 25 | }; |
| 26 | |
| 27 | const result = createDiff(args); |
| 28 | |
| 29 | expect(result).toContain("---"); |
| 30 | expect(result).toContain("+++"); |
| 31 | expect(result).toContain("@@"); |
| 32 | expect(result).toContain("-const b = 2;"); |
| 33 | expect(result).toContain("+const b = 3;"); |
| 34 | expect(result).toContain("+const c = 4;"); |
| 35 | }); |
| 36 | |
| 37 | it("should create token line diff when specified", () => { |
| 38 | const args: CreateDiffArgs = { |
| 39 | beforeContent, |
| 40 | afterContent, |
| 41 | filePath: sampleFilePath, |
| 42 | diffType: DiffFormatType.TokenLineDiff, |
| 43 | contextLines: 3, |
| 44 | }; |
| 45 | |
| 46 | const result = createDiff(args); |
| 47 | |
| 48 | // Currently returns empty string as TODO |
| 49 | expect(result).toBe(""); |
| 50 | }); |
| 51 | |
| 52 | it("should return empty string for unsupported diff types", () => { |
| 53 | const args: CreateDiffArgs = { |
| 54 | beforeContent, |
| 55 | afterContent, |
| 56 | filePath: sampleFilePath, |
| 57 | diffType: DiffFormatType.RawBeforeAfter, |
| 58 | contextLines: 3, |
| 59 | }; |
| 60 | |
| 61 | const result = createDiff(args); |
| 62 | |
| 63 | expect(result).toBe(""); |
| 64 | }); |
| 65 | |
| 66 | it("should handle empty content", () => { |
| 67 | const args: CreateDiffArgs = { |
| 68 | beforeContent: "", |
| 69 | afterContent: "new content", |
| 70 | filePath: sampleFilePath, |
| 71 | diffType: DiffFormatType.Unified, |
| 72 | contextLines: 3, |
| 73 | }; |
| 74 | |
| 75 | const result = createDiff(args); |
| 76 | |
| 77 | expect(result).toContain("+++"); |
| 78 | expect(result).toContain("+new content"); |
| 79 | }); |
| 80 | |
| 81 | it("should handle deletion", () => { |
| 82 | const args: CreateDiffArgs = { |
| 83 | beforeContent: "old content", |
| 84 | afterContent: "", |
| 85 | filePath: sampleFilePath, |
| 86 | diffType: DiffFormatType.Unified, |
| 87 | contextLines: 3, |
| 88 | }; |
| 89 | |
| 90 | const result = createDiff(args); |
| 91 | |
| 92 | expect(result).toContain("---"); |
| 93 | expect(result).toContain("-old content"); |
| 94 | }); |
| 95 | |
| 96 | it("should handle content without newlines", () => { |
| 97 | const args: CreateDiffArgs = { |
| 98 | beforeContent: "line1", |
| 99 | afterContent: "line2", |
| 100 | filePath: sampleFilePath, |
| 101 | diffType: DiffFormatType.Unified, |
| 102 | contextLines: 3, |
| 103 | }; |
| 104 | |
| 105 | const result = createDiff(args); |
| 106 | |
| 107 | expect(result).toContain("-line1"); |
| 108 | expect(result).toContain("+line2"); |
| 109 | }); |
| 110 | |
| 111 | it("should respect contextLines parameter", () => { |
| 112 | const multiLineContent = Array(10) |
| 113 | .fill("line") |
| 114 | .map((l, i) => `${l}${i}`) |
| 115 | .join("\n"); |
| 116 | const modifiedContent = multiLineContent.replace("line5", "modified5"); |
| 117 | |
| 118 | const args: CreateDiffArgs = { |
| 119 | beforeContent: multiLineContent, |
| 120 | afterContent: modifiedContent, |
| 121 | filePath: sampleFilePath, |
| 122 | diffType: DiffFormatType.Unified, |
| 123 | contextLines: 2, |
| 124 | }; |
| 125 | |
| 126 | const result = createDiff(args); |
| 127 | |
| 128 | expect(result).toContain("@@ -4,5 +4,5 @@"); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe("createBeforeAfterDiff", () => { |
| 133 | it("should return BeforeAfterDiff with normalized content", () => { |
| 134 | const result: BeforeAfterDiff = createBeforeAfterDiff( |
| 135 | beforeContent, |
| 136 | afterContent, |
| 137 | sampleFilePath, |
| 138 | ); |
| 139 | |
| 140 | expect(result).toEqual({ |
| 141 | filePath: sampleFilePath, |
| 142 | beforeContent: beforeContent + "\n", |
| 143 | afterContent: afterContent + "\n", |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | it("should not add extra newline if content already ends with one", () => { |
| 148 | const contentWithNewline = "const a = 1;\n"; |
| 149 | const result: BeforeAfterDiff = createBeforeAfterDiff( |
| 150 | contentWithNewline, |
| 151 | contentWithNewline, |
| 152 | sampleFilePath, |
| 153 | ); |
| 154 | |
| 155 | expect(result.beforeContent).toBe(contentWithNewline); |
| 156 | expect(result.afterContent).toBe(contentWithNewline); |
| 157 | }); |
| 158 | |
| 159 | it("should handle empty content", () => { |
| 160 | const result: BeforeAfterDiff = createBeforeAfterDiff( |
| 161 | "", |
| 162 | "", |
| 163 | sampleFilePath, |
| 164 | ); |
| 165 | |
| 166 | expect(result).toEqual({ |
| 167 | filePath: sampleFilePath, |
| 168 | beforeContent: "\n", |
| 169 | afterContent: "\n", |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | it("should handle mixed newline scenarios", () => { |
| 174 | const beforeWithNewline = "line1\n"; |
| 175 | const afterWithoutNewline = "line2"; |
| 176 | const result: BeforeAfterDiff = createBeforeAfterDiff( |
| 177 | beforeWithNewline, |
| 178 | afterWithoutNewline, |
| 179 | sampleFilePath, |
| 180 | ); |
| 181 | |
| 182 | expect(result.beforeContent).toBe("line1\n"); |
| 183 | expect(result.afterContent).toBe("line2\n"); |
| 184 | }); |
| 185 | }); |
| 186 | |
| 187 | describe("extractMetadataFromUnifiedDiff", () => { |
| 188 | it("should extract basic file information", () => { |
| 189 | const unifiedDiff = `--- a/old.js\ttimestamp1 |
| 190 | +++ b/new.js\ttimestamp2 |
| 191 | @@ -1,3 +1,3 @@ |
| 192 | line1 |
| 193 | -line2 |
| 194 | +modified2 |
| 195 | line3`; |
| 196 | |
| 197 | const metadata: DiffMetadata = |
| 198 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 199 | |
| 200 | expect(metadata.oldFilename).toBe("old.js"); |
| 201 | expect(metadata.newFilename).toBe("new.js"); |
| 202 | expect(metadata.oldTimestamp).toBe("timestamp1"); |
| 203 | expect(metadata.newTimestamp).toBe("timestamp2"); |
| 204 | expect(metadata.isNew).toBeFalsy(); |
| 205 | expect(metadata.isDeleted).toBeFalsy(); |
| 206 | expect(metadata.isRename).toBeTruthy(); |
| 207 | }); |
| 208 | |
| 209 | it("should detect new files", () => { |
| 210 | const unifiedDiff = `--- /dev/null |
| 211 | +++ b/new.js |
| 212 | @@ -0,0 +1,2 @@ |
| 213 | +line1 |
| 214 | +line2`; |
| 215 | |
| 216 | const metadata: DiffMetadata = |
| 217 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 218 | |
| 219 | expect(metadata.oldFilename).toBe("/dev/null"); |
| 220 | expect(metadata.newFilename).toBe("new.js"); |
| 221 | expect(metadata.isNew).toBeTruthy(); |
| 222 | expect(metadata.isDeleted).toBeFalsy(); |
| 223 | expect(metadata.isRename).toBeFalsy(); |
| 224 | }); |
| 225 | |
| 226 | it("should detect deleted files", () => { |
| 227 | const unifiedDiff = `--- a/old.js |
| 228 | +++ /dev/null |
| 229 | @@ -1,2 +0,0 @@ |
| 230 | -line1 |
| 231 | -line2`; |
| 232 | |
| 233 | const metadata: DiffMetadata = |
| 234 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 235 | |
| 236 | expect(metadata.oldFilename).toBe("old.js"); |
| 237 | expect(metadata.newFilename).toBe("/dev/null"); |
| 238 | expect(metadata.isNew).toBeFalsy(); |
| 239 | expect(metadata.isDeleted).toBeTruthy(); |
| 240 | expect(metadata.isRename).toBeFalsy(); |
| 241 | }); |
| 242 | |
| 243 | it("should parse hunk information", () => { |
| 244 | const unifiedDiff = `--- a/file.js |
| 245 | +++ b/file.js |
| 246 | @@ -1,5 +1,6 @@ function header |
| 247 | line1 |
| 248 | -line2 |
| 249 | +modified2 |
| 250 | +newline |
| 251 | line3 |
| 252 | @@ -10,3 +11,3 @@ |
| 253 | line10 |
| 254 | -line11 |
| 255 | +modified11 |
| 256 | line12`; |
| 257 | |
| 258 | const metadata: DiffMetadata = |
| 259 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 260 | |
| 261 | expect(metadata.hunks).toHaveLength(2); |
| 262 | expect(metadata.hunks![0]).toEqual({ |
| 263 | oldStart: 1, |
| 264 | oldCount: 5, |
| 265 | newStart: 1, |
| 266 | newCount: 6, |
| 267 | header: "function header", |
| 268 | lines: [ |
| 269 | { |
| 270 | content: "line1", |
| 271 | newLineNumber: 1, |
| 272 | oldLineNumber: 1, |
| 273 | type: "context", |
| 274 | }, |
| 275 | { |
| 276 | content: "line2", |
| 277 | oldLineNumber: 2, |
| 278 | type: "deletion", |
| 279 | }, |
| 280 | { |
| 281 | content: "modified2", |
| 282 | newLineNumber: 2, |
| 283 | type: "addition", |
| 284 | }, |
| 285 | { |
| 286 | content: "newline", |
| 287 | newLineNumber: 3, |
| 288 | type: "addition", |
| 289 | }, |
| 290 | { |
| 291 | content: "line3", |
| 292 | newLineNumber: 4, |
| 293 | oldLineNumber: 3, |
| 294 | type: "context", |
| 295 | }, |
| 296 | ], |
| 297 | }); |
| 298 | expect(metadata.hunks![1]).toEqual({ |
| 299 | oldStart: 10, |
| 300 | oldCount: 3, |
| 301 | newStart: 11, |
| 302 | newCount: 3, |
| 303 | header: undefined, |
| 304 | lines: [ |
| 305 | { |
| 306 | content: "line10", |
| 307 | newLineNumber: 11, |
| 308 | oldLineNumber: 10, |
| 309 | type: "context", |
| 310 | }, |
| 311 | { |
| 312 | content: "line11", |
| 313 | oldLineNumber: 11, |
| 314 | type: "deletion", |
| 315 | }, |
| 316 | { |
| 317 | content: "modified11", |
| 318 | newLineNumber: 12, |
| 319 | type: "addition", |
| 320 | }, |
| 321 | { |
| 322 | content: "line12", |
| 323 | newLineNumber: 13, |
| 324 | oldLineNumber: 12, |
| 325 | type: "context", |
| 326 | }, |
| 327 | ], |
| 328 | }); |
| 329 | }); |
| 330 | |
| 331 | it("should handle single line hunks", () => { |
| 332 | const unifiedDiff = `--- a/file.js |
| 333 | +++ b/file.js |
| 334 | @@ -1 +1 @@ |
| 335 | -old |
| 336 | +new`; |
| 337 | |
| 338 | const metadata: DiffMetadata = |
| 339 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 340 | |
| 341 | expect(metadata.hunks).toHaveLength(1); |
| 342 | expect(metadata.hunks![0]).toEqual({ |
| 343 | oldStart: 1, |
| 344 | oldCount: 1, |
| 345 | newStart: 1, |
| 346 | newCount: 1, |
| 347 | header: undefined, |
| 348 | lines: [ |
| 349 | { |
| 350 | content: "old", |
| 351 | oldLineNumber: 1, |
| 352 | type: "deletion", |
| 353 | }, |
| 354 | { |
| 355 | content: "new", |
| 356 | newLineNumber: 1, |
| 357 | type: "addition", |
| 358 | }, |
| 359 | ], |
| 360 | }); |
| 361 | }); |
| 362 | |
| 363 | it("should detect binary files", () => { |
| 364 | const unifiedDiff = `--- a/image.png |
| 365 | +++ b/image.png |
| 366 | Binary files a/image.png and b/image.png differ`; |
| 367 | |
| 368 | const metadata: DiffMetadata = |
| 369 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 370 | |
| 371 | expect(metadata.isBinary).toBeTruthy(); |
| 372 | }); |
| 373 | |
| 374 | it("should detect git binary patches", () => { |
| 375 | const unifiedDiff = `--- a/file.bin |
| 376 | +++ b/file.bin |
| 377 | GIT binary patch |
| 378 | delta 123 |
| 379 | ...`; |
| 380 | |
| 381 | const metadata: DiffMetadata = |
| 382 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 383 | |
| 384 | expect(metadata.isBinary).toBeTruthy(); |
| 385 | }); |
| 386 | |
| 387 | it("should handle files without a/ b/ prefixes", () => { |
| 388 | const unifiedDiff = `--- old.js |
| 389 | +++ new.js |
| 390 | @@ -1,2 +1,2 @@ |
| 391 | -line1 |
| 392 | +modified1 |
| 393 | line2`; |
| 394 | |
| 395 | const metadata: DiffMetadata = |
| 396 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 397 | |
| 398 | expect(metadata.oldFilename).toBe("old.js"); |
| 399 | expect(metadata.newFilename).toBe("new.js"); |
| 400 | }); |
| 401 | |
| 402 | it("should handle empty diff", () => { |
| 403 | const metadata: DiffMetadata = extractMetadataFromUnifiedDiff(""); |
| 404 | |
| 405 | expect(metadata.oldFilename).toBeUndefined(); |
| 406 | expect(metadata.newFilename).toBeUndefined(); |
| 407 | expect(metadata.hunks).toEqual([]); |
| 408 | expect(metadata.isBinary).toBeFalsy(); |
| 409 | }); |
| 410 | |
| 411 | it("should handle malformed diff gracefully", () => { |
| 412 | const malformedDiff = `not a diff |
| 413 | random text`; |
| 414 | |
| 415 | const metadata: DiffMetadata = |
| 416 | extractMetadataFromUnifiedDiff(malformedDiff); |
| 417 | |
| 418 | expect(metadata.oldFilename).toBeUndefined(); |
| 419 | expect(metadata.newFilename).toBeUndefined(); |
| 420 | expect(metadata.hunks).toEqual([]); |
| 421 | }); |
| 422 | |
| 423 | it("should not consider same filenames as rename", () => { |
| 424 | const unifiedDiff = `--- a/file.js |
| 425 | +++ b/file.js |
| 426 | @@ -1,2 +1,2 @@ |
| 427 | -line1 |
| 428 | +modified1 |
| 429 | line2`; |
| 430 | |
| 431 | const metadata: DiffMetadata = |
| 432 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 433 | |
| 434 | expect(metadata.isRename).toBeFalsy(); |
| 435 | }); |
| 436 | |
| 437 | it("should handle files with spaces in names", () => { |
| 438 | const unifiedDiff = `--- a/my file.js\t2023-01-01 00:00:00 |
| 439 | +++ b/my new file.js\t2023-01-01 00:01:00 |
| 440 | @@ -1,1 +1,1 @@ |
| 441 | -content |
| 442 | +new content`; |
| 443 | |
| 444 | const metadata: DiffMetadata = |
| 445 | extractMetadataFromUnifiedDiff(unifiedDiff); |
| 446 | |
| 447 | expect(metadata.oldFilename).toBe("my file.js"); |
| 448 | expect(metadata.newFilename).toBe("my new file.js"); |
| 449 | expect(metadata.oldTimestamp).toBe("2023-01-01 00:00:00"); |
| 450 | expect(metadata.newTimestamp).toBe("2023-01-01 00:01:00"); |
| 451 | expect(metadata.isRename).toBeTruthy(); |
| 452 | }); |
| 453 | }); |
| 454 | |
| 455 | describe("DiffFormatType enum", () => { |
| 456 | it("should have correct enum values", () => { |
| 457 | expect(DiffFormatType.Unified).toBe("unified"); |
| 458 | expect(DiffFormatType.RawBeforeAfter).toBe("beforeAfter"); |
| 459 | expect(DiffFormatType.TokenLineDiff).toBe("linediff"); |
| 460 | }); |
| 461 | }); |
| 462 | }); |