gunk-dev/the-valley

verdict_test.go

source5d56575cb86dmain's published tip history
6.9 KiB
1package main
2
3import (
4 "bytes"
5 "os/exec"
6 "regexp"
7 "strings"
8 "testing"
9)
10
11var sgr = regexp.MustCompile("\x1b\\[[0-9;]*m")
12
13func TestVerdictLines(t *testing.T) {
14 cases := []struct {
15 name string
16 v verdict
17 want string
18 glyph string // the glyph as a terminal shows it
19 }{
20 {"every check covered",
21 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4},
22 "✓ nqyw v3 approved · checks passed · landing on main\n", "\x1b[32m✓"},
23 {"checks built here",
24 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4, took: "42s"},
25 "✓ nqyw v3 approved · 4 checks passed in 42s · landing on main\n", "\x1b[32m✓"},
26 {"no check required",
27 verdict{outcome: "approved", name: "topic/quiet", required: 0, passed: 0},
28 "✓ topic/quiet approved · no checks required · landing on main\n", "\x1b[32m✓"},
29 {"a worker builds the rest",
30 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 1, worker: true, waiting: []string{"a", "b", "c"}},
31 "◐ nqyw v3 approved · lands when 3 checks pass (1/4 passed)\n", "\x1b[33m◐"},
32 {"counts not read",
33 verdict{outcome: "approved", name: "nqyw", version: "3", required: -1, passed: -1, worker: true, waiting: []string{"a", "b", "c"}},
34 "◐ nqyw v3 approved · waiting for 3 checks\n", "\x1b[33m◐"},
35 {"nothing builds the rest",
36 verdict{outcome: "approved", name: "nqyw", version: "3", required: 2, passed: 0, waiting: []string{"a", "b"}, run: "valley approve --build-here nqyw"},
37 "◇ nqyw v3 approved · waiting for 2 checks (no worker building)\n run: valley approve --build-here nqyw\n", "\x1b[36m◇"},
38 {"state unknown",
39 verdict{outcome: "unknown", name: "nqyw", version: "3", required: -1, passed: -1},
40 "? nqyw v3 approved · state unknown · see: valley status nqyw\n", "?"},
41 {"a check failed",
42 verdict{outcome: "refused", name: "nqyw", version: "3", required: 4, passed: -1, failed: []string{"valley-request"}},
43 "⊘ nqyw v3 not approved · 1 of 4 checks failed\n ✗ valley-request — see: valley status nqyw\n", "\x1b[31m⊘"},
44 {"a draft",
45 verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "change is a draft", run: "valley ready nqyw"},
46 "⊘ nqyw v3 not approved · change is a draft\n run: valley ready nqyw\n", "\x1b[31m⊘"},
47 {"a stale stack",
48 verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "stack is behind main (includes kpxm v2)", run: "valley rebase nqyw"},
49 "⊘ nqyw v3 not approved · stack is behind main (includes kpxm v2)\n run: valley rebase nqyw\n", "\x1b[31m⊘"},
50 }
51 for _, c := range cases {
52 c.v.target = "main"
53 if got := c.v.lines(terminal{width: 80}); got != c.want {
54 t.Errorf("%s, plain:\n got %q\nwant %q", c.name, got, c.want)
55 }
56 shown := c.v.lines(terminal{width: 80, bold: true, live: true})
57 if plain := sgr.ReplaceAllString(shown, ""); plain != c.want {
58 t.Errorf("%s, on a terminal, without its styling:\n got %q\nwant %q", c.name, plain, c.want)
59 }
60 if !strings.HasPrefix(shown, c.glyph) || !strings.Contains(shown, "\x1b[1m"+c.v.label()+"\x1b[0m") {
61 t.Errorf("%s, on a terminal: want the glyph as %q and the name in bold, got %q", c.name, c.glyph, shown)
62 }
63 }
64}
65
66// An approval whose answer carried to a later version names that version
67// and tells its checks as the checks line reads them for the active
68// version. A check of another runner waits, since no worker runs it.
69func TestCarriedVerdict(t *testing.T) {
70 states := func(s ...string) *jsonVersionChecks {
71 c := &jsonVersionChecks{}
72 for i, state := range s {
73 c.Required = append(c.Required, jsonCheckState{Name: string(rune('a' + i)), State: state})
74 }
75 return c
76 }
77 for _, c := range []struct {
78 name string
79 checks *jsonVersionChecks
80 want string
81 }{
82 {"running", states(checkPending, checkPending),
83 "◐ zplr v3 approved · 2 checks running · lands when they pass\n"},
84 {"one running", states(checkPassed, checkPending),
85 "◐ zplr v3 approved · 1 check running · lands when it passes\n"},
86 {"passed", states(checkPassed, checkPassed),
87 "✓ zplr v3 approved · checks passed · landing on main\n"},
88 {"failed", states(checkPassed, checkFailed, checkPending),
89 "✗ zplr v3 approved · 1 of 3 checks failed\n ✗ b — see: valley status zplr\n"},
90 {"none required", &jsonVersionChecks{},
91 "✓ zplr v3 approved · no checks required · landing on main\n"},
92 {"a command check", states(checkPassed, checkPending, checkOther),
93 "◇ zplr v3 approved · waiting for 2 checks (no worker building)\n run: valley approve zplr\n"},
94 {"not read", nil,
95 "? zplr v3 approved · checks unknown · see: valley status zplr\n"},
96 } {
97 if got := carriedVerdictOf("zplr", "3", c.checks).lines(terminal{width: 80}); got != c.want {
98 t.Errorf("%s:\n got %q\nwant %q", c.name, got, c.want)
99 }
100 }
101}
102
103func TestVerdictPrompt(t *testing.T) {
104 header := func() []string { return []string{"nqyw v3", "checks: 4 passed", "behind main (integrator merges)"} }
105 keys := []string{"[a]pprove", "[v]iew diff", "[r]eject", "[s]kip"}
106 want := "nqyw v3 · checks: 4 passed · behind main (integrator merges)\n[a]pprove [v]iew diff [r]eject [s]kip: "
107 if got := promptLines(terminal{width: 80}, header(), keys); got != want {
108 t.Errorf("plain:\n got %q\nwant %q", got, want)
109 }
110 shown := promptLines(terminal{width: 80, bold: true}, header(), keys)
111 if sgr.ReplaceAllString(shown, "") != want || !strings.Contains(shown, "[\x1b[1ma\x1b[0m]pprove") {
112 t.Errorf("on a terminal: got %q", shown)
113 }
114}
115
116// checking keeps a passing run's output back, and shows the failed check's
117// log and the refusal when one fails.
118func TestVerdictChecking(t *testing.T) {
119 v := verdict{name: "nqyw", version: "3", required: 3, passed: 1, target: "main"}
120 var out, errs bytes.Buffer
121 pass := exec.Command("sh", "-c", `echo "check a nix passed"; echo "check b nix passed"; echo "stored refs/x -> y"`)
122 if status := v.checking([]string{"a", "b"}, pass, false, &out, &errs, nil, nil); status != 0 {
123 t.Fatalf("a passing run exited %d", status)
124 }
125 if out.String() != "◐ nqyw v3 checking 2 checks: a, b\n" || errs.Len() != 0 {
126 t.Errorf("a passing run printed %q and %q", out.String(), errs.String())
127 }
128
129 out.Reset()
130 errs.Reset()
131 fail := exec.Command("sh", "-c", `echo "check a nix passed"; echo "check b nix failed"; echo; echo "--- b ---"; echo "b: build log"; echo boom; exit 1`)
132 if status := v.checking([]string{"a", "b"}, fail, false, &out, &errs, nil, nil); status != 1 {
133 t.Fatalf("a failing run exited %d", status)
134 }
135 got := errs.String()
136 for _, want := range []string{"--- b ---\nb: build log\nboom\n", "⊘ nqyw v3 not approved · 1 of 3 checks failed\n ✗ b — see: valley status nqyw\n"} {
137 if !strings.Contains(got, want) {
138 t.Errorf("a failing run's stderr lacks %q:\n%s", want, got)
139 }
140 }
141 if strings.Contains(got, "check a") {
142 t.Errorf("a failing run showed the output of a check that passed:\n%s", got)
143 }
144}