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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
|
//go:build integration
package integrationtests
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"time"
"codeberg.org/snonux/hexai/internal/askcli"
)
// repoRoot is set in TestMain before any test runs.
var repoRoot string
func findRepoRoot() string {
dir, err := os.Getwd()
if err != nil {
return ""
}
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return ""
}
func askBinaryPath() string {
return filepath.Join(repoRoot, "cmd", "ask", "ask")
}
func runAsk(ctx context.Context, args []string) (stdout, stderr bytes.Buffer, exitCode int) {
cmd := exec.CommandContext(ctx, askBinaryPath(), args...)
cmd.Dir = repoRoot
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
return
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
return bytes.Buffer{}, stderr, -1
}
return stdout, stderr, ee.ExitCode()
}
// runAskWithStdin runs ask with the given stdin. Only use this for commands
// that actually forward stdin to taskwarrior (currently only: delete).
func runAskWithStdin(ctx context.Context, args []string, stdin string) (stdout, stderr bytes.Buffer, exitCode int) {
cmd := exec.CommandContext(ctx, askBinaryPath(), args...)
cmd.Dir = repoRoot
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
return
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
return bytes.Buffer{}, stderr, -1
}
return stdout, stderr, ee.ExitCode()
}
func runTask(ctx context.Context, args []string) (stdout, stderr bytes.Buffer, exitCode int) {
cmd := exec.CommandContext(ctx, "task", args...)
cmd.Dir = repoRoot
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
return
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
return bytes.Buffer{}, stderr, -1
}
return stdout, stderr, ee.ExitCode()
}
func runTaskWithStdin(ctx context.Context, args []string, stdin string) (stdout, stderr bytes.Buffer, exitCode int) {
cmd := exec.CommandContext(ctx, "task", args...)
cmd.Dir = repoRoot
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
return
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
return bytes.Buffer{}, stderr, -1
}
return stdout, stderr, ee.ExitCode()
}
// createTask creates a new task via ask add and returns its UUID.
// ask add prints the human-facing alias ID, so we resolve the created UUID via ask info.
func createTask(ctx context.Context, desc string) (string, error) {
stdout, stderr, code := runAsk(ctx, []string{"add", "+integrationtest", desc})
if code != 0 {
return "", fmt.Errorf("create task failed (code %d): stdout=%s stderr=%s", code, stdout.String(), stderr.String())
}
id := strings.TrimSpace(stdout.String())
if id == "" {
return "", fmt.Errorf("could not extract task ID from ask add output: %s", stdout.String())
}
info, ok := getTaskInfoFast(ctx, id)
if !ok {
return "", fmt.Errorf("could not resolve task ID %q after ask add", id)
}
if info.UUID == "" {
return "", fmt.Errorf("ask info %q did not return a UUID", id)
}
return info.UUID, nil
}
func deleteTask(ctx context.Context, uuid string) {
runTaskWithStdin(ctx, []string{"uuid:" + uuid, "delete"}, "yes\n")
}
func listTasksWithTag(ctx context.Context, tag string) []askcli.TaskExport {
stdout, _, _ := runTask(ctx, []string{"export", "project:hexai", "+agent"})
var tasks []askcli.TaskExport
if err := json.Unmarshal(stdout.Bytes(), &tasks); err != nil {
return nil
}
var filtered []askcli.TaskExport
for _, t := range tasks {
if t.Status == "deleted" || t.Status == "completed" {
continue
}
for _, t2 := range t.Tags {
if t2 == tag {
filtered = append(filtered, t)
break
}
}
}
return filtered
}
type taskInfo struct {
ID string
UUID string
Description string
Status string
Started string
StartTime string
Priority string
Depends []string
Tags []string
}
var (
idFieldRx = regexp.MustCompile(`ID:\s+(.+)`)
uuidFieldRx = regexp.MustCompile(`UUID:\s+(.+)`)
descFieldRx = regexp.MustCompile(`Description:\s+(.+)`)
statusFieldRx = regexp.MustCompile(`Status:\s+(.+)`)
startedFieldRx = regexp.MustCompile(`Started:\s+(.+)`)
startTimeFieldRx = regexp.MustCompile(`Start time:\s+(.+)`)
priorityFieldRx = regexp.MustCompile(`Priority:\s+(.+)`)
dependsFieldRx = regexp.MustCompile(`Depends:\s+(.+)`)
tagsFieldRx = regexp.MustCompile(`Tags:\s+(.+)`)
uuidFormatRx = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
)
func parseTaskInfoText(output string, uuid string) taskInfo {
ti := taskInfo{UUID: uuid}
if m := idFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.ID = strings.TrimSpace(m[1])
}
if m := uuidFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.UUID = strings.TrimSpace(m[1])
}
if m := descFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.Description = strings.TrimSpace(m[1])
}
if m := statusFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.Status = strings.TrimSpace(m[1])
}
if m := startedFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.Started = strings.TrimSpace(m[1])
}
if m := startTimeFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.StartTime = strings.TrimSpace(m[1])
}
if m := priorityFieldRx.FindStringSubmatch(output); len(m) > 1 {
ti.Priority = strings.TrimSpace(m[1])
}
if m := dependsFieldRx.FindStringSubmatch(output); len(m) > 1 {
depStr := strings.TrimSpace(m[1])
if depStr != "" {
ti.Depends = strings.Split(depStr, ", ")
}
}
if m := tagsFieldRx.FindStringSubmatch(output); len(m) > 1 {
tagStr := strings.TrimSpace(m[1])
ti.Tags = strings.Split(tagStr, ", ")
}
return ti
}
func getTaskInfoFast(ctx context.Context, uuid string) (taskInfo, bool) {
stdout, _, code := runAsk(ctx, []string{"info", uuid})
if code != 0 {
return taskInfo{}, false
}
return parseTaskInfoText(stdout.String(), uuid), true
}
// getTaskInfoRaw returns the raw text output of ask info for a given UUID.
func getTaskInfoRaw(ctx context.Context, uuid string) (string, bool) {
stdout, _, code := runAsk(ctx, []string{"info", uuid})
if code != 0 {
return "", false
}
return stdout.String(), true
}
func mustTaskAlias(t *testing.T, ctx context.Context, uuid string) string {
t.Helper()
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("failed to get task info for %s", uuid)
}
if ti.ID == "" {
t.Fatalf("task info for %s did not include an alias ID", uuid)
}
return ti.ID
}
func aliasCachePath(t *testing.T, cacheRoot string) string {
t.Helper()
return filepath.Join(cacheRoot, "hexai", "ask", "task-aliases-v1.json")
}
func TestMain(m *testing.M) {
repoRoot = findRepoRoot()
if repoRoot == "" {
fmt.Fprintln(os.Stderr, "integration tests: cannot find repo root (go.mod or .git)")
os.Exit(1)
}
// Always rebuild the binary so tests reflect the current source.
askBin := askBinaryPath()
cmd := exec.Command("go", "build", "-o", askBin, "./cmd/ask/")
cmd.Dir = repoRoot
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "failed to build ask binary: %v\n%s\n", err, out)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestAdd(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for add")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
tasks := listTasksWithTag(ctx, "integrationtest")
found := false
for _, task := range tasks {
if task.UUID == uuid {
found = true
break
}
}
if !found {
t.Errorf("task %s not found in export", uuid)
}
}
// TestAddReturnsAlias verifies that ask add outputs the human-facing alias ID.
func TestAddReturnsAlias(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
stdout, _, code := runAsk(ctx, []string{"add", "+integrationtest", "uuid format check"})
if code != 0 {
t.Fatalf("ask add failed with code %d", code)
}
id := strings.TrimSpace(stdout.String())
info, ok := getTaskInfoFast(ctx, id)
if !ok {
t.Fatalf("ask info %q failed after add", id)
}
defer deleteTask(ctx, info.UUID)
if id == "" {
t.Fatal("ask add returned an empty task ID")
}
if uuidFormatRx.MatchString(id) {
t.Fatalf("ask add output %q leaked a UUID, want alias ID", id)
}
if info.ID != id {
t.Fatalf("ask info ID = %q, want %q", info.ID, id)
}
if !uuidFormatRx.MatchString(info.UUID) {
t.Fatalf("ask info UUID = %q, want valid UUID", info.UUID)
}
}
func TestList(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cacheRoot := t.TempDir()
t.Setenv("XDG_CACHE_HOME", cacheRoot)
uuid, err := createTask(ctx, "integration test task for list")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"list"})
if code != 0 {
t.Fatalf("list failed with code %d: %s", code, stdout.String())
}
alias := mustTaskAlias(t, ctx, uuid)
if !strings.Contains(stdout.String(), alias) {
t.Errorf("list output does not contain expected alias %q", alias)
}
if strings.Contains(stdout.String(), uuid) {
t.Errorf("list output should not contain raw UUID %s", uuid)
}
if !strings.Contains(stdout.String(), "integration test task for list") {
t.Errorf("list output does not contain expected task description")
}
}
func TestAll(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for all")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"all"})
if code != 0 {
t.Fatalf("all failed with code %d: %s", code, stdout.String())
}
if !strings.Contains(stdout.String(), "integration test task for all") {
t.Errorf("all output does not contain expected task description")
}
}
func TestReady(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for ready")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"ready"})
if code != 0 {
t.Fatalf("ready failed with code %d: %s", code, stdout.String())
}
if !strings.Contains(stdout.String(), "integration test task for ready") {
t.Errorf("ready output does not contain expected task description")
}
}
func TestInfo(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
uuid, err := createTask(ctx, "integration test task for info")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("info failed or returned no output")
}
if ti.UUID != uuid {
t.Errorf("info uuid mismatch: got %s, want %s", ti.UUID, uuid)
}
if ti.ID == "" {
t.Errorf("info output missing alias ID")
}
if !strings.Contains(ti.Description, "integration test task for info") {
t.Errorf("info description mismatch: %s", ti.Description)
}
aliasOutput, ok := getTaskInfoRaw(ctx, ti.ID)
if !ok {
t.Fatalf("info by alias failed")
}
if !strings.Contains(aliasOutput, "ID: "+ti.ID) {
t.Errorf("info by alias output missing alias line: %s", aliasOutput)
}
if !strings.Contains(aliasOutput, "UUID: "+uuid) {
t.Errorf("info by alias output missing uuid line: %s", aliasOutput)
}
}
func TestInfoShowsAllDependencies(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
dependency1, err := createTask(ctx, "integration test info dependency one")
if err != nil {
t.Fatalf("failed to create first dependency task: %v", err)
}
defer deleteTask(ctx, dependency1)
dependency2, err := createTask(ctx, "integration test info dependency two")
if err != nil {
t.Fatalf("failed to create second dependency task: %v", err)
}
defer deleteTask(ctx, dependency2)
dependent, err := createTask(ctx, "integration test task for info dependencies")
if err != nil {
t.Fatalf("failed to create dependent task: %v", err)
}
defer deleteTask(ctx, dependent)
if stdout, stderr, code := runAsk(ctx, []string{"dep", "add", dependent, dependency2}); code != 0 {
t.Fatalf("dep add for second dependency failed with code %d: stdout=%s stderr=%s", code, stdout.String(), stderr.String())
}
if stdout, stderr, code := runAsk(ctx, []string{"dep", "add", dependent, dependency1}); code != 0 {
t.Fatalf("dep add for first dependency failed with code %d: stdout=%s stderr=%s", code, stdout.String(), stderr.String())
}
ti, ok := getTaskInfoFast(ctx, dependent)
if !ok {
t.Fatalf("info failed for task with dependencies")
}
if len(ti.Depends) != 2 {
t.Fatalf("info dependencies count = %d, want 2: %+v", len(ti.Depends), ti.Depends)
}
alias1 := mustTaskAlias(t, ctx, dependency1)
alias2 := mustTaskAlias(t, ctx, dependency2)
wantDepends := []string{
alias1 + " (" + dependency1 + ")",
alias2 + " (" + dependency2 + ")",
}
slices.Sort(wantDepends)
if !slices.Equal(ti.Depends, wantDepends) {
t.Fatalf("info dependencies = %+v, want %+v", ti.Depends, wantDepends)
}
}
func TestAnnotate(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for annotate")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
note := "this is a test annotation"
stdout, _, code := runAsk(ctx, []string{"annotate", uuid, note})
if code != 0 {
t.Fatalf("annotate failed with code %d: %s", code, stdout.String())
}
raw, ok := getTaskInfoRaw(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after annotate")
}
if !strings.Contains(raw, note) {
t.Errorf("annotation text %q not found in task info output:\n%s", note, raw)
}
}
func TestStart(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for start")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"start", uuid})
if code != 0 {
t.Fatalf("start failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after start")
}
if ti.Started != "yes" {
t.Errorf("task started state = %q, want yes", ti.Started)
}
if ti.StartTime == "" {
t.Errorf("task start time is empty after start")
}
}
func TestStop(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for stop")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
runAsk(ctx, []string{"start", uuid})
stdout, _, code := runAsk(ctx, []string{"stop", uuid})
if code != 0 {
t.Fatalf("stop failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after stop")
}
if ti.Started != "no" {
t.Errorf("task started state = %q, want no", ti.Started)
}
if ti.StartTime != "" {
t.Errorf("task start time should be empty after stop: %s", ti.StartTime)
}
}
func TestDone(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for done")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
stdout, _, code := runAsk(ctx, []string{"done", uuid})
if code != 0 {
t.Fatalf("done failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after done")
}
if strings.ToLower(ti.Status) != "completed" {
t.Errorf("task status = %s, want completed", ti.Status)
}
deleteTask(ctx, uuid)
}
func TestPriority(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for priority")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"priority", uuid, "H"})
if code != 0 {
t.Fatalf("priority failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after priority")
}
if ti.Priority != "H" {
t.Errorf("task priority = %s, want H", ti.Priority)
}
}
func TestTag(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for tag")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"tag", uuid, "+cli"})
if code != 0 {
t.Fatalf("tag add failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after tag")
}
found := false
for _, tg := range ti.Tags {
if tg == "cli" {
found = true
break
}
}
if !found {
t.Errorf("tag cli not found on task: %+v", ti.Tags)
}
runAsk(ctx, []string{"tag", uuid, "-cli"})
ti2, _ := getTaskInfoFast(ctx, uuid)
for _, tg := range ti2.Tags {
if tg == "cli" {
t.Errorf("tag cli should have been removed")
break
}
}
}
func TestDepAdd(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid1, err := createTask(ctx, "integration test dep target")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid1)
uuid2, err := createTask(ctx, "integration test dep dependent")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid2)
stdout, _, code := runAsk(ctx, []string{"dep", "add", uuid2, uuid1})
if code != 0 {
t.Fatalf("dep add failed with code %d: %s", code, stdout.String())
}
tasks := listTasksWithTag(ctx, "integrationtest")
for _, task := range tasks {
if task.UUID == uuid2 {
found := false
for _, dep := range task.Depends {
if dep == uuid1 {
found = true
break
}
}
if !found {
t.Errorf("dependency %s not found on task", uuid1)
}
break
}
}
}
func TestDepList(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
uuid1, err := createTask(ctx, "integration test dep list target")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid1)
uuid2, err := createTask(ctx, "integration test dep list dependent")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid2)
runAsk(ctx, []string{"dep", "add", uuid2, uuid1})
stdout, _, code := runAsk(ctx, []string{"dep", "list", uuid2})
if code != 0 {
t.Fatalf("dep list failed with code %d: %s", code, stdout.String())
}
alias1 := mustTaskAlias(t, ctx, uuid1)
if !strings.Contains(stdout.String(), alias1) {
t.Errorf("dep list output does not contain target alias %q: %s", alias1, stdout.String())
}
if strings.Contains(stdout.String(), uuid1) {
t.Errorf("dep list output should not contain raw target uuid %s: %s", uuid1, stdout.String())
}
}
func TestDepRm(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid1, err := createTask(ctx, "integration test dep rm target")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid1)
uuid2, err := createTask(ctx, "integration test dep rm dependent")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid2)
runAsk(ctx, []string{"dep", "add", uuid2, uuid1})
stdout, _, code := runAsk(ctx, []string{"dep", "rm", uuid2, uuid1})
if code != 0 {
t.Fatalf("dep rm failed with code %d: %s", code, stdout.String())
}
tasks := listTasksWithTag(ctx, "integrationtest")
for _, task := range tasks {
if task.UUID == uuid2 {
for _, dep := range task.Depends {
if dep == uuid1 {
t.Errorf("dependency should have been removed")
break
}
}
break
}
}
}
func TestModify(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for modify")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"modify", uuid, "priority:H"})
if code != 0 {
t.Fatalf("modify failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok {
t.Fatalf("could not get task info after modify")
}
if ti.Priority != "H" {
t.Errorf("task priority = %s, want H", ti.Priority)
}
}
func TestDenotate(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for denotate")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
note := "annotation to remove"
runAsk(ctx, []string{"annotate", uuid, note})
// Verify the annotation is present before denotating.
rawBefore, _ := getTaskInfoRaw(ctx, uuid)
if !strings.Contains(rawBefore, note) {
t.Fatalf("annotation %q not found before denotate", note)
}
_, _, code := runAsk(ctx, []string{"denotate", uuid, note})
if code != 0 {
t.Fatalf("denotate returned non-zero code: %d", code)
}
// Verify the annotation is gone after denotating.
rawAfter, _ := getTaskInfoRaw(ctx, uuid)
if strings.Contains(rawAfter, note) {
t.Errorf("annotation %q still present after denotate", note)
}
}
func TestDelete(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for delete")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
// delete forwards stdin to taskwarrior for confirmation.
stdout, _, code := runAskWithStdin(ctx, []string{"delete", uuid}, "yes\n")
if code != 0 {
t.Fatalf("delete failed with code %d: %s", code, stdout.String())
}
tasks := listTasksWithTag(ctx, "integrationtest")
for _, task := range tasks {
if task.UUID == uuid {
t.Errorf("task should have been deleted but still exists")
break
}
}
}
func TestUrgency(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test task for urgency")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{"urgency"})
if code != 0 {
t.Fatalf("urgency failed with code %d: %s", code, stdout.String())
}
if !strings.Contains(stdout.String(), "integration test task for urgency") {
t.Errorf("urgency output does not contain expected task description")
}
}
func TestDefaultCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
uuid, err := createTask(ctx, "integration test for default command")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, _, code := runAsk(ctx, []string{})
if code != 0 {
t.Fatalf("default command (list) failed with code %d: %s", code, stdout.String())
}
if !strings.Contains(stdout.String(), "integration test for default command") {
t.Errorf("default command output does not contain expected task description")
}
}
func TestHelp(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stdout, _, code := runAsk(ctx, []string{"help"})
if code != 0 {
t.Fatalf("help returned non-zero exit code %d", code)
}
out := stdout.String()
for _, sub := range []string{"add", "list", "info", "start", "done", "delete", "annotate", "dep"} {
if !strings.Contains(out, sub) {
t.Errorf("help output missing subcommand %q", sub)
}
}
}
func TestFish(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stdout, stderr, code := runAsk(ctx, []string{"fish"})
if code != 0 {
t.Fatalf("fish returned non-zero exit code %d: stderr=%s", code, stderr.String())
}
out := stdout.String()
for _, fragment := range []string{
"# Source with: ask fish | source",
"complete -c",
"complete-uuids",
"annotate",
"delete",
} {
if !strings.Contains(out, fragment) {
t.Errorf("fish output missing %q", fragment)
}
}
if stderr.Len() != 0 {
t.Errorf("fish wrote unexpected stderr: %s", stderr.String())
}
}
func TestFishRejectsExtraArgs(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stdout, stderr, code := runAsk(ctx, []string{"fish", "extra"})
if code == 0 {
t.Fatalf("expected non-zero exit code for fish extra args, got 0")
}
if stdout.Len() != 0 {
t.Errorf("fish with extra args wrote unexpected stdout: %s", stdout.String())
}
if !strings.Contains(stderr.String(), "usage: ask fish") {
t.Errorf("fish with extra args stderr missing usage text: %s", stderr.String())
}
}
func TestCompleteUUIDs(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
uuid, err := createTask(ctx, "integration test task for complete-uuids")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
stdout, stderr, code := runAsk(ctx, []string{"complete-uuids"})
if code != 0 {
t.Fatalf("complete-uuids returned non-zero exit code %d: stderr=%s", code, stderr.String())
}
alias := mustTaskAlias(t, ctx, uuid)
if !strings.Contains(stdout.String(), alias) {
t.Errorf("complete-uuids output does not contain created task alias %s", alias)
}
if !strings.Contains(stdout.String(), uuid) {
t.Errorf("complete-uuids output does not contain created task UUID %s", uuid)
}
}
func TestAliasSelectorsAcrossUUIDCommands(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
t.Setenv("XDG_CACHE_HOME", t.TempDir())
uuid, err := createTask(ctx, "integration test task for alias selectors")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
alias := mustTaskAlias(t, ctx, uuid)
infoOut, ok := getTaskInfoRaw(ctx, alias)
if !ok {
t.Fatalf("info by alias failed")
}
if !strings.Contains(infoOut, "UUID: "+uuid) {
t.Fatalf("info by alias did not resolve the task: %s", infoOut)
}
note := "integration alias annotation"
stdout, _, code := runAsk(ctx, []string{"annotate", alias, note})
if code != 0 {
t.Fatalf("annotate by alias failed with code %d: %s", code, stdout.String())
}
if strings.TrimSpace(stdout.String()) != "ok "+alias {
t.Fatalf("annotate output = %q, want ok %s", stdout.String(), alias)
}
raw, ok := getTaskInfoRaw(ctx, uuid)
if !ok || !strings.Contains(raw, note) {
t.Fatalf("annotation %q not found after alias annotate: %s", note, raw)
}
note2 := "remove me via alias"
if _, _, code = runAsk(ctx, []string{"annotate", alias, note2}); code != 0 {
t.Fatalf("setup annotate for denotate failed with code %d", code)
}
stdout, _, code = runAsk(ctx, []string{"denotate", alias, note2})
if code != 0 {
t.Fatalf("denotate by alias failed with code %d: %s", code, stdout.String())
}
raw, ok = getTaskInfoRaw(ctx, uuid)
if !ok || strings.Contains(raw, note2) {
t.Fatalf("annotation %q still present after alias denotate: %s", note2, raw)
}
stdout, _, code = runAsk(ctx, []string{"start", alias})
if code != 0 {
t.Fatalf("start by alias failed with code %d: %s", code, stdout.String())
}
ti, ok := getTaskInfoFast(ctx, uuid)
if !ok || ti.Started != "yes" {
t.Fatalf("task not started after alias start: %+v", ti)
}
stdout, _, code = runAsk(ctx, []string{"stop", alias})
if code != 0 {
t.Fatalf("stop by alias failed with code %d: %s", code, stdout.String())
}
ti, ok = getTaskInfoFast(ctx, uuid)
if !ok || ti.Started != "no" {
t.Fatalf("task not stopped after alias stop: %+v", ti)
}
stdout, _, code = runAsk(ctx, []string{"priority", alias, "H"})
if code != 0 {
t.Fatalf("priority by alias failed with code %d: %s", code, stdout.String())
}
ti, ok = getTaskInfoFast(ctx, uuid)
if !ok || ti.Priority != "H" {
t.Fatalf("task priority not updated after alias priority: %+v", ti)
}
stdout, _, code = runAsk(ctx, []string{"modify", alias, "priority:L"})
if code != 0 {
t.Fatalf("modify by alias failed with code %d: %s", code, stdout.String())
}
ti, ok = getTaskInfoFast(ctx, uuid)
if !ok || ti.Priority != "L" {
t.Fatalf("task priority not updated after alias modify: %+v", ti)
}
stdout, _, code = runAsk(ctx, []string{"tag", alias, "+aliascheck"})
if code != 0 {
t.Fatalf("tag by alias failed with code %d: %s", code, stdout.String())
}
ti, ok = getTaskInfoFast(ctx, uuid)
if !ok || !slices.Contains(ti.Tags, "aliascheck") {
t.Fatalf("tag not added after alias tag: %+v", ti)
}
depUUID, err := createTask(ctx, "integration test task dependency alias target")
if err != nil {
t.Fatalf("failed to create dependency task: %v", err)
}
defer deleteTask(ctx, depUUID)
depAlias := mustTaskAlias(t, ctx, depUUID)
stdout, _, code = runAsk(ctx, []string{"dep", "add", alias, depAlias})
if code != 0 {
t.Fatalf("dep add by alias failed with code %d: %s", code, stdout.String())
}
stdout, _, code = runAsk(ctx, []string{"dep", "list", alias})
if code != 0 {
t.Fatalf("dep list by alias failed with code %d: %s", code, stdout.String())
}
if !strings.Contains(stdout.String(), depAlias) || strings.Contains(stdout.String(), depUUID) {
t.Fatalf("dep list by alias output = %q, want alias %q without raw UUID", stdout.String(), depAlias)
}
stdout, _, code = runAsk(ctx, []string{"dep", "rm", alias, depAlias})
if code != 0 {
t.Fatalf("dep rm by alias failed with code %d: %s", code, stdout.String())
}
stdout, _, code = runAsk(ctx, []string{"dep", "list", alias})
if code != 0 {
t.Fatalf("dep list after rm failed with code %d: %s", code, stdout.String())
}
if strings.TrimSpace(stdout.String()) != "no dependencies" {
t.Fatalf("dep list after rm = %q, want no dependencies", stdout.String())
}
doneUUID, err := createTask(ctx, "integration test alias done")
if err != nil {
t.Fatalf("failed to create done task: %v", err)
}
doneAlias := mustTaskAlias(t, ctx, doneUUID)
stdout, _, code = runAsk(ctx, []string{"done", doneAlias})
if code != 0 {
t.Fatalf("done by alias failed with code %d: %s", code, stdout.String())
}
doneInfo, ok := getTaskInfoFast(ctx, doneUUID)
if !ok || strings.ToLower(doneInfo.Status) != "completed" {
t.Fatalf("done task not completed after alias done: %+v", doneInfo)
}
deleteTask(ctx, doneUUID)
deleteUUID, err := createTask(ctx, "integration test alias delete")
if err != nil {
t.Fatalf("failed to create delete task: %v", err)
}
deleteAlias := mustTaskAlias(t, ctx, deleteUUID)
stdout, _, code = runAskWithStdin(ctx, []string{"delete", deleteAlias}, "yes\n")
if code != 0 {
t.Fatalf("delete by alias failed with code %d: %s", code, stdout.String())
}
tasks := listTasksWithTag(ctx, "integrationtest")
for _, task := range tasks {
if task.UUID == deleteUUID {
t.Fatalf("task %s still exists after alias delete", deleteUUID)
}
}
}
func TestAliasCachePrunesExpiredEntriesOlderThan120Days(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cacheRoot := t.TempDir()
t.Setenv("XDG_CACHE_HOME", cacheRoot)
uuid, err := createTask(ctx, "integration test alias cache pruning")
if err != nil {
t.Fatalf("failed to create task: %v", err)
}
defer deleteTask(ctx, uuid)
cachePath := aliasCachePath(t, cacheRoot)
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatalf("MkdirAll(%s): %v", cachePath, err)
}
seed := `{
"next_id": 37,
"entries": [
{
"uuid": "expired-task",
"alias": "z",
"created_at": "2025-01-01T00:00:00Z",
"last_accessed_at": "2025-01-01T00:00:00Z"
}
]
}`
if err := os.WriteFile(cachePath, []byte(seed), 0o600); err != nil {
t.Fatalf("WriteFile(%s): %v", cachePath, err)
}
stdout, stderr, code := runAsk(ctx, []string{"info", uuid})
if code != 0 {
t.Fatalf("info failed with code %d: stdout=%s stderr=%s", code, stdout.String(), stderr.String())
}
if strings.Contains(stdout.String(), "ID: z\n") {
t.Fatalf("info output still contains pruned alias z: %q", stdout.String())
}
if !strings.Contains(stdout.String(), "ID: 01\n") {
t.Fatalf("info output did not allocate the next monotonic alias 01: %q", stdout.String())
}
data, err := os.ReadFile(cachePath)
if err != nil {
t.Fatalf("ReadFile(%s): %v", cachePath, err)
}
if strings.Contains(string(data), "expired-task") {
t.Fatalf("expired cache entry was not pruned: %s", string(data))
}
if !strings.Contains(string(data), `"next_id": 38`) {
t.Fatalf("cache next_id was not advanced after pruning and allocation: %s", string(data))
}
}
func TestUnknownCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, stderr, code := runAsk(ctx, []string{"notacommand"})
if code == 0 {
t.Fatalf("expected non-zero exit code for unknown command, got 0")
}
if !strings.Contains(stderr.String(), "notacommand") {
t.Errorf("error output does not mention unknown command: %s", stderr.String())
}
}
|