| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // Checks a worker ran outside the sandbox say so wherever their result |
| 18 | // shows (the line, the badge, the timeline and the Ready to merge card |
| 19 | // share checksLine): never a pass in disguise. Sandboxed checks and the |
| 20 | // primary's read as before. |
| 21 | func TestChecksOutsideTheSandboxAreLabelled(t *testing.T) { |
| 22 | zero, two := 0, 2 |
| 23 | if got := checksLine(wire.ChecksPassed, "", "xcodebuild test", &zero, wire.SandboxNone); got != "Checks passed (xcodebuild test), ran outside the sandbox" { |
| 24 | t.Fatalf("%q", got) |
| 25 | } |
| 26 | if got := checksLine(wire.ChecksFailed, "", "make ci", &two, wire.SandboxNone); got != "Checks failed (make ci, exit 2), ran outside the sandbox" { |
| 27 | t.Fatalf("%q", got) |
| 28 | } |
| 29 | for _, sandbox := range []string{wire.SandboxSeatbelt, ""} { |
| 30 | if got := checksLine(wire.ChecksPassed, "", "xcodebuild test", &zero, sandbox); got != "Checks passed (xcodebuild test)" { |
| 31 | t.Fatalf("%s: %q", sandbox, got) |
| 32 | } |
| 33 | } |
| 34 | task := wire.Task{Checks: wire.ChecksPassed, ChecksSandbox: wire.SandboxNone, CandidateSHA: "c", Steps: []wire.Step{{Specialist: wire.SpecialistChecks, State: wire.StepDone, Checks: wire.ChecksPassed, CandidateSHA: "c", Gate: "xcodebuild test", ExitCode: &zero, ChecksSandbox: wire.SandboxNone}}} |
| 35 | b := checksBadge(task, nil) |
| 36 | if !strings.Contains(b.Text, "ran outside the sandbox") || !strings.Contains(b.Title, "ran outside the sandbox") { |
| 37 | t.Fatalf("badge %+v", b) |
| 38 | } |
| 39 | v := taskLineOf(task, "a@example.com") |
| 40 | if !strings.HasSuffix(v.ChecksLine, "ran outside the sandbox") { |
| 41 | t.Fatalf("card %q", v.ChecksLine) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // The Machines details show what a Mac said of itself: its platform, |
| 46 | // where checks run, and each self-probe in words. |
| 47 | func TestRunnersViewShowsProbes(t *testing.T) { |
| 48 | tainted := time.Now() |
| 49 | records := []machineRecord{{Runner: Runner{ID: "r1", Person: "a@example.com", Label: "Tito Studio", TaintedAt: &tainted, Report: &setup.RunnerReport{Platform: "darwin/arm64", Sandbox: wire.SandboxNone, |
| 50 | Probes: []setup.RunnerProbe{{ID: "P2", Verdict: wire.ProbeFailed, Detail: "allowed: test.keychain; xcodebuild ran in srt"}, {ID: "P7", Verdict: wire.ProbePassed, Detail: "srt 0.0.81"}}}}}} |
| 51 | data := pageData{Machines: machinesPage(records, "a@example.com", time.Now(), false, "")} |
| 52 | var buf bytes.Buffer |
| 53 | if err := pages["runners"].ExecuteTemplate(&buf, "content", data); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | html := buf.String() |
| 57 | for _, want := range []string{"A check has run outside the sandbox on this Mac", "darwin/arm64", "Outside the sandbox", "Self-checks", "Sandbox against a hostile build", "Failed", "allowed: test.keychain; xcodebuild ran in srt", "Sandbox tools", `data-probe="P2" data-verdict="failed"`} { |
| 58 | if !strings.Contains(html, want) { |
| 59 | t.Fatalf("%q missing:\n%s", want, html) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // curl -fsSL https://<instance>/install.sh | sh runs this release's |
| 65 | // versioned bootstrap for this instance; an origin or team that is not |
| 66 | // plain is refused rather than written into shell. |
| 67 | func TestInstallScript(t *testing.T) { |
| 68 | body, err := installScriptBody("https://equipo-staging.example", "abc1234", "team1") |
| 69 | want := "#!/bin/sh\nset -eu\ncurl --fail --silent --show-error --proto '=https' --tlsv1.2 'https://equipo-staging.example/releases/abc1234/install-v1.sh' | sh -s -- 'https://equipo-staging.example' 'abc1234' 'team1'\n" |
| 70 | if err != nil || body != want { |
| 71 | t.Fatalf("%q %v", body, err) |
| 72 | } |
| 73 | for _, bad := range [][3]string{{"https://x.example'", "abc1234", "t"}, {"https://x.example", "dev", "t"}, {"https://x.example", "abc1234", "t'; rm -rf ~"}} { |
| 74 | if _, err := installScriptBody(bad[0], bad[1], bad[2]); err == nil { |
| 75 | t.Fatalf("%v accepted", bad) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // A probe detail is codes with parameters (wire.ProbeDetail), worded |
| 81 | // here from copy.go; free text from a runner is never shown |
| 82 | // (whole-branch fix 1, #6). Every code has its wording, with one %s per |
| 83 | // parameter, pending review and listed for it. |
| 84 | func TestProbeDetailCodes(t *testing.T) { |
| 85 | if got := probeDetail(wire.ProbeDetailTurnBusy); got == wire.ProbeDetailTurnBusy || got != copyText("runner.probe_detail.turn_busy") || got == "" { |
| 86 | t.Fatalf("turn_busy shown as %q", got) |
| 87 | } |
| 88 | for _, free := range []string{"srt 0.0.81", "not run yet", "not proven: the runner changed since it ran", "allowed: home.cat"} { |
| 89 | if got := probeDetail(free); got != "" { |
| 90 | t.Fatalf("free text %q shown as %q", free, got) |
| 91 | } |
| 92 | } |
| 93 | d := wire.ProbeDetails(wire.ProbeDetail("allowed", "home.cat tcp"), wire.ProbeDetail("not_run")) |
| 94 | if got, want := probeDetail(d), fmt.Sprintf(copyText("runner.probe_detail.allowed"), "home.cat tcp")+" "+copyText("runner.probe_detail.not_run"); got != want || strings.Contains(got, "%") { |
| 95 | t.Fatalf("%q shown as %q, want %q", d, got, want) |
| 96 | } |
| 97 | // A part that does not decode is left out; the rest is shown. |
| 98 | if got := probeDetail(wire.ProbeDetail("not_run") + ";words here"); got != copyText("runner.probe_detail.not_run") { |
| 99 | t.Fatalf("mixed shown as %q", got) |
| 100 | } |
| 101 | src, err := os.ReadFile("copy.go") |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | raw, err := os.ReadFile("../../docs/superpowers/plans/2026-11-28-mac-slice-f-copy.json") |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | var listed []struct{ Key, Draft, Reviewed string } |
| 110 | if err := json.Unmarshal(raw, &listed); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | drafts := map[string]string{} |
| 114 | for _, e := range listed { |
| 115 | drafts[e.Key] = e.Draft |
| 116 | } |
| 117 | for code, n := range wire.ProbeDetailCodes { |
| 118 | key := "runner.probe_detail." + code |
| 119 | text := copyText(key) |
| 120 | if text == "" || strings.Count(text, "%s") != n || strings.Count(text, "%") != n || strings.ContainsAny(text, "—!") { |
| 121 | t.Errorf("%s: %q for %d parameters", key, text, n) |
| 122 | } |
| 123 | line := regexp.MustCompile(`(?m)^\s*"` + regexp.QuoteMeta(key) + `":.*$`).Find(src) |
| 124 | // Pending, or as Luna reviewed it (2026-11-29). |
| 125 | if !strings.Contains(string(line), "COPY REVIEW PENDING") && !strings.Contains(string(line), "COPY ACCEPTED BY DELEGATION (Luna, 2026-11-29)") { |
| 126 | t.Errorf("%s is neither pending nor reviewed", key) |
| 127 | } |
| 128 | if drafts[key] != text { |
| 129 | t.Errorf("%s: listed for review as %q", key, drafts[key]) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // An attempt whose end a Mac can never prove is said on the Machines |
| 135 | // page, worded here from its code; no code, nothing. |
| 136 | func TestUnprovenEndShown(t *testing.T) { |
| 137 | if got := unprovenText(wire.UnprovenRecordIncomplete, 0); got == "" || got == wire.UnprovenRecordIncomplete || got != copyText("runner.unproven.record_incomplete") { |
| 138 | t.Fatalf("shown as %q", got) |
| 139 | } |
| 140 | // A Mac that could not prove a credential launch ended (task 10). |
| 141 | if got := unprovenText(wire.UnprovenCredential, 0); got == "" || got != copyText("runner.unproven.credential_unproven") { |
| 142 | t.Fatalf("credential_unproven shown as %q", got) |
| 143 | } |
| 144 | if unprovenText("", 0) != "" || unprovenText("no_such_code", 0) != "" { |
| 145 | t.Fatal("an unknown or empty code said something") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // An idle read that found processes no record accounts for says how |
| 150 | // many, and nothing else about them; both texts say a restart clears it. |
| 151 | func TestUnprovenProcessesLeftShown(t *testing.T) { |
| 152 | got := unprovenText(wire.UnprovenProcessesLeft, 3) |
| 153 | if want := strings.ReplaceAll(copyText("runner.unproven.processes_left"), "%d", "3"); got != want || !strings.Contains(got, "3") { |
| 154 | t.Fatalf("shown as %q, want %q", got, want) |
| 155 | } |
| 156 | if unprovenText(wire.UnprovenProcessesLeft, 0) != "" { |
| 157 | t.Fatal("a count of none said something") |
| 158 | } |
| 159 | for _, key := range []string{"runner.unproven.processes_left", "runner.unproven.record_incomplete"} { |
| 160 | if got := strings.ToLower(copyText(key)); !strings.Contains(got, "restart the mac") && !strings.Contains(got, "restart it") { |
| 161 | t.Fatalf("%s does not say what clears it: %q", key, copyText(key)) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |