| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | // reviewerGHStub logs every gh call like the default stub and answers the ones |
| 15 | // launch makes: the gh user, the repo's push collaborators (two pages, as |
| 16 | // --paginate prints them), and the PR lookups of a --pr launch. With |
| 17 | // failCollaborators set, the collaborators call fails as it does for a token |
| 18 | // that cannot read them. |
| 19 | func reviewerGHStub(h *Harness, failCollaborators bool) string { |
| 20 | return fmt.Sprintf(`#!/usr/bin/env bash |
| 21 | dir=%q |
| 22 | { printf 'gh'; for a in "$@"; do printf ' %%q' "$a"; done; printf '\n'; } >> "$dir/gh.argv" |
| 23 | case "$*" in |
| 24 | "api user -q .login") echo acme-agent ;; |
| 25 | "api --paginate repos/acme/widget/collaborators?permission=push&affiliation=all") |
| 26 | if %t; then echo "gh: Must have push access to view repository collaborators. (HTTP 403)" >&2; exit 1; fi |
| 27 | echo '[{"login":"acme-agent","type":"User"},{"login":"alice","type":"User"}]' |
| 28 | echo '[{"login":"renovate","type":"Bot"},{"login":"acme","type":"User"}]' ;; |
| 29 | "pr view --json headRefName"*) echo fix-branch ;; |
| 30 | "pr view --json url"*) echo https://github.com/acme/widget/pull/12 ;; |
| 31 | esac |
| 32 | exit 0 |
| 33 | `, h.E2EDir, failCollaborators) |
| 34 | } |
| 35 | |
| 36 | // TestLaunchResolvesPRReviewer: with no pr_reviewer configured, a launch that |
| 37 | // will open a GitHub PR asks gh for the repo's push collaborators and requests |
| 38 | // review from the owner; when that gh call fails it warns and launches without |
| 39 | // a reviewer. Runs that don't open a new PR (pr-fix, direct-push) never ask, |
| 40 | // and a configured pr_reviewer is used without running gh at all. |
| 41 | func TestLaunchResolvesPRReviewer(t *testing.T) { |
| 42 | t.Parallel() |
| 43 | for _, tc := range []struct { |
| 44 | name string |
| 45 | configured string // pr_reviewer |
| 46 | github bool |
| 47 | collabFails bool |
| 48 | args []string |
| 49 | wantReviewer string // "" means the prompt must not request a review |
| 50 | wantQuery bool // collaborators API called |
| 51 | wantWarning bool |
| 52 | wantNoGH bool |
| 53 | }{ |
| 54 | {name: "configured pr_reviewer", configured: "carol", github: true, args: []string{"tidy the README"}, wantReviewer: "carol", wantNoGH: true}, |
| 55 | {name: "github origin", github: true, args: []string{"tidy the README"}, wantReviewer: "acme", wantQuery: true}, |
| 56 | {name: "collaborators call fails", github: true, collabFails: true, args: []string{"tidy the README"}, wantQuery: true, wantWarning: true}, |
| 57 | {name: "direct-push origin", args: []string{"tidy the README"}}, |
| 58 | {name: "pr-fix", github: true, args: []string{"--pr", "12", "address the review"}}, |
| 59 | } { |
| 60 | t.Run(tc.name, func(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | h := NewHarness(t) |
| 63 | h.AmendRepoConfig(map[string]any{"pr_reviewer": tc.configured}) |
| 64 | h.WriteStub("gh", reviewerGHStub(h, tc.collabFails)) |
| 65 | h.git(h.RepoDir, "push", "-q", "origin", "main:refs/heads/fix-branch") |
| 66 | if tc.github { |
| 67 | h.UseGitHubOrigin() |
| 68 | } |
| 69 | |
| 70 | res := h.RunKlaus(append([]string{"launch"}, tc.args...)...) |
| 71 | if res.ExitCode != 0 { |
| 72 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 73 | } |
| 74 | if got := strings.Contains(h.GHArgv(), "/collaborators"); got != tc.wantQuery { |
| 75 | t.Errorf("collaborators API called = %v, want %v; gh calls:\n%s", got, tc.wantQuery, h.GHArgv()) |
| 76 | } |
| 77 | if tc.wantNoGH && h.GHArgv() != "" { |
| 78 | t.Errorf("launch ran gh with pr_reviewer set:\n%s", h.GHArgv()) |
| 79 | } |
| 80 | warned := strings.Contains(res.Stderr, "warning: no PR reviewer") && strings.Contains(res.Stderr, "set pr_reviewer") |
| 81 | if warned != tc.wantWarning { |
| 82 | t.Errorf("reviewer warning printed = %v, want %v\nstderr:\n%s", warned, tc.wantWarning, res.Stderr) |
| 83 | } |
| 84 | |
| 85 | h.WaitForClaudeStart(30 * time.Second) |
| 86 | sysPrompt := h.WorkerSystemPrompt() |
| 87 | if tc.wantReviewer != "" { |
| 88 | if want := "Use --reviewer " + tc.wantReviewer + " when creating the PR."; !strings.Contains(sysPrompt, want) { |
| 89 | t.Errorf("system prompt missing %q:\n%s", want, sysPrompt) |
| 90 | } |
| 91 | } else if strings.Contains(sysPrompt, "--reviewer") { |
| 92 | t.Errorf("system prompt should not request a reviewer:\n%s", sysPrompt) |
| 93 | } |
| 94 | |
| 95 | h.ReleaseClaude() |
| 96 | h.WaitForState(h.RunIDs()[0], func(s *run.State) bool { return s.TmuxPane == nil && s.Worktree == "" }, 30*time.Second) |
| 97 | }) |
| 98 | } |
| 99 | } |