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
|
package askcli
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"testing"
)
func TestHandleProjects_ListsUniqueProjects(t *testing.T) {
d := NewDispatcher(nil)
d.findTaskBinary = func() (string, error) { return "task", nil }
d.runTaskCommand = func(ctx context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
tasks := []TaskExport{
{UUID: "1", Project: "hexai", Status: "pending", Urgency: 1},
{UUID: "2", Project: "dtail", Status: "pending", Urgency: 2, Start: "2026-01-01T00:00:00Z"},
{UUID: "3", Project: "hexai", Status: "pending", Urgency: 3},
{UUID: "4", Project: "", Status: "pending", Urgency: 4},
}
_, _ = io.WriteString(stdout, taskExportJSON(tasks))
return nil
}
ctx := context.Background()
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"projects"}, nil, &stdout, &stderr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if code != 0 {
t.Fatalf("unexpected exit code: %d", code)
}
lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
if len(lines) != 1 || lines[0] != "hexai" {
t.Fatalf("expected [hexai], got %q", lines)
}
}
func TestHandleProjects_JSONOutput(t *testing.T) {
d := NewDispatcher(nil)
d.findTaskBinary = func() (string, error) { return "task", nil }
d.runTaskCommand = func(ctx context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
tasks := []TaskExport{
{UUID: "1", Project: "hexai", Status: "pending", Urgency: 1},
{UUID: "2", Project: "dtail", Status: "pending", Urgency: 2},
}
_, _ = io.WriteString(stdout, taskExportJSON(tasks))
return nil
}
ctx := context.Background()
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"--json", "projects"}, nil, &stdout, &stderr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if code != 0 {
t.Fatalf("unexpected exit code: %d", code)
}
if !strings.Contains(stdout.String(), `"dtail"`) || !strings.Contains(stdout.String(), `"hexai"`) {
t.Fatalf("unexpected JSON output: %s", stdout.String())
}
}
func TestHandleProjects_EmptyResult(t *testing.T) {
d := NewDispatcher(nil)
d.findTaskBinary = func() (string, error) { return "task", nil }
d.runTaskCommand = func(ctx context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
_, _ = io.WriteString(stdout, "[]")
return nil
}
ctx := context.Background()
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"projects"}, nil, &stdout, &stderr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if code != 0 {
t.Fatalf("unexpected exit code: %d", code)
}
if stdout.String() != "" {
t.Fatalf("expected no output, got %q", stdout.String())
}
}
func TestHandleProjects_ForwardsTaskExportError(t *testing.T) {
d := NewDispatcher(nil)
d.findTaskBinary = func() (string, error) { return "task", nil }
d.runTaskCommand = func(ctx context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
return fmt.Errorf("some error")
}
ctx := context.Background()
var stdout, stderr bytes.Buffer
code, err := d.Dispatch(ctx, []string{"projects"}, nil, &stdout, &stderr)
if err == nil {
t.Fatal("expected error")
}
if code != 1 {
t.Fatalf("expected exit code 1, got %d", code)
}
}
func taskExportJSON(tasks []TaskExport) string {
data, err := json.Marshal(tasks)
if err != nil {
panic(err)
}
return string(data)
}
|