| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // barberLike is staging's barber task as it was: a change approved with |
| 11 | // a preview config, its preview failed (no credential), Paul's Merge on |
| 12 | // the card failed at the hub; then the start-up switch made it an |
| 13 | // exploration. |
| 14 | func (r *toolRig) barberLike(key string) string { |
| 15 | r.t.Helper() |
| 16 | r.asksBeforeMerging() |
| 17 | r.previewsOn() |
| 18 | id, pv := r.approvedWithPreview(key, baseX, candX, true) |
| 19 | if pv == nil { |
| 20 | r.t.Fatal("no preview") |
| 21 | } |
| 22 | r.step(key+"-pv-fail", wire.StepReport{TaskID: id, StepID: pv.StepID, Attempt: 1, State: wire.StepFailed, Reason: "preview_failed", BaseSHA: baseX, CandidateSHA: candX, Termination: true, Findings: []wire.Finding{}}) |
| 23 | card := r.mergeCardsWait(id) |
| 24 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 25 | r.t.Fatal(err) |
| 26 | } |
| 27 | m := r.dispatched(wire.SpecialistMerge, baseX, candX, 0) |
| 28 | r.step(key+"-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX, CandidateSHA: candX}) |
| 29 | r.step(key+"-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: baseX, CandidateSHA: candX, Termination: true}) |
| 30 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 31 | if switched, err := r.inst.Store.Chat().ExploreOnce(r.ctx, id); err != nil || !switched { |
| 32 | r.t.Fatalf("switch: %t %v", switched, err) |
| 33 | } |
| 34 | r.directionCard(id) |
| 35 | return id |
| 36 | } |
| 37 | |
| 38 | // TestExploreNeedsYou: an exploration waiting for its direction is in |
| 39 | // Needs you, the API's and the page's, and counted on the rail. |
| 40 | func TestExploreNeedsYou(t *testing.T) { |
| 41 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 42 | id := r.barberLike("ny") |
| 43 | if k := r.task(id); k.NeedsYou != wire.NeedsDirection { |
| 44 | t.Fatalf("needs %q", k.NeedsYou) |
| 45 | } |
| 46 | needs, err := r.client.NeedsYou(r.ctx) |
| 47 | if err != nil || len(needs.Items) != 1 || needs.Items[0].ID != id { |
| 48 | t.Fatalf("API Needs you %+v %v", needs.Items, err) |
| 49 | } |
| 50 | page := r.get(r.inst.Cookie(), "/tasks?needs=you", 200) |
| 51 | _, item, ok := strings.Cut(page, `data-decision="`+id+`"`) |
| 52 | if !ok { |
| 53 | t.Fatalf("Needs you page lacks the decision: %s", page) |
| 54 | } |
| 55 | // The question opens it; the preview line and the branch link sit |
| 56 | // under it, beside the answers. |
| 57 | q, line, link, answer := strings.Index(item, "What next for Barber mockups?"), strings.Index(item, "There's no preview of this version."), strings.Index(item, "Open the branch on Amadan"), strings.Index(item, `value="build"`) |
| 58 | if q < 0 || line < 0 || link < 0 || answer < 0 || !(q < line && line < link && link < answer) { |
| 59 | t.Fatalf("order: question %d, line %d, link %d, answer %d in %s", q, line, link, answer, item[:min(len(item), 3000)]) |
| 60 | } |
| 61 | if before, _, _ := strings.Cut(item, "What next for"); strings.Contains(before, "preview") || strings.Contains(before, "Amadan") { |
| 62 | t.Fatalf("something before the question: %s", before) |
| 63 | } |
| 64 | // The task's line: one sentence, through "It waits on you". |
| 65 | task := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 66 | if strings.Contains(task, "..") || strings.Contains(task, "waiting for your choice. It waits") || strings.Count(task, "It waits on you.") == 0 { |
| 67 | t.Fatal("the waiting line doubles up") |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // trackOf is the full stage track on a task page: its stops' labels and |
| 72 | // the current one. |
| 73 | func trackOf(t *testing.T, page string) (stops []string, current string) { |
| 74 | t.Helper() |
| 75 | _, rest, ok := strings.Cut(page, "data-track") |
| 76 | if !ok { |
| 77 | t.Fatal("no track") |
| 78 | } |
| 79 | rest, _, _ = strings.Cut(rest, "</ol>") |
| 80 | for _, li := range strings.Split(rest, "<li")[1:] { |
| 81 | label := li |
| 82 | if i := strings.LastIndex(label, "</span>"); i >= 0 { |
| 83 | label = label[:i] |
| 84 | } |
| 85 | if i := strings.LastIndex(label, ">"); i >= 0 { |
| 86 | label = label[i+1:] |
| 87 | } |
| 88 | stops = append(stops, label) |
| 89 | if strings.Contains(li, `aria-current="step"`) { |
| 90 | current = label |
| 91 | } |
| 92 | } |
| 93 | return stops, current |
| 94 | } |
| 95 | |
| 96 | // TestExploreTrackAndHistory: an exploration's track has its own stops, |
| 97 | // never a merge stage, and stands at Preview while it waits for its |
| 98 | // direction; the merge it had before the switch is neither a step nor a |
| 99 | // stopped line anywhere, only one line saying it was dropped. |
| 100 | func TestExploreTrackAndHistory(t *testing.T) { |
| 101 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 102 | id := r.barberLike("tr") |
| 103 | page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 104 | stops, current := trackOf(t, page) |
| 105 | if strings.Join(stops, ",") != "Noted,Looking into it,In progress,Review,Preview,Decided" || current != "Preview" { |
| 106 | t.Fatalf("track %q, current %q", stops, current) |
| 107 | } |
| 108 | conv := r.get(r.inst.Cookie(), "/conversation", 200) |
| 109 | list := r.get(r.inst.Cookie(), "/tasks", 200) |
| 110 | for _, p := range []string{page, conv, list} { |
| 111 | for _, bad := range []string{"Ready to merge", "Amadan did not merge it", "Merge stopped", "Merging"} { |
| 112 | if i := strings.Index(p, bad); i >= 0 { |
| 113 | t.Fatalf("a page says %q: ...%s...", bad, p[max(0, i-300):min(len(p), i+100)]) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | if !strings.Contains(page, "An earlier attempt to add this change was dropped when the task became an exploration.") || !strings.Contains(conv, "An earlier attempt to add this change was dropped when the task became an exploration.") { |
| 118 | t.Fatal("no line for the dropped merge") |
| 119 | } |
| 120 | // A change keeps its own track. |
| 121 | change := r.judgedTask("tc", "Fix the README typo", baseX, strings.Repeat("7", 40), wire.ChecksPassed, "") |
| 122 | if stops, _ := trackOf(t, r.get(r.inst.Cookie(), "/tasks/"+change, 200)); stops[4] != "Ready to merge" { |
| 123 | t.Fatalf("a change's track %q", stops) |
| 124 | } |
| 125 | } |
| 126 | |