-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_issue_test.go
More file actions
533 lines (465 loc) · 16.3 KB
/
format_issue_test.go
File metadata and controls
533 lines (465 loc) · 16.3 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package main
import (
"bytes"
"path/filepath"
"strings"
"testing"
"time"
)
func TestPrintIssueDetails_BasicIssue(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "ID "+issue.ID) {
t.Errorf("expected output to contain 'ID %s', got: %s", issue.ID, output)
}
if !strings.Contains(output, "Title Test issue") {
t.Errorf("expected output to contain 'Title Test issue', got: %s", output)
}
if !strings.Contains(output, "Status open") {
t.Errorf("expected output to contain 'Status open', got: %s", output)
}
}
func TestPrintIssueDetails_WithDependencies(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue1, _ := store.AddIssue("Main issue")
issue2, _ := store.AddIssue("Dependency")
_ = store.AddDependency(issue1.ID, issue2.ID)
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue1, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "Depends on") {
t.Errorf("expected output to contain 'Depends on', got: %s", output)
}
if !strings.Contains(output, " "+issue2.ID+" open Dependency") {
t.Errorf("expected output to contain ' %s open Dependency', got: %s", issue2.ID, output)
}
}
func TestPrintIssueDetails_WithBlocks(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue1, _ := store.AddIssue("Main issue")
issue2, _ := store.AddIssue("Blocked issue")
_ = store.AddBlocker(issue1.ID, issue2.ID)
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue1, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "Blocks") {
t.Errorf("expected output to contain 'Blocks', got: %s", output)
}
if !strings.Contains(output, " "+issue2.ID+" open Blocked issue") {
t.Errorf("expected output to contain ' %s open Blocked issue', got: %s", issue2.ID, output)
}
}
func TestPrintIssueDetails_WithComments(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue")
_ = store.AddComment(issue.ID, "First comment")
_ = store.AddComment(issue.ID, "Second comment")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "Comments") {
t.Errorf("expected output to contain 'Comments', got: %s", output)
}
if !strings.Contains(output, " First comment") {
t.Errorf("expected output to contain ' First comment', got: %s", output)
}
if !strings.Contains(output, " Second comment") {
t.Errorf("expected output to contain ' Second comment', got: %s", output)
}
}
func TestPrintIssueDetails_NoOptionalSections(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Standalone issue")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
if strings.Contains(output, "Depends on") {
t.Errorf("expected output NOT to contain 'Depends on', got: %s", output)
}
if strings.Contains(output, "Blocks") {
t.Errorf("expected output NOT to contain 'Blocks', got: %s", output)
}
if strings.Contains(output, "Comments") {
t.Errorf("expected output NOT to contain 'Comments', got: %s", output)
}
}
func TestPrintIssueDetails_KeyFormatting(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue")
dep, _ := store.AddIssue("Dependency")
block, _ := store.AddIssue("Blocked")
_ = store.AddDependency(issue.ID, dep.ID)
_ = store.AddBlocker(issue.ID, block.ID)
_ = store.AddComment(issue.ID, "Test comment")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := buf.String()
// Check that keys are bold (1m) and color 5 (38;5;5m)
if !strings.Contains(output, "\033[1m\033[38;5;5mID\033[0m") {
t.Errorf("expected 'ID' key to be bold and color 5, got: %s", output)
}
if !strings.Contains(output, "\033[1m\033[38;5;5mTitle\033[0m") {
t.Errorf("expected 'Title' key to be bold and color 5, got: %s", output)
}
if !strings.Contains(output, "\033[1m\033[38;5;5mStatus\033[0m") {
t.Errorf("expected 'Status' key to be bold and color 5, got: %s", output)
}
if !strings.Contains(output, "\033[1m\033[38;5;5mDepends on\033[0m") {
t.Errorf("expected 'Depends on' key to be bold and color 5, got: %s", output)
}
if !strings.Contains(output, "\033[1m\033[38;5;5mBlocks\033[0m") {
t.Errorf("expected 'Blocks' key to be bold and color 5, got: %s", output)
}
if !strings.Contains(output, "\033[1m\033[38;5;5mComments\033[0m") {
t.Errorf("expected 'Comments' key to be bold and color 5, got: %s", output)
}
// Check that there are no colons after the keys
stripped := stripANSI(output)
if strings.Contains(stripped, "ID:") {
t.Errorf("expected no colon after 'ID', got: %s", stripped)
}
if strings.Contains(stripped, "Title:") {
t.Errorf("expected no colon after 'Title', got: %s", stripped)
}
if strings.Contains(stripped, "Status:") {
t.Errorf("expected no colon after 'Status', got: %s", stripped)
}
if strings.Contains(stripped, "Depends on:") {
t.Errorf("expected no colon after 'Depends on', got: %s", stripped)
}
if strings.Contains(stripped, "Blocks:") {
t.Errorf("expected no colon after 'Blocks', got: %s", stripped)
}
if strings.Contains(stripped, "Comments:") {
t.Errorf("expected no colon after 'Comments', got: %s", stripped)
}
}
func TestPrintIssueList_Basic(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue1, _ := store.AddIssue("First issue")
issue2, _ := store.AddIssue("Second issue")
_ = store.CloseIssue(issue2.ID, "")
_ = store.Save(filePath)
issues := []*Issue{issue1, issue2}
maxIDLen := max(len(issue1.ID), len(issue2.ID))
var buf bytes.Buffer
err := printIssueList(&buf, issues, maxIDLen, store)
if err != nil {
t.Fatalf("printIssueList failed: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, issue1.ID) {
t.Errorf("expected output to contain '%s', got: %s", issue1.ID, output)
}
if !strings.Contains(output, "First issue") {
t.Errorf("expected output to contain 'First issue', got: %s", output)
}
if !strings.Contains(output, issue2.ID) {
t.Errorf("expected output to contain '%s', got: %s", issue2.ID, output)
}
if !strings.Contains(output, "Second issue") {
t.Errorf("expected output to contain 'Second issue', got: %s", output)
}
}
func TestPrintIssueDetails_WithStaleDepends(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue with stale dependency")
// Manually inject stale reference
issue.DependsOn = []string{"nonexistent-id"}
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails should not fail on stale refs: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "Depends on") {
t.Errorf("expected output to contain 'Depends on', got: %s", output)
}
if !strings.Contains(output, "nonexistent-id (not found)") {
t.Errorf("expected output to contain 'nonexistent-id (not found)', got: %s", output)
}
}
func TestPrintIssueDetails_WithStaleBlocks(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue with stale blocker")
// Manually inject stale reference
issue.Blocks = []string{"nonexistent-block"}
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails should not fail on stale refs: %v", err)
}
output := stripANSI(buf.String())
if !strings.Contains(output, "Blocks") {
t.Errorf("expected output to contain 'Blocks', got: %s", output)
}
if !strings.Contains(output, "nonexistent-block (not found)") {
t.Errorf("expected output to contain 'nonexistent-block (not found)', got: %s", output)
}
}
func TestPrintIssueDetails_WithMixedValidAndStaleRefs(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
main, _ := store.AddIssue("Main issue")
validDep, _ := store.AddIssue("Valid dependency")
validBlock, _ := store.AddIssue("Valid blocker")
_ = store.AddDependency(main.ID, validDep.ID)
_ = store.AddBlocker(main.ID, validBlock.ID)
// Manually inject stale references
main.DependsOn = append(main.DependsOn, "stale-dep")
main.Blocks = append(main.Blocks, "stale-block")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, main, store)
if err != nil {
t.Fatalf("PrintIssueDetails should not fail on stale refs: %v", err)
}
output := stripANSI(buf.String())
// Check valid refs are shown normally
if !strings.Contains(output, validDep.ID+" open Valid dependency") {
t.Errorf("expected output to contain valid dependency, got: %s", output)
}
if !strings.Contains(output, validBlock.ID+" open Valid blocker") {
t.Errorf("expected output to contain valid blocker, got: %s", output)
}
// Check stale refs are shown with (not found)
if !strings.Contains(output, "stale-dep (not found)") {
t.Errorf("expected output to contain 'stale-dep (not found)', got: %s", output)
}
if !strings.Contains(output, "stale-block (not found)") {
t.Errorf("expected output to contain 'stale-block (not found)', got: %s", output)
}
}
func TestPrintIssueDetails_Whitespace(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue")
dep, _ := store.AddIssue("Dependency")
_ = store.AddDependency(issue.ID, dep.ID)
_ = store.AddComment(issue.ID, "Test comment")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := buf.String()
stripped := stripANSI(output)
// Should start with newline
if !strings.HasPrefix(output, "\n") {
t.Errorf("expected output to start with newline")
}
// Should have timestamps followed by blank line before Depends on
if !strings.Contains(stripped, "Updated") {
t.Errorf("expected Updated timestamp, got: %s", stripped)
}
// Check for blank line before Depends on (after timestamps)
if !strings.Contains(stripped, "\n\nDepends on") {
t.Errorf("expected blank line before Depends on, got: %s", stripped)
}
// Should have blank line between Depends on and Comments
if !strings.Contains(stripped, "Dependency\n\nComments") {
t.Errorf("expected blank line between Depends on and Comments, got: %s", stripped)
}
// Should end with newline
if !strings.HasSuffix(output, "\n") {
t.Errorf("expected output to end with newline")
}
}
func TestFormatRelativeTime_Seconds(t *testing.T) {
now := time.Now()
tests := []struct {
name string
duration time.Duration
expected string
}{
{"4 seconds ago", 4 * time.Second, "4s ago"},
{"1 second ago", 1 * time.Second, "1s ago"},
{"30 seconds ago", 30 * time.Second, "30s ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
past := now.Add(-tt.duration)
result := formatRelativeTime(past)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestFormatRelativeTime_Minutes(t *testing.T) {
now := time.Now()
tests := []struct {
name string
duration time.Duration
expected string
}{
{"1 minute ago", 1 * time.Minute, "1m ago"},
{"5 minutes ago", 5 * time.Minute, "5m ago"},
{"59 minutes ago", 59 * time.Minute, "59m ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
past := now.Add(-tt.duration)
result := formatRelativeTime(past)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestFormatRelativeTime_Hours(t *testing.T) {
now := time.Now()
tests := []struct {
name string
duration time.Duration
expected string
}{
{"1 hour ago", 1 * time.Hour, "1h ago"},
{"3 hours ago", 3 * time.Hour, "3h ago"},
{"23 hours ago", 23 * time.Hour, "23h ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
past := now.Add(-tt.duration)
result := formatRelativeTime(past)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestFormatRelativeTime_Days(t *testing.T) {
now := time.Now()
tests := []struct {
name string
duration time.Duration
expected string
}{
{"1 day ago", 24 * time.Hour, "1d ago"},
{"2 days ago", 48 * time.Hour, "2d ago"},
{"5 days ago", 120 * time.Hour, "5d ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
past := now.Add(-tt.duration)
result := formatRelativeTime(past)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestFormatRelativeTime_MixedUnits(t *testing.T) {
now := time.Now()
tests := []struct {
name string
duration time.Duration
expected string
}{
// Mixed durations should show only the largest unit
{"5 hours 31 minutes 4 seconds", 5*time.Hour + 31*time.Minute + 4*time.Second, "5h ago"},
{"2 days 5 hours 31 minutes", 2*24*time.Hour + 5*time.Hour + 31*time.Minute, "2d ago"},
{"31 minutes 45 seconds", 31*time.Minute + 45*time.Second, "31m ago"},
{"1 hour 59 minutes 59 seconds", 1*time.Hour + 59*time.Minute + 59*time.Second, "1h ago"},
{"23 hours 59 minutes 59 seconds", 23*time.Hour + 59*time.Minute + 59*time.Second, "23h ago"},
{"10 days 12 hours 30 minutes", 10*24*time.Hour + 12*time.Hour + 30*time.Minute, "10d ago"},
{"59 seconds", 59 * time.Second, "59s ago"},
{"1 minute 30 seconds", 1*time.Minute + 30*time.Second, "1m ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
past := now.Add(-tt.duration)
result := formatRelativeTime(past)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestPrintIssueDetails_WithRelativeTimestamps(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "mint-issues.yaml")
t.Setenv("MINT_STORE_FILE", filePath)
store, _ := LoadStore(filePath)
issue, _ := store.AddIssue("Test issue")
_ = store.Save(filePath)
var buf bytes.Buffer
err := PrintIssueDetails(&buf, issue, store)
if err != nil {
t.Fatalf("PrintIssueDetails failed: %v", err)
}
output := stripANSI(buf.String())
// Check that Created timestamp has relative time in parentheses
if !strings.Contains(output, "Created") {
t.Errorf("expected 'Created' in output, got: %s", output)
}
// Look for pattern like "2025-12-10 11:01:25 (0s ago)" or similar
if !strings.Contains(output, "ago)") {
t.Errorf("expected relative time in Created field, got: %s", output)
}
// Check that Updated timestamp has relative time in parentheses
if !strings.Contains(output, "Updated") {
t.Errorf("expected 'Updated' in output, got: %s", output)
}
}