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

import (
	"fmt"
	"strings"
	"testing"
)

func TestFormatTaskList(t *testing.T) {
	tasks := []TaskExport{
		{UUID: "uuid-1", Description: "Short task", Status: "pending", Priority: "H", Tags: []string{"cli"}, Urgency: 15.0},
		{UUID: "uuid-2", Description: strings.Repeat("a", 100), Status: "completed", Priority: "M", Tags: []string{"agent", "test"}, Urgency: 5.0},
		{UUID: "uuid-3", Description: "No tags task", Status: "waiting", Priority: "L", Tags: []string{}, Urgency: 8.0},
	}
	aliases := map[string]string{"uuid-1": "0", "uuid-2": "1", "uuid-3": "2"}
	output := FormatTaskList(tasks, aliases)
	lines := strings.Split(strings.TrimSpace(output), "\n")
	if len(lines) < 3 {
		t.Fatalf("FormatTaskList produced too few lines: %d", len(lines))
	}
	if !strings.Contains(lines[0], "ID") || !strings.Contains(lines[0], "Prio") {
		t.Fatalf("header missing ID or Prio column: %s", lines[0])
	}
	if strings.Contains(lines[0], "Priority") {
		t.Fatalf("header should use compact Prio label: %s", lines[0])
	}
	if !strings.Contains(lines[0], "Started") {
		t.Fatalf("header missing Started column: %s", lines[0])
	}
	if !strings.Contains(lines[2], "0") || strings.Contains(lines[2], "uuid-1") {
		t.Fatalf("first task line should show alias only: %s", lines[2])
	}
	if !strings.Contains(lines[3], "...") {
		t.Fatalf("long description should be truncated with ...: %s", lines[3])
	}
}

func TestFormatTaskList_AlignsHeaderAndSeparator(t *testing.T) {
	tasks := []TaskExport{
		{
			UUID:        "uuid-short",
			Description: "Short task",
			Status:      "pending",
			Priority:    "H",
			Tags:        []string{"cli"},
			Urgency:     1.0,
		},
		{
			UUID:        "uuid-with-a-longer-value",
			Description: strings.Repeat("x", 60),
			Status:      "completed",
			Priority:    "M",
			Tags:        []string{"agent", "cli"},
			Urgency:     12.3,
		},
	}

	aliases := map[string]string{"uuid-short": "0", "uuid-with-a-longer-value": "00"}
	output := FormatTaskList(tasks, aliases)
	lines := strings.Split(strings.TrimSuffix(output, "\n"), "\n")
	if len(lines) != 4 {
		t.Fatalf("FormatTaskList produced %d lines, want 4: %q", len(lines), output)
	}

	widths := taskListWidthsFor(tasks, aliases)
	wantHeader := fmt.Sprintf("%-*s | %-*s | %-*s | %-*s | %-*s | %-*s | %-*s",
		widths.Urgency, "Urgency",
		widths.Priority, "Prio",
		widths.ID, "ID",
		widths.Status, "Status",
		widths.Started, "Started",
		widths.Tags, "Tags",
		widths.Description, "Description",
	)
	if lines[0] != wantHeader {
		t.Fatalf("header = %q, want %q", lines[0], wantHeader)
	}
	if len(lines[1]) != len(wantHeader) {
		t.Fatalf("separator length = %d, want %d", len(lines[1]), len(wantHeader))
	}
}

func TestFormatTaskList_FallsBackToUUIDWithoutAlias(t *testing.T) {
	tasks := []TaskExport{{UUID: "uuid-1", Description: "Task", Status: "pending", Priority: "H", Urgency: 1.0}}

	output := FormatTaskList(tasks, nil)
	if !strings.Contains(output, "uuid-1") {
		t.Fatalf("FormatTaskList should fall back to UUID when alias is unavailable: %s", output)
	}
}

func TestFormatTaskInfo(t *testing.T) {
	task := TaskExport{
		UUID:        "test-uuid",
		Description: "Test description",
		Status:      "pending",
		Priority:    "H",
		Tags:        []string{"cli", "agent"},
		Start:       "2026-03-22T10:00:00Z",
		Urgency:     15.5,
		Depends:     []string{"dep-1", "dep-2"},
		Annotations: []struct {
			Description string `json:"description"`
			Entry       string `json:"entry"`
		}{
			{Description: "First note", Entry: "2026-03-22T11:00:00Z"},
		},
	}
	output := FormatTaskInfo(task, "0", map[string]string{"dep-1": "1", "dep-2": "2"})
	if !strings.Contains(output, "ID:          0") {
		t.Fatalf("FormatTaskInfo missing alias ID: %s", output)
	}
	if !strings.Contains(output, "test-uuid") {
		t.Fatalf("FormatTaskInfo missing UUID: %s", output)
	}
	if !strings.Contains(output, "H") {
		t.Fatalf("FormatTaskInfo missing priority H: %s", output)
	}
	if !strings.Contains(output, "Started:     yes") {
		t.Fatalf("FormatTaskInfo missing explicit started state: %s", output)
	}
	if !strings.Contains(output, "Start time:  2026-03-22T10:00:00Z") {
		t.Fatalf("FormatTaskInfo missing start timestamp: %s", output)
	}
	if !strings.Contains(output, "cli, agent") {
		t.Fatalf("FormatTaskInfo missing tags: %s", output)
	}
	if !strings.Contains(output, "1 (dep-1)") || !strings.Contains(output, "2 (dep-2)") {
		t.Fatalf("FormatTaskInfo missing formatted depends: %s", output)
	}
	if !strings.Contains(output, "First note") {
		t.Fatalf("FormatTaskInfo missing annotation: %s", output)
	}
}

func TestFormatSuccess(t *testing.T) {
	output := FormatSuccess("0")
	if !strings.Contains(output, "ok") || !strings.Contains(output, "0") {
		t.Fatalf("FormatSuccess = %q, want ok + alias", output)
	}
}

func TestFormatError(t *testing.T) {
	err := &testError{msg: "something went wrong"}
	output := FormatError(err, "0")
	if !strings.Contains(output, "error") || !strings.Contains(output, "0") || !strings.Contains(output, "something went wrong") {
		t.Fatalf("FormatError = %q, want error + alias + message", output)
	}
}

func TestFormatError_NoUUID(t *testing.T) {
	err := &testError{msg: "generic error"}
	output := FormatError(err, "")
	if !strings.Contains(output, "error") || !strings.Contains(output, "generic error") {
		t.Fatalf("FormatError = %q, want error + message", output)
	}
}

func TestIsNumericID(t *testing.T) {
	if !IsNumericID("123") {
		t.Error("IsNumericID(\"123\") = false, want true")
	}
	if !IsNumericID("0") {
		t.Error("IsNumericID(\"0\") = false, want true")
	}
	if IsNumericID("uuid-123") {
		t.Error("IsNumericID(\"uuid-123\") = true, want false")
	}
	if IsNumericID("abc") {
		t.Error("IsNumericID(\"abc\") = true, want false")
	}
	if IsNumericID("12a") {
		t.Error("IsNumericID(\"12a\") = true, want false")
	}
	if IsNumericID("") {
		t.Error("IsNumericID(\"\") = true, want false")
	}
}

func TestNormalizeUUID(t *testing.T) {
	cases := []struct {
		input string
		want  string
	}{
		{"fc390139-cc08-413f-a411-f2feae4875a3", "fc390139-cc08-413f-a411-f2feae4875a3"},
		{"uuid:fc390139-cc08-413f-a411-f2feae4875a3", "fc390139-cc08-413f-a411-f2feae4875a3"},
		{"fc390139", "fc390139"},
		{"uuid:fc390139", "fc390139"},
		{"", ""},
	}
	for _, c := range cases {
		got := NormalizeUUID(c.input)
		if got != c.want {
			t.Errorf("NormalizeUUID(%q) = %q, want %q", c.input, got, c.want)
		}
	}
}

func TestRejectNumericID(t *testing.T) {
	output := RejectNumericID()
	if !strings.Contains(output, "use UUID") {
		t.Fatalf("RejectNumericID = %q, want use UUID message", output)
	}
}

func TestFormatTaskInfo_NoOptionalFields(t *testing.T) {
	task := TaskExport{
		UUID:        "simple-uuid",
		Description: "Simple task",
		Status:      "pending",
		Priority:    "M",
		Tags:        []string{},
		Urgency:     0,
	}
	output := FormatTaskInfo(task, "0", nil)
	if !strings.Contains(output, "simple-uuid") {
		t.Fatalf("FormatTaskInfo missing UUID: %s", output)
	}
	if !strings.Contains(output, "Started:     no") {
		t.Fatalf("FormatTaskInfo should show Started: no when not started: %s", output)
	}
	if strings.Contains(output, "Tags:") {
		t.Fatalf("FormatTaskInfo should not contain Tags: for empty tags: %s", output)
	}
	if strings.Contains(output, "Depends:") {
		t.Fatalf("FormatTaskInfo should not contain Depends: for empty depends: %s", output)
	}
}

type testError struct {
	msg string
}

func (e *testError) Error() string {
	return e.msg
}