| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // The task page's one line says what happened in words: a stopped step |
| 13 | // with its reason (never "Stopped:" twice), and a merge without checks |
| 14 | // never claims they passed. |
| 15 | func TestTaskSummaryWords(t *testing.T) { |
| 16 | now := time.Now() |
| 17 | stopped := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageInReview, Steps: []wire.Step{{Specialist: wire.SpecialistChecks, State: wire.StepFailed, Reason: "runner_restarted", Attempt: 1, Terminated: true}}, NeedsYou: wire.NeedsRetry} |
| 18 | v := taskLineOf(stopped, "a@example.com") |
| 19 | v.richOf(stopped, "a@example.com", nil, now) |
| 20 | if got := summaryOf(stopped, v); got != "Checks stopped: the runner restarted. It waits on you." { |
| 21 | t.Fatalf("stopped: %q", got) |
| 22 | } |
| 23 | merged := wire.Task{ID: "tk2", Creator: "a@example.com", Stage: wire.StageDone, Merged: &wire.Merged{SHA: strings.Repeat("e", 40), Into: "main", WithoutChecks: true, At: now}} |
| 24 | v = taskLineOf(merged, "b@example.com") |
| 25 | v.richOf(merged, "b@example.com", nil, now) |
| 26 | if got := summaryOf(merged, v); strings.Contains(got, "passed") || !strings.Contains(got, "without checks") { |
| 27 | t.Fatalf("merged without checks: %q", got) |
| 28 | } |
| 29 | merged.Merged.WithoutChecks = false |
| 30 | if got := summaryOf(merged, v); got != "Merged into main. Checks and review passed before it went in." { |
| 31 | t.Fatalf("merged: %q", got) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // A merge the hub refused shows the person why: the stopped merge's |
| 36 | // details carry the hub's status, code and message from the runner's |
| 37 | // report, as written (the runner redacts and bounds them). |
| 38 | func TestStoppedMergeShowsTheHubAnswer(t *testing.T) { |
| 39 | now := time.Now() |
| 40 | tail := "status: 409\ncode: archived\nmessage: this branch is archived; unarchive it before merging" |
| 41 | task := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageReadyToMerge, Steps: []wire.Step{{Specialist: wire.SpecialistMerge, State: wire.StepFailed, Reason: "merge_failed", Attempt: 1, Terminated: true, LogTail: tail, FinishedAt: &now}}} |
| 42 | v := taskLineOf(task, "a@example.com") |
| 43 | v.richOf(task, "a@example.com", nil, now) |
| 44 | for _, it := range timelineOf(task, v) { |
| 45 | if it.Kind == "stopped" && it.Headline == "Amadan did not merge it." { |
| 46 | if html := string(pasteBox.Render(it.Markdown)); !strings.Contains(html, "code: archived") || !strings.Contains(html, "message: this branch is archived; unarchive it before merging") { |
| 47 | t.Fatalf("details %q", html) |
| 48 | } |
| 49 | return |
| 50 | } |
| 51 | } |
| 52 | t.Fatalf("no stopped merge in %+v", timelineOf(task, v)) |
| 53 | } |
| 54 | |
| 55 | // A merge the runner refused for want of access says what is missing, |
| 56 | // on the stage line and in the timeline, not that Amadan did not merge |
| 57 | // it; the runner's words on which precondition failed are its details. |
| 58 | func TestStoppedMergeWithoutAccess(t *testing.T) { |
| 59 | now := time.Now() |
| 60 | words := copyText("work.reason." + wire.ReasonMergeCredential) |
| 61 | if words == "" { |
| 62 | t.Fatal("no words for merge_credential") |
| 63 | } |
| 64 | tail := "error: forge: no usable forge credential for the repository" |
| 65 | task := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageReadyToMerge, Steps: []wire.Step{{Specialist: wire.SpecialistMerge, State: wire.StepFailed, Reason: wire.ReasonMergeCredential, Attempt: 1, Terminated: true, LogTail: tail, FinishedAt: &now}}} |
| 66 | v := taskLineOf(task, "a@example.com") |
| 67 | v.richOf(task, "a@example.com", nil, now) |
| 68 | if v.StageLine != fmt.Sprintf(copyText("work.stopped"), words) { |
| 69 | t.Fatalf("stage line %q", v.StageLine) |
| 70 | } |
| 71 | for _, it := range timelineOf(task, v) { |
| 72 | if it.Kind == "stopped" && it.Headline == sentenceCase(words)+"." { |
| 73 | if html := string(pasteBox.Render(it.Markdown)); !strings.Contains(html, "no usable forge credential") { |
| 74 | t.Fatalf("details %q", html) |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | } |
| 79 | t.Fatalf("no stopped merge in %+v", timelineOf(task, v)) |
| 80 | } |
| 81 | |
| 82 | // A merge stopped for want of access that waits on the person asks no |
| 83 | // "Try again?": nothing the person can retry helps until access is |
| 84 | // given. Another stopped merge still asks. |
| 85 | func TestStoppedMergeWithoutAccessAsksNoRetry(t *testing.T) { |
| 86 | now := time.Now() |
| 87 | for reason, ask := range map[string]string{wire.ReasonMergeCredential: "", "merge_forbidden": "", "merge_failed": copyText("work.decide.retry_ask")} { |
| 88 | task := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageMerging, NeedsYou: wire.NeedsRetry, Steps: []wire.Step{{Specialist: wire.SpecialistMerge, State: wire.StepFailed, Reason: reason, Attempt: 1, Terminated: true, FinishedAt: &now}}} |
| 89 | v := taskLineOf(task, "a@example.com") |
| 90 | v.richOf(task, "a@example.com", nil, now) |
| 91 | d := decisionOf(task, v) |
| 92 | if d == nil || d.Ask != ask || !strings.Contains(d.Why, copyText("work.reason."+reason)) { |
| 93 | t.Fatalf("%s: %+v", reason, d) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // A push the hub refused shows the person why: the stopped step's details |
| 99 | // carry the refusal and git's answer from the runner's report, as written |
| 100 | // (the runner redacts and bounds them). |
| 101 | func TestStoppedPushShowsTheRefusal(t *testing.T) { |
| 102 | now := time.Now() |
| 103 | tail := "refusal: refused\npush: ! refs/heads/equipo/tk1-x [remote rejected] (pre-receive hook declined) remote: you cannot push to the default branch" |
| 104 | task := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageInProgress, Steps: []wire.Step{{Specialist: wire.SpecialistEngineering, State: wire.StepFailed, Reason: "push_failed", Attempt: 1, Terminated: true, LogTail: tail, FinishedAt: &now}}} |
| 105 | v := taskLineOf(task, "a@example.com") |
| 106 | v.richOf(task, "a@example.com", nil, now) |
| 107 | for _, it := range timelineOf(task, v) { |
| 108 | if it.Kind == "stopped" && it.Headline == "The push to Amadan failed." { |
| 109 | if html := string(pasteBox.Render(it.Markdown)); !strings.Contains(html, "refusal: refused") || !strings.Contains(html, "[remote rejected] (pre-receive hook declined) remote: you cannot push to the default branch") { |
| 110 | t.Fatalf("details %q", html) |
| 111 | } |
| 112 | return |
| 113 | } |
| 114 | } |
| 115 | t.Fatalf("no stopped push in %+v", timelineOf(task, v)) |
| 116 | } |
| 117 | |
| 118 | // A timeline entry leads with one plain sentence: the specialist's own |
| 119 | // headline when it wrote one, else the first sentence that names no file; |
| 120 | // never a path, cut at a word. |
| 121 | func TestTimelineHeadlines(t *testing.T) { |
| 122 | for _, tc := range []struct{ explicit, text, want string }{ |
| 123 | {"Fixed swipe-back. Couldn't test on iOS here.", "Long summary.", "Fixed swipe-back. Couldn't test on iOS here."}, |
| 124 | {"Edited ios/Woodstar/Host.swift", "The fix lets the stack's pop gesture run.\n\nMore.", "The fix lets the stack's pop gesture run."}, |
| 125 | {"", "The fix:\n\n- `AccountNavigationHost.swift:15` now allows both gestures.\n\nSwipe-back works on every pane now. Tested nothing.", "Swipe-back works on every pane now."}, |
| 126 | {"", "**Found it:** hiding the navigation bar turned off swipe-back in panes/swipe.go. It is set in the host.", "It is set in the host."}, |
| 127 | {"", "## Summary\nHiding the navigation bar turned off swipe-back. See the host.", "Hiding the navigation bar turned off swipe-back."}, |
| 128 | {"", strings.Repeat("word ", 40), strings.TrimSpace(strings.Repeat("word ", 22)) + "…"}, |
| 129 | {"", "internal/equipo/static/live.js:88", ""}, |
| 130 | } { |
| 131 | got := headline(tc.explicit, tc.text) |
| 132 | if got != tc.want { |
| 133 | t.Fatalf("headline(%q, %q) = %q, want %q", tc.explicit, tc.text, got, tc.want) |
| 134 | } |
| 135 | if pathToken.MatchString(strings.TrimSuffix(got, "…")) { |
| 136 | t.Fatalf("a path in %q", got) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // TestTaskSummaryWaitsOnce: a task waiting on its person says why in one |
| 142 | // sentence, through "%s. It waits on %s.": never a doubled full stop, |
| 143 | // never "Needs you" beside "It waits on", for every waiting reason and |
| 144 | // for the creator and a teammate alike. |
| 145 | func TestTaskSummaryWaitsOnce(t *testing.T) { |
| 146 | now := time.Now() |
| 147 | for _, need := range []string{wire.NeedsDirection, wire.NeedsFixRounds, wire.NeedsReviewBlocked, wire.NeedsBranchMoved, wire.NeedsMergeConflict, wire.NeedsMachineRemoved} { |
| 148 | for _, viewer := range []string{"a@example.com", "b@example.com"} { |
| 149 | k := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageInReview, NeedsYou: need, Kind: wire.KindExplore} |
| 150 | if need != wire.NeedsDirection { |
| 151 | k.Kind = "" |
| 152 | } |
| 153 | v := taskLineOf(k, viewer) |
| 154 | v.richOf(k, viewer, nil, now) |
| 155 | got := summaryOf(k, v) |
| 156 | if strings.Contains(got, "..") || strings.Contains(got, "Needs you") || strings.Count(got, "It waits on") != 1 || strings.Count(got, ".") != 2 || !strings.HasSuffix(got, ".") { |
| 157 | t.Errorf("%s for %s: %q", need, viewer, got) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | // Any reason without a phrase of its own still loses its full stop. |
| 162 | k := wire.Task{Stage: wire.StageInReview, NeedsYou: "a_new_need"} |
| 163 | if got := waitReason(k, taskLine{StageLine: "Something else."}); got != "Something else" { |
| 164 | t.Fatalf("fallback %q", got) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestRosterCancelledHoldIsReport(t *testing.T) { |
| 169 | task := wire.Task{ID: "tk_roster", Title: "Cancelled work", Creator: "owner@example.com", Stage: wire.StageCancelled, CopyHold: true} |
| 170 | v := cardView(wire.Card{ID: "c_roster", Kind: wire.CardTask, TaskID: task.ID}, nil, map[string]wire.Task{task.ID: task}) |
| 171 | v.Receipt = true |
| 172 | if v.Interactive() || v.Task.CopyHold { |
| 173 | t.Fatal("cancelled task still asks about its old Copy hold") |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // An Engineering step that ends with no change says why in the words |
| 178 | // that fit: nothing needed changing on a first pass; no further changes |
| 179 | // to the candidate once an earlier round pushed one; and Equipo's refusal |
| 180 | // when the step's tool requests were refused. The summary, the decision |
| 181 | // card, the stage line and the timeline agree. |
| 182 | func TestStoppedEngineeringNoChangeWords(t *testing.T) { |
| 183 | now := time.Now() |
| 184 | cand := strings.Repeat("c", 40) |
| 185 | pushed := wire.Step{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, CandidateSHA: cand, Pushed: true, Attempt: 1, Terminated: true, FinishedAt: &now} |
| 186 | checked := wire.Step{ID: "s2", Specialist: wire.SpecialistChecks, State: wire.StepDone, CandidateSHA: cand, Attempt: 1, Terminated: true, FinishedAt: &now} |
| 187 | stopped := func(id, reason string) wire.Step { |
| 188 | return wire.Step{ID: id, Specialist: wire.SpecialistEngineering, State: wire.StepFailed, Reason: reason, Attempt: 1, Terminated: true, FinishedAt: &now} |
| 189 | } |
| 190 | for _, tc := range []struct { |
| 191 | name string |
| 192 | steps []wire.Step |
| 193 | candidate string |
| 194 | words string |
| 195 | }{ |
| 196 | {"first pass, nothing to change", []wire.Step{stopped("s1", "no_changes")}, "", "nothing needed changing"}, |
| 197 | {"a later round, a candidate already", []wire.Step{pushed, checked, stopped("s3", "no_changes")}, cand, "it made no further changes to the candidate"}, |
| 198 | {"tools refused, first pass", []wire.Step{stopped("s1", "tools_refused")}, "", "Equipo refused the commands it needed to run"}, |
| 199 | {"tools refused, a candidate already", []wire.Step{pushed, checked, stopped("s3", "tools_refused")}, cand, "Equipo refused the commands it needed to run"}, |
| 200 | } { |
| 201 | t.Run(tc.name, func(t *testing.T) { |
| 202 | k := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageInProgress, Steps: tc.steps, CandidateSHA: tc.candidate, NeedsYou: wire.NeedsRetry} |
| 203 | v := taskLineOf(k, "a@example.com") |
| 204 | v.richOf(k, "a@example.com", nil, now) |
| 205 | if got, want := summaryOf(k, v), "Engineering stopped: "+tc.words+". It waits on you."; got != want { |
| 206 | t.Errorf("summary %q, want %q", got, want) |
| 207 | } |
| 208 | if d := decisionOf(k, v); d == nil || d.Why != "Engineering stopped: "+tc.words+", on attempt 1." { |
| 209 | t.Errorf("decision %+v", d) |
| 210 | } |
| 211 | if v.StageLine != "Stopped: "+tc.words { |
| 212 | t.Errorf("stage line %q", v.StageLine) |
| 213 | } |
| 214 | found := false |
| 215 | for _, it := range timelineOf(k, v) { |
| 216 | if it.Kind == "stopped" { |
| 217 | found = true |
| 218 | if it.Headline != sentenceCase(tc.words)+"." { |
| 219 | t.Errorf("timeline %q", it.Headline) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | if !found { |
| 224 | t.Errorf("no stopped entry in %+v", timelineOf(k, v)) |
| 225 | } |
| 226 | }) |
| 227 | } |
| 228 | // The timeline words follow the candidate as it stood when that step |
| 229 | // stopped: a first pass that changed nothing still says nothing |
| 230 | // needed changing after a later round pushed a candidate. |
| 231 | k := wire.Task{ID: "tk1", Creator: "a@example.com", Stage: wire.StageInReview, CandidateSHA: cand, Steps: []wire.Step{stopped("s0", "no_changes"), pushed, checked}} |
| 232 | v := taskLineOf(k, "a@example.com") |
| 233 | v.richOf(k, "a@example.com", nil, now) |
| 234 | for _, it := range timelineOf(k, v) { |
| 235 | if it.Kind == "stopped" && it.Headline != "Nothing needed changing." { |
| 236 | t.Errorf("earlier stop %q", it.Headline) |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |