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
|
package askcli
import (
"bytes"
"context"
"io"
"strings"
"testing"
)
func TestHandleInfo_Success(t *testing.T) {
jsonData := `[{"uuid":"test-uuid","description":"Test task","status":"pending","priority":"H","tags":["cli","agent"],"urgency":15.0,"depends":["dep-1"],"annotations":[{"description":"Note 1","entry":"2026-03-22T10:00:00Z"}]}]`
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
if args[0] == "uuid" {
io.WriteString(stdout, jsonData)
}
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"info", "test-uuid"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("info code = %d, want 0", code)
}
output := stdout.String()
if !strings.Contains(output, "test-uuid") {
t.Fatalf("output missing UUID: %s", output)
}
if !strings.Contains(output, "H") {
t.Fatalf("output missing priority: %s", output)
}
}
func TestHandleInfo_NumericID(t *testing.T) {
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"info", "123"}, nil, &stdout, &stderr)
if code != 1 {
t.Fatalf("info code = %d, want 1 for numeric ID", code)
}
}
func TestHandleInfo_MissingUUID(t *testing.T) {
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"info"}, nil, &stdout, &stderr)
if code != 1 {
t.Fatalf("info code = %d, want 1 for missing UUID", code)
}
}
func TestHandleAdd_Success(t *testing.T) {
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
io.WriteString(stdout, "Created task 123.\nUUID: abc-123-def")
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"add", "New task description"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("add code = %d, want 0", code)
}
output := stdout.String()
if !strings.Contains(output, "abc-123-def") {
t.Fatalf("output missing UUID: %s", output)
}
}
func TestHandleAdd_MissingDescription(t *testing.T) {
d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
return 0, nil
}})
var stdout, stderr bytes.Buffer
code, _ := d.Dispatch(context.Background(), []string{"add"}, nil, &stdout, &stderr)
if code != 1 {
t.Fatalf("add code = %d, want 1 for missing description", code)
}
}
func TestHandleAdd_MultipleWords(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, "Created task 123.\nUUID: xyz-789")
return 0, nil
}})
var stdout, stderr bytes.Buffer
d.Dispatch(context.Background(), []string{"add", "Multi", "word", "description"}, nil, &stdout, &stderr)
if len(capturedArgs) < 2 || capturedArgs[0] != "add" {
t.Fatalf("capturedArgs = %v, want [add, Multi word description]", capturedArgs)
}
}
|