| 1 | package report |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "the-valley/flow/internal/event" |
| 10 | ) |
| 11 | |
| 12 | var day = time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC) |
| 13 | |
| 14 | // at is a time on the test's day, such as "10:30". |
| 15 | func at(t *testing.T, hhmm string) time.Time { |
| 16 | t.Helper() |
| 17 | d, err := time.Parse("15:04", hhmm) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | return day.Add(time.Duration(d.Hour())*time.Hour + time.Duration(d.Minute())*time.Minute) |
| 22 | } |
| 23 | |
| 24 | func ev(ts time.Time, runID, typ string, data map[string]interface{}) event.Event { |
| 25 | return event.Event{Timestamp: ts.Format(time.RFC3339), RunID: runID, Type: typ, Data: data} |
| 26 | } |
| 27 | |
| 28 | // fields are a run event's keys: repo_key, change_id, pr_number when pr |
| 29 | // isn't "", and dispatch_reason. |
| 30 | func fields(repo, change, pr, reason string) map[string]interface{} { |
| 31 | f := map[string]interface{}{"repo_key": repo, "change_id": change, "dispatch_reason": reason} |
| 32 | if pr != "" { |
| 33 | f["pr_number"] = pr |
| 34 | } |
| 35 | return f |
| 36 | } |
| 37 | |
| 38 | // newRun is a run's events in the current format: agent:started at start |
| 39 | // and, unless end is zero, the finalize phase ending at end. |
| 40 | func newRun(id string, start, end time.Time, f map[string]interface{}) []event.Event { |
| 41 | evts := []event.Event{ev(start, id, event.AgentStarted, f)} |
| 42 | if !end.IsZero() { |
| 43 | data := map[string]interface{}{"phase": event.PhaseFinalize, "started_at": end.Format(time.RFC3339Nano), "ended_at": end.Format(time.RFC3339Nano), "duration_ms": 0} |
| 44 | for k, v := range f { |
| 45 | data[k] = v |
| 46 | } |
| 47 | evts = append(evts, ev(end, id, event.PhaseCompleted, data)) |
| 48 | } |
| 49 | return evts |
| 50 | } |
| 51 | |
| 52 | // build reports on one session's events, read back as from its log. |
| 53 | func build(t *testing.T, evts ...[]event.Event) *Report { |
| 54 | t.Helper() |
| 55 | var all []event.Event |
| 56 | for _, e := range evts { |
| 57 | all = append(all, e...) |
| 58 | } |
| 59 | b, err := json.Marshal(all) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | var read []event.Event |
| 64 | if err := json.Unmarshal(b, &read); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | return Build([][]event.Event{read}, Options{}) |
| 68 | } |
| 69 | |
| 70 | func changesByKey(rep *Report) map[string]Change { |
| 71 | m := map[string]Change{} |
| 72 | for _, c := range rep.Changes { |
| 73 | m[c.Key] = c |
| 74 | } |
| 75 | return m |
| 76 | } |
| 77 | |
| 78 | // TestWaitIsBetweenTheChangesRuns runs four runs on one change: A 10:00 to |
| 79 | // 12:00, B 10:30 to 11:00 within it, C 11:30 to 12:30, while A still runs, |
| 80 | // and D from 13:00, which never ends; then E at 14:00. The change waits only |
| 81 | // from 12:30, when A and C have both ended, to D, and how long it waited for |
| 82 | // E is unknown. |
| 83 | func TestWaitIsBetweenTheChangesRuns(t *testing.T) { |
| 84 | f := fields("acme/widget", "c1", "", event.DispatchLaunch) |
| 85 | rep := build(t, |
| 86 | newRun("A", at(t, "10:00"), at(t, "12:00"), f), |
| 87 | newRun("B", at(t, "10:30"), at(t, "11:00"), f), |
| 88 | newRun("C", at(t, "11:30"), at(t, "12:30"), f), |
| 89 | newRun("D", at(t, "13:00"), time.Time{}, f), |
| 90 | newRun("E", at(t, "14:00"), at(t, "15:00"), f), |
| 91 | ) |
| 92 | if len(rep.Changes) != 1 { |
| 93 | t.Fatalf("changes %+v, want one", rep.Changes) |
| 94 | } |
| 95 | var waits []int64 |
| 96 | var unknown []bool |
| 97 | for _, fu := range rep.Changes[0].FollowUps { |
| 98 | waits = append(waits, fu.WaitMS) |
| 99 | unknown = append(unknown, fu.WaitUnknown) |
| 100 | } |
| 101 | wantWaits, wantUnknown := []int64{0, 0, (30 * time.Minute).Milliseconds(), 0}, []bool{false, false, false, true} |
| 102 | for i := range wantWaits { |
| 103 | if len(waits) != len(wantWaits) || waits[i] != wantWaits[i] || unknown[i] != wantUnknown[i] { |
| 104 | t.Fatalf("follow-up waits %v unknown %v, want %v unknown %v", waits, unknown, wantWaits, wantUnknown) |
| 105 | } |
| 106 | } |
| 107 | if o := rep.Overall; o.WaitMS != (30*time.Minute).Milliseconds() || o.UnknownWaits != 1 { |
| 108 | t.Errorf("overall wait %dms, unknown %d; want 30m, 1", o.WaitMS, o.UnknownWaits) |
| 109 | } |
| 110 | var text strings.Builder |
| 111 | if err := rep.WriteText(&text); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if want := "1 follow-up(s) started after a run with no end on their change: their wait is unknown."; !strings.Contains(text.String(), want) { |
| 115 | t.Errorf("table should say %q:\n%s", want, text.String()) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // TestChangeIdentities groups runs whose events name their change in |
| 120 | // different ways: |
| 121 | // |
| 122 | // - PR 9: an old run that knows only its PR (named "widget" until its PR |
| 123 | // URL says acme/widget), and a ci-fix run with the PR and a change ID, |
| 124 | // which the pipeline then approves |
| 125 | // - change "dup": a Change-Id copied into acme/widget and acme/gizmo |
| 126 | // - change c10 on a renamed repo: the first run starts as acme/gadget-old |
| 127 | // and finalizes as acme/gadget with PR 3, a follow-up still says |
| 128 | // acme/gadget-old, and the approval says ACME/Gadget |
| 129 | func TestChangeIdentities(t *testing.T) { |
| 130 | old := []event.Event{ |
| 131 | ev(at(t, "08:00"), "r3", event.AgentStarted, map[string]interface{}{"id": "r3", "target_repo": "widget"}), |
| 132 | ev(at(t, "09:00"), "r3", event.AgentCompleted, map[string]interface{}{"id": "r3"}), |
| 133 | ev(at(t, "09:00"), "r3", event.AgentPRCreated, map[string]interface{}{"id": "r3", "pr_url": "https://github.com/acme/widget/pull/9", "pr_number": "9"}), |
| 134 | } |
| 135 | approve := func(ts time.Time, url, pr string) []event.Event { |
| 136 | return []event.Event{ev(ts, pr, event.PRApproved, map[string]interface{}{"pr_url": url, "pr_number": pr})} |
| 137 | } |
| 138 | renamed := newRun("n1", at(t, "10:00"), time.Time{}, fields("acme/gadget-old", "c10", "", event.DispatchLaunch)) |
| 139 | renamed = append(renamed, newRun("n1", at(t, "10:00"), at(t, "10:30"), fields("acme/gadget", "c10", "3", event.DispatchLaunch))[1:]...) |
| 140 | rep := build(t, |
| 141 | old, |
| 142 | newRun("r7", at(t, "11:00"), at(t, "11:30"), fields("acme/widget", "c9", "9", event.DispatchCIFix)), |
| 143 | approve(at(t, "11:40"), "https://github.com/acme/widget/pull/9", "9"), |
| 144 | newRun("g1", at(t, "08:00"), at(t, "08:30"), fields("acme/widget", "dup", "", event.DispatchLaunch)), |
| 145 | newRun("g2", at(t, "08:00"), at(t, "08:30"), fields("acme/gizmo", "dup", "", event.DispatchLaunch)), |
| 146 | renamed, |
| 147 | newRun("n2", at(t, "11:00"), at(t, "11:20"), fields("acme/gadget-old", "c10", "", event.DispatchChangeFollowUp)), |
| 148 | approve(at(t, "12:00"), "https://github.com/ACME/Gadget/pull/3", "3"), |
| 149 | ) |
| 150 | |
| 151 | changes := changesByKey(rep) |
| 152 | if len(rep.Changes) != 4 { |
| 153 | t.Errorf("changes %+v, want acme/widget#9, acme/widget@dup, acme/gizmo@dup and acme/gadget#3", rep.Changes) |
| 154 | } |
| 155 | if c := changes["acme/widget#9"]; c.Runs != 2 || len(c.FollowUps) != 1 || c.FollowUps[0].RunID != "r7" || |
| 156 | c.WaitMS != (2*time.Hour).Milliseconds() || c.ApprovalWaitMS != (10*time.Minute).Milliseconds() { |
| 157 | t.Errorf("acme/widget#9 %+v, want r3 then r7 2h later, approved 10m after", c) |
| 158 | } |
| 159 | for _, k := range []string{"acme/widget@dup", "acme/gizmo@dup"} { |
| 160 | if c := changes[k]; c.Runs != 1 || len(c.FollowUps) != 0 { |
| 161 | t.Errorf("%s %+v, want its own run only", k, c) |
| 162 | } |
| 163 | } |
| 164 | if c := changes["acme/gadget#3"]; c.Runs != 2 || len(c.FollowUps) != 1 || c.FollowUps[0].RunID != "n2" || |
| 165 | c.ApprovalWaitMS != (40*time.Minute).Milliseconds() { |
| 166 | t.Errorf("acme/gadget#3 %+v, want n1 then n2, approved 40m after n2", c) |
| 167 | } |
| 168 | if o := rep.Overall; o.Changes != 4 || o.FollowUps != 2 || o.Approvals != 2 { |
| 169 | t.Errorf("overall changes %d, follow-ups %d, approvals %d; want 4, 2, 2", o.Changes, o.FollowUps, o.Approvals) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // TestCheckIncludingPreReview runs a command line of go test and |
| 174 | // _pre-review from 10:00 to 10:05, whose pre-review reports itself from |
| 175 | // 10:01, and a lint alongside it from 10:02 to 10:03. The test check keeps |
| 176 | // only the minute before the pre-review; the lint, which ran no pre-review, |
| 177 | // keeps its own. |
| 178 | func TestCheckIncludingPreReview(t *testing.T) { |
| 179 | f := fields("acme/widget", "c1", "", event.DispatchLaunch) |
| 180 | check := func(kind string, start, end time.Time, extra map[string]interface{}) event.Event { |
| 181 | data := map[string]interface{}{"kind": kind, "started_at": start.Format(time.RFC3339Nano), "ended_at": end.Format(time.RFC3339Nano), "duration_ms": end.Sub(start).Milliseconds()} |
| 182 | for k, v := range extra { |
| 183 | data[k] = v |
| 184 | } |
| 185 | return ev(end, "r1", event.CheckCompleted, data) |
| 186 | } |
| 187 | rep := build(t, |
| 188 | newRun("r1", at(t, "09:50"), at(t, "10:10"), f), |
| 189 | []event.Event{ |
| 190 | check(event.CheckLint, at(t, "10:02"), at(t, "10:03"), nil), |
| 191 | check(event.CheckPreReview, at(t, "10:01"), at(t, "10:05"), map[string]interface{}{"source": "pre-review"}), |
| 192 | check(event.CheckTest, at(t, "10:00"), at(t, "10:05"), map[string]interface{}{"source": "stream", "includes_pre_review": true}), |
| 193 | }, |
| 194 | ) |
| 195 | checks := rep.Overall.Checks |
| 196 | for kind, want := range map[string]time.Duration{event.CheckTest: time.Minute, event.CheckPreReview: 4 * time.Minute, event.CheckLint: time.Minute} { |
| 197 | if c := checks[kind]; c == nil || c.Count != 1 || c.MS != want.Milliseconds() { |
| 198 | t.Errorf("%s checks %+v, want one of %v", kind, c, want) |
| 199 | } |
| 200 | } |
| 201 | } |