| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | func TestCandidateGateDiagnostics(t *testing.T) { |
| 16 | for _, tc := range []struct { |
| 17 | name string |
| 18 | manifest, basePass, candidatePass bool |
| 19 | }{ |
| 20 | {"no_manifest_pass", false, false, true}, {"no_manifest_fail", false, false, false}, |
| 21 | {"budgets_change_base_fails", true, false, true}, {"budgets_change_base_passes", true, true, true}, |
| 22 | } { |
| 23 | manifest := tc.manifest |
| 24 | t.Run(tc.name, func(t *testing.T) { |
| 25 | e, f, r, _ := hubRig(t) |
| 26 | seed := filepath.Join(filepath.Dir(f.dir), "seed") |
| 27 | writeFile(t, seed, "Makefile", "ci:\n\t@test \"$$(cat size.txt)\" -le \"$$(cat budgets.txt)\"\n") |
| 28 | writeFile(t, seed, "size.txt", "1\n") |
| 29 | writeFile(t, seed, "budgets.txt", "1\n") |
| 30 | if manifest { |
| 31 | writeFile(t, seed, ".equipo/gates.json", `{"version":1,"files":["budgets.txt"]}`) |
| 32 | } |
| 33 | gitOut(t, seed, "add", "-A") |
| 34 | gitOut(t, seed, "commit", "-qm", "base gate") |
| 35 | r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 36 | gitOut(t, seed, "checkout", "-q", "-B", r.Branch) |
| 37 | size := "2\n" |
| 38 | if tc.basePass { |
| 39 | size = "1\n" |
| 40 | } |
| 41 | budget := "2\n" |
| 42 | if !tc.candidatePass { |
| 43 | budget = "1\n" |
| 44 | } |
| 45 | writeFile(t, seed, "size.txt", size) |
| 46 | writeFile(t, seed, "budgets.txt", budget) |
| 47 | gitOut(t, seed, "commit", "-qam", "legitimate budget update") |
| 48 | r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 49 | gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) |
| 50 | r.Specialist = wire.SpecialistChecks |
| 51 | scratch := filepath.Join(filepath.Dir(f.dir), "sandbox") |
| 52 | _ = os.Mkdir(scratch, 0700) |
| 53 | self, _ := os.Executable() |
| 54 | m, err := containment.New(containment.Config{Binary: "/usr/bin/bwrap", RunnerBinary: self, ScratchRoot: scratch}) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | e.O.Manager = m |
| 59 | if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 63 | want := wire.ChecksPassed |
| 64 | if !tc.candidatePass || manifest && !tc.basePass { |
| 65 | want = wire.ChecksFailed |
| 66 | } |
| 67 | wantBase := "" |
| 68 | if manifest && tc.basePass { |
| 69 | wantBase = r.BaseSHA |
| 70 | } |
| 71 | if rep.Checks != want || rep.BaseChecksSHA != wantBase { |
| 72 | t.Fatalf("diagnostic failed or certified itself: %+v", rep) |
| 73 | } |
| 74 | raw, _ := json.Marshal(rep) |
| 75 | var fields map[string]any |
| 76 | _ = json.Unmarshal(raw, &fields) |
| 77 | if manifest { |
| 78 | baseOutcome := wire.ChecksFailed |
| 79 | if tc.basePass { |
| 80 | baseOutcome = wire.ChecksPassed |
| 81 | } |
| 82 | if fields["base_checks"] != baseOutcome { |
| 83 | t.Fatalf("base result lost: %s", raw) |
| 84 | } |
| 85 | if !tc.basePass && rep.Reason != "base_checks_failed" { |
| 86 | t.Fatalf("base failure not identified: %+v", rep) |
| 87 | } |
| 88 | } |
| 89 | exit := "2" |
| 90 | if tc.basePass { |
| 91 | exit = "0" |
| 92 | } |
| 93 | if manifest && (!strings.Contains(rep.Output, "Candidate gate diagnostic") || !strings.Contains(rep.Output, "Base gate certification (exit "+exit)) { |
| 94 | t.Fatalf("both outcomes missing: %s", rep.Output) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |