| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // hostedReport is the primary's checks report with the hub's hosted CI |
| 11 | // verdict on d's candidate, bound to the task's branch. |
| 12 | func hostedReport(d *wire.DispatchCommand, branch, state string, failed ...string) wire.StepReport { |
| 13 | 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, |
| 14 | Hosted: &wire.HostedResult{Repo: "amadan.net/oficina/sample", Branch: branch, SHA: d.CandidateSHA, State: state, ReportedAt: 1794402264, Failed: append([]string{}, failed...)}} |
| 15 | switch state { |
| 16 | case wire.HostedSuccess: |
| 17 | rep.Checks, rep.ExitCode, rep.Output = wire.ChecksPassed, exit(0), "Hosted CI on oficina/sample: passed." |
| 18 | case wire.HostedFailure: |
| 19 | rep.Checks, rep.ExitCode = wire.ChecksFailed, exit(1) |
| 20 | rep.Output = "Hosted CI on oficina/sample: failed.\nFailed: " + strings.Join(failed, "; ") + "\n=== compile\nrastrillo: budget size: 1 problem(s)\n" |
| 21 | } |
| 22 | return rep |
| 23 | } |
| 24 | |
| 25 | // TestContractHostedChecksPass: a checks step on the primary is offered |
| 26 | // hosted CI; the hub's pass on the candidate is checks that ran (Review |
| 27 | // reads it, the task is ready to merge), recorded as hosted, but the hub |
| 28 | // cannot say where the verdict ran, so Equipo asks rather than merging on |
| 29 | // its own. |
| 30 | func TestContractHostedChecksPass(t *testing.T) { |
| 31 | r := newToolRigAt(t, wire.ProtocolHostedChecks) |
| 32 | ctx := r.ctx |
| 33 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 34 | id, _ := r.pushed("h", "Fix the README typo", base, cand) |
| 35 | task, _ := r.client.Task(ctx, id) |
| 36 | r.expect(wire.FrameWatchMerge) |
| 37 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 38 | if !c.HostedChecks { |
| 39 | t.Fatalf("checks on the primary not offered hosted CI: %+v", c) |
| 40 | } |
| 41 | r.step("h-c", hostedReport(c, task.Branch, wire.HostedSuccess)) |
| 42 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 43 | if rv.HostedChecks || !strings.Contains(rv.Brief, "hosted CI passed.") { |
| 44 | t.Fatalf("review dispatch %+v", rv) |
| 45 | } |
| 46 | k, _ := r.client.Task(ctx, id) |
| 47 | if k.Checks != wire.ChecksPassed || k.ChecksSandbox != wire.SandboxHosted { |
| 48 | t.Fatalf("task %+v", k) |
| 49 | } |
| 50 | r.verdict("h-r", rv, wire.VerdictApprove) |
| 51 | card := r.mergeCardsWait(id) |
| 52 | if !strings.Contains(card.Question, "its hosted checks passed, but I need to verify those runs before I can merge it on my own") { |
| 53 | t.Fatalf("merge card %q", card.Question) |
| 54 | } |
| 55 | for _, h := range r.held { |
| 56 | if d, ok := h.body.(*wire.DispatchCommand); ok && d.Specialist == wire.SpecialistMerge { |
| 57 | t.Fatal("merged on its own on a hosted pass") |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // TestContractHostedChecksFailure: the hub's failure starts the fix round, |
| 63 | // whose feedback names the failing units from the stored binding before |
| 64 | // the output's tail. |
| 65 | func TestContractHostedChecksFailure(t *testing.T) { |
| 66 | r := newToolRigAt(t, wire.ProtocolHostedChecks) |
| 67 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 68 | id, _ := r.pushed("f", "Fix the README typo", base, cand) |
| 69 | task, _ := r.client.Task(r.ctx, id) |
| 70 | r.expect(wire.FrameWatchMerge) |
| 71 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 72 | r.step("f-c", hostedReport(c, task.Branch, wire.HostedFailure, "compile", "check shard 5 of 23")) |
| 73 | fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) |
| 74 | want := "The repository's hosted CI failed on " + cand[:7] + "." |
| 75 | 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") { |
| 76 | t.Fatalf("fix round feedback %q", fix.Feedback) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // TestContractHostedBindingRules: a hosted verdict counts only when every |
| 81 | // part of its binding is exactly what was judged, from a protocol 5 |
| 82 | // primary; any other report is ignored and the step waits on. |
| 83 | func TestContractHostedBindingRules(t *testing.T) { |
| 84 | for name, bend := range map[string]func(*wire.StepReport){ |
| 85 | "another repository": func(v *wire.StepReport) { v.Hosted.Repo = "amadan.net/oficina/other" }, |
| 86 | "another branch": func(v *wire.StepReport) { v.Hosted.Branch = "equipo/other" }, |
| 87 | "another commit": func(v *wire.StepReport) { v.Hosted.SHA = strings.Repeat("9", 40) }, |
| 88 | "state disagrees": func(v *wire.StepReport) { v.Hosted.State = wire.HostedFailure }, |
| 89 | "no report time": func(v *wire.StepReport) { v.Hosted.ReportedAt = 0 }, |
| 90 | "no binding": func(v *wire.StepReport) { v.Hosted = nil }, |
| 91 | "binding, other gate": func(v *wire.StepReport) { |
| 92 | v.Gate = "make ci" |
| 93 | }, |
| 94 | "too many units": func(v *wire.StepReport) { |
| 95 | v.Hosted.Failed = make([]string, wire.MaxHostedFailed+1) |
| 96 | for i := range v.Hosted.Failed { |
| 97 | v.Hosted.Failed[i] = "unit" |
| 98 | } |
| 99 | }, |
| 100 | "a unit too long": func(v *wire.StepReport) { v.Hosted.Failed = []string{strings.Repeat("u", wire.MaxHostedName+1)} }, |
| 101 | } { |
| 102 | t.Run(name, func(t *testing.T) { |
| 103 | r := newToolRigAt(t, wire.ProtocolHostedChecks) |
| 104 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 105 | id, _ := r.pushed("b", "Fix the README typo", base, cand) |
| 106 | task, _ := r.client.Task(r.ctx, id) |
| 107 | r.expect(wire.FrameWatchMerge) |
| 108 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 109 | rep := hostedReport(c, task.Branch, wire.HostedSuccess) |
| 110 | bend(&rep) |
| 111 | r.step("b-c", rep) |
| 112 | if k, _ := r.client.Task(r.ctx, id); k.Checks != "" || k.Steps[1].State == wire.StepDone { |
| 113 | t.Fatalf("counted: %+v", k.Steps[1]) |
| 114 | } |
| 115 | // The exact binding still counts afterwards. |
| 116 | r.step("b-c-ok", hostedReport(c, task.Branch, wire.HostedSuccess)) |
| 117 | r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 118 | }) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // TestContractHostedChecksOldRunner: a runner at protocol 4 is never |
| 123 | // offered hosted CI, and a hosted binding from it counts for nothing. |
| 124 | func TestContractHostedChecksOldRunner(t *testing.T) { |
| 125 | r := newToolRigAt(t, wire.ProtocolMergeFacts) |
| 126 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 127 | id, _ := r.pushed("o", "Fix the README typo", base, cand) |
| 128 | task, _ := r.client.Task(r.ctx, id) |
| 129 | r.expect(wire.FrameWatchMerge) |
| 130 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 131 | if c.HostedChecks || strings.Contains(string(r.runner.Received[len(r.runner.Received)-1]), "hosted_checks") { |
| 132 | t.Fatalf("offered to a protocol 4 runner: %+v", c) |
| 133 | } |
| 134 | r.step("o-c", hostedReport(c, task.Branch, wire.HostedSuccess)) |
| 135 | if k, _ := r.client.Task(r.ctx, id); k.Checks != "" { |
| 136 | t.Fatalf("a protocol 4 runner's hosted verdict counted: %+v", k) |
| 137 | } |
| 138 | } |
| 139 | |