summaryrefslogtreecommitdiff
path: root/internal/askcli/command_start_deps_test.go
blob: 9ba197b5583b0a97ed7d6f66e2dd1eeb194c659c (plain)
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
package askcli

import (
	"bytes"
	"context"
	"io"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

func TestHandleStart_BlockedWhenDependencyNotCompleted(t *testing.T) {
	dir := t.TempDir()
	oldRoot := taskAliasCacheRoot
	oldNow := nowTaskAliasCache
	fixedNow := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC)
	taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
	nowTaskAliasCache = func() time.Time { return fixedNow }
	defer func() {
		taskAliasCacheRoot = oldRoot
		nowTaskAliasCache = oldNow
	}()

	writeTaskAliasCacheForTest(t, taskAliasCache{
		NextID: 2,
		Entries: []taskAliasCacheEntry{
			{UUID: "main-uuid", Alias: "0", CreatedAt: fixedNow},
			{UUID: "dep-uuid", Alias: "1", CreatedAt: fixedNow},
		},
	})

	var startCalls int
	d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
		switch {
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"main-uuid","description":"Main","status":"pending","priority":"M","tags":[],"urgency":0,"depends":["dep-uuid"]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:dep-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"dep-uuid","description":"Dep","status":"pending","priority":"M","tags":[],"urgency":0,"depends":[]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "start":
			startCalls++
			return 0, nil
		default:
			t.Fatalf("unexpected runner args: %v", args)
			return 1, nil
		}
	}})

	var stdout, stderr bytes.Buffer
	code, _ := d.Dispatch(context.Background(), []string{"start", "main-uuid"}, &bytes.Buffer{}, &stdout, &stderr)
	if code != 1 {
		t.Fatalf("start code = %d, want 1", code)
	}
	if startCalls != 0 {
		t.Fatalf("task start ran %d times, want 0", startCalls)
	}
	if !strings.Contains(stderr.String(), "cannot start until all dependencies are completed") {
		t.Fatalf("stderr = %q, want dependency gate message", stderr.String())
	}
	if !strings.Contains(stderr.String(), "1 (pending)") {
		t.Fatalf("stderr should name incomplete dependency with alias and status: %q", stderr.String())
	}
}

func TestHandleStart_AllowedWhenDependencyCompleted(t *testing.T) {
	dir := t.TempDir()
	oldRoot := taskAliasCacheRoot
	oldNow := nowTaskAliasCache
	fixedNow := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC)
	taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
	nowTaskAliasCache = func() time.Time { return fixedNow }
	defer func() {
		taskAliasCacheRoot = oldRoot
		nowTaskAliasCache = oldNow
	}()

	writeTaskAliasCacheForTest(t, taskAliasCache{
		NextID: 2,
		Entries: []taskAliasCacheEntry{
			{UUID: "main-uuid", Alias: "0", CreatedAt: fixedNow},
			{UUID: "dep-uuid", Alias: "1", CreatedAt: fixedNow},
		},
	})

	d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
		switch {
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"main-uuid","description":"Main","status":"pending","priority":"M","tags":[],"urgency":0,"depends":["dep-uuid"]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:dep-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"dep-uuid","description":"Dep","status":"completed","priority":"M","tags":[],"urgency":0,"depends":[]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "start":
			return 0, nil
		default:
			t.Fatalf("unexpected runner args: %v", args)
			return 1, nil
		}
	}})

	var stdout, stderr bytes.Buffer
	code, _ := d.Dispatch(context.Background(), []string{"start", "main-uuid"}, &bytes.Buffer{}, &stdout, &stderr)
	if code != 0 {
		t.Fatalf("start code = %d stderr=%q", code, stderr.String())
	}
	if !strings.Contains(stdout.String(), "ok 0") {
		t.Fatalf("stdout = %q, want ok + alias", stdout.String())
	}
}

func TestHandleStart_CompletedStatusIsCaseInsensitive(t *testing.T) {
	dir := t.TempDir()
	oldRoot := taskAliasCacheRoot
	oldNow := nowTaskAliasCache
	fixedNow := time.Date(2026, 3, 26, 12, 0, 0, 0, time.UTC)
	taskAliasCacheRoot = func() (string, error) { return filepath.Join(dir, "hexai"), nil }
	nowTaskAliasCache = func() time.Time { return fixedNow }
	defer func() {
		taskAliasCacheRoot = oldRoot
		nowTaskAliasCache = oldNow
	}()

	writeTaskAliasCacheForTest(t, taskAliasCache{
		NextID: 2,
		Entries: []taskAliasCacheEntry{
			{UUID: "main-uuid", Alias: "0", CreatedAt: fixedNow},
			{UUID: "dep-uuid", Alias: "1", CreatedAt: fixedNow},
		},
	})

	d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
		switch {
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"main-uuid","description":"Main","status":"pending","priority":"M","tags":[],"urgency":0,"depends":["dep-uuid"]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:dep-uuid" && args[1] == "export":
			_, _ = io.WriteString(stdout, `[{"uuid":"dep-uuid","description":"Dep","status":"COMPLETED","priority":"M","tags":[],"urgency":0,"depends":[]}]`)
			return 0, nil
		case len(args) == 2 && args[0] == "uuid:main-uuid" && args[1] == "start":
			return 0, nil
		default:
			t.Fatalf("unexpected runner args: %v", args)
			return 1, nil
		}
	}})

	var stdout, stderr bytes.Buffer
	code, _ := d.Dispatch(context.Background(), []string{"start", "main-uuid"}, &bytes.Buffer{}, &stdout, &stderr)
	if code != 0 {
		t.Fatalf("start code = %d stderr=%q", code, stderr.String())
	}
	if !strings.Contains(stdout.String(), "ok 0") {
		t.Fatalf("stdout = %q, want ok + alias", stdout.String())
	}
}