summaryrefslogtreecommitdiff
path: root/internal/askcli/dispatch_test.go
blob: 91f47844ea56dea3fd74e785bb731596b633466e (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
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
package askcli

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

func TestDispatcher_Help(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"help"}, nil, &stdout, io.Discard)
	if code != 0 {
		t.Fatalf("help exit code = %d, want 0", code)
	}
	if err != nil {
		t.Fatalf("help returned error: %v", err)
	}
	output := stdout.String()
	if !strings.Contains(output, "ask - task management CLI") {
		t.Fatalf("help missing title: %s", output)
	}
	if !strings.Contains(output, "ask list") {
		t.Fatalf("help missing list subcommand: %s", output)
	}
	if !strings.Contains(output, "ask all") {
		t.Fatalf("help missing all subcommand: %s", output)
	}
	if !strings.Contains(output, "ask fish") {
		t.Fatalf("help missing fish subcommand: %s", output)
	}
}

func TestDispatcher_UnknownSubcommand(t *testing.T) {
	d := NewDispatcher(nil)
	var stderr bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"foobar"}, nil, io.Discard, &stderr)
	if code != 1 {
		t.Fatalf("unknown subcommand exit code = %d, want 1", code)
	}
	if err != nil {
		t.Fatalf("unknown subcommand returned unexpected error: %v", err)
	}
	output := stderr.String()
	if !strings.Contains(output, "unknown subcommand") {
		t.Fatalf("unknown subcommand output missing: %s", output)
	}
}

func TestDispatcher_CompleteUUIDsSubcommand(t *testing.T) {
	d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
		if strings.Join(args, " ") != "status:pending export" {
			t.Fatalf("args = %v, want pending export", args)
		}
		_, _ = io.WriteString(stdout, `[{"uuid":"uuid-1"}]`)
		return 0, nil
	}})
	var stdout, stderr bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"complete-uuids"}, nil, &stdout, &stderr)
	if err != nil {
		t.Fatalf("complete-uuids returned error: %v", err)
	}
	if code != 0 {
		t.Fatalf("complete-uuids code = %d, want 0", code)
	}
	if got := stdout.String(); got != "0\nuuid-1\n" {
		t.Fatalf("stdout = %q, want selector list", got)
	}
}

func TestDispatcher_LongHelp(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout bytes.Buffer
	d.Dispatch(context.Background(), []string{"help"}, nil, &stdout, io.Discard)
	output := stdout.String()
	for _, sub := range []string{"add", "list", "all", "ready", "info", "annotate", "start", "stop", "done", "priority", "tag", "dep", "urgency", "modify", "denotate", "delete", "fish"} {
		if !strings.Contains(output, "ask "+sub) {
			t.Errorf("help missing subcommand: ask %s", sub)
		}
	}
}

func TestDispatcher_FishSubcommand(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout, stderr bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"fish"}, nil, &stdout, &stderr)
	if err != nil {
		t.Fatalf("fish returned error: %v", err)
	}
	if code != 0 {
		t.Fatalf("fish code = %d, want 0", code)
	}
	exe, err := os.Executable()
	if err != nil {
		t.Fatalf("os.Executable: %v", err)
	}
	if got := stdout.String(); got != FishCompletionFor(exe) {
		t.Fatalf("fish output mismatch\n--- got ---\n%s\n--- want ---\n%s", got, FishCompletionFor(exe))
	}
	if stderr.Len() != 0 {
		t.Fatalf("fish wrote unexpected stderr: %q", stderr.String())
	}
}

func TestDispatcher_FishSubcommandRejectsExtraArgs(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout, stderr bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"fish", "extra"}, nil, &stdout, &stderr)
	if err != nil {
		t.Fatalf("fish extra args returned error: %v", err)
	}
	if code != 1 {
		t.Fatalf("fish extra args code = %d, want 1", code)
	}
	if stdout.Len() != 0 {
		t.Fatalf("fish extra args wrote unexpected stdout: %q", stdout.String())
	}
	if got := stderr.String(); !strings.Contains(got, "usage: ask fish") {
		t.Fatalf("fish extra args stderr = %q, want usage", got)
	}
}

func TestDispatcher_DispatchPersistsJSONFlagOnDispatcher(t *testing.T) {
	d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
		if strings.Join(args, " ") != "status:pending export" {
			t.Fatalf("args = %v, want list export args", args)
		}
		_, _ = io.WriteString(stdout, `[]`)
		return 0, nil
	}})

	var stdout, stderr bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{"--json", "list"}, nil, &stdout, &stderr)
	if err != nil {
		t.Fatalf("Dispatch returned error: %v", err)
	}
	if code != 0 {
		t.Fatalf("Dispatch code = %d, want 0", code)
	}
	if !d.jsonOutput {
		t.Fatal("Dispatch did not persist jsonOutput on the dispatcher")
	}
	if got := stdout.String(); got != "[]\n" {
		t.Fatalf("stdout = %q, want JSON output", got)
	}
	if stderr.Len() != 0 {
		t.Fatalf("stderr = %q, want empty", stderr.String())
	}
}

func TestParseGlobalFlags(t *testing.T) {
	filtered, jsonOutput := parseGlobalFlags([]string{"--json", "list", "--json", "extra"})
	if !jsonOutput {
		t.Fatalf("json flag not detected")
	}
	if got := strings.Join(filtered, " "); got != "list extra" {
		t.Fatalf("filtered args = %q, want \"list extra\"", got)
	}
}

func TestDispatcher_AllSubcommandsReachExecutor(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, 27, 12, 0, 0, 0, time.UTC) }
	defer func() {
		taskAliasCacheRoot = oldRoot
		nowTaskAliasCache = oldNow
	}()

	taskJSONFor := func(uuid string) string {
		return `[{"uuid":"` + uuid + `","description":"Test","status":"pending","priority":"M","tags":[],"urgency":10,"depends":[]}]`
	}

	cases := []struct {
		name      string
		args      []string
		wantCalls [][]string
	}{
		{
			name:      "list",
			args:      []string{"list"},
			wantCalls: [][]string{{"status:pending", "export"}},
		},
		{
			name:      "all",
			args:      []string{"all"},
			wantCalls: [][]string{{"export"}},
		},
		{
			name:      "ready",
			args:      []string{"ready"},
			wantCalls: [][]string{{"+READY", "export"}},
		},
		{
			name:      "urgency",
			args:      []string{"urgency"},
			wantCalls: [][]string{{"export"}},
		},
		{
			name:      "complete-uuids",
			args:      []string{"complete-uuids"},
			wantCalls: [][]string{{"status:pending", "export"}},
		},
		{
			name:      "info",
			args:      []string{"info", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}},
		},
		{
			name:      "add",
			args:      []string{"add", "new task description"},
			wantCalls: [][]string{{"add", "rc.verbose=nothing", "rc.verbose=new-uuid", "new task description"}},
		},
		{
			name:      "annotate",
			args:      []string{"annotate", "test-uuid", "note"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "annotate", "note"}},
		},
		{
			name:      "start",
			args:      []string{"start", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "start"}},
		},
		{
			name:      "stop",
			args:      []string{"stop", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "stop"}},
		},
		{
			name:      "done",
			args:      []string{"done", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "done"}},
		},
		{
			name:      "priority",
			args:      []string{"priority", "test-uuid", "H"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "modify", "priority:H"}},
		},
		{
			name:      "tag",
			args:      []string{"tag", "test-uuid", "+cli"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "modify", "+cli"}},
		},
		{
			name:      "modify",
			args:      []string{"modify", "test-uuid", "priority:H"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "modify", "priority:H"}},
		},
		{
			name:      "denotate",
			args:      []string{"denotate", "test-uuid", "text"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "denotate", "text"}},
		},
		{
			name:      "delete",
			args:      []string{"delete", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:test-uuid", "delete"}},
		},
		{
			name:      "dep list",
			args:      []string{"dep", "list", "test-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}},
		},
		{
			name:      "dep add",
			args:      []string{"dep", "add", "test-uuid", "dep-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:dep-uuid", "export"}, {"uuid:test-uuid", "modify", "depends:dep-uuid"}},
		},
		{
			name:      "dep rm",
			args:      []string{"dep", "rm", "test-uuid", "dep-uuid"},
			wantCalls: [][]string{{"uuid:test-uuid", "export"}, {"uuid:dep-uuid", "export"}, {"uuid:test-uuid", "modify", "depends:-dep-uuid"}},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			var calls [][]string
			d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
				calls = append(calls, append([]string(nil), args...))
				switch strings.Join(args, " ") {
				case "export", "status:pending export", "+READY export":
					_, _ = io.WriteString(stdout, taskJSONFor("test-uuid"))
				case "uuid:test-uuid export":
					_, _ = io.WriteString(stdout, taskJSONFor("test-uuid"))
				case "uuid:dep-uuid export":
					_, _ = io.WriteString(stdout, taskJSONFor("dep-uuid"))
				case "add rc.verbose=nothing rc.verbose=new-uuid new task description":
					_, _ = io.WriteString(stdout, "Created task task-uuid-abc.\n")
				case "uuid:test-uuid annotate note":
				case "uuid:test-uuid start":
				case "uuid:test-uuid stop":
				case "uuid:test-uuid done":
				case "uuid:test-uuid modify priority:H":
				case "uuid:test-uuid modify +cli":
				case "uuid:test-uuid modify depends:dep-uuid":
				case "uuid:test-uuid modify depends:-dep-uuid":
				case "uuid:test-uuid denotate text":
				case "uuid:test-uuid delete":
				default:
					t.Fatalf("unexpected runner args: %v", args)
				}
				return 0, nil
			}})

			var stdout, stderr bytes.Buffer
			code, err := d.Dispatch(context.Background(), tc.args, nil, &stdout, &stderr)
			if err != nil {
				t.Fatalf("Dispatch returned error: %v", err)
			}
			if code != 0 {
				t.Fatalf("Dispatch code = %d, want 0", code)
			}
			if !reflect.DeepEqual(calls, tc.wantCalls) {
				t.Fatalf("runner calls = %#v, want %#v", calls, tc.wantCalls)
			}
			if stderr.Len() != 0 {
				t.Fatalf("stderr = %q, want empty", stderr.String())
			}
		})
	}
}

type spyRunner struct {
	runFn func(context.Context, []string, io.Reader, io.Writer, io.Writer) (int, error)
}

func (s *spyRunner) Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
	return s.runFn(ctx, args, stdin, stdout, stderr)
}