package thintest import ( "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // hostedReport is the primary's checks report with the hub's hosted CI // verdict on d's candidate, bound to the task's branch. func hostedReport(d *wire.DispatchCommand, branch, state string, failed ...string) wire.StepReport { rep := wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, BaseSHA: d.BaseSHA, CandidateSHA: d.CandidateSHA, Termination: true, Findings: []wire.Finding{}, Gate: wire.GateHosted, Hosted: &wire.HostedResult{Repo: "amadan.net/oficina/sample", Branch: branch, SHA: d.CandidateSHA, State: state, ReportedAt: 1794402264, Failed: append([]string{}, failed...)}} switch state { case wire.HostedSuccess: rep.Checks, rep.ExitCode, rep.Output = wire.ChecksPassed, exit(0), "Hosted CI on oficina/sample: passed." case wire.HostedFailure: rep.Checks, rep.ExitCode = wire.ChecksFailed, exit(1) rep.Output = "Hosted CI on oficina/sample: failed.\nFailed: " + strings.Join(failed, "; ") + "\n=== compile\nrastrillo: budget size: 1 problem(s)\n" } return rep } // TestContractHostedChecksPass: a checks step on the primary is offered // hosted CI; the hub's pass on the candidate is checks that ran (Review // reads it, the task is ready to merge), recorded as hosted, but the hub // cannot say where the verdict ran, so Equipo asks rather than merging on // its own. func TestContractHostedChecksPass(t *testing.T) { r := newToolRigAt(t, wire.ProtocolHostedChecks) ctx := r.ctx base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("h", "Fix the README typo", base, cand) task, _ := r.client.Task(ctx, id) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) if !c.HostedChecks { t.Fatalf("checks on the primary not offered hosted CI: %+v", c) } r.step("h-c", hostedReport(c, task.Branch, wire.HostedSuccess)) rv := r.dispatched(wire.SpecialistReview, base, cand, 0) if rv.HostedChecks || !strings.Contains(rv.Brief, "hosted CI passed.") { t.Fatalf("review dispatch %+v", rv) } k, _ := r.client.Task(ctx, id) if k.Checks != wire.ChecksPassed || k.ChecksSandbox != wire.SandboxHosted { t.Fatalf("task %+v", k) } r.verdict("h-r", rv, wire.VerdictApprove) card := r.mergeCardsWait(id) if !strings.Contains(card.Question, "its hosted checks passed, but I need to verify those runs before I can merge it on my own") { t.Fatalf("merge card %q", card.Question) } for _, h := range r.held { if d, ok := h.body.(*wire.DispatchCommand); ok && d.Specialist == wire.SpecialistMerge { t.Fatal("merged on its own on a hosted pass") } } } // TestContractHostedChecksFailure: the hub's failure starts the fix round, // whose feedback names the failing units from the stored binding before // the output's tail. func TestContractHostedChecksFailure(t *testing.T) { r := newToolRigAt(t, wire.ProtocolHostedChecks) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("f", "Fix the README typo", base, cand) task, _ := r.client.Task(r.ctx, id) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.step("f-c", hostedReport(c, task.Branch, wire.HostedFailure, "compile", "check shard 5 of 23")) fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) want := "The repository's hosted CI failed on " + cand[:7] + "." if !strings.Contains(fix.Feedback, want) || !strings.Contains(fix.Feedback, "\n=== compile\n") || !strings.Contains(fix.Feedback, `CI failed job: "check shard 5 of 23"`) || !strings.Contains(fix.Feedback, "budget size: 1 problem") { t.Fatalf("fix round feedback %q", fix.Feedback) } } // TestContractHostedBindingRules: a hosted verdict counts only when every // part of its binding is exactly what was judged, from a protocol 5 // primary; any other report is ignored and the step waits on. func TestContractHostedBindingRules(t *testing.T) { for name, bend := range map[string]func(*wire.StepReport){ "another repository": func(v *wire.StepReport) { v.Hosted.Repo = "amadan.net/oficina/other" }, "another branch": func(v *wire.StepReport) { v.Hosted.Branch = "equipo/other" }, "another commit": func(v *wire.StepReport) { v.Hosted.SHA = strings.Repeat("9", 40) }, "state disagrees": func(v *wire.StepReport) { v.Hosted.State = wire.HostedFailure }, "no report time": func(v *wire.StepReport) { v.Hosted.ReportedAt = 0 }, "no binding": func(v *wire.StepReport) { v.Hosted = nil }, "binding, other gate": func(v *wire.StepReport) { v.Gate = "make ci" }, "too many units": func(v *wire.StepReport) { v.Hosted.Failed = make([]string, wire.MaxHostedFailed+1) for i := range v.Hosted.Failed { v.Hosted.Failed[i] = "unit" } }, "a unit too long": func(v *wire.StepReport) { v.Hosted.Failed = []string{strings.Repeat("u", wire.MaxHostedName+1)} }, } { t.Run(name, func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolHostedChecks) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("b", "Fix the README typo", base, cand) task, _ := r.client.Task(r.ctx, id) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) rep := hostedReport(c, task.Branch, wire.HostedSuccess) bend(&rep) r.step("b-c", rep) if k, _ := r.client.Task(r.ctx, id); k.Checks != "" || k.Steps[1].State == wire.StepDone { t.Fatalf("counted: %+v", k.Steps[1]) } // The exact binding still counts afterwards. r.step("b-c-ok", hostedReport(c, task.Branch, wire.HostedSuccess)) r.dispatched(wire.SpecialistReview, base, cand, 0) }) } } // TestContractHostedChecksOldRunner: a runner at protocol 4 is never // offered hosted CI, and a hosted binding from it counts for nothing. func TestContractHostedChecksOldRunner(t *testing.T) { r := newToolRigAt(t, wire.ProtocolMergeFacts) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("o", "Fix the README typo", base, cand) task, _ := r.client.Task(r.ctx, id) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) if c.HostedChecks || strings.Contains(string(r.runner.Received[len(r.runner.Received)-1]), "hosted_checks") { t.Fatalf("offered to a protocol 4 runner: %+v", c) } r.step("o-c", hostedReport(c, task.Branch, wire.HostedSuccess)) if k, _ := r.client.Task(r.ctx, id); k.Checks != "" { t.Fatalf("a protocol 4 runner's hosted verdict counted: %+v", k) } }