|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import type { z } from 'zod'; |
| 3 | +import type { getDiffResponseSchema } from '@/features/git/schemas'; |
| 4 | +import { formatDiffAsGitDiff } from './utils'; |
| 5 | + |
| 6 | +type GetDiffResult = z.infer<typeof getDiffResponseSchema>; |
| 7 | + |
| 8 | +describe('formatDiffAsGitDiff', () => { |
| 9 | + it('should format a simple file change correctly', () => { |
| 10 | + const input: GetDiffResult = { |
| 11 | + files: [ |
| 12 | + { |
| 13 | + oldPath: 'file.txt', |
| 14 | + newPath: 'file.txt', |
| 15 | + hunks: [ |
| 16 | + { |
| 17 | + oldRange: { start: 1, lines: 3 }, |
| 18 | + newRange: { start: 1, lines: 4 }, |
| 19 | + heading: undefined, |
| 20 | + body: ' context line\n-removed line\n+added line\n context line', |
| 21 | + }, |
| 22 | + ], |
| 23 | + }, |
| 24 | + ], |
| 25 | + }; |
| 26 | + |
| 27 | + const expected = `--- a/file.txt |
| 28 | ++++ b/file.txt |
| 29 | +@@ -1,3 +1,4 @@ |
| 30 | + context line |
| 31 | +-removed line |
| 32 | ++added line |
| 33 | + context line |
| 34 | +`; |
| 35 | + |
| 36 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle file deletion', () => { |
| 40 | + const input: GetDiffResult = { |
| 41 | + files: [ |
| 42 | + { |
| 43 | + oldPath: 'deleted.txt', |
| 44 | + newPath: null, |
| 45 | + hunks: [ |
| 46 | + { |
| 47 | + oldRange: { start: 1, lines: 2 }, |
| 48 | + newRange: { start: 0, lines: 0 }, |
| 49 | + heading: undefined, |
| 50 | + body: '-line 1\n-line 2', |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }; |
| 56 | + |
| 57 | + const expected = `--- a/deleted.txt |
| 58 | ++++ /dev/null |
| 59 | +@@ -1,2 +0,0 @@ |
| 60 | +-line 1 |
| 61 | +-line 2 |
| 62 | +`; |
| 63 | + |
| 64 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle file addition', () => { |
| 68 | + const input: GetDiffResult = { |
| 69 | + files: [ |
| 70 | + { |
| 71 | + oldPath: null, |
| 72 | + newPath: 'new.txt', |
| 73 | + hunks: [ |
| 74 | + { |
| 75 | + oldRange: { start: 0, lines: 0 }, |
| 76 | + newRange: { start: 1, lines: 2 }, |
| 77 | + heading: undefined, |
| 78 | + body: '+line 1\n+line 2', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }; |
| 84 | + |
| 85 | + const expected = `--- /dev/null |
| 86 | ++++ b/new.txt |
| 87 | +@@ -0,0 +1,2 @@ |
| 88 | ++line 1 |
| 89 | ++line 2 |
| 90 | +`; |
| 91 | + |
| 92 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should include hunk heading when present', () => { |
| 96 | + const input: GetDiffResult = { |
| 97 | + files: [ |
| 98 | + { |
| 99 | + oldPath: 'code.ts', |
| 100 | + newPath: 'code.ts', |
| 101 | + hunks: [ |
| 102 | + { |
| 103 | + oldRange: { start: 10, lines: 5 }, |
| 104 | + newRange: { start: 10, lines: 6 }, |
| 105 | + heading: 'function myFunction()', |
| 106 | + body: ' function myFunction() {\n+ console.log("new line");\n return true;\n }', |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + ], |
| 111 | + }; |
| 112 | + |
| 113 | + const expected = `--- a/code.ts |
| 114 | ++++ b/code.ts |
| 115 | +@@ -10,5 +10,6 @@ function myFunction() |
| 116 | + function myFunction() { |
| 117 | ++ console.log("new line"); |
| 118 | + return true; |
| 119 | + } |
| 120 | +`; |
| 121 | + |
| 122 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle multiple files', () => { |
| 126 | + const input: GetDiffResult = { |
| 127 | + files: [ |
| 128 | + { |
| 129 | + oldPath: 'file1.txt', |
| 130 | + newPath: 'file1.txt', |
| 131 | + hunks: [ |
| 132 | + { |
| 133 | + oldRange: { start: 1, lines: 1 }, |
| 134 | + newRange: { start: 1, lines: 2 }, |
| 135 | + heading: undefined, |
| 136 | + body: ' old\n+new', |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + { |
| 141 | + oldPath: 'file2.txt', |
| 142 | + newPath: 'file2.txt', |
| 143 | + hunks: [ |
| 144 | + { |
| 145 | + oldRange: { start: 1, lines: 1 }, |
| 146 | + newRange: { start: 1, lines: 1 }, |
| 147 | + heading: undefined, |
| 148 | + body: ' unchanged', |
| 149 | + }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + ], |
| 153 | + }; |
| 154 | + |
| 155 | + const expected = `--- a/file1.txt |
| 156 | ++++ b/file1.txt |
| 157 | +@@ -1,1 +1,2 @@ |
| 158 | + old |
| 159 | ++new |
| 160 | +--- a/file2.txt |
| 161 | ++++ b/file2.txt |
| 162 | +@@ -1,1 +1,1 @@ |
| 163 | + unchanged |
| 164 | +`; |
| 165 | + |
| 166 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should handle multiple hunks in a single file', () => { |
| 170 | + const input: GetDiffResult = { |
| 171 | + files: [ |
| 172 | + { |
| 173 | + oldPath: 'file.txt', |
| 174 | + newPath: 'file.txt', |
| 175 | + hunks: [ |
| 176 | + { |
| 177 | + oldRange: { start: 1, lines: 2 }, |
| 178 | + newRange: { start: 1, lines: 2 }, |
| 179 | + heading: undefined, |
| 180 | + body: ' line 1\n+line 2', |
| 181 | + }, |
| 182 | + { |
| 183 | + oldRange: { start: 10, lines: 2 }, |
| 184 | + newRange: { start: 11, lines: 2 }, |
| 185 | + heading: undefined, |
| 186 | + body: '-line 10\n line 11', |
| 187 | + }, |
| 188 | + ], |
| 189 | + }, |
| 190 | + ], |
| 191 | + }; |
| 192 | + |
| 193 | + const expected = `--- a/file.txt |
| 194 | ++++ b/file.txt |
| 195 | +@@ -1,2 +1,2 @@ |
| 196 | + line 1 |
| 197 | ++line 2 |
| 198 | +@@ -10,2 +11,2 @@ |
| 199 | +-line 10 |
| 200 | + line 11 |
| 201 | +`; |
| 202 | + |
| 203 | + expect(formatDiffAsGitDiff(input)).toBe(expected); |
| 204 | + }); |
| 205 | +}); |
0 commit comments