| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // legacyMacChecks is an iOS change's checks as an instance before |
| 12 | // 2026-11-28 placed them with no Mac enrolled: on the Linux primary, which |
| 13 | // judged them not run (needs_macos). An instance now leaves them waiting |
| 14 | // for a Mac runner (waiting_mac); the tasks that reached Ready to merge |
| 15 | // that way before still wait for a Mac, and these contracts keep them. |
| 16 | // The fixture moves the waiting step onto the primary as the old |
| 17 | // placement did and returns its dispatch, for the primary to report. |
| 18 | func (r *runnersRig) legacyMacChecks(task, sha string) *wire.DispatchCommand { |
| 19 | r.t.Helper() |
| 20 | s := r.checksStep(task) |
| 21 | if s.Placement != wire.PlacementMac { |
| 22 | r.t.Fatalf("checks not waiting for a Mac: %+v", s) |
| 23 | } |
| 24 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET placement = '', waiting_since = NULL, needs = '', runner_id = ? WHERE id = ?", r.primary.fr.Welcome.RunnerID, s.ID); err != nil { |
| 25 | r.t.Fatal(err) |
| 26 | } |
| 27 | return &wire.DispatchCommand{TaskID: task, StepID: s.ID, Attempt: s.Attempt, Specialist: wire.SpecialistChecks, BaseSHA: s.BaseSHA, CandidateSHA: sha} |
| 28 | } |
| 29 | |
| 30 | // waitingForMac runs an iOS change with no Mac enrolled to Ready to merge: |
| 31 | // its checks end on the primary as not run (needs_macos, legacyMacChecks), |
| 32 | // review approves, and the merge card asks. It returns the task, its |
| 33 | // candidate and the card. |
| 34 | func (r *runnersRig) waitingForMac(title string) (task, sha string, card wire.Card) { |
| 35 | r.t.Helper() |
| 36 | task, sha = r.candidate(title, true) |
| 37 | c := r.legacyMacChecks(task, sha) |
| 38 | r.checks(task+"-c", c, wire.ChecksCannotRun, nil, "needs_macos", "") |
| 39 | _, body := r.expect(wire.FrameDispatch) |
| 40 | r.verdict(task+"-r", body.(*wire.DispatchCommand), wire.VerdictApprove) |
| 41 | r.wait("waiting for a Mac", func(c *Conversation) bool { |
| 42 | for _, k := range c.Cards { |
| 43 | if k.Kind == wire.CardMerge && k.TaskID == task && c.Tasks[task].Stage == wire.StageReadyToMerge { |
| 44 | card = k |
| 45 | return true |
| 46 | } |
| 47 | } |
| 48 | return false |
| 49 | }) |
| 50 | return task, sha, card |
| 51 | } |
| 52 | |
| 53 | // checksRuns are the task's checks steps. |
| 54 | func (r *runnersRig) checksRuns(task string) []wire.Step { |
| 55 | r.t.Helper() |
| 56 | k, err := r.client.Task(r.ctx, task) |
| 57 | if err != nil { |
| 58 | r.t.Fatal(err) |
| 59 | } |
| 60 | var out []wire.Step |
| 61 | for _, s := range k.Steps { |
| 62 | if s.Specialist == wire.SpecialistChecks { |
| 63 | out = append(out, s) |
| 64 | } |
| 65 | } |
| 66 | return out |
| 67 | } |
| 68 | |
| 69 | // macRecheck reads the Mac's dispatch of a task's checks again, on sha. |
| 70 | func macRecheck(t *testing.T, w *peer, task, sha string) *wire.DispatchCommand { |
| 71 | t.Helper() |
| 72 | _, body := w.expect(wire.FrameDispatch) |
| 73 | d := body.(*wire.DispatchCommand) |
| 74 | if d.Specialist != wire.SpecialistChecks || d.TaskID != task || d.CandidateSHA != sha || d.Attempt != 1 || strings.Join(d.Needs, ",") != "macos,xcode" { |
| 75 | t.Fatalf("checks again on the Mac %+v", d) |
| 76 | } |
| 77 | return d |
| 78 | } |
| 79 | |
| 80 | // A task waiting for a Mac (Wait for a Mac on its card) gets its checks |
| 81 | // again, on the exact candidate, the moment a Mac that fits enrols: once, |
| 82 | // a reconnect places nothing more. They pass on the Mac (sandboxed), so |
| 83 | // the result remains diagnostic until the person chooses Merge. |
| 84 | func TestContractMacRecheckOnEnrol(t *testing.T) { |
| 85 | r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) |
| 86 | project := r.project() |
| 87 | // A routine change (measured by the review) that asked only because |
| 88 | // nobody had said whether main ships; the owner says it doesn't. |
| 89 | task, sha, _ := r.waitingForMacMeasured("Swipe back on iOS") |
| 90 | r.shipsNo(project) |
| 91 | card := r.cardOf(task) |
| 92 | if out, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); err != nil || out.Card.Choice != wire.ChoiceWaitMac { |
| 93 | t.Fatalf("wait %+v %v", out, err) |
| 94 | } |
| 95 | w, _ := r.newWorker("Tito Studio") |
| 96 | d := macRecheck(t, w, task, sha) |
| 97 | if runs := r.checksRuns(task); len(runs) != 2 || runs[1].ID != d.StepID || runs[1].RunnerLabel != "Tito Studio" { |
| 98 | t.Fatalf("checks runs %+v", runs) |
| 99 | } |
| 100 | w = r.reconnectPeer(w) |
| 101 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 102 | if runs := r.checksRuns(task); len(runs) != 2 { |
| 103 | t.Fatalf("a reconnect ran the checks again: %+v", runs) |
| 104 | } |
| 105 | r.relay(w, []byte("PACK swipe back")) |
| 106 | if !w.lease(d.StepID, 1) { |
| 107 | t.Fatal("start refused") |
| 108 | } |
| 109 | pass := passed(task, d.StepID, 1, sha) |
| 110 | pass.ChecksSandbox = wire.SandboxSeatbelt |
| 111 | w.send(wire.FrameStep, pass) |
| 112 | r.wait("worker pass still needs a decision", func(c *Conversation) bool { |
| 113 | return c.Tasks[task].Stage == wire.StageReadyToMerge && c.Tasks[task].Checks == wire.ChecksPassed |
| 114 | }) |
| 115 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 116 | k, err := r.client.Task(r.ctx, task) |
| 117 | if err != nil || k.MergeDecision == nil || k.MergeDecision.Class != "ask" { |
| 118 | t.Fatalf("worker pass authorized merge: %+v %v", k, err) |
| 119 | } |
| 120 | missingBase := false |
| 121 | for _, reason := range k.MergeDecision.Reasons { |
| 122 | if reason.Name == "trusted_base_checks" && !reason.OK { |
| 123 | missingBase = true |
| 124 | } |
| 125 | } |
| 126 | if !missingBase { |
| 127 | t.Fatalf("missing base evidence not recorded: %+v", k.MergeDecision) |
| 128 | } |
| 129 | card = r.mergeCardsWait(task) |
| 130 | if card.State != wire.CardOpen { |
| 131 | t.Fatalf("merge card not open: %+v", card) |
| 132 | } |
| 133 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | _, body := r.expect(wire.FrameDispatch) |
| 137 | if m := body.(*wire.DispatchCommand); m.Specialist != wire.SpecialistMerge || m.TaskID != task || m.CandidateSHA != sha { |
| 138 | t.Fatalf("merge dispatch %+v", m) |
| 139 | } |
| 140 | if k, _ := r.client.Task(r.ctx, task); k.Stage != wire.StageMerging || k.Checks != wire.ChecksPassed || k.MergeDecision == nil || !strings.HasPrefix(k.MergeDecision.DecidedBy, "person:") { |
| 141 | t.Fatalf("after the Mac's pass %+v", k) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Only tasks still waiting: a Linux runner enrolling places nothing; a |
| 146 | // Mac enrolling skips cancelled tasks, merged ones, ones whose person |
| 147 | // chose Merge (without checks, even back at Ready after the hub refused |
| 148 | // it) and ones whose branch moved off the |
| 149 | // candidate, and re-runs only the one still waiting (left with its card |
| 150 | // open, which stays open: it asked for more than checks alone). |
| 151 | func TestContractMacRecheckOnlyWaiting(t *testing.T) { |
| 152 | r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) |
| 153 | waiting, sha, open := r.waitingForMac("Still waiting") |
| 154 | cancelled, _, _ := r.waitingForMac("Cancelled one") |
| 155 | if _, err := r.client.CancelTask(r.ctx, cancelled); err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | merged, _, mc := r.waitingForMac("Merged") |
| 159 | if _, err := r.client.Answer(r.ctx, mc.ID, wire.ChoiceMerge, mc.Revision); err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | _, body := r.expect(wire.FrameDispatch) |
| 163 | r.mergedStep("merged-m", body.(*wire.DispatchCommand), strings.Repeat("f", 40)) |
| 164 | chosen, _, cc := r.waitingForMac("Merge chosen") |
| 165 | if _, err := r.client.Answer(r.ctx, cc.ID, wire.ChoiceMerge, cc.Revision); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | _, body = r.expect(wire.FrameDispatch) |
| 169 | m := body.(*wire.DispatchCommand) |
| 170 | if m.Specialist != wire.SpecialistMerge { |
| 171 | t.Fatalf("merge chosen %+v", m) |
| 172 | } |
| 173 | // The hub refused it: back at Ready to merge, with Retry. |
| 174 | r.step("chosen-m", wire.StepReport{TaskID: chosen, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "amadan_ci_pending", BaseSHA: m.BaseSHA, CandidateSHA: m.CandidateSHA, Termination: true}) |
| 175 | moved, msha, _ := r.waitingForMac("Moved") |
| 176 | if err := r.runner.Send(r.ctx, "moved-b", wire.FrameBranch, wire.BranchReport{TaskID: moved, CandidateSHA: msha, Head: strings.Repeat("9", 40)}); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | r.wait("ended or moved", func(c *Conversation) bool { |
| 180 | return c.Tasks[moved].NeedsYou == wire.NeedsBranchMoved && c.Tasks[merged].Stage == wire.StageDone && c.Tasks[cancelled].Stage == wire.StageCancelled && c.Tasks[chosen].Stage == wire.StageReadyToMerge |
| 181 | }) |
| 182 | |
| 183 | linux := WorkerHello(0) |
| 184 | linux.Label, linux.Platform, linux.Sandbox = "Build box", &wire.Platform{OS: "linux", Arch: "amd64"}, wire.SandboxBubblewrap |
| 185 | linux.Capabilities = &wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux, wire.TagGo}} |
| 186 | lr, err := DialRunnerHello(r.ctx, r.inst.URL, r.inst.EnrolRunner(t).Token, linux) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | t.Cleanup(lr.Close) |
| 191 | (&peer{t: t, r: r.toolRig, fr: lr}).none(wire.FrameDispatch, 200*time.Millisecond) |
| 192 | for _, id := range []string{waiting, cancelled, merged, chosen, moved} { |
| 193 | if runs := r.checksRuns(id); len(runs) != 1 { |
| 194 | t.Fatalf("a Linux runner re-ran %s's checks: %+v", id, runs) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | w, _ := r.newWorker("Tito Studio") |
| 199 | macRecheck(t, w, waiting, sha) |
| 200 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 201 | for _, id := range []string{cancelled, merged, chosen, moved} { |
| 202 | if runs := r.checksRuns(id); len(runs) != 1 { |
| 203 | t.Fatalf("checks of %s re-ran: %+v", id, runs) |
| 204 | } |
| 205 | } |
| 206 | // Merge authority's ruling: its card asked for a reason other than |
| 207 | // checks alone and nobody chose Wait, so the SHA stays asked and the |
| 208 | // card stays open while the checks run again. |
| 209 | time.Sleep(200 * time.Millisecond) |
| 210 | if c := r.cardOf(waiting); c.ID != open.ID || c.State != wire.CardOpen { |
| 211 | t.Fatalf("card after the re-run started %+v", c) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // The run follows the usual paths: ending not run again on the Mac |
| 216 | // (needs_macos) asks the card afresh, and that Mac reconnecting runs |
| 217 | // nothing (once per candidate per enrolment); another Mac enrolling runs |
| 218 | // them once more, and checks that fail send Engineering its fix round. |
| 219 | func TestContractMacRecheckOutcomes(t *testing.T) { |
| 220 | r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) |
| 221 | task, sha, _ := r.waitingForMac("Swipe back on iOS") |
| 222 | a, _ := r.newWorker("Mac A") |
| 223 | d := macRecheck(t, a, task, sha) |
| 224 | r.relay(a, []byte("PACK a")) |
| 225 | if !a.lease(d.StepID, 1) { |
| 226 | t.Fatal("start refused") |
| 227 | } |
| 228 | a.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Reason: "needs_macos", CandidateSHA: sha, Checks: wire.ChecksCannotRun, Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) |
| 229 | var card wire.Card |
| 230 | r.wait("asked again", func(c *Conversation) bool { |
| 231 | for _, k := range c.Cards { |
| 232 | if k.Kind == wire.CardMerge && k.TaskID == task && k.State == wire.CardOpen { |
| 233 | card = k |
| 234 | } |
| 235 | } |
| 236 | return card.ID != "" && c.Tasks[task].Stage == wire.StageReadyToMerge |
| 237 | }) |
| 238 | a = r.reconnectPeer(a) |
| 239 | a.none(wire.FrameDispatch, 200*time.Millisecond) |
| 240 | if runs := r.checksRuns(task); len(runs) != 2 { |
| 241 | t.Fatalf("the same Mac reconnecting re-ran them: %+v", runs) |
| 242 | } |
| 243 | // Mac A away, so placement takes the newcomer. |
| 244 | a.fr.Close() |
| 245 | time.Sleep(100 * time.Millisecond) |
| 246 | b, _ := r.newWorker("Mac B") |
| 247 | _, body := b.expect(wire.FrameDispatch) |
| 248 | d2 := body.(*wire.DispatchCommand) |
| 249 | if d2.TaskID != task || d2.CandidateSHA != sha || d2.StepID == d.StepID { |
| 250 | t.Fatalf("second Mac's checks %+v", d2) |
| 251 | } |
| 252 | r.relay(b, []byte("PACK b")) |
| 253 | if !b.lease(d2.StepID, 1) { |
| 254 | t.Fatal("start refused") |
| 255 | } |
| 256 | one := 1 |
| 257 | b.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d2.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: sha, Checks: wire.ChecksFailed, Gate: "xcodebuild test", ExitCode: &one, Output: "** TEST FAILED **", ChecksSandbox: wire.SandboxSeatbelt, Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) |
| 258 | _, body = r.expect(wire.FrameDispatch) |
| 259 | if e := body.(*wire.DispatchCommand); e.Specialist != wire.SpecialistEngineering || e.TaskID != task || e.Round != 1 { |
| 260 | t.Fatalf("fix round %+v", e) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // A Mac that comes back online (a heartbeat after it was offline) runs |
| 265 | // the waiting checks too, once: it connected before its capability report |
| 266 | // was in (no tags, so nothing then), went quiet, and says hello as a Mac. |
| 267 | // Even with those checks ended not run again, its coming back again, or |
| 268 | // reconnecting, runs nothing more. |
| 269 | func TestContractMacRecheckBackOnline(t *testing.T) { |
| 270 | r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) |
| 271 | task, sha, _ := r.waitingForMac("Swipe back on iOS") |
| 272 | token := r.inst.EnrolRunner(t).Token |
| 273 | bare := WorkerHello(0) |
| 274 | bare.Capabilities = nil |
| 275 | fr, err := DialRunnerHello(r.ctx, r.inst.URL, token, bare) |
| 276 | if err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | t.Cleanup(fr.Close) |
| 280 | w := &peer{t: t, r: r.toolRig, fr: fr} |
| 281 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 282 | if runs := r.checksRuns(task); len(runs) != 1 { |
| 283 | t.Fatalf("a runner with no tags re-ran the checks: %+v", runs) |
| 284 | } |
| 285 | clock := time.Now().UTC() |
| 286 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 287 | back := func() { |
| 288 | clock = clock.Add(2 * time.Minute) |
| 289 | w.heartbeat(WorkerHello(0)) |
| 290 | } |
| 291 | back() |
| 292 | d := macRecheck(t, w, task, sha) |
| 293 | r.relay(w, []byte("PACK swipe back")) |
| 294 | if !w.lease(d.StepID, 1) { |
| 295 | t.Fatal("start refused") |
| 296 | } |
| 297 | w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Reason: "needs_macos", CandidateSHA: sha, Checks: wire.ChecksCannotRun, Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) |
| 298 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[task].Stage == wire.StageReadyToMerge }) |
| 299 | back() |
| 300 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 301 | fr.Close() |
| 302 | time.Sleep(100 * time.Millisecond) |
| 303 | again, err := DialRunnerHello(r.ctx, r.inst.URL, token, WorkerHello(fr.Welcome.Generation)) |
| 304 | if err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | t.Cleanup(again.Close) |
| 308 | (&peer{t: t, r: r.toolRig, fr: again}).none(wire.FrameDispatch, 200*time.Millisecond) |
| 309 | if runs := r.checksRuns(task); len(runs) != 2 { |
| 310 | t.Fatalf("coming back again re-ran the checks: %+v", runs) |
| 311 | } |
| 312 | } |
| 313 | |