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
|
package askcli
import (
"bytes"
"context"
"io"
"path/filepath"
"strings"
"testing"
"time"
)
func TestHandleList_Success(t *testing.T) {
dir := t.TempDir()
oldRoot := taskAliasCacheRoot
oldNow := nowTaskAliasCache
taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) }
defer func() {
taskAliasCacheRoot = oldRoot
nowTaskAliasCache = oldNow
}()
writeTaskAliasCacheForTest(t, taskAliasCache{
NextID: 2,
Entries: []taskAliasCacheEntry{
{UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()},
{UUID: "uuid-2", Alias: "1", CreatedAt: nowTaskAliasCache()},
},
})
jsonData := `[{"uuid":"uuid-1","description":"Task 1","status":"pending","priority":"H","tags":["cli"],"start":"2026-03-26T10:00:00Z","urgency":15.0,"depends":[]},{"uuid":"uuid-2","description":"Task 2","status":"completed","priority":"M","tags":["agent"],"urgency":10.0,"depends":[]}]`
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
for _, arg := range args {
if arg == "export" {
_, _ = io.WriteString(stdout, jsonData)
return 0, nil
}
}
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("list code = %d, want 0", code)
}
output := stdout.String()
if !strings.Contains(output, "ID") || strings.Contains(output, "UUID") {
t.Fatalf("output should use ID column: %s", output)
}
if !strings.Contains(output, "0") || !strings.Contains(output, "1") || strings.Contains(output, "uuid-1") || strings.Contains(output, "uuid-2") {
t.Fatalf("output missing aliases or leaking UUIDs: %s", output)
}
if !strings.Contains(output, "Started") || !strings.Contains(output, "yes") || !strings.Contains(output, "no") {
t.Fatalf("output missing explicit started state: %s", output)
}
}
func TestHandleList_SortedByPriority(t *testing.T) {
dir := t.TempDir()
oldRoot := taskAliasCacheRoot
oldNow := nowTaskAliasCache
taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) }
defer func() {
taskAliasCacheRoot = oldRoot
nowTaskAliasCache = oldNow
}()
writeTaskAliasCacheForTest(t, taskAliasCache{
NextID: 2,
Entries: []taskAliasCacheEntry{
{UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()},
{UUID: "uuid-2", Alias: "1", CreatedAt: nowTaskAliasCache()},
},
})
jsonData := `[{"uuid":"uuid-2","description":"Task 2","status":"pending","priority":"M","tags":[],"urgency":10.0,"depends":[]},{"uuid":"uuid-1","description":"Task 1","status":"pending","priority":"H","tags":[],"urgency":5.0,"depends":[]}]`
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
for _, arg := range args {
if arg == "export" {
_, _ = io.WriteString(stdout, jsonData)
return 0, nil
}
}
return 0, nil
}})
var stdout bytes.Buffer
d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &bytes.Buffer{})
output := stdout.String()
lines := strings.Split(strings.TrimSpace(output), "\n")
taskLine1 := lines[2]
if !strings.Contains(taskLine1, "0") || strings.Contains(taskLine1, "uuid-1") {
t.Fatalf("first task should be H priority alias 0, got: %s", taskLine1)
}
}
func TestHandleList_EmptyList(t *testing.T) {
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
for _, arg := range args {
if arg == "export" {
_, _ = io.WriteString(stdout, "[]")
return 0, nil
}
}
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"list"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("list code = %d, want 0 for empty list", code)
}
}
func TestHandleAll_Success(t *testing.T) {
dir := t.TempDir()
oldRoot := taskAliasCacheRoot
oldNow := nowTaskAliasCache
taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) }
defer func() {
taskAliasCacheRoot = oldRoot
nowTaskAliasCache = oldNow
}()
writeTaskAliasCacheForTest(t, taskAliasCache{
NextID: 1,
Entries: []taskAliasCacheEntry{
{UUID: "uuid-1", Alias: "0", CreatedAt: nowTaskAliasCache()},
},
})
jsonData := `[{"uuid":"uuid-1","description":"Done task","status":"completed","priority":"M","tags":[],"urgency":0.0,"depends":[]}]`
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
for _, arg := range args {
if arg == "export" {
_, _ = io.WriteString(stdout, jsonData)
return 0, nil
}
}
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"all"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("all code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "0") || strings.Contains(stdout.String(), "uuid-1") {
t.Fatalf("output should show alias only: %s", stdout.String())
}
}
func TestHandleReady_Success(t *testing.T) {
dir := t.TempDir()
oldRoot := taskAliasCacheRoot
oldNow := nowTaskAliasCache
taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
nowTaskAliasCache = func() time.Time { return time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC) }
defer func() {
taskAliasCacheRoot = oldRoot
nowTaskAliasCache = oldNow
}()
writeTaskAliasCacheForTest(t, taskAliasCache{
NextID: 1,
Entries: []taskAliasCacheEntry{
{UUID: "uuid-ready", Alias: "0", CreatedAt: nowTaskAliasCache()},
},
})
jsonData := `[{"uuid":"uuid-ready","description":"Ready task","status":"pending","priority":"H","tags":["READY"],"urgency":20.0,"depends":[]}]`
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
for _, arg := range args {
if arg == "export" {
_, _ = io.WriteString(stdout, jsonData)
return 0, nil
}
}
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"ready"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("ready code = %d, want 0", code)
}
if !strings.Contains(stdout.String(), "0") || strings.Contains(stdout.String(), "uuid-ready") {
t.Fatalf("output should show alias only: %s", stdout.String())
}
}
func TestHandleList_PassesFilters(t *testing.T) {
var capturedArgs []string
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
capturedArgs = args
_, _ = io.WriteString(stdout, "[]")
return 0, nil
}})
var stdout, stderr bytes.Buffer
d.Dispatch(context.Background(), []string{"list", "+READY", "limit:5", "sort:priority-"}, nil, &stdout, &stderr)
if len(capturedArgs) < 2 {
t.Fatalf("expected export args, got %v", capturedArgs)
}
hasExport := false
for _, arg := range capturedArgs {
if arg == "export" {
hasExport = true
break
}
}
if !hasExport {
t.Fatalf("expected export in args, got %v", capturedArgs)
}
}
|