package thintest import ( "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/chat" "amadan.net/oficina/equipo/internal/wire" ) // engineeringStep is the task's first Engineering step. func (r *runnersRig) engineeringStep(task string) wire.Step { r.t.Helper() k, err := r.client.Task(r.ctx, task) if err != nil { r.t.Fatal(err) } for _, s := range k.Steps { if s.Specialist == wire.SpecialistEngineering { return s } } r.t.Fatalf("no engineering step: %+v", k.Steps) return wire.Step{} } func (r *runnersRig) needsYou(task string) string { r.t.Helper() k, err := r.client.Task(r.ctx, task) if err != nil { r.t.Fatal(err) } return k.NeedsYou } // Checks that need a Mac, with no Mac runner enrolled, no longer go to the // Linux primary to end as needs_macos (task tkdn6i2ex2xj, 2026-11-28: a // Mac sidebar change checked and reviewed on docs-t1 alone). They wait, // needing the person (a Mac runner), past the offline limit too, and run // on the Mac as soon as one enrols. func TestContractChecksWaitForAMacRunner(t *testing.T) { r := newRunnersRig(t) task, sha := r.candidate("Mac sidebar", true) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) s := r.checksStep(task) if s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" || strings.Join(s.Needs, ",") != "macos,xcode" { t.Fatalf("checks did not wait for a Mac: %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacRunner { t.Fatalf("needs you %q", got) } clock := time.Now().UTC().Add(chat.OfflineLimit + time.Hour) r.inst.Store.Chat().SetClock(func() time.Time { return clock }) if err := r.inst.Store.Chat().Place(r.ctx); err != nil { t.Fatal(err) } if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac { t.Fatalf("waiting for a Mac ended by the offline limit: %+v", s) } w, _ := r.newWorker("Tito Studio") _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.TaskID != task || d.Specialist != wire.SpecialistChecks || d.CandidateSHA != sha || strings.Join(d.Needs, ",") != "macos,xcode" { t.Fatalf("checks on the new Mac: %+v", d) } if s := r.checksStep(task); s.Placement != "" || s.RunnerLabel != "Tito Studio" { t.Fatalf("not placed on the Mac: %+v", s) } if got := r.needsYou(task); got != "" { t.Fatalf("still needs you: %q", got) } } // Engineering for a repository whose gate needs Xcode (the base's // catalog says so) never runs on the Linux primary: with no Mac runner it // waits for one; with one, it still waits, saying why, because a Mac // runner does not take Engineering yet (only checks, until slice F). A // repository that needs no Mac is unchanged: Engineering on the primary. func TestContractMacEngineeringWaits(t *testing.T) { r := newRunnersRig(t) h := primaryHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) task := taskID(t, r.call("c-mac", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-mac", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) s := r.engineeringStep(task) if s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { t.Fatalf("engineering did not wait for a Mac: %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacRunner { t.Fatalf("needs you %q", got) } w, _ := r.newWorker("Tito Studio") w.none(wire.FrameDispatch, 200*time.Millisecond) r.primary.none(wire.FrameDispatch, 0) if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMacEngineering || s.RunnerID != "" { t.Fatalf("engineering with a Mac that cannot take it: %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacEngineering { t.Fatalf("needs you %q", got) } // Not Mac work: Engineering on the primary, as before. h.Catalog[0].NeedsMacOS = false r.primary.heartbeat(h) plain := taskID(t, r.call("c-go", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Go fix", "brief": "b"})) r.call("s-go", wire.ToolStartTask, map[string]string{"task": plain, "brief": "go"}) // (The first task, no longer Mac work by its catalog, may go first.) for range 2 { _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.Specialist != wire.SpecialistEngineering || d.RunnerID == "" { t.Fatalf("engineering %+v", d) } if d.TaskID == plain { return } } t.Fatal("plain engineering not dispatched to the primary") } // An older primary (protocol 1, no relay) is no way round the rule: // Mac Engineering and checks that need macOS wait for a Mac there too, // rather than run, or be judged needs_macos, on Linux (Astra, // mac-placement-wait). func TestContractMacWorkWaitsOnAnOlderPrimary(t *testing.T) { // The tool rig's runner is a protocol 1 primary. older := func() *runnersRig { r := newToolRig(t) return &runnersRig{toolRig: r, primary: &peer{t: t, r: r, fr: r.runner}, creds: map[*FakeRunner][2]string{}} } r := older() h := toolHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) task := taskID(t, r.call("c-old", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-old", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) if n := r.heldKinds(wire.FrameDispatch); n != 0 { t.Fatalf("%d dispatches to an older primary", n) } if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { t.Fatalf("Mac engineering on an older primary: %+v", s) } r = older() task = taskID(t, r.call("c-side", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac sidebar", "brief": "b"})) r.call("s-side", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) sha := strings.Repeat("f", 40) r.step("o1", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}}) 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{}}) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) if n := r.heldKinds(wire.FrameDispatch); n != 0 { t.Fatalf("%d checks dispatches to an older primary", n) } if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" { t.Fatalf("Mac checks on an older primary: %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacRunner { t.Fatalf("needs you %q", got) } } // Engineering dispatched to the Linux primary before this rule (an // instance upgraded with it queued, here: the catalog said Xcode only // after the dispatch) is refused when the primary asks to start it, and // waits for a Mac like new work. func TestContractMacEngineeringQueuedBeforeTheRule(t *testing.T) { r := newRunnersRig(t) task := taskID(t, r.call("c-q", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-q", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.Specialist != wire.SpecialistEngineering { t.Fatalf("dispatch %+v", d) } h := primaryHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) if r.primary.lease(d.StepID, d.Attempt) { t.Fatal("Mac engineering started on the Linux primary") } if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.RunnerID != "" || s.Attempt != d.Attempt+1 { t.Fatalf("not waiting for a Mac: %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacRunner { t.Fatalf("needs you %q", got) } } // Checks of a change that touched no Apple file still wait for a Mac // when the repository's own gate needs Xcode (the base's catalog), and // run on the Mac once one enrols. func TestContractChecksWaitForTheGatesMac(t *testing.T) { r := newRunnersRig(t) task := taskID(t, r.call("c-g", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Readme", "brief": "b"})) r.call("s-g", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) // Engineering ran before the gate needed Xcode; its checks come after. h := primaryHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) sha := strings.Repeat("e", 40) r.step("g1", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}}) 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{}}) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) if s := r.checksStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac || !strings.Contains(strings.Join(s.Needs, ","), "xcode") { t.Fatalf("gate-only checks did not wait for a Mac: %+v", s) } w, _ := r.newWorker("Tito Studio") _, body = w.expect(wire.FrameDispatch) if c := body.(*wire.DispatchCommand); c.TaskID != task || c.Specialist != wire.SpecialistChecks || c.CandidateSHA != sha { t.Fatalf("checks on the new Mac: %+v", c) } } // An exploration of an Xcode repository waits for a Mac under Needs you, // like any task, and can be cancelled from there. func TestContractExplorationWaitsForAMac(t *testing.T) { r := newRunnersRig(t) h := primaryHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) 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})) r.call("s-x", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) r.primary.none(wire.FrameDispatch, 200*time.Millisecond) if s := r.engineeringStep(task); s.State != wire.StepQueued || s.Placement != wire.PlacementMac { t.Fatalf("exploration engineering %+v", s) } if got := r.needsYou(task); got != wire.NeedsMacRunner { t.Fatalf("an exploration waiting for a Mac needs you %q", got) } if _, err := r.client.CancelTask(r.ctx, task); err != nil { t.Fatal(err) } if k, err := r.client.Task(r.ctx, task); err != nil || k.Stage != wire.StageCancelled || k.NeedsYou != "" { t.Fatalf("not cancelled: %+v %v", k, err) } } // An older primary asks for no start authorization: Engineering queued // for it before Mac work waited for a Mac (or before the catalog said the // gate needs Xcode) is placed again when it says hello, and its old // dispatch is not served again (Astra round 2, mac-placement-wait). func TestContractMacEngineeringQueuedOnAnOlderPrimary(t *testing.T) { r := newToolRig(t) p := &peer{t: t, r: r, fr: r.runner} task := taskID(t, r.call("c-oq", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-oq", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) if n := r.heldKinds(wire.FrameDispatch); n != 1 { t.Fatalf("%d engineering dispatches", n) } h := toolHello(0) h.Catalog[0].NeedsMacOS = true p.heartbeat(h) k, err := r.client.Task(r.ctx, task) if err != nil { t.Fatal(err) } for _, s := range k.Steps { if s.Specialist == wire.SpecialistEngineering && (s.State != wire.StepQueued || s.Placement != wire.PlacementMac || s.Attempt != 2) { t.Fatalf("queued Mac engineering left on the older primary: %+v", s) } } if k.NeedsYou != wire.NeedsMacRunner { t.Fatalf("needs you %q", k.NeedsYou) } } // A delivered dispatch is left to the older primary that holds it: only // one it has not received is placed again, so no attempt it may already // run is lost track of (Astra round 3, mac-placement-wait). func TestContractMacEngineeringDeliveredToAnOlderPrimary(t *testing.T) { r := newToolRig(t) p := &peer{t: t, r: r, fr: r.runner} task := taskID(t, r.call("c-od", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-od", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) h := toolHello(0) h.Catalog[0].NeedsMacOS = true p.heartbeat(h) k, err := r.client.Task(r.ctx, task) if err != nil { t.Fatal(err) } for _, s := range k.Steps { if s.Specialist == wire.SpecialistEngineering && (s.ID != d.StepID || s.Attempt != d.Attempt || s.Placement != "") { t.Fatalf("a delivered attempt was replaced: %+v", s) } } } // A cancelled task's Mac Engineering is not let start on the Linux // primary while its cancellation is on the way. func TestContractCancelledMacEngineeringDoesNotStart(t *testing.T) { r := newRunnersRig(t) task := taskID(t, r.call("c-cx", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Mac app", "brief": "b"})) r.call("s-cx", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) h := primaryHello(0) h.Catalog[0].NeedsMacOS = true r.primary.heartbeat(h) if _, err := r.client.CancelTask(r.ctx, task); err != nil { t.Fatal(err) } if r.primary.lease(d.StepID, d.Attempt) { t.Fatal("a cancelled task's Mac engineering started on the Linux primary") } }