-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_test.go
More file actions
228 lines (209 loc) · 8.05 KB
/
Copy pathrender_test.go
File metadata and controls
228 lines (209 loc) · 8.05 KB
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
package gatekit
import (
"strings"
"testing"
"time"
)
// overdueItem builds a due RenderItem for the renderer tests. FormatDecision
// never re-tiers anything, so these are constructed directly rather than
// driven through Evaluate.
func overdueItem(module, id, title, reason string, age time.Duration) RenderItem {
return RenderItem{
Item: Item{Title: title, Reason: reason},
Module: module,
Key: module + "/" + id,
Tier: Overdue,
AgeSince: Duration(age),
}
}
// TestFormatDecisionListsEveryOverdueItem is the counterpart to the tier
// rule that every due item blocks: if all of them block, all of them have to
// be shown. A renderer that summarized any of them would be describing a
// gate that does not exist.
func TestFormatDecisionListsEveryOverdueItem(t *testing.T) {
var items []RenderItem
for i := 0; i < 12; i++ {
items = append(items, overdueItem("mod", string(rune('a'+i)),
"Person "+string(rune('A'+i)), "asked a question", time.Duration(i+1)*time.Hour))
}
got := FormatDecision(Decision{Overdue: items})
if !strings.Contains(got, "12 items overdue:") {
t.Errorf("missing overdue count header, got %q", got)
}
for i := 0; i < 12; i++ {
name := "Person " + string(rune('A'+i))
if !strings.Contains(got, name) {
t.Errorf("overdue item %q was not listed: %q", name, got)
}
}
}
func TestFormatDecisionOneTablePerSource(t *testing.T) {
dec := Decision{Overdue: []RenderItem{
overdueItem("reply-gate-matrix", "1", "Alice", "asked a question", 50*24*time.Hour),
overdueItem("reply-gate-fastmail", "2", "Bob", "awaiting reply", 9*24*time.Hour),
overdueItem("reply-gate-matrix", "3", "Carol", "no reply", 3*24*time.Hour),
}}
got := FormatDecision(dec)
if !strings.Contains(got, "reply-gate-matrix (2)") {
t.Errorf("expected a matrix table header with its own count, got %q", got)
}
if !strings.Contains(got, "reply-gate-fastmail (1)") {
t.Errorf("expected a fastmail table header with its own count, got %q", got)
}
if n := strings.Count(got, "AGE"); n != 2 {
t.Errorf("expected one column header row per source, got %d in %q", n, got)
}
// The source carrying the oldest item leads, matching the oldest-first
// order used everywhere else -- otherwise the table order would depend
// on which file the loader happened to read first.
if strings.Index(got, "reply-gate-matrix (2)") > strings.Index(got, "reply-gate-fastmail (1)") {
t.Errorf("source with the oldest item should lead, got %q", got)
}
}
// TestFormatDecisionColumnsAlign is the whole point of a table over a list:
// if the columns do not line up, it is a list with extra whitespace.
func TestFormatDecisionColumnsAlign(t *testing.T) {
// One row must be long enough to be truncated: a clipped cell ends in a
// multi-byte ellipsis, and measuring widths in bytes rather than runes
// pads exactly those rows short. Without a clipped row here, the whole
// alignment check passes over the one case that actually broke.
clipped := overdueItem("mod", "3", "Carol", strings.Repeat("z", 200), 3*time.Hour)
clipped.URL = "https://example.test/c"
withLink := overdueItem("mod", "1", "Al", "short", 2*time.Hour)
withLink.URL = "https://example.test/a"
longName := overdueItem("mod", "2", "Bartholomew", "a considerably longer reason", 400*24*time.Hour)
longName.URL = "https://example.test/b"
dec := Decision{Overdue: []RenderItem{withLink, longName, clipped}}
got := FormatDecision(dec)
var rows []string
for _, line := range strings.Split(got, "\n") {
if strings.HasPrefix(line, " ") && strings.TrimSpace(line) != "" {
rows = append(rows, line)
}
}
if len(rows) != 4 {
t.Fatalf("want a header row plus 3 item rows, got %d: %q", len(rows), got)
}
// Compare real column boundaries, not "is there a character here". A
// row whose last column starts two places early still has a non-space
// rune at the header's offset (URLs are long), so checking one position
// proves nothing -- the whole list of field starts has to match.
fieldStarts := func(line string) []int {
r := []rune(line)
var out []int
for i := 0; i < len(r); i++ {
if r[i] == ' ' {
continue
}
if i >= 2 && r[i-1] == ' ' && r[i-2] == ' ' {
out = append(out, i)
}
}
return out
}
want := fieldStarts(rows[0])
if len(want) != 4 {
t.Fatalf("header should have 4 columns, found starts %v in %q", want, rows[0])
}
for _, r := range rows[1:] {
got := fieldStarts(r)
if len(got) != len(want) {
t.Errorf("row has %d columns, header has %d: %q", len(got), len(want), r)
continue
}
for i := range want {
if got[i] != want[i] {
t.Errorf("column %d starts at %d, header has it at %d: %q", i, got[i], want[i], r)
}
}
}
}
func TestFormatDecisionTruncatesLongReasonsButNeverURLs(t *testing.T) {
long := strings.Repeat("x", 200)
url := "https://matrix.to/#/!" + strings.Repeat("y", 40) + ":d.graysons.network"
ri := overdueItem("mod", "1", "Alice", long, time.Hour)
ri.URL = url
got := FormatDecision(Decision{Overdue: []RenderItem{ri}})
if strings.Contains(got, long) {
t.Error("a 200-char reason should be clipped, not rendered whole")
}
if !strings.Contains(got, "…") {
t.Errorf("a clipped cell must show it was clipped, got %q", got)
}
if !strings.Contains(got, url) {
t.Errorf("the URL must survive intact -- a truncated URL is not a URL: %q", got)
}
}
func TestFormatDecisionSummarizesHeldWithItsReason(t *testing.T) {
snoozed := overdueItem("mod", "1", "Snoozed", "", time.Hour)
snoozed.Tier, snoozed.SnoozeHeld = Held, true
degraded := overdueItem("mod", "2", "Degraded", "", time.Hour)
degraded.Tier, degraded.SuppressedByDegrade = Held, true
got := FormatDecision(Decision{
Overdue: []RenderItem{overdueItem("mod", "3", "Blocking", "", time.Hour)},
Held: []RenderItem{snoozed, degraded},
})
if !strings.Contains(got, "2 due but held") {
t.Errorf("expected a held count, got %q", got)
}
// The reason is the entire value of the line: a held item is
// indistinguishable from an answered one without it.
if !strings.Contains(got, "1 snoozed") || !strings.Contains(got, "1 on a degraded source") {
t.Errorf("expected held items broken down by why they are held, got %q", got)
}
if strings.Contains(got, "Snoozed") {
t.Errorf("held items are summarized, not listed individually, got %q", got)
}
}
func TestFormatDecisionReportsNextPendingDueTime(t *testing.T) {
pending := RenderItem{
Item: Item{Title: "Later"}, Module: "mod", Key: "mod/1", Tier: Pending,
BecomesOverdueIn: Duration(3 * time.Hour),
}
got := FormatDecision(Decision{
Overdue: []RenderItem{overdueItem("mod", "2", "Now", "", time.Hour)},
Pending: []RenderItem{pending},
})
if !strings.Contains(got, "1 pending (next due in 3h: Later)") {
t.Errorf("expected a pending line naming the next item to come due, got %q", got)
}
}
func TestFormatDecisionEmptyDecisionRendersEmptyString(t *testing.T) {
if got := FormatDecision(Decision{}); got != "" {
t.Errorf("expected empty string for an empty Decision, got %q", got)
}
}
func TestFormatDecisionRepeatsEscapeValvesOnEveryBlock(t *testing.T) {
got := FormatDecision(Decision{Overdue: []RenderItem{overdueItem("mod", "1", "Only", "", time.Hour)}})
if !strings.Contains(got, "gatekit bypass") ||
!strings.Contains(got, "gatekit snooze") ||
!strings.Contains(got, "gatekit off") {
t.Errorf("expected escape valves reminder on a block, got %q", got)
}
}
func TestFormatDecisionOmitsEscapeValvesWhenNothingIsOverdue(t *testing.T) {
held := overdueItem("mod", "1", "Not blocking", "", time.Hour)
held.Tier, held.SnoozeHeld = Held, true
got := FormatDecision(Decision{Held: []RenderItem{held}})
if strings.Contains(got, "Escape:") {
t.Errorf("expected no escape-valves line when nothing is overdue, got %q", got)
}
}
func TestCompactDurationUsesOneUnit(t *testing.T) {
cases := []struct {
d time.Duration
want string
}{
{57 * 24 * time.Hour, "57d"},
{25 * time.Hour, "1d"},
{3 * time.Hour, "3h"},
{90 * time.Minute, "1h"},
{45 * time.Second, "45s"},
{-time.Hour, "0s"},
}
for _, c := range cases {
if got := compactDuration(c.d); got != c.want {
t.Errorf("compactDuration(%s) = %q, want %q", c.d, got, c.want)
}
}
}