Repository navigation
Expand file tree
/
Copy pathdiff_preview_test.go
More file actions
424 lines (367 loc) · 10 KB
/
Copy pathdiff_preview_test.go
File metadata and controls
424 lines (367 loc) · 10 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package diff
import (
"strings"
"testing"
)
func TestRecordChangeCapturesDiff(t *testing.T) {
dp := NewDiffPreview()
old := "line1\nline2\nline3\n"
new := "line1\nmodified\nline3\n"
dp.RecordChange("src/file.go", old, new)
if len(dp.Changes) != 1 {
t.Fatalf("expected 1 change, got %d", len(dp.Changes))
}
change := dp.Changes[0]
if change.Path != "src/file.go" {
t.Errorf("expected path src/file.go, got %s", change.Path)
}
if change.Type != "modified" {
t.Errorf("expected type modified, got %s", change.Type)
}
if len(change.Hunks) == 0 {
t.Error("expected at least one hunk")
}
}
func TestComputeDiffFindsAdditionsAndDeletions(t *testing.T) {
old := "a\nb\nc\nd\n"
new := "a\nb\nX\nd\n"
hunks := ComputeDiff(old, new)
if len(hunks) == 0 {
t.Fatal("expected at least one hunk")
}
var additions, deletions int
for _, hunk := range hunks {
for _, line := range hunk.Lines {
switch line.Type {
case "add":
additions++
case "remove":
deletions++
}
}
}
if additions != 1 {
t.Errorf("expected 1 addition, got %d", additions)
}
if deletions != 1 {
t.Errorf("expected 1 deletion, got %d", deletions)
}
}
func TestRenderUnifiedFormat(t *testing.T) {
dp := NewDiffPreview()
old := "line1\nline2\nline3\n"
new := "line1\nchanged\nline3\n"
dp.RecordChange("src/auth.go", old, new)
output := RenderUnified(&dp.Changes[0])
if !strings.Contains(output, "--- a/src/auth.go") {
t.Error("missing old file header")
}
if !strings.Contains(output, "+++ b/src/auth.go") {
t.Error("missing new file header")
}
if !strings.Contains(output, "@@") {
t.Error("missing hunk header")
}
if !strings.Contains(output, "-line2") {
t.Error("missing removed line")
}
if !strings.Contains(output, "+changed") {
t.Error("missing added line")
}
}
func TestRenderSummaryMultipleFiles(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("src/auth.go", "a\nb\nc\n", "a\nb\nc\nd\ne\n")
dp.RecordChange("src/middleware.go", "", "package middleware\n\nfunc New() {}\n")
dp.RecordChange("src/old_auth.go", "package old\n\nfunc Old() {}\n", "")
summary := dp.RenderSummary()
if !strings.Contains(summary, "Pending Changes:") {
t.Error("missing header")
}
if !strings.Contains(summary, "M src/auth.go") {
t.Error("missing modified file")
}
if !strings.Contains(summary, "A src/middleware.go") {
t.Error("missing added file")
}
if !strings.Contains(summary, "D src/old_auth.go") {
t.Error("missing deleted file")
}
if !strings.Contains(summary, "Total: 3 files") {
t.Error("missing total line")
}
}
func TestApproveRejectWorkflow(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("a.go", "old\n", "new\n")
dp.RecordChange("b.go", "old\n", "new\n")
dp.RecordChange("c.go", "old\n", "new\n")
// Initially all are pending
pending := dp.GetPending()
if len(pending) != 3 {
t.Fatalf("expected 3 pending, got %d", len(pending))
}
// Approve one
dp.Approve("a.go")
approved := dp.GetApproved()
if len(approved) != 1 {
t.Errorf("expected 1 approved, got %d", len(approved))
}
if approved[0].Path != "a.go" {
t.Errorf("expected a.go approved, got %s", approved[0].Path)
}
// Reject one
dp.Reject("b.go", "needs refactoring")
pending = dp.GetPending()
if len(pending) != 1 {
t.Errorf("expected 1 pending, got %d", len(pending))
}
if pending[0].Path != "c.go" {
t.Errorf("expected c.go pending, got %s", pending[0].Path)
}
// Check rejected has comment
for _, change := range dp.Changes {
if change.Path == "b.go" {
if !change.Rejected {
t.Error("b.go should be rejected")
}
if change.Comment != "needs refactoring" {
t.Errorf("expected comment 'needs refactoring', got '%s'", change.Comment)
}
}
}
}
func TestApproveAll(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("a.go", "x\n", "y\n")
dp.RecordChange("b.go", "x\n", "y\n")
dp.ApproveAll()
approved := dp.GetApproved()
if len(approved) != 2 {
t.Errorf("expected 2 approved, got %d", len(approved))
}
pending := dp.GetPending()
if len(pending) != 0 {
t.Errorf("expected 0 pending, got %d", len(pending))
}
}
func TestRejectAll(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("a.go", "x\n", "y\n")
dp.RecordChange("b.go", "x\n", "y\n")
dp.RejectAll("not ready")
pending := dp.GetPending()
if len(pending) != 0 {
t.Errorf("expected 0 pending, got %d", len(pending))
}
for _, change := range dp.Changes {
if !change.Rejected {
t.Errorf("expected %s to be rejected", change.Path)
}
if change.Comment != "not ready" {
t.Errorf("expected comment 'not ready', got '%s'", change.Comment)
}
}
}
func TestDiffPreviewStatsCalculation(t *testing.T) {
dp := NewDiffPreview()
old := "line1\nline2\nline3\n"
new := "line1\nmodified\nnewline\nline3\n"
dp.RecordChange("file.go", old, new)
change := dp.Changes[0]
if change.Stats.Additions < 1 {
t.Errorf("expected at least 1 addition, got %d", change.Stats.Additions)
}
if change.Stats.Deletions < 1 {
t.Errorf("expected at least 1 deletion, got %d", change.Stats.Deletions)
}
if change.Stats.NetChange != change.Stats.Additions-change.Stats.Deletions {
t.Error("net change should be additions minus deletions")
}
}
func TestEmptyDiffNoChanges(t *testing.T) {
content := "same content\n"
hunks := ComputeDiff(content, content)
if len(hunks) != 0 {
t.Errorf("expected 0 hunks for identical content, got %d", len(hunks))
}
}
func TestNewFileAllAdditions(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("new.go", "", "package new\n\nfunc Hello() {}\n")
change := dp.Changes[0]
if change.Type != "created" {
t.Errorf("expected type created, got %s", change.Type)
}
if change.Stats.Additions == 0 {
t.Error("expected additions for new file")
}
if change.Stats.Deletions != 0 {
t.Errorf("expected 0 deletions for new file, got %d", change.Stats.Deletions)
}
}
func TestDeletedFileAllDeletions(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("old.go", "package old\n\nfunc Bye() {}\n", "")
change := dp.Changes[0]
if change.Type != "deleted" {
t.Errorf("expected type deleted, got %s", change.Type)
}
if change.Stats.Deletions == 0 {
t.Error("expected deletions for deleted file")
}
if change.Stats.Additions != 0 {
t.Errorf("expected 0 additions for deleted file, got %d", change.Stats.Additions)
}
}
func TestLargeDiffMultipleHunks(t *testing.T) {
// Create content with changes far apart so they form separate hunks
var oldLines []string
var newLines []string
for i := 0; i < 50; i++ {
oldLines = append(oldLines, "unchanged line")
newLines = append(newLines, "unchanged line")
}
// Change at line 5
oldLines[4] = "old line 5"
newLines[4] = "new line 5"
// Change at line 45 (far enough for separate hunk with context of 3)
oldLines[44] = "old line 45"
newLines[44] = "new line 45"
old := strings.Join(oldLines, "\n") + "\n"
new := strings.Join(newLines, "\n") + "\n"
hunks := ComputeDiff(old, new)
if len(hunks) < 2 {
t.Errorf("expected at least 2 hunks for distant changes, got %d", len(hunks))
}
}
func TestMyersAlgorithmKnownInputs(t *testing.T) {
tests := []struct {
name string
a []string
b []string
wantAdd int
wantDel int
}{
{
name: "single insertion",
a: []string{"a", "b"},
b: []string{"a", "X", "b"},
wantAdd: 1,
wantDel: 0,
},
{
name: "single deletion",
a: []string{"a", "X", "b"},
b: []string{"a", "b"},
wantAdd: 0,
wantDel: 1,
},
{
name: "substitution",
a: []string{"a", "b", "c"},
b: []string{"a", "X", "c"},
wantAdd: 1,
wantDel: 1,
},
{
name: "all different",
a: []string{"a", "b"},
b: []string{"c", "d"},
wantAdd: 2,
wantDel: 2,
},
{
name: "empty to something",
a: []string{},
b: []string{"a", "b", "c"},
wantAdd: 3,
wantDel: 0,
},
{
name: "something to empty",
a: []string{"a", "b", "c"},
b: []string{},
wantAdd: 0,
wantDel: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lines := ComputeMyersDiff(tt.a, tt.b)
var adds, dels int
for _, line := range lines {
switch line.Type {
case "add":
adds++
case "remove":
dels++
}
}
if adds != tt.wantAdd {
t.Errorf("expected %d additions, got %d", tt.wantAdd, adds)
}
if dels != tt.wantDel {
t.Errorf("expected %d deletions, got %d", tt.wantDel, dels)
}
})
}
}
func TestContextLinesAroundChanges(t *testing.T) {
// With 10 lines and a change at line 5, we should see context lines around it
old := "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
new := "1\n2\n3\n4\nFIVE\n6\n7\n8\n9\n10\n"
hunks := ComputeDiff(old, new)
if len(hunks) == 0 {
t.Fatal("expected at least one hunk")
}
hunk := hunks[0]
// Should have context lines
hasContext := false
for _, line := range hunk.Lines {
if line.Type == "context" {
hasContext = true
break
}
}
if !hasContext {
t.Error("expected context lines around the change")
}
}
func TestClearEmptiesState(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("a.go", "old\n", "new\n")
dp.RecordChange("b.go", "old\n", "new\n")
if len(dp.Changes) != 2 {
t.Fatalf("expected 2 changes before clear, got %d", len(dp.Changes))
}
dp.Clear()
if len(dp.Changes) != 0 {
t.Errorf("expected 0 changes after clear, got %d", len(dp.Changes))
}
pending := dp.GetPending()
if len(pending) != 0 {
t.Errorf("expected 0 pending after clear, got %d", len(pending))
}
}
func TestNewDiffPreviewHasSessionID(t *testing.T) {
dp := NewDiffPreview()
if dp.SessionID == "" {
t.Error("expected non-empty session ID")
}
if len(dp.SessionID) != 16 {
t.Errorf("expected 16 char hex session ID, got %d chars", len(dp.SessionID))
}
}
func TestRenderAllCombinesDiffs(t *testing.T) {
dp := NewDiffPreview()
dp.RecordChange("a.go", "old\n", "new\n")
dp.RecordChange("b.go", "foo\n", "bar\n")
output := dp.RenderAll()
if !strings.Contains(output, "a/a.go") {
t.Error("missing first file in combined diff")
}
if !strings.Contains(output, "a/b.go") {
t.Error("missing second file in combined diff")
}
}