package thin import ( "encoding/json" "fmt" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/wire" ) func writeTree(t *testing.T, files map[string]string) string { t.Helper() dir := t.TempDir() for p, body := range files { if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, p), []byte(body), 0700); err != nil { t.Fatal(err) } } return dir } // The gate is the repository's own, in Amadan's order: .amadan/ci.d, // then .amadan/ci, then a make ci target; otherwise none. func TestFindGate(t *testing.T) { for _, tc := range []struct { files map[string]string want string }{ {map[string]string{".amadan/ci.d/10-vet": "go vet", ".amadan/ci": "x", "Makefile": "ci:\n\ttrue\n"}, ".amadan/ci.d"}, {map[string]string{".amadan/ci": "make ci", "Makefile": "ci:\n\ttrue\n"}, ".amadan/ci"}, {map[string]string{"Makefile": "test:\n\tgo test\nci: test\n"}, "make ci"}, {map[string]string{"GNUmakefile": "ci::\n\ttrue\n"}, "make ci"}, {map[string]string{"Makefile": "ci := yes\ncity:\n\ttrue\n"}, ""}, {map[string]string{"README.md": "# x"}, ""}, {map[string]string{".amadan/ci.d/README": ""}, ".amadan/ci.d"}, } { g, ok := findGate(writeTree(t, tc.files)) if g.name != tc.want || ok != (tc.want != "") { t.Fatalf("%v: gate %q (%v), want %q", tc.files, g.name, ok, tc.want) } } // A symlinked gate pointing out of the tree is not read. dir := writeTree(t, map[string]string{"x": ""}) if err := os.Symlink("/etc/passwd", filepath.Join(dir, "Makefile")); err != nil { t.Fatal(err) } if g, ok := findGate(dir); ok { t.Fatalf("followed a symlink: %+v", g) } } func TestLineTailKeepsTheLast200Lines(t *testing.T) { tail := &lineTail{max: wire.MaxChecksLines} for i := 1; i <= 500; i++ { fmt.Fprintf(tail, "line %d\n", i) } fmt.Fprint(tail, strings.Repeat("y", 900)+"\nend") lines := strings.Split(tail.String(), "\n") if len(lines) != wire.MaxChecksLines+1 || lines[0] != "line 302" || lines[len(lines)-1] != "end" || len(lines[len(lines)-2]) != 500 { t.Fatalf("%d lines, first %q, last %q", len(lines), lines[0], lines[len(lines)-1]) } } func TestVerdictOf(t *testing.T) { 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."}]}`)) 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." { t.Fatalf("%q %q %q %+v %v", v, h, s, f, ok) } // An older reviewer without a headline still gives its verdict. if v, h, _, _, ok := verdictOf(json.RawMessage(`{"verdict":"approve","summary":"x"}`)); !ok || v != wire.VerdictApprove || h != "" { t.Fatalf("no headline: %q %q %v", v, h, ok) } for _, bad := range []string{`{"verdict":"lgtm","summary":"x"}`, `{"verdict":"approve","summary":"x","extra":1}`, `not json`} { if _, _, _, _, ok := verdictOf(json.RawMessage(bad)); ok { t.Fatalf("accepted %s", bad) } } } // Review runs away from Engineering's account when it can: another // account of the family (same family), else a fresh session on the same // one (same family, same account). func TestReviewerAccount(t *testing.T) { b := func(id, account string) runner.AccountBinding { return runner.AccountBinding{ID: id, LocalID: id, PersonID: "p@x", Provider: "claude", Family: "claude", Model: "m", AccountID: account} } all := []runner.AccountBinding{b("claude-a", "paul@tito.io"), b("claude-c", "paul@tito.io"), b("claude-b", "other@x")} got, label, err := pickReviewer(all, "p@x", "claude-a", nil) if err != nil || got.ID != "claude-b" || label != wire.ReviewSameFamily { t.Fatalf("%+v %s %v", got, label, err) } 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 { t.Fatalf("unhealthy other account used: %+v %s", got, label) } if got, label, _ = pickReviewer(all[:1], "p@x", "", nil); got.ID != "claude-a" || label != wire.ReviewSameAccount { t.Fatalf("%+v %s", got, label) } if _, _, err = pickReviewer(all, "someone@else", "", nil); err == nil { t.Fatal("reviewed with another person's account") } } // Engineering's reply starts with "Headline: ..."; the runner sends it // apart from the summary (and keeps it out of the commit message). A // reply without one is all summary. func TestSplitHeadline(t *testing.T) { for _, tc := range []struct{ in, headline, rest string }{ {"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."}, {"**Headline:** Fixed it.\nRest.", "Fixed it.", "Rest."}, {"headline: lower case works\n", "lower case works", ""}, {"The host now lets the pop gesture run.", "", "The host now lets the pop gesture run."}, {"Note: not a headline\nmore", "", "Note: not a headline\nmore"}, } { h, rest := splitHeadline(tc.in) if h != tc.headline || rest != tc.rest { t.Errorf("splitHeadline(%q) = %q, %q", tc.in, h, rest) } } }