oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mergegate
2
3import (
4 "math/rand/v2"
5 "os"
6 "slices"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11 "amadan.net/rastrillo/rastrillo/budget"
12)
13
14func TestMain(m *testing.M) { os.Exit(budget.Main(m)) }
15
16var (
17 cand = strings.Repeat("2", 40)
18 base = strings.Repeat("1", 40)
19)
20
21func smallChange() *wire.ChangeFacts {
22 return &wire.ChangeFacts{ReviewStep: "s_r", Attempt: 1, Repo: "amadan.net/woodstar/woodstar", Base: base, SHA: cand, Files: 2, Lines: 40, Complete: true,
23 Entries: []wire.ChangeEntry{
24 {Status: "M", New: "Woodstar/Views/PaneView.swift", OldMode: "100644", NewMode: "100644"},
25 {Status: "M", New: "Woodstar/Views/EdgeSwipe.swift", OldMode: "100644", NewMode: "100644"},
26 }}
27}
28
29// passing is a merge without checks that every criterion allows.
30func passing() Inputs {
31 p := DefaultPolicy()
32 p.MainShips = ShipsNo
33 return Inputs{Candidate: cand, Verdict: wire.VerdictApprove, BaseChecks: true, Checks: wire.ChecksCannotRun, ChecksReason: "needs_macos", Change: smallChange(), Protocol: wire.ProtocolInjectionDefences, Policy: p}
34}
35
36func failing(d Decision) []string {
37 var out []string
38 for _, c := range d.Reasons {
39 if !c.OK {
40 out = append(out, c.Name)
41 }
42 }
43 return out
44}
45
46// TestDecideEachCriterion: every criterion holding merges; each one
47// failing alone asks, and names that criterion.
48func TestDecideEachCriterion(t *testing.T) {
49 if d := Decide(passing()); d.Class != ClassAct || d.Checks || len(failing(d)) != 0 {
50 t.Fatalf("all pass: %+v", d)
51 }
52 cases := map[string]func(*Inputs){
53 CritEnvironment: func(in *Inputs) { in.ChecksReason = "checks_timeout" },
54 CritProtocol: func(in *Inputs) { in.Protocol = wire.ProtocolInjectionDefences - 1 },
55 CritFacts: func(in *Inputs) { in.Change = nil },
56 CritSmall: func(in *Inputs) { in.Change.Lines = 301 },
57 CritSensitive: func(in *Inputs) { in.Change.Entries[0].New = "Woodstar/Billing/Checkout.swift" },
58 CritShips: func(in *Inputs) { in.Policy.MainShips = ShipsUnknown },
59 CritPolicy: func(in *Inputs) { in.Policy.Mode = ModeAsk },
60 CritHold: func(in *Inputs) { in.Hold = "this touches the pane stack" },
61 }
62 for name, change := range cases {
63 in := passing()
64 change(&in)
65 d := Decide(in)
66 if d.Class != ClassAsk || !slices.Contains(failing(d), name) {
67 t.Errorf("%s failing: %+v", name, d)
68 }
69 }
70 more := map[string]func(*Inputs){
71 "files": func(in *Inputs) { in.Change.Files = 16 },
72 "incomplete": func(in *Inputs) { in.Change.Complete = false },
73 "binary": func(in *Inputs) { in.Change.Binary = 1 },
74 "special": func(in *Inputs) { in.Change.Special = true },
75 "signing": func(in *Inputs) { in.Change.Signing = true },
76 "rename from": func(in *Inputs) { in.Change.Entries[0].Old = "Woodstar/Auth/Login.swift" },
77 "ships yes": func(in *Inputs) { in.Policy.MainShips = ShipsYes },
78 "checks none": func(in *Inputs) { in.Checks, in.ChecksReason = wire.ChecksNone, "" },
79 "needs network": func(in *Inputs) { in.ChecksReason = "needs_network" },
80 "unavailable": func(in *Inputs) { in.ChecksReason = "checks_unavailable" },
81 "protocol 1": func(in *Inputs) { in.Protocol = wire.Protocol1 },
82 "extra sensitive": func(in *Inputs) { in.Policy.ExtraSensitive = []string{"Woodstar/Views/**"} },
83 "limit lines zero": func(in *Inputs) { in.Policy.MaxLines = 0 },
84 "revert inexact": func(in *Inputs) {
85 in.RevertSHA = strings.Repeat("e", 40)
86 in.Change.Revert = &wire.RevertCheck{SHA: in.RevertSHA, OnTop: true}
87 },
88 }
89 for name, change := range more {
90 in := passing()
91 change(&in)
92 if d := Decide(in); d.Class != ClassAsk {
93 t.Errorf("%s: %+v", name, d)
94 }
95 }
96 in := passing()
97 in.RevertSHA = strings.Repeat("e", 40)
98 in.Change.Revert = &wire.RevertCheck{SHA: in.RevertSHA, OnTop: true, Exact: true}
99 if d := Decide(in); d.Class != ClassAct {
100 t.Fatalf("an exact revert: %+v", d)
101 }
102}
103
104// TestDecideNever: not approved, checks failed or still running, or no
105// candidate: nothing at all, even for a person.
106func TestDecideNever(t *testing.T) {
107 cases := []func(*Inputs){
108 func(in *Inputs) { in.Candidate = "" },
109 func(in *Inputs) { in.Verdict = wire.VerdictChanges },
110 func(in *Inputs) { in.Verdict = "" },
111 func(in *Inputs) { in.Checks = wire.ChecksFailed },
112 func(in *Inputs) { in.Checks = "" },
113 func(in *Inputs) { in.Checks = "maybe" },
114 }
115 for i, change := range cases {
116 in := passing()
117 change(&in)
118 in.Person = true
119 if d := Decide(in); d.Class != ClassNever {
120 t.Errorf("case %d: %+v", i, d)
121 }
122 }
123}
124
125// TestDecideChecksPassedPath: checked work merges on its own only when it
126// first earned Ready with checks passed, inside the sandbox, on a
127// protocol 3 runner, with no hold and the project not asking. Size,
128// sensitivity and shipping do not apply to it.
129func TestDecideChecksPassedPath(t *testing.T) {
130 ok := Inputs{Candidate: cand, Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed, BaseChecks: true, Change: smallChange(), Eligible: true, Protocol: wire.ProtocolInjectionDefences, Policy: DefaultPolicy()}
131 if d := Decide(ok); d.Class != ClassAct || !d.Checks {
132 t.Fatalf("checked: %+v", d)
133 }
134 for name, change := range map[string]func(*Inputs){
135 CritEligible: func(in *Inputs) { in.Eligible = false },
136 CritSandbox: func(in *Inputs) { in.OutsideSandbox = true },
137 CritProtocol: func(in *Inputs) { in.Protocol = wire.ProtocolInjectionDefences - 1 },
138 CritPolicy: func(in *Inputs) { in.Policy.Mode = ModeAsk },
139 CritHold: func(in *Inputs) { in.Hold = "wait" },
140 "no protocol": func(in *Inputs) { in.Protocol = 0 },
141 } {
142 in := ok
143 change(&in)
144 if d := Decide(in); d.Class != ClassAsk {
145 t.Errorf("%s: %+v", name, d)
146 }
147 }
148}
149
150// TestDecideRecheckedPass: checks that passed on a Mac re-run, for a
151// candidate that first reached Ready unchecked, merge on their own only
152// for a routine change: measured, small, nothing sensitive, main not a
153// release, no hold, the project not asking.
154func TestDecideRecheckedPass(t *testing.T) {
155 in := passing()
156 in.Checks, in.ChecksReason, in.Eligible, in.Rechecked = wire.ChecksPassed, "", true, true
157 if d := Decide(in); d.Class != ClassAct || !d.Checks {
158 t.Fatalf("routine re-run pass: %+v", d)
159 }
160 for name, change := range map[string]func(*Inputs){
161 CritSensitive: func(in *Inputs) { in.Change.Entries[0].New = "ios/StoreKit/Purchase.swift" },
162 CritShips: func(in *Inputs) { in.Policy.MainShips = ShipsYes },
163 CritSmall: func(in *Inputs) { in.Change.Lines = 5000 },
164 CritFacts: func(in *Inputs) { in.Change = nil },
165 CritHold: func(in *Inputs) { in.Hold = "device first" },
166 CritEligible: func(in *Inputs) { in.Eligible = false },
167 CritSandbox: func(in *Inputs) { in.OutsideSandbox = true },
168 } {
169 in := passing()
170 in.Checks, in.ChecksReason, in.Eligible, in.Rechecked = wire.ChecksPassed, "", true, true
171 change(&in)
172 if d := Decide(in); d.Class != ClassAsk || !slices.Contains(failing(d), name) {
173 t.Errorf("%s: %+v", name, d)
174 }
175 }
176}
177
178// TestDecidePersonMerge: a person's Merge acts on approved work whatever
179// the policy, the hold or the runner say; never on unapproved work.
180func TestDecidePersonMerge(t *testing.T) {
181 in := passing()
182 in.Person, in.Change, in.Protocol, in.Hold, in.Policy.Mode = true, nil, wire.Protocol1, "hold", ModeAsk
183 if d := Decide(in); d.Class != ClassAct || d.DecidedBy != DecidedPerson {
184 t.Fatalf("person: %+v", d)
185 }
186}
187
188// TestDecideStance: Equipo's call on an ask card. Only size or the
189// project's ask mode leave it recommending the merge.
190func TestDecideStance(t *testing.T) {
191 in := passing()
192 in.Change.Lines = 900
193 if d := Decide(in); d.Stance != StanceMerge {
194 t.Fatalf("big: %+v", d)
195 }
196 in = passing()
197 in.Policy.Mode = ModeAsk
198 if d := Decide(in); d.Stance != StanceMerge {
199 t.Fatalf("ask mode: %+v", d)
200 }
201 for name, change := range map[string]func(*Inputs){
202 "sensitive": func(in *Inputs) { in.Change.Entries[0].New = "go.mod" },
203 "ships": func(in *Inputs) { in.Policy.MainShips = ShipsUnknown },
204 "hold": func(in *Inputs) { in.Hold = "no" },
205 "facts": func(in *Inputs) { in.Change = nil },
206 } {
207 in := passing()
208 change(&in)
209 if d := Decide(in); d.Stance != StanceWait {
210 t.Errorf("%s: %+v", name, d)
211 }
212 }
213}
214
215// oracle is the rule written out once more, independently: Equipo may
216// merge on its own without checks only when every criterion holds.
217func oracle(in Inputs) bool {
218 if in.Candidate == "" || in.Verdict != wire.VerdictApprove || in.Checks != wire.ChecksCannotRun {
219 return false
220 }
221 if in.ChecksReason != "needs_macos" && in.ChecksReason != "runner_offline" {
222 return false
223 }
224 if in.Protocol < wire.ProtocolInjectionDefences || in.Change == nil || !in.Change.Complete || in.Hold != "" || in.Policy.Mode != ModeRules || in.Policy.MainShips != ShipsNo {
225 return false
226 }
227 if in.Policy.MaxLines < 0 || in.Policy.MaxLines > 2000 || in.Policy.MaxFiles < 0 || in.Policy.MaxFiles > 100 {
228 return false
229 }
230 c := in.Change
231 if c.Lines > in.Policy.MaxLines || c.Files > in.Policy.MaxFiles || c.Binary > 0 || c.Special || c.Signing {
232 return false
233 }
234 if len(Sensitive(c.Entries, in.Policy.ExtraSensitive)) > 0 {
235 return false
236 }
237 if in.RevertSHA != "" && (c.Revert == nil || c.Revert.SHA != in.RevertSHA || !c.Revert.OnTop || !c.Revert.Exact) {
238 return false
239 }
240 return true
241}
242
243// TestDecideNeverLooser: over random inputs, Decide merges unchecked work
244// on its own only when the oracle does, a hold never yields a merge
245// without a person, and no policy value waives a criterion.
246func TestDecideNeverLooser(t *testing.T) {
247 rng := rand.New(rand.NewPCG(1, 2))
248 pick := func(xs ...string) string { return xs[rng.IntN(len(xs))] }
249 paths := []string{"Woodstar/Views/Pane.swift", "README.md", "go.mod", "app/payments/x.go", "db/migrations/1.sql", ".amadan/ci", "Sources/Keyboard.swift", "infra/main.tf", "lib/tokenizer.go"}
250 for i := 0; i < 20000; i++ {
251 in := Inputs{
252 Candidate: pick(cand, ""),
253 Verdict: pick(wire.VerdictApprove, wire.VerdictApprove, wire.VerdictChanges, ""),
254 Checks: pick(wire.ChecksCannotRun, wire.ChecksCannotRun, wire.ChecksPassed, wire.ChecksNone, wire.ChecksFailed, ""),
255 ChecksReason: pick("needs_macos", "runner_offline", "checks_timeout", "needs_network", ""),
256 OutsideSandbox: rng.IntN(4) == 0,
257 Eligible: rng.IntN(2) == 0,
258 Protocol: rng.IntN(wire.ProtocolInjectionDefences + 1),
259 Hold: pick("", "", "", "careful"),
260 Policy: Policy{Mode: pick(ModeRules, ModeRules, ModeAsk, "loose"), MaxLines: rng.IntN(3000) - 500, MaxFiles: rng.IntN(200) - 20, MainShips: pick(ShipsNo, ShipsNo, ShipsYes, ShipsUnknown, "maybe")},
261 RevertSHA: pick("", "", strings.Repeat("e", 40)),
262 Rechecked: rng.IntN(3) == 0,
263 }
264 if rng.IntN(5) > 0 {
265 c := &wire.ChangeFacts{Complete: rng.IntN(6) > 0, Files: rng.IntN(30), Lines: rng.IntN(800), Binary: rng.IntN(3) / 2, Special: rng.IntN(8) == 0, Signing: rng.IntN(8) == 0}
266 for j := 0; j < 1+rng.IntN(3); j++ {
267 c.Entries = append(c.Entries, wire.ChangeEntry{Status: "M", New: paths[rng.IntN(len(paths))], Old: pick("", "", paths[rng.IntN(len(paths))])})
268 }
269 if rng.IntN(2) == 0 {
270 c.Revert = &wire.RevertCheck{SHA: pick(strings.Repeat("e", 40), cand), OnTop: rng.IntN(2) == 0, Exact: rng.IntN(2) == 0}
271 }
272 in.Change = c
273 }
274 d := Decide(in)
275 if d.Class == ClassAct && !d.Checks && !oracle(in) {
276 t.Fatalf("merged without checks against the rule: %+v -> %+v", in, d)
277 }
278 if oracle(in) && d.Class != ClassAct {
279 t.Fatalf("refused what the rule allows: %+v -> %+v", in, d)
280 }
281 if in.Hold != "" && d.Class == ClassAct {
282 t.Fatalf("a hold merged: %+v", in)
283 }
284 if d.Class == ClassAct && d.Checks && (in.Checks != wire.ChecksPassed || !in.Eligible || in.OutsideSandbox || in.Protocol < wire.ProtocolInjectionDefences || in.Policy.Mode != ModeRules) {
285 t.Fatalf("checked merge against the rule: %+v", in)
286 }
287 if d.Class == ClassAct && d.Checks && in.Rechecked {
288 unchecked := in
289 unchecked.Checks, unchecked.ChecksReason = wire.ChecksCannotRun, "needs_macos"
290 if !oracle(unchecked) {
291 t.Fatalf("a re-run pass merged what the unchecked rule would not: %+v", in)
292 }
293 }
294 }
295}
296
297// TestSensitivePaths: each class, at the root and nested, on either side
298// of a rename; whole tokens only; a project's globs only add.
299func TestSensitivePaths(t *testing.T) {
300 yes := []string{
301 "db/migrations/0001_init.sql", "internal/equipo/migrations/0058_x.sql", "schema.prisma", "app/db/schema.rb",
302 ".env", "config/.env.production", "certs/server.pem", "ios/AuthKey.p8",
303 "Sources/Auth/LoginView.swift", "lib/session_store.go", "pkg/crypto/aes.go", "api/tokens.go", "OAuthClient.swift", "keys/README.md",
304 "Woodstar/Billing/Checkout.swift", "server/stripe_webhook.go", "StoreKit/Purchase.swift",
305 ".amadan/ci", ".amadan/ci.d/10-test", ".github/workflows/ci.yml", "Makefile", "tools/Makefile", "build/rules.mk",
306 "go.mod", "go.sum", "server/go.mod", "web/package.json", "web/package-lock.json", "app/yarn.lock", "Cargo.lock", "Package.swift", "Woodstar.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved", "Podfile.lock", "app/build.gradle.kts", ".gitmodules", "vendor/x/y.go",
307 "Woodstar/Woodstar.entitlements", "fastlane/Fastfile", "Config/Release.xcconfig", "VERSION", "scripts/release.sh",
308 "infra/main.tf", "Dockerfile", "deploy/k8s/app.yaml", "docker-compose.yml", "fly.toml", "systemd/equipo.service",
309 }
310 for _, p := range yes {
311 if len(Sensitive([]wire.ChangeEntry{{Status: "M", New: p}}, nil)) == 0 {
312 t.Errorf("not sensitive: %s", p)
313 }
314 if len(Sensitive([]wire.ChangeEntry{{Status: "R100", Old: p, New: "docs/moved.txt"}}, nil)) == 0 {
315 t.Errorf("rename from %s not sensitive", p)
316 }
317 }
318 no := []string{"Sources/Keyboard.swift", "lib/tokenizer.go", "Woodstar/Views/PaneView.swift", "README.md", "docs/monkey.md", "Sources/KeyboardShortcuts.swift", "Woodstar.xcodeproj/project.pbxproj", "Sources/SessionlessView.swift", "app/Authorship.swift"}
319 for _, p := range no {
320 if m := Sensitive([]wire.ChangeEntry{{Status: "M", New: p}}, nil); len(m) != 0 {
321 t.Errorf("sensitive: %s %+v", p, m)
322 }
323 }
324 if len(Sensitive([]wire.ChangeEntry{{Status: "M", New: "Woodstar/Views/PaneView.swift"}}, []string{"Woodstar/Views/*.swift"})) == 0 {
325 t.Error("a project's glob did not add")
326 }
327 if len(Sensitive([]wire.ChangeEntry{{Status: "M", New: "a/b/Woodstar/Views/PaneView.swift"}}, []string{"Woodstar/Views/*.swift"})) == 0 {
328 t.Error("a project's glob does not match at depth")
329 }
330}
331
332// TestValidPolicy: numbers inside their bounds, known modes, at most 20
333// clean globs.
334func TestValidPolicy(t *testing.T) {
335 p := DefaultPolicy()
336 if !ValidPolicy(p) {
337 t.Fatal("defaults invalid")
338 }
339 for name, change := range map[string]func(*Policy){
340 "lines": func(p *Policy) { p.MaxLines = MaxLinesCeiling + 1 },
341 "files": func(p *Policy) { p.MaxFiles = -1 },
342 "mode": func(p *Policy) { p.Mode = "loose" },
343 "ships": func(p *Policy) { p.MainShips = "maybe" },
344 "globs": func(p *Policy) { p.ExtraSensitive = make([]string, 21) },
345 "glob": func(p *Policy) { p.ExtraSensitive = []string{"[bad"} },
346 "abs": func(p *Policy) { p.ExtraSensitive = []string{"/etc/**"} },
347 "length": func(p *Policy) { p.ExtraSensitive = []string{strings.Repeat("a", 201)} },
348 } {
349 q := DefaultPolicy()
350 change(&q)
351 if ValidPolicy(q) {
352 t.Errorf("%s accepted", name)
353 }
354 }
355}
356
357// TestTighter: what the coordinator may apply on its own.
358func TestTighter(t *testing.T) {
359 old := DefaultPolicy()
360 for name, p := range map[string]Policy{
361 "ask": {Mode: ModeAsk, MaxLines: 300, MaxFiles: 15, MainShips: ShipsUnknown},
362 "lines": {Mode: ModeRules, MaxLines: 100, MaxFiles: 15, MainShips: ShipsUnknown},
363 "ships": {Mode: ModeRules, MaxLines: 300, MaxFiles: 15, MainShips: ShipsYes},
364 "globs": {Mode: ModeRules, MaxLines: 300, MaxFiles: 15, MainShips: ShipsUnknown, ExtraSensitive: []string{"x/**"}},
365 } {
366 if !Tighter(old, p) {
367 t.Errorf("%s not tighter", name)
368 }
369 }
370 strict := Policy{Mode: ModeAsk, MaxLines: 100, MaxFiles: 5, MainShips: ShipsYes, ExtraSensitive: []string{"x/**"}}
371 for name, p := range map[string]Policy{
372 "rules": {Mode: ModeRules, MaxLines: 100, MaxFiles: 5, MainShips: ShipsYes, ExtraSensitive: []string{"x/**"}},
373 "lines": {Mode: ModeAsk, MaxLines: 101, MaxFiles: 5, MainShips: ShipsYes, ExtraSensitive: []string{"x/**"}},
374 "ships no": {Mode: ModeAsk, MaxLines: 100, MaxFiles: 5, MainShips: ShipsNo, ExtraSensitive: []string{"x/**"}},
375 "unknown": {Mode: ModeAsk, MaxLines: 100, MaxFiles: 5, MainShips: ShipsUnknown, ExtraSensitive: []string{"x/**"}},
376 "globs": {Mode: ModeAsk, MaxLines: 100, MaxFiles: 5, MainShips: ShipsYes},
377 } {
378 if Tighter(strict, p) {
379 t.Errorf("%s counted as tighter", name)
380 }
381 }
382}
383
384// TestDecideExploreNever: an exploration never merges, whatever else
385// holds, a person's Merge included.
386func TestDecideExploreNever(t *testing.T) {
387 base := Inputs{Candidate: "c", Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed, BaseChecks: true, Change: smallChange(), Eligible: true, Protocol: MinProtocol, Policy: DefaultPolicy()}
388 if d := Decide(base); d.Class != ClassAct {
389 t.Fatalf("base %+v", d)
390 }
391 for _, person := range []bool{false, true} {
392 in := base
393 in.Explore, in.Person = true, person
394 if d := Decide(in); d.Class != ClassNever || len(d.Reasons) != 1 || d.Reasons[0].Name != CritChange {
395 t.Fatalf("person %t: %+v", person, d)
396 }
397 }
398}
399