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

import (
	"bytes"
	"context"
	"io"
	"strings"
	"testing"
)

func TestDispatcher_Help(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout bytes.Buffer
	code, err := d.Dispatch(context.Background(), []string{}, 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, "Filters:") {
		t.Fatalf("help missing Filters section: %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_LongHelp(t *testing.T) {
	d := NewDispatcher(nil)
	var stdout bytes.Buffer
	d.Dispatch(context.Background(), []string{}, nil, &stdout, io.Discard)
	output := stdout.String()
	for _, sub := range []string{"add", "list", "info", "annotate", "start", "stop", "done", "priority", "tag", "dep", "urgency", "modify", "denotate", "delete", "export"} {
		if !strings.Contains(output, "ask "+sub) {
			t.Errorf("help missing subcommand: ask %s", sub)
		}
	}
}

func TestDispatcher_AllSubcommandsReachExecutor(t *testing.T) {
	subcommands := []string{"add", "list", "info", "annotate", "start", "stop", "done", "priority", "tag", "dep", "urgency", "modify", "denotate", "export"}
	subcommandArgs := map[string][]string{
		"delete": {"delete", "test-uuid"},
	}
	for _, sub := range subcommands {
		var stdout, stderr bytes.Buffer
		calls := 0
		d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout_, stderr_ io.Writer) (int, error) {
			calls++
			return 0, nil
		}})
		args := []string{sub}
		if extra, ok := subcommandArgs[sub]; ok {
			args = extra
		}
		code, _ := d.Dispatch(context.Background(), args, nil, &stdout, &stderr)
		if code != 0 {
			t.Errorf("subcommand %q code = %d, want 0", sub, code)
		}
	}
}

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)
}