package thin import ( "context" "encoding/json" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/wire" ) func TestCandidateGateDiagnostics(t *testing.T) { for _, tc := range []struct { name string manifest, basePass, candidatePass bool }{ {"no_manifest_pass", false, false, true}, {"no_manifest_fail", false, false, false}, {"budgets_change_base_fails", true, false, true}, {"budgets_change_base_passes", true, true, true}, } { manifest := tc.manifest t.Run(tc.name, func(t *testing.T) { e, f, r, _ := hubRig(t) seed := filepath.Join(filepath.Dir(f.dir), "seed") writeFile(t, seed, "Makefile", "ci:\n\t@test \"$$(cat size.txt)\" -le \"$$(cat budgets.txt)\"\n") writeFile(t, seed, "size.txt", "1\n") writeFile(t, seed, "budgets.txt", "1\n") if manifest { writeFile(t, seed, ".equipo/gates.json", `{"version":1,"files":["budgets.txt"]}`) } gitOut(t, seed, "add", "-A") gitOut(t, seed, "commit", "-qm", "base gate") r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "checkout", "-q", "-B", r.Branch) size := "2\n" if tc.basePass { size = "1\n" } budget := "2\n" if !tc.candidatePass { budget = "1\n" } writeFile(t, seed, "size.txt", size) writeFile(t, seed, "budgets.txt", budget) gitOut(t, seed, "commit", "-qam", "legitimate budget update") r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) r.Specialist = wire.SpecialistChecks scratch := filepath.Join(filepath.Dir(f.dir), "sandbox") _ = os.Mkdir(scratch, 0700) self, _ := os.Executable() m, err := containment.New(containment.Config{Binary: "/usr/bin/bwrap", RunnerBinary: self, ScratchRoot: scratch}) if err != nil { t.Fatal(err) } e.O.Manager = m if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { t.Fatal(err) } rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) want := wire.ChecksPassed if !tc.candidatePass || manifest && !tc.basePass { want = wire.ChecksFailed } wantBase := "" if manifest && tc.basePass { wantBase = r.BaseSHA } if rep.Checks != want || rep.BaseChecksSHA != wantBase { t.Fatalf("diagnostic failed or certified itself: %+v", rep) } raw, _ := json.Marshal(rep) var fields map[string]any _ = json.Unmarshal(raw, &fields) if manifest { baseOutcome := wire.ChecksFailed if tc.basePass { baseOutcome = wire.ChecksPassed } if fields["base_checks"] != baseOutcome { t.Fatalf("base result lost: %s", raw) } if !tc.basePass && rep.Reason != "base_checks_failed" { t.Fatalf("base failure not identified: %+v", rep) } } exit := "2" if tc.basePass { exit = "0" } if manifest && (!strings.Contains(rep.Output, "Candidate gate diagnostic") || !strings.Contains(rep.Output, "Base gate certification (exit "+exit)) { t.Fatalf("both outcomes missing: %s", rep.Output) } }) } }