| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | // TestProvenanceTrailersAcrossPRFix drives two real launches: a fresh run |
| 17 | // commits through its worktree's hook and opens PR #4242 (per the stub log), |
| 18 | // then a --pr run on that PR inherits the change ID and amends the commit. |
| 19 | // The commit ends up with one Change-Id and both runs' Klaus-Run trailers. |
| 20 | func TestProvenanceTrailersAcrossPRFix(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | h := NewHarness(t) |
| 23 | |
| 24 | res := h.RunKlaus("launch", "add a health check endpoint") |
| 25 | if res.ExitCode != 0 { |
| 26 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 27 | } |
| 28 | first, err := h.ReadState(h.RunIDs()[0]) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if !run.ValidChangeID(first.ChangeID) { |
| 33 | t.Fatalf("fresh run's change_id = %q, want a new jj-form ID", first.ChangeID) |
| 34 | } |
| 35 | if !strings.Contains(res.Stdout, "change: "+first.ChangeID+" (new)") { |
| 36 | t.Errorf("launch output should name the new change ID:\n%s", res.Stdout) |
| 37 | } |
| 38 | |
| 39 | // The agent commits and pushes; the hook stamps the commit. |
| 40 | h.WaitForClaudeStart(30 * time.Second) |
| 41 | if err := os.WriteFile(filepath.Join(first.Worktree, "health.go"), []byte("package health\n"), 0o644); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | h.git(first.Worktree, "add", "-A") |
| 45 | h.git(first.Worktree, "commit", "-qm", "Add a health check") |
| 46 | h.git(first.Worktree, "push", "-q", "origin", first.Branch) |
| 47 | wantFirst := "Change-Id: " + first.ChangeID + "\nKlaus-Run: " + first.ID |
| 48 | if got := h.GitOutput(first.Worktree, "log", "-1", "--format=%(trailers:only,unfold)"); got != wantFirst { |
| 49 | t.Errorf("first commit trailers = %q, want %q", got, wantFirst) |
| 50 | } |
| 51 | if shared := h.GitOutput(h.RepoDir, "config", "--local", "--list"); strings.Contains(shared, "core.hookspath=") { |
| 52 | t.Errorf("shared config should have no core.hooksPath:\n%s", shared) |
| 53 | } |
| 54 | |
| 55 | h.ReleaseClaude() |
| 56 | h.WaitForState(first.ID, func(s *run.State) bool { return s.TmuxPane == nil && s.Worktree == "" }, 30*time.Second) |
| 57 | |
| 58 | // gh answers for PR #4242, whose head is the first run's branch. |
| 59 | h.WriteStub("gh", fmt.Sprintf(`#!/usr/bin/env bash |
| 60 | case "$*" in |
| 61 | *headRefName*) echo %q ;; |
| 62 | *"--json url"*) echo https://github.com/acme/widget/pull/4242 ;; |
| 63 | esac |
| 64 | exit 0 |
| 65 | `, first.Branch)) |
| 66 | for _, f := range []string{"claude.release", "claude.started"} { |
| 67 | os.Remove(filepath.Join(h.E2EDir, f)) |
| 68 | } |
| 69 | |
| 70 | res = h.RunKlaus("launch", "--pr", "4242", "address review") |
| 71 | if res.ExitCode != 0 { |
| 72 | t.Fatalf("launch --pr exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 73 | } |
| 74 | var fix *run.State |
| 75 | for _, id := range h.RunIDs() { |
| 76 | if id != first.ID { |
| 77 | fix, _ = h.ReadState(id) |
| 78 | } |
| 79 | } |
| 80 | if fix == nil { |
| 81 | t.Fatalf("no --pr run recorded: %v", h.RunIDs()) |
| 82 | } |
| 83 | if fix.ChangeID != first.ChangeID { |
| 84 | t.Fatalf("--pr run change_id = %q, want the opener's %q\nstdout:\n%s", fix.ChangeID, first.ChangeID, res.Stdout) |
| 85 | } |
| 86 | |
| 87 | h.WaitForClaudeStart(30 * time.Second) |
| 88 | h.git(fix.Worktree, "commit", "-q", "--amend", "--no-edit") |
| 89 | want := wantFirst + "\nKlaus-Run: " + fix.ID |
| 90 | if got := h.GitOutput(fix.Worktree, "log", "-1", "--format=%(trailers:only,unfold)"); got != want { |
| 91 | t.Errorf("amended commit trailers = %q, want %q", got, want) |
| 92 | } |
| 93 | |
| 94 | h.ReleaseClaude() |
| 95 | h.WaitForState(fix.ID, func(s *run.State) bool { return s.TmuxPane == nil }, 30*time.Second) |
| 96 | } |