gunk-dev/the-valley

reported_once_test.go

sourcec14b0e68e9c3main's published tip history
5.5 KiB
1package pipeline
2
3import (
4 "context"
5 "fmt"
6 "testing"
7
8 "the-valley/flow/internal/event"
9 "the-valley/flow/internal/run"
10)
11
12func prURL(n string) string { return "https://github.com/maintainer/klaus/pull/" + n }
13
14// prEvents returns the pr:approved and pr:merged events in the session's log.
15func prEvents(t *testing.T, dir string) []event.Event {
16 t.Helper()
17 var out []event.Event
18 for _, e := range readEvents(t, dir) {
19 if e.Type == event.PRApproved || e.Type == event.PRMerged {
20 out = append(out, e)
21 }
22 }
23 return out
24}
25
26// TestLongMergedPRsAreNotReReported replays the 2026-10-21 incident: run
27// states for PRs merged days earlier, approved through klaus, polled every
28// pass. When GitHub briefly couldn't be asked for their state (UNKNOWN) while
29// the other queries still answered, the pipeline took them for open, tracked
30// them afresh, reported them approved, tried to merge them, and reported them
31// merged again once the next poll got through. None of that may happen, on
32// any pass or after a restart.
33func TestLongMergedPRsAreNotReReported(t *testing.T) {
34 dir := t.TempDir()
35 approved := true
36 var runStates []*run.State
37 for _, n := range []string{"329", "330", "331"} {
38 u := prURL(n)
39 runStates = append(runStates, &run.State{ID: "run-" + n, PRURL: &u, Approved: &approved})
40 }
41 statuses := func(state string) map[string]*PRStatus {
42 out := make(map[string]*PRStatus)
43 for _, n := range []string{"329", "330", "331"} {
44 s := &PRStatus{PRNumber: n, PRURL: prURL(n), State: state, TargetRepo: "klaus"}
45 if state == "UNKNOWN" {
46 s.CI, s.Conflicts = "passing", "none"
47 }
48 out[n] = s
49 }
50 return out
51 }
52
53 for restart := 0; restart < 2; restart++ {
54 c := controllerIn(t, dir)
55 c.SetAutoMergeOnApproval(true)
56 merges := 0
57 c.SetMergePRs(func(context.Context, string, []string) error {
58 merges++
59 return nil
60 })
61
62 for _, state := range []string{"MERGED", "UNKNOWN", "MERGED", "UNKNOWN", "UNKNOWN", "MERGED"} {
63 c.HandleGHStatus(context.Background(), statuses(state), runStates)
64 if n := len(c.PipelineStates()); n != 0 {
65 t.Fatalf("restart %d, %s pass: %d long-merged PRs tracked, want none", restart, state, n)
66 }
67 }
68 if merges != 0 {
69 t.Errorf("restart %d: %d merge attempts on long-merged PRs", restart, merges)
70 }
71 }
72 if evts := readEvents(t, dir); len(evts) != 0 {
73 t.Errorf("long-merged PRs emitted %d events, want none: %+v", len(evts), evts)
74 }
75}
76
77// TestNewTransitionsAreReportedOnce: a PR klaus sees open, then approved,
78// then merged, reports each once, through UNKNOWN passes, repeated polls and
79// restarts.
80func TestNewTransitionsAreReportedOnce(t *testing.T) {
81 dir := t.TempDir()
82 status := &PRStatus{PRNumber: "336", PRURL: prURL("336"), State: "OPEN", CI: "pending", TargetRepo: "klaus"}
83 statuses := map[string]*PRStatus{"336": status}
84 poll := func(c *Controller, state string) {
85 status.State = state
86 c.HandleGHStatus(context.Background(), statuses, nil)
87 }
88
89 c := controllerIn(t, dir)
90 poll(c, "OPEN")
91 status.CI, status.Conflicts, status.ReviewDecision = "passing", "none", "APPROVED"
92 poll(c, "OPEN")
93 poll(c, "UNKNOWN")
94 poll(c, "OPEN")
95
96 c = controllerIn(t, dir)
97 poll(c, "OPEN")
98 poll(c, "MERGED")
99 poll(c, "UNKNOWN")
100 poll(c, "MERGED")
101
102 c = controllerIn(t, dir)
103 poll(c, "MERGED")
104 poll(c, "UNKNOWN")
105 poll(c, "MERGED")
106
107 evts := prEvents(t, dir)
108 var got []string
109 for _, e := range evts {
110 got = append(got, fmt.Sprintf("%s %v", e.Type, e.Data["pr_url"]))
111 }
112 want := []string{"pr:approved " + prURL("336"), "pr:merged " + prURL("336")}
113 if fmt.Sprint(got) != fmt.Sprint(want) {
114 t.Errorf("events = %q, want %q", got, want)
115 }
116}
117
118// TestMergeWhileDashboardDownIsReported: klaus saw the PR open, the dashboard
119// was down when it merged, so the restarted dashboard's first look at it is
120// MERGED. That merge was never reported; it is, once.
121func TestMergeWhileDashboardDownIsReported(t *testing.T) {
122 dir := t.TempDir()
123 statuses := map[string]*PRStatus{
124 "337": {PRNumber: "337", PRURL: prURL("337"), State: "OPEN", CI: "pending", TargetRepo: "klaus"},
125 }
126 controllerIn(t, dir).HandleGHStatus(context.Background(), statuses, nil)
127
128 statuses["337"] = &PRStatus{PRNumber: "337", PRURL: prURL("337"), State: "MERGED"}
129 for restart := 0; restart < 2; restart++ {
130 controllerIn(t, dir).HandleGHStatus(context.Background(), statuses, nil)
131 }
132
133 if got := countEvents(prEvents(t, dir), event.PRMerged); got != 1 {
134 t.Errorf("pr:merged emitted %d times, want 1", got)
135 }
136}
137
138// TestAutoMergedPRIsReportedOnce: a merge klaus made itself is reported when
139// the merge returns, and not again when the next poll sees it merged.
140func TestAutoMergedPRIsReportedOnce(t *testing.T) {
141 dir := t.TempDir()
142 c := controllerIn(t, dir)
143 c.SetAutoMergeOnApproval(true)
144 c.SetMergePRs(func(context.Context, string, []string) error { return nil })
145
146 statuses := map[string]*PRStatus{
147 "338": {PRNumber: "338", PRURL: prURL("338"), State: "OPEN", CI: "passing",
148 Conflicts: "none", ReviewDecision: "APPROVED", TargetRepo: "klaus"},
149 }
150 c.HandleGHStatus(context.Background(), statuses, nil)
151 statuses["338"] = &PRStatus{PRNumber: "338", PRURL: prURL("338"), State: "MERGED"}
152 c.HandleGHStatus(context.Background(), statuses, nil)
153 controllerIn(t, dir).HandleGHStatus(context.Background(), statuses, nil)
154
155 var merged []event.Event
156 for _, e := range prEvents(t, dir) {
157 if e.Type == event.PRMerged {
158 merged = append(merged, e)
159 }
160 }
161 if len(merged) != 1 {
162 t.Fatalf("pr:merged emitted %d times, want 1", len(merged))
163 }
164 if got := merged[0].Data["pr_url"]; got != prURL("338") {
165 t.Errorf("pr:merged pr_url = %v, want %s", got, prURL("338"))
166 }
167}