| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 11 | "amadan.net/oficina/equipo/internal/runner/mac/creds" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // TestMacEngineeringViews: every line slice F adds, where it shows. The |
| 16 | // step names the Mac when Engineering ran on a worker (only then: house |
| 17 | // rule 2026-11-07.8) and, without Xcode access, says the Mac's checks |
| 18 | // build it; the waits and stops have their own words; Needs you says |
| 19 | // when a Mac holding Claude takes the outside-the-sandbox choice away, and |
| 20 | // what the switched-off case waits for; the Machines page shows F1 to F7, |
| 21 | // the home guard (as whom to fix it), a Keychain item, the hold, the |
| 22 | // taint, and whether Engineering can run there. |
| 23 | func TestMacEngineeringViews(t *testing.T) { |
| 24 | const me = "owner@example.com" |
| 25 | for _, key := range []string{"work.step.made_on", "work.step.no_xcode", "work.step.waiting_base", "work.decide.sandbox_held", "work.decide.unsandboxed_withdrawn", "runner.engineering", "runner.mac.holds_claude"} { |
| 26 | if copyText(key) == "" { |
| 27 | t.Fatalf("no copy for %s", key) |
| 28 | } |
| 29 | } |
| 30 | started := time.Now() |
| 31 | |
| 32 | // The step: made on a Mac, without Xcode access. |
| 33 | eng := wire.Step{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Attempt: 1, Summary: "Swipe back", RunnerID: "w-studio", RunnerLabel: "Tito Studio", MadeOn: "Tito Studio", StartedAt: &started} |
| 34 | task := wire.Task{ID: "t1", Creator: me, Stage: wire.StageInReview, Steps: []wire.Step{eng}} |
| 35 | v := taskLineOf(task, me) |
| 36 | if want := fmt.Sprintf(copyText("work.step.made_on"), "Tito Studio"); !strings.HasSuffix(v.Steps[0].Line, " · "+want) || v.StepLine != v.Steps[0].Line { |
| 37 | t.Fatalf("step line %q", v.Steps[0].Line) |
| 38 | } |
| 39 | if v.Steps[0].Result != copyText("work.step.no_xcode") { |
| 40 | t.Fatalf("no-Xcode line %q", v.Steps[0].Result) |
| 41 | } |
| 42 | // With sandboxed Xcode access: named, no such line. |
| 43 | eng.XcodeAccess = wire.XcodeAccessSandboxed |
| 44 | task.Steps = []wire.Step{eng} |
| 45 | if v := taskLineOf(task, me); v.Steps[0].Result != "" || !strings.Contains(v.Steps[0].Line, "Tito Studio") { |
| 46 | t.Fatalf("with Xcode access %+v", v.Steps[0]) |
| 47 | } |
| 48 | // On the main runner (the instance names none): neither line. |
| 49 | main := wire.Step{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Attempt: 1, RunnerID: "r-desk", RunnerLabel: "docs-t1", StartedAt: &started} |
| 50 | task.Steps = []wire.Step{main} |
| 51 | if v := taskLineOf(task, me); strings.Contains(v.Steps[0].Line, "docs-t1") || v.Steps[0].Result != "" { |
| 52 | t.Fatalf("the main runner named: %+v", v.Steps[0]) |
| 53 | } |
| 54 | |
| 55 | // The wait for the Mac's base. |
| 56 | task.Steps = []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepQueued, Attempt: 1, Placement: wire.PlacementBase, RunnerLabel: "Tito Studio"}} |
| 57 | task.Stage = wire.StageInProgress |
| 58 | if v := taskLineOf(task, me); !strings.HasSuffix(v.StepLine, " · "+fmt.Sprintf(copyText("work.step.waiting_base"), "Tito Studio")) { |
| 59 | t.Fatalf("waiting_base line %q", v.StepLine) |
| 60 | } |
| 61 | |
| 62 | // The stops slice F adds: each its own words, with Try again. |
| 63 | for _, reason := range []string{wire.ReasonBaseUnavailable, wire.ReasonWorkspaceTooLarge, wire.ReasonSecretInCandidate, wire.ReasonEngineeringUnavailable} { |
| 64 | words := copyText("work.reason." + reason) |
| 65 | task.Steps = []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepFailed, Reason: reason, Terminated: true, Attempt: 1}} |
| 66 | if v := taskLineOf(task, me); words == "" || v.StageLine != fmt.Sprintf(copyText("work.stopped"), words) || !v.Retry { |
| 67 | t.Fatalf("%s: %q retry %v", reason, v.StageLine, v.Retry) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Needs you: checks waiting for a Mac that holds Claude. No choice, |
| 72 | // and the card says why; the Mac free, the choice is offered. |
| 73 | checks := wire.Step{ID: "c1", Specialist: wire.SpecialistChecks, State: wire.StepQueued, Attempt: 2, Placement: wire.PlacementSandbox, RunnerLabel: "Tito Studio", SandboxChoice: wire.SandboxChoiceHeld} |
| 74 | task = wire.Task{ID: "t1", Creator: me, Stage: wire.StageInProgress, NeedsYou: wire.NeedsMacSandbox, Steps: []wire.Step{checks}} |
| 75 | v = taskLineOf(task, me) |
| 76 | v.Mine, v.Cancel = true, true |
| 77 | d := decisionOf(task, v) |
| 78 | if d.Unsandboxed != nil || !d.SandboxHeld || d.Ask != fmt.Sprintf(copyText("work.decide.sandbox_held"), "Tito Studio") { |
| 79 | t.Fatalf("held: %+v", d) |
| 80 | } |
| 81 | html := decisionHTML(t, task, v) |
| 82 | if strings.Contains(html, "data-task-unsandboxed") || !strings.Contains(html, "data-sandbox-held") { |
| 83 | t.Fatalf("held card:\n%s", html) |
| 84 | } |
| 85 | checks.SandboxChoice = wire.SandboxChoiceOffered |
| 86 | task.Steps = []wire.Step{checks} |
| 87 | if d := decisionOf(task, v); d.Unsandboxed == nil || d.SandboxHeld || d.Ask != copyText("work.decide.sandbox_ask") { |
| 88 | t.Fatalf("offered: %+v", d) |
| 89 | } |
| 90 | // Not known either way: nothing offered. |
| 91 | checks.SandboxChoice = "" |
| 92 | task.Steps = []wire.Step{checks} |
| 93 | if d := decisionOf(task, v); d.Unsandboxed != nil { |
| 94 | t.Fatalf("offered without the instance's word: %+v", d) |
| 95 | } |
| 96 | // The switched-off case's line says it is switched off, not that Macs |
| 97 | // cannot. Its card's why is main's approved text (207d3237). |
| 98 | task = wire.Task{ID: "t1", Creator: me, Stage: wire.StageInProgress, NeedsYou: wire.NeedsMacEngineering, Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepQueued, Placement: wire.PlacementMacEngineering, Attempt: 1}}} |
| 99 | v = taskLineOf(task, me) |
| 100 | if v.StageLine != copyText("work.needs.mac_engineering") || !strings.Contains(v.StageLine, "switched off") { |
| 101 | t.Fatalf("switch off: %q", v.StageLine) |
| 102 | } |
| 103 | if d := decisionOf(task, v); d.Why != copyText("work.decide.mac_engineering_why") { |
| 104 | t.Fatalf("switch off why %q", d.Why) |
| 105 | } |
| 106 | |
| 107 | // The Machines page. |
| 108 | if homeMode != creds.CodeHomeMode { |
| 109 | t.Fatalf("home_mode is %q in the guard", creds.CodeHomeMode) |
| 110 | } |
| 111 | f := func(id, verdict string) wire.Probe { return wire.Probe{ID: id, Verdict: verdict, Detail: "synthetic"} } |
| 112 | caps := &wire.Capabilities{Tags: []string{wire.TagMacOS, wire.TagXcode}, RunnerUser: "paulequipo", |
| 113 | Probes: []wire.Probe{f("P2", wire.ProbeDenied), f("F1", wire.ProbeDenied), f("F2", wire.ProbeFailed), f("F3", wire.ProbeDenied), f("F4", wire.ProbePassed), f("F5", wire.ProbeSkipped), f("F6", wire.ProbeUnavailable), f("F7", wire.ProbeDenied)}, |
| 114 | Engineering: &wire.EngineeringStatus{State: wire.EngineeringHomeOpen, Reason: creds.CodeHomeMode}, |
| 115 | ModelHold: true, |
| 116 | HoldingItems: []string{"Claude Code-credentials-1a2b3c4d"}} |
| 117 | hello := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Sandbox: wire.SandboxSeatbelt, Capabilities: caps} |
| 118 | held, tainted := time.Now(), time.Now() |
| 119 | rec := machineRecord{Runner: Runner{ID: "w-studio", Person: me, Label: "Tito Studio", Role: setup.RoleWorker, TaintedAt: &tainted, CredentialHeldAt: &held, Report: reportOf(hello)}} |
| 120 | page := machinesHTML(t, machinesPage([]machineRecord{rec}, me, time.Now(), false, "")) |
| 121 | for _, want := range []string{ |
| 122 | `data-probe="F1" data-verdict="denied"><span>` + escape(copyText("runner.probe.F1")), |
| 123 | `data-probe="F2" data-verdict="failed"><span>` + escape(copyText("runner.probe.F2")), |
| 124 | `data-probe="F7" data-verdict="denied"><span>` + escape(copyText("runner.probe.F7")), |
| 125 | escape(fmt.Sprintf(copyText("runner.mac.home_open"), "paulequipo")), |
| 126 | escape(fmt.Sprintf(copyText("runner.mac.keychain_item"), "Claude Code-credentials-1a2b3c4d", "paulequipo")), |
| 127 | escape(copyText("runner.tainted_held")), |
| 128 | `<dt>` + escape(copyText("runner.engineering")) + `</dt><dd data-engineering="home_open">` + escape(copyText("runner.engineering.home_open")) + `</dd>`, |
| 129 | } { |
| 130 | if !strings.Contains(page, want) { |
| 131 | t.Fatalf("Machines page lacks %q:\n%s", want, page) |
| 132 | } |
| 133 | } |
| 134 | for _, id := range wire.EngineeringProbeIDs { |
| 135 | if copyText("runner.probe."+id) == "" || !strings.Contains(page, `data-probe="`+id+`"`) { |
| 136 | t.Fatalf("%s not shown", id) |
| 137 | } |
| 138 | } |
| 139 | // Another guard code names the folder case; each state its own words. |
| 140 | caps.Engineering = &wire.EngineeringStatus{State: wire.EngineeringHomeOpen, Reason: "acl"} |
| 141 | if r := reportOf(hello); !slicesHas(r.Guards, fmt.Sprintf(copyText("runner.mac.home_open_folder"), "paulequipo")) { |
| 142 | t.Fatalf("folder guard %+v", r.Guards) |
| 143 | } |
| 144 | seen := map[string]string{} |
| 145 | for _, state := range wire.EngineeringStates { |
| 146 | caps.Engineering = &wire.EngineeringStatus{State: state} |
| 147 | r := reportOf(hello) |
| 148 | if r.Engineering == "" || r.EngineeringState != state { |
| 149 | t.Fatalf("%s: no words", state) |
| 150 | } |
| 151 | if other, ok := seen[r.Engineering]; ok { |
| 152 | t.Fatalf("%s and %s say the same", state, other) |
| 153 | } |
| 154 | seen[r.Engineering] = state |
| 155 | } |
| 156 | // Without the runner user, no line names a fix as nobody; a Linux |
| 157 | // runner or a Mac below 37 says nothing of Engineering. |
| 158 | caps.RunnerUser, caps.Engineering = "", &wire.EngineeringStatus{State: wire.EngineeringHomeOpen, Reason: creds.CodeHomeMode} |
| 159 | if r := reportOf(hello); len(r.Guards) != 0 || r.Engineering == "" { |
| 160 | t.Fatalf("without a user %+v", r) |
| 161 | } |
| 162 | caps.Engineering, caps.HoldingItems = nil, nil |
| 163 | if r := reportOf(hello); r.Engineering != "" || len(r.Guards) != 0 { |
| 164 | t.Fatalf("no status %+v", r) |
| 165 | } |
| 166 | plain := rec |
| 167 | plain.CredentialHeldAt, plain.TaintedAt = nil, nil |
| 168 | if page := machinesHTML(t, machinesPage([]machineRecord{plain}, me, time.Now(), false, "")); strings.Contains(page, "data-holds-claude") || strings.Contains(page, "data-tainted") { |
| 169 | t.Fatal("a Mac holding nothing, untainted, says otherwise") |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // decisionHTML renders the task's decision card as the task page does. |
| 174 | func decisionHTML(t *testing.T, task wire.Task, v taskLine) string { |
| 175 | t.Helper() |
| 176 | v.Decision = decisionOf(task, v) |
| 177 | var b strings.Builder |
| 178 | if err := pages["work_task"].ExecuteTemplate(&b, "decision", map[string]any{"Task": v, "From": "task"}); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | return b.String() |
| 182 | } |
| 183 | |
| 184 | func escape(s string) string { |
| 185 | return regexp.MustCompile(`'`).ReplaceAllString(strings.NewReplacer("&", "&", "<", "<", ">", ">", `"`, """).Replace(s), "'") |
| 186 | } |
| 187 | |
| 188 | func slicesHas(list []string, s string) bool { |
| 189 | for _, x := range list { |
| 190 | if x == s { |
| 191 | return true |
| 192 | } |
| 193 | } |
| 194 | return false |
| 195 | } |
| 196 | |
| 197 | // TestMachinesTaintWithHold: a tainted Mac that also holds Claude (or |
| 198 | // something one left) says so in one line from copy.go, and never tells |
| 199 | // the creator to choose a run outside the sandbox, which D1 hides there. |
| 200 | // Tainted alone keeps the accepted line; held alone, the hold's line. |
| 201 | func TestMachinesTaintWithHold(t *testing.T) { |
| 202 | const me = "owner@example.com" |
| 203 | if copyText("runner.tainted_held") == "" { |
| 204 | t.Fatal("no copy for runner.tainted_held") |
| 205 | } |
| 206 | at := time.Now() |
| 207 | page := func(tainted, held bool) string { |
| 208 | t.Helper() |
| 209 | r := Runner{ID: "w-studio", Person: me, Label: "Tito Studio", Role: setup.RoleWorker} |
| 210 | if tainted { |
| 211 | r.TaintedAt = &at |
| 212 | } |
| 213 | if held { |
| 214 | r.CredentialHeldAt = &at |
| 215 | } |
| 216 | return machinesHTML(t, machinesPage([]machineRecord{{Runner: r}}, me, at, false, "")) |
| 217 | } |
| 218 | both := page(true, true) |
| 219 | if !strings.Contains(both, escape(copyText("runner.tainted_held"))) || strings.Contains(both, escape(copyText("runner.tainted"))) || strings.Contains(both, escape(copyText("runner.mac.holds_claude"))) { |
| 220 | t.Fatalf("tainted and held:\n%s", both) |
| 221 | } |
| 222 | for _, offer := range []string{"chooses to run them outside the sandbox", escape(copyText("work.decide.unsandboxed"))} { |
| 223 | if strings.Contains(both, offer) { |
| 224 | t.Fatalf("tainted and held recommends %q", offer) |
| 225 | } |
| 226 | } |
| 227 | if p := page(true, false); !strings.Contains(p, escape(copyText("runner.tainted"))) || strings.Contains(p, "data-holds-claude") { |
| 228 | t.Fatal("tainted alone") |
| 229 | } |
| 230 | if p := page(false, true); !strings.Contains(p, escape(copyText("runner.mac.holds_claude"))) || strings.Contains(p, "data-tainted") { |
| 231 | t.Fatal("held alone") |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Whole-branch fix 2, #3: a Mac's commit made while no primary is there |
| 236 | // to publish it says it is waiting for one. |
| 237 | func TestWorkerCommitWaitsForAPrimary(t *testing.T) { |
| 238 | task := wire.Task{ID: "t1", Stage: wire.StageInProgress, Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Attempt: 1, MadeOn: "Tito Studio", XcodeAccess: wire.XcodeAccessSandboxed, Placement: wire.PlacementWaiting, CandidateSHA: strings.Repeat("c", 40)}}} |
| 239 | v := taskLineOf(task, "a@example.com") |
| 240 | if want := copyText("work.step.waiting_publish"); want == "" || !strings.Contains(v.Steps[0].Line, want) { |
| 241 | t.Fatalf("line %q", v.Steps[0].Line) |
| 242 | } |
| 243 | task.Steps[0].Placement = "" |
| 244 | if v := taskLineOf(task, "a@example.com"); strings.Contains(v.Steps[0].Line, copyText("work.step.waiting_publish")) { |
| 245 | t.Fatalf("line %q", v.Steps[0].Line) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Whole-branch fix 2, #5: a Mac not ready because P2 is not proven |
| 250 | // says that its changes cannot be scanned until it is. |
| 251 | func TestMachinesSayP2Decides(t *testing.T) { |
| 252 | for _, st := range []wire.EngineeringStatus{{State: wire.EngineeringProbesFailed, Reason: "P2"}, {State: wire.EngineeringProbesPending, Reason: "F4 P2"}} { |
| 253 | hello := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagMacOS}, Engineering: &st}} |
| 254 | r := reportOf(hello) |
| 255 | if want := copyText("runner.engineering.needs_p2"); want == "" || !strings.Contains(r.Engineering, want) || !strings.HasPrefix(r.Engineering, copyText("runner.engineering."+st.State)) { |
| 256 | t.Fatalf("%+v shown as %q", st, r.Engineering) |
| 257 | } |
| 258 | } |
| 259 | hello := wire.Hello{Platform: &wire.Platform{OS: "darwin", Arch: "arm64"}, Capabilities: &wire.Capabilities{Tags: []string{wire.TagMacOS}, Engineering: &wire.EngineeringStatus{State: wire.EngineeringProbesFailed, Reason: "F2"}}} |
| 260 | if r := reportOf(hello); strings.Contains(r.Engineering, "P2") || r.Engineering != copyText("runner.engineering.probes_failed") { |
| 261 | t.Fatalf("F2 shown as %q", r.Engineering) |
| 262 | } |
| 263 | } |
| 264 | |