forked from dorkitude/linctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud_test.go
More file actions
2192 lines (1939 loc) · 58.8 KB
/
crud_test.go
File metadata and controls
2192 lines (1939 loc) · 58.8 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
// Package-level state set in TestMain
var (
binaryPath string
teamKey string
teamUUID string
testPrefix string
// Cross-subtest shared IDs
testProjectID string
testLabelID string
testLabelName string
testIssueID string // identifier like ROB-123
testIssueUUID string
testViewID string
testCycleID string
)
// runCLI executes the binary with given args and returns stdout, stderr, exit code.
func runCLI(t *testing.T, args ...string) (string, string, int) {
t.Helper()
cmd := exec.Command(binaryPath, args...)
var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
exitCode := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
} else {
t.Fatalf("failed to run command %v: %v", args, err)
}
}
return stdout.String(), stderr.String(), exitCode
}
// runCLISuccess runs the binary and fails the test on non-zero exit.
func runCLISuccess(t *testing.T, args ...string) string {
t.Helper()
stdout, stderr, exitCode := runCLI(t, args...)
if exitCode != 0 {
t.Fatalf("command %v failed (exit %d)\nstdout: %s\nstderr: %s", args, exitCode, stdout, stderr)
}
return stdout
}
// runCLIFail runs the binary and fails the test if exit IS zero.
func runCLIFail(t *testing.T, args ...string) string {
t.Helper()
stdout, stderr, exitCode := runCLI(t, args...)
if exitCode == 0 {
t.Fatalf("command %v succeeded but expected failure\nstdout: %s\nstderr: %s", args, stdout, stderr)
}
// Return combined output since errors may be on stdout (cobra) or stderr
return stdout + stderr
}
// parseJSONArray parses a JSON array string into []map[string]interface{}.
func parseJSONArray(t *testing.T, jsonStr string) []map[string]interface{} {
t.Helper()
var arr []map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &arr); err != nil {
t.Fatalf("failed to parse JSON array: %v\njson: %s", err, truncate(jsonStr, 500))
}
return arr
}
// parseJSONObject parses a JSON object string into map[string]interface{}.
func parseJSONObject(t *testing.T, jsonStr string) map[string]interface{} {
t.Helper()
var obj map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &obj); err != nil {
t.Fatalf("failed to parse JSON object: %v\njson: %s", err, truncate(jsonStr, 500))
}
return obj
}
// extractField parses JSON (object or array-first-element) and returns a top-level field as string.
func extractField(t *testing.T, jsonStr string, field string) string {
t.Helper()
trimmed := strings.TrimSpace(jsonStr)
var obj map[string]interface{}
if strings.HasPrefix(trimmed, "[") {
arr := parseJSONArray(t, trimmed)
if len(arr) == 0 {
t.Fatalf("extractField: empty JSON array, looking for field %q", field)
}
obj = arr[0]
} else {
obj = parseJSONObject(t, trimmed)
}
val, ok := obj[field]
if !ok {
t.Fatalf("extractField: field %q not found in JSON", field)
}
return fmt.Sprintf("%v", val)
}
// extractID is shorthand for extractField(t, jsonStr, "id").
func extractID(t *testing.T, jsonStr string) string {
t.Helper()
return extractField(t, jsonStr, "id")
}
// findByField finds the first element in a JSON array where field==value.
func findByField(t *testing.T, jsonStr, field, value string) map[string]interface{} {
t.Helper()
arr := parseJSONArray(t, jsonStr)
for _, obj := range arr {
if fmt.Sprintf("%v", obj[field]) == value {
return obj
}
}
t.Fatalf("findByField: no element with %s=%s found in array of %d elements", field, value, len(arr))
return nil
}
// jsonArrayLen returns the length of a JSON array.
func jsonArrayLen(t *testing.T, jsonStr string) int {
t.Helper()
arr := parseJSONArray(t, jsonStr)
return len(arr)
}
// truncate limits a string to maxLen chars for error messages.
func truncate(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}
// assertContains fails if substr is not found in s.
func assertContains(t *testing.T, s, substr string) {
t.Helper()
if !strings.Contains(s, substr) {
t.Errorf("expected output to contain %q, got: %s", substr, truncate(s, 300))
}
}
// assertNotEmpty fails if s is empty or whitespace-only.
func assertNotEmpty(t *testing.T, s string) {
t.Helper()
if strings.TrimSpace(s) == "" {
t.Errorf("expected non-empty output")
}
}
func TestMain(m *testing.M) {
// Build binary
fmt.Println("Building linear-cli test binary...")
build := exec.Command("go", "build", "-o", "linear-cli.test", ".")
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
fmt.Fprintf(os.Stderr, "FATAL: failed to build binary: %v\n", err)
os.Exit(1)
}
wd, _ := os.Getwd()
binaryPath = wd + "/linear-cli.test"
testPrefix = fmt.Sprintf("crud-test-%d", time.Now().Unix())
// Verify auth
fmt.Println("Verifying authentication...")
cmd := exec.Command(binaryPath, "auth", "status", "--json")
out, err := cmd.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "FATAL: not authenticated. Run 'linear-cli auth' first.\n")
os.Exit(1)
}
if !strings.Contains(string(out), "true") {
fmt.Fprintf(os.Stderr, "FATAL: auth status check failed: %s\n", string(out))
os.Exit(1)
}
// Discover team
fmt.Println("Discovering team...")
cmd = exec.Command(binaryPath, "team", "list", "--json")
out, err = cmd.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "FATAL: failed to list teams: %v\n", err)
os.Exit(1)
}
var teams []map[string]interface{}
if err := json.Unmarshal(out, &teams); err != nil || len(teams) == 0 {
fmt.Fprintf(os.Stderr, "FATAL: no teams found or JSON parse error: %v\n", err)
os.Exit(1)
}
teamKey = fmt.Sprintf("%v", teams[0]["key"])
teamUUID = fmt.Sprintf("%v", teams[0]["id"])
fmt.Printf("Test config: team=%s (%s), prefix=%s\n", teamKey, teamUUID, testPrefix)
exitCode := m.Run()
// Cleanup binary
os.Remove(binaryPath)
os.Exit(exitCode)
}
// =============================================================================
// TestCRUD - ordered subtests
// =============================================================================
func TestCRUD(t *testing.T) {
// Ordered subtests - some produce IDs consumed by later ones.
// Go runs subtests sequentially within a parent test.
//
// Shared resource cleanup is registered at THIS level so resources
// persist across all subtests, not just the one that created them.
// Register cleanup for shared resources at parent level.
// These run in LIFO order after all subtests complete.
t.Cleanup(func() {
// Clean up issues (archive)
if testIssueID != "" {
runCLI(t, "issue", "archive", testIssueID)
}
// Clean up cycle
if testCycleID != "" {
runCLI(t, "cycle", "archive", testCycleID)
}
// Clean up project (last since other entities may reference it)
if testProjectID != "" {
runCLI(t, "project", "delete", testProjectID)
}
// Clean up label if still around
if testLabelID != "" {
runCLI(t, "label", "delete", testLabelID)
}
// Clean up view if still around
if testViewID != "" {
runCLI(t, "view", "delete", testViewID)
}
})
t.Run("Team", testTeam)
t.Run("User", testUser)
t.Run("Label", testLabel)
t.Run("Project", testProject)
t.Run("Initiative", testInitiative)
t.Run("Milestone", testMilestone)
t.Run("Document", testDocument)
t.Run("Cycle", testCycle)
t.Run("Issue", testIssue)
t.Run("IssueTree", testIssueTree)
t.Run("Comment", testComment)
t.Run("Relation", testRelation)
t.Run("Attachment", testAttachment)
t.Run("View", testView)
t.Run("Favorite", testFavorite)
t.Run("StatusUpdate", testStatusUpdate)
t.Run("Inbox", testInbox)
t.Run("GraphQL", testGraphQL)
t.Run("ErrorHandling", testErrorHandling)
}
// =============================================================================
// Team tests (read-only)
// =============================================================================
func testTeam(t *testing.T) {
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "team", "list")
assertNotEmpty(t, out)
assertContains(t, out, teamKey)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "team", "list", "-p")
assertNotEmpty(t, out)
})
t.Run("List_JSON", func(t *testing.T) {
out := runCLISuccess(t, "team", "list", "--json")
arr := parseJSONArray(t, out)
if len(arr) == 0 {
t.Fatal("expected at least one team")
}
if _, ok := arr[0]["key"]; !ok {
t.Error("expected 'key' field in team JSON")
}
})
t.Run("List_Limit", func(t *testing.T) {
out := runCLISuccess(t, "team", "list", "--json", "--limit", "1")
arr := parseJSONArray(t, out)
if len(arr) != 1 {
t.Errorf("expected 1 team with --limit 1, got %d", len(arr))
}
})
t.Run("Get", func(t *testing.T) {
out := runCLISuccess(t, "team", "get", teamKey)
assertContains(t, out, teamKey)
})
t.Run("Get_JSON", func(t *testing.T) {
out := runCLISuccess(t, "team", "get", teamKey, "--json")
key := extractField(t, out, "key")
if key != teamKey {
t.Errorf("expected team key %s, got %s", teamKey, key)
}
})
t.Run("Members", func(t *testing.T) {
out := runCLISuccess(t, "team", "members", teamKey)
assertNotEmpty(t, out)
})
t.Run("Members_JSON", func(t *testing.T) {
out := runCLISuccess(t, "team", "members", teamKey, "--json")
arr := parseJSONArray(t, out)
if len(arr) == 0 {
t.Fatal("expected at least one team member")
}
})
t.Run("States", func(t *testing.T) {
out := runCLISuccess(t, "team", "states", teamKey)
assertNotEmpty(t, out)
})
t.Run("States_JSON", func(t *testing.T) {
out := runCLISuccess(t, "team", "states", teamKey, "--json")
arr := parseJSONArray(t, out)
if len(arr) == 0 {
t.Fatal("expected at least one team state")
}
if _, ok := arr[0]["type"]; !ok {
t.Error("expected 'type' field in state JSON")
}
})
t.Run("States_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "team", "states", teamKey, "-p")
assertNotEmpty(t, out)
})
}
// =============================================================================
// User tests (read-only)
// =============================================================================
func testUser(t *testing.T) {
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "user", "list")
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "user", "list", "-p")
assertNotEmpty(t, out)
})
t.Run("List_JSON", func(t *testing.T) {
out := runCLISuccess(t, "user", "list", "--json")
arr := parseJSONArray(t, out)
if len(arr) == 0 {
t.Fatal("expected at least one user")
}
if _, ok := arr[0]["email"]; !ok {
t.Error("expected 'email' field in user JSON")
}
})
t.Run("List_Active", func(t *testing.T) {
out := runCLISuccess(t, "user", "list", "--json", "--active")
arr := parseJSONArray(t, out)
for _, u := range arr {
if active, ok := u["active"]; ok && active == false {
t.Error("found inactive user when --active filter was used")
}
}
})
t.Run("List_Limit", func(t *testing.T) {
out := runCLISuccess(t, "user", "list", "--json", "--limit", "1")
arr := parseJSONArray(t, out)
if len(arr) != 1 {
t.Errorf("expected 1 user with --limit 1, got %d", len(arr))
}
})
t.Run("Me_Table", func(t *testing.T) {
out := runCLISuccess(t, "user", "me")
assertNotEmpty(t, out)
})
t.Run("Me_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "user", "me", "-p")
assertNotEmpty(t, out)
})
t.Run("Me_JSON", func(t *testing.T) {
out := runCLISuccess(t, "user", "me", "--json")
id := extractField(t, out, "id")
if id == "" {
t.Error("expected non-empty user ID from 'me'")
}
})
t.Run("Get_JSON", func(t *testing.T) {
// Get our own user ID first
meOut := runCLISuccess(t, "user", "me", "--json")
userID := extractField(t, meOut, "id")
out := runCLISuccess(t, "user", "get", userID, "--json")
gotID := extractField(t, out, "id")
if gotID != userID {
t.Errorf("expected user ID %s, got %s", userID, gotID)
}
})
}
// =============================================================================
// Label tests (CRUD)
// =============================================================================
func testLabel(t *testing.T) {
labelName := testPrefix + "-label"
var labelID string
t.Run("Create", func(t *testing.T) {
out := runCLISuccess(t, "label", "create",
"--name", labelName,
"--color", "#e11d48",
"--description", "Test label for CRUD tests",
"--team-id", teamUUID,
"--json",
)
labelID = extractID(t, out)
if labelID == "" {
t.Fatal("expected non-empty label ID")
}
testLabelID = labelID
testLabelName = labelName
name := extractField(t, out, "name")
if name != labelName {
t.Errorf("expected label name %s, got %s", labelName, name)
}
})
t.Run("List_JSON", func(t *testing.T) {
if labelID == "" {
t.Skip("no label created")
}
out := runCLISuccess(t, "label", "list", "--json", "--team", teamKey)
arr := parseJSONArray(t, out)
found := false
for _, l := range arr {
if fmt.Sprintf("%v", l["id"]) == labelID {
found = true
break
}
}
if !found {
t.Errorf("created label %s not found in list", labelID)
}
})
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "label", "list")
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "label", "list", "-p")
assertContains(t, out, "# Labels")
})
t.Run("Update", func(t *testing.T) {
if labelID == "" {
t.Skip("no label created")
}
updatedName := labelName + "-updated"
out := runCLISuccess(t, "label", "update", labelID,
"--name", updatedName,
"--color", "#2563eb",
"--description", "Updated description",
"--json",
)
name := extractField(t, out, "name")
if name != updatedName {
t.Errorf("expected updated name %s, got %s", updatedName, name)
}
color := extractField(t, out, "color")
if color != "#2563eb" {
t.Errorf("expected color #2563eb, got %s", color)
}
// Keep the original name for issue tests
testLabelName = updatedName
})
t.Run("Delete", func(t *testing.T) {
if labelID == "" {
t.Skip("no label created")
}
runCLISuccess(t, "label", "delete", labelID)
// Clear shared state
testLabelID = ""
testLabelName = ""
})
}
// =============================================================================
// Project tests (CRUD)
// =============================================================================
func testProject(t *testing.T) {
projectName := testPrefix + "-project"
var projectID string
t.Run("Create", func(t *testing.T) {
out := runCLISuccess(t, "project", "create",
"--name", projectName,
"--description", "Test project for CRUD tests",
"--state", "planned",
"--team-ids", teamUUID,
"--json",
)
projectID = extractID(t, out)
if projectID == "" {
t.Fatal("expected non-empty project ID")
}
testProjectID = projectID
name := extractField(t, out, "name")
if name != projectName {
t.Errorf("expected project name %s, got %s", projectName, name)
}
})
t.Run("List_JSON", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
out := runCLISuccess(t, "project", "list", "--json", "--newer-than", "all_time")
arr := parseJSONArray(t, out)
found := false
for _, p := range arr {
if fmt.Sprintf("%v", p["id"]) == projectID {
found = true
break
}
}
if !found {
t.Errorf("created project %s not found in list", projectID)
}
})
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "project", "list")
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "project", "list", "-p")
assertContains(t, out, "# Projects")
})
t.Run("List_StateFilter", func(t *testing.T) {
out := runCLISuccess(t, "project", "list", "--json", "--state", "planned")
arr := parseJSONArray(t, out)
for _, p := range arr {
if fmt.Sprintf("%v", p["state"]) != "planned" {
t.Errorf("expected state 'planned', got %v", p["state"])
}
}
})
t.Run("List_Limit", func(t *testing.T) {
out := runCLISuccess(t, "project", "list", "--json", "--limit", "1", "--newer-than", "all_time")
arr := parseJSONArray(t, out)
if len(arr) > 1 {
t.Errorf("expected at most 1 project with --limit 1, got %d", len(arr))
}
})
t.Run("Get_Table", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
out := runCLISuccess(t, "project", "get", projectID)
assertContains(t, out, "Project:")
})
t.Run("Get_JSON", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
out := runCLISuccess(t, "project", "get", projectID, "--json")
id := extractID(t, out)
if id != projectID {
t.Errorf("expected project ID %s, got %s", projectID, id)
}
})
t.Run("Get_Plaintext", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
out := runCLISuccess(t, "project", "get", projectID, "-p")
assertContains(t, out, "# ")
})
t.Run("Update", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
updatedName := projectName + "-updated"
out := runCLISuccess(t, "project", "update", projectID,
"--name", updatedName,
"--description", "Updated project description",
"--json",
)
name := extractField(t, out, "name")
if name != updatedName {
t.Errorf("expected updated name %s, got %s", updatedName, name)
}
})
t.Run("Issues_Empty", func(t *testing.T) {
if projectID == "" {
t.Skip("no project created")
}
// New project should have no issues
out := runCLISuccess(t, "project", "issues", projectID, "--json")
arr := parseJSONArray(t, out)
if len(arr) != 0 {
t.Errorf("expected 0 issues for new project, got %d", len(arr))
}
})
// Note: project cleanup is handled by TestCRUD's t.Cleanup since
// later subtests (Milestone, Issue, etc.) depend on testProjectID.
}
// =============================================================================
// Initiative tests (CRUD)
// =============================================================================
func testInitiative(t *testing.T) {
initName := testPrefix + "-initiative"
var initID string
t.Run("Create", func(t *testing.T) {
out := runCLISuccess(t, "initiative", "create",
"--name", initName,
"--description", "Test initiative for CRUD tests",
"--status", "Planned",
"--json",
)
initID = extractID(t, out)
if initID == "" {
t.Fatal("expected non-empty initiative ID")
}
name := extractField(t, out, "name")
if name != initName {
t.Errorf("expected initiative name %s, got %s", initName, name)
}
})
t.Run("List_JSON", func(t *testing.T) {
out := runCLISuccess(t, "initiative", "list", "--json", "--include-completed")
arr := parseJSONArray(t, out)
found := false
for _, i := range arr {
if fmt.Sprintf("%v", i["id"]) == initID {
found = true
break
}
}
if !found {
t.Errorf("created initiative %s not found in list", initID)
}
})
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "initiative", "list")
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "initiative", "list", "-p")
assertContains(t, out, "# Initiatives")
})
t.Run("Get_JSON", func(t *testing.T) {
if initID == "" {
t.Skip("no initiative created")
}
out := runCLISuccess(t, "initiative", "get", initID, "--json")
id := extractID(t, out)
if id != initID {
t.Errorf("expected initiative ID %s, got %s", initID, id)
}
})
t.Run("Update", func(t *testing.T) {
if initID == "" {
t.Skip("no initiative created")
}
updatedName := initName + "-updated"
out := runCLISuccess(t, "initiative", "update", initID,
"--name", updatedName,
"--description", "Updated initiative description",
"--json",
)
name := extractField(t, out, "name")
if name != updatedName {
t.Errorf("expected updated name %s, got %s", updatedName, name)
}
})
t.Run("AddProject", func(t *testing.T) {
if initID == "" || testProjectID == "" {
t.Skip("no initiative or project created")
}
runCLISuccess(t, "initiative", "add-project", initID, testProjectID)
})
t.Run("Projects", func(t *testing.T) {
if initID == "" {
t.Skip("no initiative created")
}
out := runCLISuccess(t, "initiative", "projects", initID, "--json")
// Should have at least the project we added
arr := parseJSONArray(t, out)
if testProjectID != "" && len(arr) == 0 {
t.Error("expected at least one project after add-project")
}
})
t.Run("RemoveProject", func(t *testing.T) {
if initID == "" || testProjectID == "" {
t.Skip("no initiative or project created")
}
runCLISuccess(t, "initiative", "remove-project", initID, testProjectID)
})
t.Run("ProjectFlag", func(t *testing.T) {
// LINE-30: Test --initiative flag on project create and update
if initID == "" {
t.Skip("no initiative created")
}
// Create a new test initiative for this test
testInitName := testPrefix + "-init-flag"
testInitOut := runCLISuccess(t, "initiative", "create",
"--name", testInitName,
"--description", "Test initiative for project flag tests",
"--status", "Planned",
"--json",
)
testInitID := extractID(t, testInitOut)
if testInitID == "" {
t.Fatal("expected non-empty test initiative ID")
}
// Create project with --initiative flag
projName := testPrefix + "-proj-with-init"
projOut := runCLISuccess(t, "project", "create",
"--name", projName,
"--team-ids", teamUUID,
"--initiative", testInitID,
"--json",
)
projID := extractID(t, projOut)
if projID == "" {
t.Fatal("expected non-empty project ID")
}
// Verify project is linked to initiative
initProjsOut := runCLISuccess(t, "initiative", "projects", testInitID, "--json")
arr := parseJSONArray(t, initProjsOut)
found := false
for _, p := range arr {
if fmt.Sprintf("%v", p["id"]) == projID {
found = true
break
}
}
if !found {
t.Errorf("project %s not found in initiative %s projects list", projID, testInitID)
}
// Update project to unlink with --initiative none
runCLISuccess(t, "project", "update", projID,
"--initiative", "none",
"--json",
)
// Verify project is no longer linked to initiative
initProjsOut = runCLISuccess(t, "initiative", "projects", testInitID, "--json")
arr = parseJSONArray(t, initProjsOut)
found = false
for _, p := range arr {
if fmt.Sprintf("%v", p["id"]) == projID {
found = true
break
}
}
if found {
t.Errorf("project %s should not be in initiative %s projects list after unlinking", projID, testInitID)
}
// Cleanup
t.Cleanup(func() {
runCLI(t, "project", "delete", projID)
runCLI(t, "initiative", "delete", testInitID)
})
})
t.Run("Delete", func(t *testing.T) {
if initID == "" {
t.Skip("no initiative created")
}
runCLISuccess(t, "initiative", "delete", initID)
})
}
// =============================================================================
// Milestone tests (CRUD - nested under project)
// =============================================================================
func testMilestone(t *testing.T) {
if testProjectID == "" {
t.Skip("no project available for milestone tests")
}
msName := testPrefix + "-milestone"
var msID string
t.Run("Create", func(t *testing.T) {
out := runCLISuccess(t, "project", "milestone", "create", testProjectID,
"--name", msName,
"--description", "Test milestone",
"--target-date", "2026-12-31",
"--json",
)
msID = extractID(t, out)
if msID == "" {
t.Fatal("expected non-empty milestone ID")
}
name := extractField(t, out, "name")
if name != msName {
t.Errorf("expected milestone name %s, got %s", msName, name)
}
})
t.Run("List_JSON", func(t *testing.T) {
out := runCLISuccess(t, "project", "milestone", "list", testProjectID, "--json")
arr := parseJSONArray(t, out)
found := false
for _, ms := range arr {
if fmt.Sprintf("%v", ms["id"]) == msID {
found = true
break
}
}
if !found {
t.Errorf("created milestone %s not found in list", msID)
}
})
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "project", "milestone", "list", testProjectID)
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "project", "milestone", "list", testProjectID, "-p")
assertContains(t, out, "# Milestones")
})
t.Run("Get_JSON", func(t *testing.T) {
if msID == "" {
t.Skip("no milestone created")
}
out := runCLISuccess(t, "project", "milestone", "get", msID, "--json")
id := extractID(t, out)
if id != msID {
t.Errorf("expected milestone ID %s, got %s", msID, id)
}
})
t.Run("Update", func(t *testing.T) {
if msID == "" {
t.Skip("no milestone created")
}
updatedName := msName + "-updated"
out := runCLISuccess(t, "project", "milestone", "update", msID,
"--name", updatedName,
"--description", "Updated milestone",
"--json",
)
name := extractField(t, out, "name")
if name != updatedName {
t.Errorf("expected updated name %s, got %s", updatedName, name)
}
})
t.Run("Delete", func(t *testing.T) {
if msID == "" {
t.Skip("no milestone created")
}
runCLISuccess(t, "project", "milestone", "delete", msID)
})
}
// =============================================================================
// Document tests (CRUD)
// =============================================================================
func testDocument(t *testing.T) {
docTitle := testPrefix + "-document"
var docID string
t.Run("Create", func(t *testing.T) {
args := []string{"document", "create",
"--title", docTitle,
"--content", "# Test Document\n\nThis is a test document for CRUD tests.",
"--json",
}
if testProjectID != "" {
args = append(args, "--project", testProjectID)
} else {
args = append(args, "--team", teamKey)
}
out := runCLISuccess(t, args...)
docID = extractID(t, out)
if docID == "" {
t.Fatal("expected non-empty document ID")
}
title := extractField(t, out, "title")
if title != docTitle {
t.Errorf("expected document title %s, got %s", docTitle, title)
}
})
t.Run("List_JSON", func(t *testing.T) {
out := runCLISuccess(t, "document", "list", "--json")
arr := parseJSONArray(t, out)
found := false
for _, d := range arr {
if fmt.Sprintf("%v", d["id"]) == docID {
found = true
break
}
}
if !found {
t.Errorf("created document %s not found in list", docID)
}
})
t.Run("List_Table", func(t *testing.T) {
out := runCLISuccess(t, "document", "list")
assertNotEmpty(t, out)
})
t.Run("List_Plaintext", func(t *testing.T) {
out := runCLISuccess(t, "document", "list", "-p")
assertContains(t, out, "# Documents")
})
t.Run("Get_JSON", func(t *testing.T) {
if docID == "" {
t.Skip("no document created")
}
out := runCLISuccess(t, "document", "get", docID, "--json")
id := extractID(t, out)
if id != docID {
t.Errorf("expected document ID %s, got %s", docID, id)
}
})
t.Run("Get_Plaintext", func(t *testing.T) {
if docID == "" {
t.Skip("no document created")
}
out := runCLISuccess(t, "document", "get", docID, "-p")
assertContains(t, out, "# ")
})
t.Run("Search", func(t *testing.T) {