| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // engineeringStep is the task's first Engineering step. |
| 13 | func (r *runnersRig) engineeringStep(task string) wire.Step { |
| 14 | r.t.Helper() |
| 15 | k, err := r.client.Task(r.ctx, task) |
| 16 | if err != nil { |
| 17 | r.t.Fatal(err) |
| 18 | } |
| 19 | for _, s := range k.Steps { |
| 20 | if s.Specialist == wire.SpecialistEngineering { |
| 21 | return s |
| 22 | } |
| 23 | } |
| 24 | r.t.Fatalf("no engineering step: %+v", k.Steps) |
| 25 | return wire.Step{} |
| 26 | } |
| 27 | |
| 28 | func (r *runnersRig) needsYou(task string) string { |
| 29 | r.t.Helper() |
| 30 | k, err := r.client.Task(r.ctx, task) |
| 31 | if err != nil { |
| 32 | r.t.Fatal(err) |
| 33 | } |
| 34 | return k.NeedsYou |
| 35 | } |
| 36 | |
| 37 | // Checks that need a Mac, with no Mac runner enrolled, no longer go to the |
| 38 | // Linux primary to end as needs_macos (task tkdn6i2ex2xj, 2026-11-28: a |
| 39 | // Mac sidebar change checked and reviewed on docs-t1 alone). They wait, |
| 40 | // needing the person (a Mac runner), past the offline limit too, and run |
| 41 | // on the Mac as soon as one enrols. |
| 42 | func TestContractChecksWaitForAMacRunner(t *testing.T) { |
| 43 | r := newRunnersRig(t) |
| 44 | task, sha := r.candidate("Mac sidebar", true) |
| 45 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 46 | s := r.checksStep(task) |
| 47 | if s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" || strings.Join(s.Needs, ",") != "macos,xcode" { |
| 48 | t.Fatalf("checks did not wait for a Mac: %+v", s) |
| 49 | } |
| 50 | if got := r.needsYou(task); got != wire.NeedsMacRunner { |
| 51 | t.Fatalf("needs you %q", got) |
| 52 | } |
| 53 | clock := time.Now().UTC().Add(chat.OfflineLimit + time.Hour) |
| 54 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 55 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac { |
| 59 | t.Fatalf("waiting for a Mac ended by the offline limit: %+v", s) |
| 60 | } |
| 61 | w, _ := r.newWorker("Tito Studio") |
| 62 | _, body := w.expect(wire.FrameDispatch) |
| 63 | d := body.(*wire.DispatchCommand) |
| 64 | if d.TaskID != task || d.Specialist != wire.SpecialistChecks || d.CandidateSHA != sha || strings.Join(d.Needs, ",") != "macos,xcode" { |
| 65 | t.Fatalf("checks on the new Mac: %+v", d) |
| 66 | } |
| 67 | if s := r.checksStep(task); s.Placement != "" || s.RunnerLabel != "Tito Studio" { |
| 68 | t.Fatalf("not placed on the Mac: %+v", s) |
| 69 | } |
| 70 | if got := r.needsYou(task); got != "" { |
| 71 | t.Fatalf("still needs you: %q", got) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Engineering for a repository whose gate needs Xcode (the base's |
| 76 | // catalog says so) never runs on the Linux primary: with no Mac runner it |
| 77 | // waits for one; with one, it still waits, saying why, because a Mac |
| 78 | // runner does not take Engineering yet (only checks, until slice F). A |
| 79 | // repository that needs no Mac is unchanged: Engineering on the primary. |
| 80 | func TestContractMacEngineeringWaits(t *testing.T) { |
| 81 | r := newRunnersRig(t) |
| 82 | h := primaryHello(0) |
| 83 | h.Catalog[0].NeedsMacOS = true |
| 84 | r.primary.heartbeat(h) |
| 85 | task := taskID(t, r.call("c-mac", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 86 | r.call("s-mac", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 87 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 88 | s := r.engineeringStep(task) |
| 89 | if s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { |
| 90 | t.Fatalf("engineering did not wait for a Mac: %+v", s) |
| 91 | } |
| 92 | if got := r.needsYou(task); got != wire.NeedsMacRunner { |
| 93 | t.Fatalf("needs you %q", got) |
| 94 | } |
| 95 | w, _ := r.newWorker("Tito Studio") |
| 96 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 97 | r.primary.none(wire.FrameDispatch, 0) |
| 98 | if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMacEngineering || s.RunnerID != "" { |
| 99 | t.Fatalf("engineering with a Mac that cannot take it: %+v", s) |
| 100 | } |
| 101 | if got := r.needsYou(task); got != wire.NeedsMacEngineering { |
| 102 | t.Fatalf("needs you %q", got) |
| 103 | } |
| 104 | |
| 105 | // Not Mac work: Engineering on the primary, as before. |
| 106 | h.Catalog[0].NeedsMacOS = false |
| 107 | r.primary.heartbeat(h) |
| 108 | plain := taskID(t, r.call("c-go", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Go fix", "brief": "b"})) |
| 109 | r.call("s-go", wire.ToolStartTask, map[string]string{"task": plain, "brief": "go"}) |
| 110 | // (The first task, no longer Mac work by its catalog, may go first.) |
| 111 | for range 2 { |
| 112 | _, body := r.expect(wire.FrameDispatch) |
| 113 | d := body.(*wire.DispatchCommand) |
| 114 | if d.Specialist != wire.SpecialistEngineering || d.RunnerID == "" { |
| 115 | t.Fatalf("engineering %+v", d) |
| 116 | } |
| 117 | if d.TaskID == plain { |
| 118 | return |
| 119 | } |
| 120 | } |
| 121 | t.Fatal("plain engineering not dispatched to the primary") |
| 122 | } |
| 123 | |
| 124 | // An older primary (protocol 1, no relay) is no way round the rule: |
| 125 | // Mac Engineering and checks that need macOS wait for a Mac there too, |
| 126 | // rather than run, or be judged needs_macos, on Linux (Astra, |
| 127 | // mac-placement-wait). |
| 128 | func TestContractMacWorkWaitsOnAnOlderPrimary(t *testing.T) { |
| 129 | // The tool rig's runner is a protocol 1 primary. |
| 130 | older := func() *runnersRig { |
| 131 | r := newToolRig(t) |
| 132 | return &runnersRig{toolRig: r, primary: &peer{t: t, r: r, fr: r.runner}, creds: map[*FakeRunner][2]string{}} |
| 133 | } |
| 134 | r := older() |
| 135 | h := toolHello(0) |
| 136 | h.Catalog[0].NeedsMacOS = true |
| 137 | r.primary.heartbeat(h) |
| 138 | task := taskID(t, r.call("c-old", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 139 | r.call("s-old", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 140 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 141 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 142 | t.Fatalf("%d dispatches to an older primary", n) |
| 143 | } |
| 144 | if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { |
| 145 | t.Fatalf("Mac engineering on an older primary: %+v", s) |
| 146 | } |
| 147 | |
| 148 | r = older() |
| 149 | task = taskID(t, r.call("c-side", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac sidebar", "brief": "b"})) |
| 150 | r.call("s-side", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 151 | _, body := r.expect(wire.FrameDispatch) |
| 152 | d := body.(*wire.DispatchCommand) |
| 153 | sha := strings.Repeat("f", 40) |
| 154 | r.step("o1", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}}) |
| 155 | r.step("o2", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: sha, Pushed: true, Termination: true, TerminationKind: wire.TerminationNamespace, Apple: true, Findings: []wire.Finding{}}) |
| 156 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 157 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 158 | t.Fatalf("%d checks dispatches to an older primary", n) |
| 159 | } |
| 160 | if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { |
| 161 | t.Fatalf("Mac checks on an older primary: %+v", s) |
| 162 | } |
| 163 | if got := r.needsYou(task); got != wire.NeedsMacRunner { |
| 164 | t.Fatalf("needs you %q", got) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Engineering dispatched to the Linux primary before this rule (an |
| 169 | // instance upgraded with it queued, here: the catalog said Xcode only |
| 170 | // after the dispatch) is refused when the primary asks to start it, and |
| 171 | // waits for a Mac like new work. |
| 172 | func TestContractMacEngineeringQueuedBeforeTheRule(t *testing.T) { |
| 173 | r := newRunnersRig(t) |
| 174 | task := taskID(t, r.call("c-q", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 175 | r.call("s-q", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 176 | _, body := r.expect(wire.FrameDispatch) |
| 177 | d := body.(*wire.DispatchCommand) |
| 178 | if d.Specialist != wire.SpecialistEngineering { |
| 179 | t.Fatalf("dispatch %+v", d) |
| 180 | } |
| 181 | h := primaryHello(0) |
| 182 | h.Catalog[0].NeedsMacOS = true |
| 183 | r.primary.heartbeat(h) |
| 184 | if r.primary.lease(d.StepID, d.Attempt) { |
| 185 | t.Fatal("Mac engineering started on the Linux primary") |
| 186 | } |
| 187 | if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" || s.Attempt != d.Attempt+1 { |
| 188 | t.Fatalf("not waiting for a Mac: %+v", s) |
| 189 | } |
| 190 | if got := r.needsYou(task); got != wire.NeedsMacRunner { |
| 191 | t.Fatalf("needs you %q", got) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Checks of a change that touched no Apple file still wait for a Mac |
| 196 | // when the repository's own gate needs Xcode (the base's catalog), and |
| 197 | // run on the Mac once one enrols. |
| 198 | func TestContractChecksWaitForTheGatesMac(t *testing.T) { |
| 199 | r := newRunnersRig(t) |
| 200 | task := taskID(t, r.call("c-g", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Readme", "brief": "b"})) |
| 201 | r.call("s-g", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 202 | _, body := r.expect(wire.FrameDispatch) |
| 203 | d := body.(*wire.DispatchCommand) |
| 204 | // Engineering ran before the gate needed Xcode; its checks come after. |
| 205 | h := primaryHello(0) |
| 206 | h.Catalog[0].NeedsMacOS = true |
| 207 | r.primary.heartbeat(h) |
| 208 | sha := strings.Repeat("e", 40) |
| 209 | r.step("g1", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}}) |
| 210 | r.step("g2", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: sha, Pushed: true, Termination: true, TerminationKind: wire.TerminationNamespace, Findings: []wire.Finding{}}) |
| 211 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 212 | if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || !strings.Contains(strings.Join(s.Needs, ","), "xcode") { |
| 213 | t.Fatalf("gate-only checks did not wait for a Mac: %+v", s) |
| 214 | } |
| 215 | w, _ := r.newWorker("Tito Studio") |
| 216 | _, body = w.expect(wire.FrameDispatch) |
| 217 | if c := body.(*wire.DispatchCommand); c.TaskID != task || c.Specialist != wire.SpecialistChecks || c.CandidateSHA != sha { |
| 218 | t.Fatalf("checks on the new Mac: %+v", c) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // An exploration of an Xcode repository waits for a Mac under Needs you, |
| 223 | // like any task, and can be cancelled from there. |
| 224 | func TestContractExplorationWaitsForAMac(t *testing.T) { |
| 225 | r := newRunnersRig(t) |
| 226 | h := primaryHello(0) |
| 227 | h.Catalog[0].NeedsMacOS = true |
| 228 | r.primary.heartbeat(h) |
| 229 | task := taskID(t, r.call("c-x", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Mac mockups", "brief": "Two mockups.", "separate": true, "kind": wire.KindExplore})) |
| 230 | r.call("s-x", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 231 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 232 | if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac { |
| 233 | t.Fatalf("exploration engineering %+v", s) |
| 234 | } |
| 235 | if got := r.needsYou(task); got != wire.NeedsMacRunner { |
| 236 | t.Fatalf("an exploration waiting for a Mac needs you %q", got) |
| 237 | } |
| 238 | if _, err := r.client.CancelTask(r.ctx, task); err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | if k, err := r.client.Task(r.ctx, task); err != nil || k.Stage != wire.StageCancelled || k.NeedsYou != "" { |
| 242 | t.Fatalf("not cancelled: %+v %v", k, err) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // An older primary asks for no start authorization: Engineering queued |
| 247 | // for it before Mac work waited for a Mac (or before the catalog said the |
| 248 | // gate needs Xcode) is placed again when it says hello, and its old |
| 249 | // dispatch is not served again (Astra round 2, mac-placement-wait). |
| 250 | func TestContractMacEngineeringQueuedOnAnOlderPrimary(t *testing.T) { |
| 251 | r := newToolRig(t) |
| 252 | p := &peer{t: t, r: r, fr: r.runner} |
| 253 | task := taskID(t, r.call("c-oq", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 254 | r.call("s-oq", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 255 | if n := r.heldKinds(wire.FrameDispatch); n != 1 { |
| 256 | t.Fatalf("%d engineering dispatches", n) |
| 257 | } |
| 258 | h := toolHello(0) |
| 259 | h.Catalog[0].NeedsMacOS = true |
| 260 | p.heartbeat(h) |
| 261 | k, err := r.client.Task(r.ctx, task) |
| 262 | if err != nil { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | for _, s := range k.Steps { |
| 266 | if s.Specialist == wire.SpecialistEngineering && (s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.Attempt != 2) { |
| 267 | t.Fatalf("queued Mac engineering left on the older primary: %+v", s) |
| 268 | } |
| 269 | } |
| 270 | if k.NeedsYou != wire.NeedsMacRunner { |
| 271 | t.Fatalf("needs you %q", k.NeedsYou) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // A delivered dispatch is left to the older primary that holds it: only |
| 276 | // one it has not received is placed again, so no attempt it may already |
| 277 | // run is lost track of (Astra round 3, mac-placement-wait). |
| 278 | func TestContractMacEngineeringDeliveredToAnOlderPrimary(t *testing.T) { |
| 279 | r := newToolRig(t) |
| 280 | p := &peer{t: t, r: r, fr: r.runner} |
| 281 | task := taskID(t, r.call("c-od", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 282 | r.call("s-od", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 283 | _, body := r.expect(wire.FrameDispatch) |
| 284 | d := body.(*wire.DispatchCommand) |
| 285 | h := toolHello(0) |
| 286 | h.Catalog[0].NeedsMacOS = true |
| 287 | p.heartbeat(h) |
| 288 | k, err := r.client.Task(r.ctx, task) |
| 289 | if err != nil { |
| 290 | t.Fatal(err) |
| 291 | } |
| 292 | for _, s := range k.Steps { |
| 293 | if s.Specialist == wire.SpecialistEngineering && (s.ID != d.StepID || s.Attempt != d.Attempt || s.Placement != "") { |
| 294 | t.Fatalf("a delivered attempt was replaced: %+v", s) |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // A cancelled task's Mac Engineering is not let start on the Linux |
| 300 | // primary while its cancellation is on the way. |
| 301 | func TestContractCancelledMacEngineeringDoesNotStart(t *testing.T) { |
| 302 | r := newRunnersRig(t) |
| 303 | task := taskID(t, r.call("c-cx", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) |
| 304 | r.call("s-cx", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 305 | _, body := r.expect(wire.FrameDispatch) |
| 306 | d := body.(*wire.DispatchCommand) |
| 307 | h := primaryHello(0) |
| 308 | h.Catalog[0].NeedsMacOS = true |
| 309 | r.primary.heartbeat(h) |
| 310 | if _, err := r.client.CancelTask(r.ctx, task); err != nil { |
| 311 | t.Fatal(err) |
| 312 | } |
| 313 | if r.primary.lease(d.StepID, d.Attempt) { |
| 314 | t.Fatal("a cancelled task's Mac engineering started on the Linux primary") |
| 315 | } |
| 316 | } |
| 317 | |