gunk-dev/the-valley

verdict_test.go

sourceb9df3fe59e29main's published tip history
5.3 KiB
1package main
2
3import (
4 "bytes"
5 "regexp"
6 "strings"
7 "testing"
8)
9
10var sgr = regexp.MustCompile("\x1b\\[[0-9;]*m")
11
12func TestVerdictLines(t *testing.T) {
13 cases := []struct {
14 name string
15 v verdict
16 want string
17 glyph string // the glyph as a terminal shows it
18 }{
19 {"every check covered",
20 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4},
21 "✓ nqyw v3 approved · checks passed · landing on main\n", "\x1b[32m✓"},
22 {"checks built here",
23 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 4, took: "42s"},
24 "✓ nqyw v3 approved · 4 checks passed in 42s · landing on main\n", "\x1b[32m✓"},
25 {"no check required",
26 verdict{outcome: "approved", name: "topic/quiet", required: 0, passed: 0},
27 "✓ topic/quiet approved · no checks required · landing on main\n", "\x1b[32m✓"},
28 {"a worker builds the rest",
29 verdict{outcome: "approved", name: "nqyw", version: "3", required: 4, passed: 1, worker: true, waiting: []string{"a", "b", "c"}},
30 "◐ nqyw v3 approved · lands when 3 checks pass (1/4 passed)\n", "\x1b[33m◐"},
31 {"counts not read",
32 verdict{outcome: "approved", name: "nqyw", version: "3", required: -1, passed: -1, worker: true, waiting: []string{"a", "b", "c"}},
33 "◐ nqyw v3 approved · waiting for 3 checks\n", "\x1b[33m◐"},
34 {"nothing builds the rest",
35 verdict{outcome: "approved", name: "nqyw", version: "3", required: 2, passed: 0, waiting: []string{"a", "b"}, run: "valley approve --build-here nqyw"},
36 "◇ nqyw v3 approved · waiting for 2 checks (no worker building)\n run: valley approve --build-here nqyw\n", "\x1b[36m◇"},
37 {"state unknown",
38 verdict{outcome: "unknown", name: "nqyw", version: "3", required: -1, passed: -1},
39 "? nqyw v3 approved · state unknown · see: valley status nqyw\n", "?"},
40 {"a check failed",
41 verdict{outcome: "refused", name: "nqyw", version: "3", required: 4, passed: -1, failed: []string{"valley-request"}},
42 "⊘ nqyw v3 not approved · 1 of 4 checks failed\n ✗ valley-request — see: valley status nqyw\n", "\x1b[31m⊘"},
43 {"a draft",
44 verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "change is a draft", run: "valley ready nqyw"},
45 "⊘ nqyw v3 not approved · change is a draft\n run: valley ready nqyw\n", "\x1b[31m⊘"},
46 {"a stale stack",
47 verdict{outcome: "refused", name: "nqyw", version: "3", required: -1, passed: -1, why: "stack is behind main (includes kpxm v2)", run: "valley rebase nqyw"},
48 "⊘ nqyw v3 not approved · stack is behind main (includes kpxm v2)\n run: valley rebase nqyw\n", "\x1b[31m⊘"},
49 }
50 for _, c := range cases {
51 c.v.target = "main"
52 if got := c.v.lines(terminal{width: 80}); got != c.want {
53 t.Errorf("%s, plain:\n got %q\nwant %q", c.name, got, c.want)
54 }
55 shown := c.v.lines(terminal{width: 80, bold: true, live: true})
56 if plain := sgr.ReplaceAllString(shown, ""); plain != c.want {
57 t.Errorf("%s, on a terminal, without its styling:\n got %q\nwant %q", c.name, plain, c.want)
58 }
59 if !strings.HasPrefix(shown, c.glyph) || !strings.Contains(shown, "\x1b[1m"+c.v.label()+"\x1b[0m") {
60 t.Errorf("%s, on a terminal: want the glyph as %q and the name in bold, got %q", c.name, c.glyph, shown)
61 }
62 }
63}
64
65func TestVerdictPrompt(t *testing.T) {
66 header := func() []string { return []string{"nqyw v3", "checks: 4 passed", "behind main (integrator merges)"} }
67 keys := []string{"[a]pprove", "[v]iew diff", "[r]eject", "[s]kip"}
68 want := "nqyw v3 · checks: 4 passed · behind main (integrator merges)\n[a]pprove [v]iew diff [r]eject [s]kip: "
69 if got := promptLines(terminal{width: 80}, header(), keys); got != want {
70 t.Errorf("plain:\n got %q\nwant %q", got, want)
71 }
72 shown := promptLines(terminal{width: 80, bold: true}, header(), keys)
73 if sgr.ReplaceAllString(shown, "") != want || !strings.Contains(shown, "[\x1b[1ma\x1b[0m]pprove") {
74 t.Errorf("on a terminal: got %q", shown)
75 }
76}
77
78// checking keeps a passing run's output back, and shows the failed check's
79// log and the refusal when one fails.
80func TestVerdictChecking(t *testing.T) {
81 v := verdict{name: "nqyw", version: "3", required: 3, passed: 1, target: "main"}
82 var out, errs bytes.Buffer
83 pass := []string{"sh", "-c", `echo "check a nix passed"; echo "check b nix passed"; echo "stored refs/x -> y"`}
84 if status := v.checking([]string{"a", "b"}, pass, false, &out, &errs); status != 0 {
85 t.Fatalf("a passing run exited %d", status)
86 }
87 if out.String() != "◐ nqyw v3 checking 2 checks: a, b\n" || errs.Len() != 0 {
88 t.Errorf("a passing run printed %q and %q", out.String(), errs.String())
89 }
90
91 out.Reset()
92 errs.Reset()
93 fail := []string{"sh", "-c", `echo "check a nix passed"; echo "check b nix failed"; echo; echo "--- b ---"; echo "b: build log"; echo boom; exit 1`}
94 if status := v.checking([]string{"a", "b"}, fail, false, &out, &errs); status != 1 {
95 t.Fatalf("a failing run exited %d", status)
96 }
97 got := errs.String()
98 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"} {
99 if !strings.Contains(got, want) {
100 t.Errorf("a failing run's stderr lacks %q:\n%s", want, got)
101 }
102 }
103 if strings.Contains(got, "check a") {
104 t.Errorf("a failing run showed the output of a check that passed:\n%s", got)
105 }
106}