| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | func writeTree(t *testing.T, files map[string]string) string { |
| 16 | t.Helper() |
| 17 | dir := t.TempDir() |
| 18 | for p, body := range files { |
| 19 | if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0700); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | if err := os.WriteFile(filepath.Join(dir, p), []byte(body), 0700); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | return dir |
| 27 | } |
| 28 | |
| 29 | // The gate is the repository's own, in Amadan's order: .amadan/ci.d, |
| 30 | // then .amadan/ci, then a make ci target; otherwise none. |
| 31 | func TestFindGate(t *testing.T) { |
| 32 | for _, tc := range []struct { |
| 33 | files map[string]string |
| 34 | want string |
| 35 | }{ |
| 36 | {map[string]string{".amadan/ci.d/10-vet": "go vet", ".amadan/ci": "x", "Makefile": "ci:\n\ttrue\n"}, ".amadan/ci.d"}, |
| 37 | {map[string]string{".amadan/ci": "make ci", "Makefile": "ci:\n\ttrue\n"}, ".amadan/ci"}, |
| 38 | {map[string]string{"Makefile": "test:\n\tgo test\nci: test\n"}, "make ci"}, |
| 39 | {map[string]string{"GNUmakefile": "ci::\n\ttrue\n"}, "make ci"}, |
| 40 | {map[string]string{"Makefile": "ci := yes\ncity:\n\ttrue\n"}, ""}, |
| 41 | {map[string]string{"README.md": "# x"}, ""}, |
| 42 | {map[string]string{".amadan/ci.d/README": ""}, ".amadan/ci.d"}, |
| 43 | } { |
| 44 | g, ok := findGate(writeTree(t, tc.files)) |
| 45 | if g.name != tc.want || ok != (tc.want != "") { |
| 46 | t.Fatalf("%v: gate %q (%v), want %q", tc.files, g.name, ok, tc.want) |
| 47 | } |
| 48 | } |
| 49 | // A symlinked gate pointing out of the tree is not read. |
| 50 | dir := writeTree(t, map[string]string{"x": ""}) |
| 51 | if err := os.Symlink("/etc/passwd", filepath.Join(dir, "Makefile")); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if g, ok := findGate(dir); ok { |
| 55 | t.Fatalf("followed a symlink: %+v", g) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestLineTailKeepsTheLast200Lines(t *testing.T) { |
| 60 | tail := &lineTail{max: wire.MaxChecksLines} |
| 61 | for i := 1; i <= 500; i++ { |
| 62 | fmt.Fprintf(tail, "line %d\n", i) |
| 63 | } |
| 64 | fmt.Fprint(tail, strings.Repeat("y", 900)+"\nend") |
| 65 | lines := strings.Split(tail.String(), "\n") |
| 66 | if len(lines) != wire.MaxChecksLines+1 || lines[0] != "line 302" || lines[len(lines)-1] != "end" || len(lines[len(lines)-2]) != 500 { |
| 67 | t.Fatalf("%d lines, first %q, last %q", len(lines), lines[0], lines[len(lines)-1]) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestVerdictOf(t *testing.T) { |
| 72 | v, h, s, f, ok := verdictOf(json.RawMessage(`{"verdict":"changes_requested","headline":" One off-by-one to fix. ","summary":" Fix it. ","findings":[{"headline":"Counts one too many.","path":"/workspace/a/b.go","line":12,"text":"Off by one."}]}`)) |
| 73 | if !ok || v != wire.VerdictChanges || h != "One off-by-one to fix." || s != "Fix it." || len(f) != 1 || f[0].Path != "a/b.go" || f[0].Line != 12 || f[0].Headline != "Counts one too many." { |
| 74 | t.Fatalf("%q %q %q %+v %v", v, h, s, f, ok) |
| 75 | } |
| 76 | // An older reviewer without a headline still gives its verdict. |
| 77 | if v, h, _, _, ok := verdictOf(json.RawMessage(`{"verdict":"approve","summary":"x"}`)); !ok || v != wire.VerdictApprove || h != "" { |
| 78 | t.Fatalf("no headline: %q %q %v", v, h, ok) |
| 79 | } |
| 80 | for _, bad := range []string{`{"verdict":"lgtm","summary":"x"}`, `{"verdict":"approve","summary":"x","extra":1}`, `not json`} { |
| 81 | if _, _, _, _, ok := verdictOf(json.RawMessage(bad)); ok { |
| 82 | t.Fatalf("accepted %s", bad) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Review runs away from Engineering's account when it can: another |
| 88 | // account of the family (same family), else a fresh session on the same |
| 89 | // one (same family, same account). |
| 90 | func TestReviewerAccount(t *testing.T) { |
| 91 | b := func(id, account string) runner.AccountBinding { |
| 92 | return runner.AccountBinding{ID: id, LocalID: id, PersonID: "p@x", Provider: "claude", Family: "claude", Model: "m", AccountID: account} |
| 93 | } |
| 94 | all := []runner.AccountBinding{b("claude-a", "paul@tito.io"), b("claude-c", "paul@tito.io"), b("claude-b", "other@x")} |
| 95 | got, label, err := pickReviewer(all, "p@x", "claude-a", nil) |
| 96 | if err != nil || got.ID != "claude-b" || label != wire.ReviewSameFamily { |
| 97 | t.Fatalf("%+v %s %v", got, label, err) |
| 98 | } |
| 99 | if got, label, _ = pickReviewer(all, "p@x", "claude-a", func(id string) bool { return id != "claude-b" }); got.AccountID != "paul@tito.io" || label != wire.ReviewSameAccount { |
| 100 | t.Fatalf("unhealthy other account used: %+v %s", got, label) |
| 101 | } |
| 102 | if got, label, _ = pickReviewer(all[:1], "p@x", "", nil); got.ID != "claude-a" || label != wire.ReviewSameAccount { |
| 103 | t.Fatalf("%+v %s", got, label) |
| 104 | } |
| 105 | if _, _, err = pickReviewer(all, "someone@else", "", nil); err == nil { |
| 106 | t.Fatal("reviewed with another person's account") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Engineering's reply starts with "Headline: ..."; the runner sends it |
| 111 | // apart from the summary (and keeps it out of the commit message). A |
| 112 | // reply without one is all summary. |
| 113 | func TestSplitHeadline(t *testing.T) { |
| 114 | for _, tc := range []struct{ in, headline, rest string }{ |
| 115 | {"Headline: Fixed swipe-back. Couldn't test on iOS here.\n\nThe host now lets the pop gesture run.", "Fixed swipe-back. Couldn't test on iOS here.", "The host now lets the pop gesture run."}, |
| 116 | {"**Headline:** Fixed it.\nRest.", "Fixed it.", "Rest."}, |
| 117 | {"headline: lower case works\n", "lower case works", ""}, |
| 118 | {"The host now lets the pop gesture run.", "", "The host now lets the pop gesture run."}, |
| 119 | {"Note: not a headline\nmore", "", "Note: not a headline\nmore"}, |
| 120 | } { |
| 121 | h, rest := splitHeadline(tc.in) |
| 122 | if h != tc.headline || rest != tc.rest { |
| 123 | t.Errorf("splitHeadline(%q) = %q, %q", tc.in, h, rest) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |