| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // Merge authority, thin v1 (spec 2026-11-11): the gate decides every |
| 16 | // merge before dispatch and again at each hub request; an automatic |
| 17 | // merge needs wire.ProtocolMergeFacts at both moments. |
| 18 | |
| 19 | func (c *Client) MergePolicy(ctx context.Context, project string) (out wire.MergePolicy, err error) { |
| 20 | return out, c.do(ctx, "GET", "/api/v1/projects/"+project+"/merge-policy", nil, &out) |
| 21 | } |
| 22 | |
| 23 | func (c *Client) SetMergePolicy(ctx context.Context, project string, in wire.SetMergePolicy) (out wire.MergePolicy, err error) { |
| 24 | return out, c.do(ctx, "PUT", "/api/v1/projects/"+project+"/merge-policy", in, &out) |
| 25 | } |
| 26 | |
| 27 | func (c *Client) UndoMerge(ctx context.Context, id string, revision int64) (out wire.Task, err error) { |
| 28 | return out, c.do(ctx, "POST", "/api/v1/tasks/"+id+"/undo-merge", wire.UndoMerge{Revision: revision}, &out) |
| 29 | } |
| 30 | |
| 31 | // facts is a small, ordinary change as the runner measures it for review |
| 32 | // step d. |
| 33 | func facts(d *wire.DispatchCommand, paths ...string) *wire.ChangeFacts { |
| 34 | if len(paths) == 0 { |
| 35 | paths = []string{"ios/Woodstar/AccountNavigationHost.swift"} |
| 36 | } |
| 37 | f := &wire.ChangeFacts{ReviewStep: d.StepID, Attempt: d.Attempt, Repo: d.Repo, Base: d.BaseSHA, SHA: d.CandidateSHA, Files: len(paths), Lines: 12, Complete: true, Entries: []wire.ChangeEntry{}} |
| 38 | for _, p := range paths { |
| 39 | f.Entries = append(f.Entries, wire.ChangeEntry{Status: "M", New: p, OldMode: "100644", NewMode: "100644"}) |
| 40 | } |
| 41 | return f |
| 42 | } |
| 43 | |
| 44 | // measured is judgedTask whose review reports change facts (nil: none). |
| 45 | func (r *toolRig) measured(key, title, base, cand, outcome, reason string, f func(*wire.DispatchCommand) *wire.ChangeFacts) (string, *wire.DispatchCommand) { |
| 46 | r.t.Helper() |
| 47 | id, _ := r.pushed(key, title, base, cand) |
| 48 | r.expect(wire.FrameWatchMerge) |
| 49 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 50 | code := exit(0) |
| 51 | if outcome != wire.ChecksPassed { |
| 52 | code = nil |
| 53 | } |
| 54 | r.checks(key+"-c", c, outcome, code, reason, "") |
| 55 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 56 | var change *wire.ChangeFacts |
| 57 | if f != nil { |
| 58 | change = f(rv) |
| 59 | } |
| 60 | r.step(key+"-r", wire.StepReport{TaskID: rv.TaskID, StepID: rv.StepID, Attempt: rv.Attempt, State: wire.StepDone, BaseSHA: rv.BaseSHA, CandidateSHA: rv.CandidateSHA, Termination: true, Family: "codex", Account: "codex-a", Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Summary: "Read the change.", Change: change}) |
| 61 | return id, rv |
| 62 | } |
| 63 | |
| 64 | // lease sends a lease check on runner and returns "" when acked, else |
| 65 | // the refusal's reason. |
| 66 | func (r *toolRig) lease(runner *FakeRunner, id string, check wire.LeaseCheck) string { |
| 67 | r.t.Helper() |
| 68 | check.Generation = runner.Welcome.Generation |
| 69 | if err := runner.Send(r.ctx, id, wire.FrameLease, check); err != nil { |
| 70 | r.t.Fatal(err) |
| 71 | } |
| 72 | for { |
| 73 | f, body, err := runner.Next(r.ctx) |
| 74 | if err != nil { |
| 75 | r.t.Fatal(err) |
| 76 | } |
| 77 | switch v := body.(type) { |
| 78 | case *wire.Ack: |
| 79 | if v.ID == id { |
| 80 | return "" |
| 81 | } |
| 82 | case *wire.Refused: |
| 83 | if v.ID == id { |
| 84 | return v.Reason |
| 85 | } |
| 86 | } |
| 87 | if runner == r.runner && f.Type != wire.FrameAck { |
| 88 | r.held = append(r.held, heldFrame{f, body, runner.Received[len(runner.Received)-1]}) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // projectOf is the task's project id. |
| 94 | func (r *toolRig) projectOf(id string) string { |
| 95 | r.t.Helper() |
| 96 | k, err := r.client.Task(r.ctx, id) |
| 97 | if err != nil { |
| 98 | r.t.Fatal(err) |
| 99 | } |
| 100 | return k.ProjectID |
| 101 | } |
| 102 | |
| 103 | // shipsNo tells Equipo, as the owner on the project page, that the |
| 104 | // project's main is not a release. |
| 105 | func (r *toolRig) shipsNo(project string) { |
| 106 | r.t.Helper() |
| 107 | p, err := r.client.MergePolicy(r.ctx, project) |
| 108 | if err != nil { |
| 109 | r.t.Fatal(err) |
| 110 | } |
| 111 | if _, err := r.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: p.Mode, MaxLines: p.MaxLines, MaxFiles: p.MaxFiles, MainShips: "no", ExtraSensitive: p.ExtraSensitive, Revision: p.Revision}); err != nil { |
| 112 | r.t.Fatal(err) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // project is the sample project's id, added once. |
| 117 | func (r *toolRig) project() string { |
| 118 | r.t.Helper() |
| 119 | p, err := r.client.AddProject(r.ctx, "oficina/sample") |
| 120 | if err != nil { |
| 121 | r.t.Fatal(err) |
| 122 | } |
| 123 | return p.ID |
| 124 | } |
| 125 | |
| 126 | func (r *toolRig) mergeCards(id string) []wire.Card { |
| 127 | var out []wire.Card |
| 128 | r.conv.Locked(func(c *Conversation) { |
| 129 | for _, k := range c.Cards { |
| 130 | if k.Kind == wire.CardMerge && k.TaskID == id { |
| 131 | out = append(out, k) |
| 132 | } |
| 133 | } |
| 134 | }) |
| 135 | return out |
| 136 | } |
| 137 | |
| 138 | // TestContractMergeAuthorityAct: a Woodstar-shaped change whose checks |
| 139 | // need a Mac, small, ordinary, approved, on a project whose main is not a |
| 140 | // release, merges on its own: the merge step is Equipo's, its request is |
| 141 | // confirmed by the gate again, and the person gets a receipt with Undo |
| 142 | // (the hub named the merge commit) instead of a line, with the decision |
| 143 | // on the task. |
| 144 | func TestContractMergeAuthorityAct(t *testing.T) { |
| 145 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 146 | ctx := r.ctx |
| 147 | r.shipsNo(r.project()) |
| 148 | base, cand, merge := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) |
| 149 | id, _ := r.measured("a", "Swipe back from the edge", base, cand, wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 150 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 151 | if len(r.mergeCards(id)) != 0 { |
| 152 | t.Fatal("asked although every rule held") |
| 153 | } |
| 154 | if refused := r.lease(r.runner, "a-lease", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused != "" { |
| 155 | t.Fatalf("start refused %q", refused) |
| 156 | } |
| 157 | if refused := r.lease(r.runner, "a-confirm", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMerge}); refused != "" { |
| 158 | t.Fatalf("confirmation refused %q", refused) |
| 159 | } |
| 160 | r.step("a-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 161 | r.step("a-m", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}, MergeSHA: merge, Into: "main", Strategy: wire.MergeCommit, MergeSource: wire.MergeSourceHub, MergeParents: []string{strings.Repeat("9", 40), cand}}) |
| 162 | r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 163 | k, _ := r.client.Task(ctx, id) |
| 164 | if k.Merged == nil || !k.Merged.WithoutChecks || !k.UndoOffered || k.MergeDecision == nil || k.MergeDecision.Class != "act" || k.MergeDecision.DecidedBy != "policy" { |
| 165 | t.Fatalf("merged %+v decision %+v undo %v", k.Merged, k.MergeDecision, k.UndoOffered) |
| 166 | } |
| 167 | var receipt wire.Card |
| 168 | r.wait("receipt", func(c *Conversation) bool { |
| 169 | for _, card := range c.Cards { |
| 170 | if card.Kind == wire.CardMergeReceipt && card.TaskID == id { |
| 171 | receipt = card |
| 172 | return true |
| 173 | } |
| 174 | } |
| 175 | return false |
| 176 | }) |
| 177 | want := "Merged [Swipe back from the edge](/tasks/" + id + ") into Sample without checks: approved by Review, a small change, 12 lines in one file, nothing ships until a release." |
| 178 | if receipt.Question != want || !slices.Equal(receipt.Options, []string{"undo"}) { |
| 179 | t.Fatalf("receipt %+v", receipt) |
| 180 | } |
| 181 | for _, text := range r.replies() { |
| 182 | if strings.HasPrefix(text, "Merged ") { |
| 183 | t.Fatalf("a line as well as the receipt: %q", text) |
| 184 | } |
| 185 | } |
| 186 | if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, "data-merge-receipt") || !strings.Contains(page, `data-merge-choice="undo"`) { |
| 187 | t.Fatalf("receipt on the page: %s", page) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // TestContractMergeAuthorityAsks: each failing criterion gives the merge |
| 192 | // card, worded as Equipo's call, and nothing is dispatched. |
| 193 | func TestContractMergeAuthorityAsks(t *testing.T) { |
| 194 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 195 | project := r.project() |
| 196 | r.shipsNo(project) |
| 197 | base := strings.Repeat("1", 40) |
| 198 | cases := []struct { |
| 199 | name, reason, want string |
| 200 | f func(*wire.DispatchCommand) *wire.ChangeFacts |
| 201 | options []string |
| 202 | }{ |
| 203 | {"facts", "needs_macos", "I'd wait: the runner didn't say what changed.", nil, []string{"merge", "wait_mac"}}, |
| 204 | {"payments", "needs_macos", "I'd wait: it touches payments (ios/Woodstar/Billing/Checkout.swift).", func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 205 | return facts(d, "ios/Woodstar/Billing/Checkout.swift") |
| 206 | }, []string{"merge", "wait_mac"}}, |
| 207 | {"lockfile", "needs_macos", "I'd wait: it touches dependencies (ios/Woodstar.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved).", func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 208 | return facts(d, "ios/Woodstar.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved") |
| 209 | }, []string{"merge", "wait_mac"}}, |
| 210 | {"big", "needs_macos", "I'd merge: approved by Review, nothing ships until a release, but it's larger than I can merge on my own (900 lines in one file).", func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 211 | f := facts(d) |
| 212 | f.Lines = 900 |
| 213 | return f |
| 214 | }, []string{"merge", "wait_mac"}}, |
| 215 | {"timeout", "checks_timeout", "I'd wait: checks couldn't run because they did not finish in time.", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }, []string{"merge", "wait"}}, |
| 216 | } |
| 217 | for i, c := range cases { |
| 218 | cand := strings.Repeat(string(rune('2'+i)), 40) |
| 219 | id, _ := r.measured("k"+itoa(i), "Case "+c.name, base, cand, wire.ChecksCannotRun, c.reason, c.f) |
| 220 | var card wire.Card |
| 221 | r.wait(c.name+" card", func(conv *Conversation) bool { |
| 222 | for _, k := range conv.Cards { |
| 223 | if k.Kind == wire.CardMerge && k.TaskID == id { |
| 224 | card = k |
| 225 | return true |
| 226 | } |
| 227 | } |
| 228 | return false |
| 229 | }) |
| 230 | if card.Question != "Merge Case "+c.name+"? "+c.want || !slices.Equal(card.Options, c.options) { |
| 231 | t.Errorf("%s: %q %v", c.name, card.Question, card.Options) |
| 232 | } |
| 233 | if k, _ := r.client.Task(r.ctx, id); k.MergeDecision == nil || k.MergeDecision.Class != "ask" { |
| 234 | t.Errorf("%s decision %+v", c.name, k.MergeDecision) |
| 235 | } |
| 236 | } |
| 237 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 238 | t.Fatalf("%d dispatches while asking", n) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // TestContractMergeFenceOldRunner: Paul's "Refuse old runners". At |
| 243 | // protocol 1 an automatic merge is never dispatched (checks passed |
| 244 | // included): the card asks, recommending the merge. A merge dispatched to |
| 245 | // a ProtocolMergeFacts runner that reconnects below it is refused at its |
| 246 | // lease, whatever it asks, settled with nothing sent, and asked about; the |
| 247 | // person's Merge on that card tries the failed attempt again. |
| 248 | func TestContractMergeFenceOldRunner(t *testing.T) { |
| 249 | old := newToolRig(t) |
| 250 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 251 | id := old.judgedTask("o", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 252 | cards := old.mergeCardsWait(id) |
| 253 | if !strings.Contains(cards.Question, "your runner needs an update before I merge on my own") || old.heldKinds(wire.FrameDispatch) != 0 { |
| 254 | t.Fatalf("protocol 1: %+v (%d dispatches)", cards, old.heldKinds(wire.FrameDispatch)) |
| 255 | } |
| 256 | |
| 257 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 258 | id = r.judgedTask("f", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 259 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 260 | r.dispatchAcked(m.StepID) |
| 261 | // The runner comes back at protocol 2 (an older build) before the |
| 262 | // request: its lease names the attempt but cannot carry the purpose. |
| 263 | two, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, primaryAt(wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, wire.Protocol2)) |
| 264 | if err != nil { |
| 265 | t.Fatal(err) |
| 266 | } |
| 267 | t.Cleanup(two.Close) |
| 268 | // Fenced at the connection: the attempt is settled and cancelled, and |
| 269 | // its lease refused. |
| 270 | cancelled := false |
| 271 | for !cancelled { |
| 272 | f, body, err := two.Next(r.ctx) |
| 273 | if err != nil { |
| 274 | t.Fatal(err) |
| 275 | } |
| 276 | if c, ok := body.(*wire.CancelStepCommand); ok && c.StepID == m.StepID && c.Attempt == m.Attempt { |
| 277 | cancelled = true |
| 278 | } |
| 279 | if f.Durable() { |
| 280 | two.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| 281 | } |
| 282 | } |
| 283 | if refused := r.lease(two, "f-lease", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused == "" { |
| 284 | t.Fatal("an old runner's lease for an automatic merge was granted") |
| 285 | } |
| 286 | r.wait("ready again, asking", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 287 | card := r.mergeCardsWait(id) |
| 288 | if !strings.Contains(card.Question, "your runner needs an update before I merge on my own") { |
| 289 | t.Fatalf("fenced card %+v", card) |
| 290 | } |
| 291 | k, _ := r.client.Task(r.ctx, id) |
| 292 | if last := k.Steps[len(k.Steps)-1]; last.Specialist != wire.SpecialistMerge || last.State != wire.StepFailed || last.Reason != "merge_not_authorized" { |
| 293 | t.Fatalf("settled %+v", last) |
| 294 | } |
| 295 | // The person's Merge tries the failed attempt again, as theirs. |
| 296 | r.runner = two |
| 297 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 298 | t.Fatal(err) |
| 299 | } |
| 300 | _, body := r.expect(wire.FrameDispatch) |
| 301 | again := body.(*wire.DispatchCommand) |
| 302 | if again.StepID != m.StepID || again.Attempt != 2 || again.Specialist != wire.SpecialistMerge { |
| 303 | t.Fatalf("retried %+v", again) |
| 304 | } |
| 305 | if refused := r.lease(two, "f-lease-2", wire.LeaseCheck{StepID: again.StepID, Attempt: again.Attempt}); refused != "" { |
| 306 | t.Fatalf("the person's merge refused on an old runner: %q", refused) |
| 307 | } |
| 308 | |
| 309 | // Protocol 1 cannot name the step in its lease at all: the fence at the |
| 310 | // connection is all there is. A merge dispatched at ProtocolMergeFacts |
| 311 | // is settled, cancelled and asked about the moment the runner comes |
| 312 | // back at protocol 1. |
| 313 | r1 := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 314 | id1 := r1.judgedTask("p1", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 315 | m1 := r1.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 316 | r1.dispatchAcked(m1.StepID) |
| 317 | one, err := DialRunnerHello(r1.ctx, r1.inst.URL, r1.rig.runner, wire.Hello{Generation: r1.runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "old"}}) |
| 318 | if err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | t.Cleanup(one.Close) |
| 322 | if one.Welcome.Protocol > wire.Protocol1 { |
| 323 | t.Fatalf("welcome %+v", one.Welcome) |
| 324 | } |
| 325 | for { |
| 326 | f, body, err := one.Next(r1.ctx) |
| 327 | if err != nil { |
| 328 | t.Fatal(err) |
| 329 | } |
| 330 | if f.Durable() { |
| 331 | one.Send(r1.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| 332 | } |
| 333 | if c, ok := body.(*wire.CancelStepCommand); ok && c.StepID == m1.StepID { |
| 334 | break |
| 335 | } |
| 336 | } |
| 337 | r1.wait("asked after the fence", func(c *Conversation) bool { return c.Tasks[id1].Stage == wire.StageReadyToMerge }) |
| 338 | if card := r1.mergeCardsWait(id1); !strings.Contains(card.Question, "your runner needs an update before I merge on my own") { |
| 339 | t.Fatalf("protocol 1 fence card %+v", card) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // dispatchAcked waits until the instance has the runner's ack of the |
| 344 | // step's dispatch, so a fence after it cancels (rather than drops) it. |
| 345 | func (r *toolRig) dispatchAcked(step string) { |
| 346 | r.t.Helper() |
| 347 | deadline := time.Now().Add(20 * time.Second) |
| 348 | for { |
| 349 | var n int |
| 350 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ? AND acked_at IS NOT NULL AND body LIKE ?", wire.FrameDispatch, `%"step_id":"`+step+`"%`).Scan(&n); err != nil { |
| 351 | r.t.Fatal(err) |
| 352 | } |
| 353 | if n > 0 { |
| 354 | return |
| 355 | } |
| 356 | if time.Now().After(deadline) { |
| 357 | r.t.Fatal("the dispatch was never acked") |
| 358 | } |
| 359 | time.Sleep(20 * time.Millisecond) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // mergeCardsWait waits for the task's merge card. |
| 364 | func (r *toolRig) mergeCardsWait(id string) wire.Card { |
| 365 | r.t.Helper() |
| 366 | var card wire.Card |
| 367 | r.wait("merge card", func(c *Conversation) bool { |
| 368 | for _, k := range c.Cards { |
| 369 | if k.Kind == wire.CardMerge && k.TaskID == id && k.State == wire.CardOpen { |
| 370 | card = k |
| 371 | return true |
| 372 | } |
| 373 | } |
| 374 | return false |
| 375 | }) |
| 376 | return card |
| 377 | } |
| 378 | |
| 379 | // TestContractConfirmRechecks: right before the hub request the gate |
| 380 | // decides again, checked merges included: a hold, or the project moving |
| 381 | // to ask, refuses the request; nothing is sent and the person is asked. |
| 382 | func TestContractConfirmRechecks(t *testing.T) { |
| 383 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 384 | project := r.project() |
| 385 | r.shipsNo(project) |
| 386 | base := strings.Repeat("1", 40) |
| 387 | // Checks passed, held by the coordinator before the request. |
| 388 | cand := strings.Repeat("2", 40) |
| 389 | id := r.judgedTask("h", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 390 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 391 | if res := r.call("h-hold", wire.ToolHoldMerge, map[string]string{"task": id, "reason": "the person wants to read this one first"}); res.IsError { |
| 392 | t.Fatalf("hold %+v", res) |
| 393 | } |
| 394 | if refused := r.lease(r.runner, "h-confirm", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMerge}); refused != wire.RefusedMergeNotAuthorized { |
| 395 | t.Fatalf("a held merge's request: %q", refused) |
| 396 | } |
| 397 | if card := r.mergeCardsWait(id); !strings.Contains(card.Question, "I'd wait: the person wants to read this one first") { |
| 398 | t.Fatalf("held card %+v", card) |
| 399 | } |
| 400 | // Without checks: the project moves to ask between dispatch and the |
| 401 | // request. |
| 402 | cand = strings.Repeat("3", 40) |
| 403 | id = func() string { |
| 404 | id, _ := r.measured("p", "Swipe back", base, cand, wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 405 | return id |
| 406 | }() |
| 407 | m = r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 408 | p, _ := r.client.MergePolicy(r.ctx, project) |
| 409 | if _, err := r.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: "ask", MaxLines: p.MaxLines, MaxFiles: p.MaxFiles, MainShips: p.MainShips, ExtraSensitive: p.ExtraSensitive, Revision: p.Revision}); err != nil { |
| 410 | t.Fatal(err) |
| 411 | } |
| 412 | if refused := r.lease(r.runner, "p-confirm", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMerge}); refused != wire.RefusedMergeNotAuthorized { |
| 413 | t.Fatalf("a request after the project asks: %q", refused) |
| 414 | } |
| 415 | if card := r.mergeCardsWait(id); !strings.Contains(card.Question, "I ask before every merge in this project") { |
| 416 | t.Fatalf("ask-mode card %+v", card) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // TestContractNoQueuedAutoMerge: with the primary offline nothing is |
| 421 | // decided or queued; the next sync is the next trigger. |
| 422 | func TestContractNoQueuedAutoMerge(t *testing.T) { |
| 423 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 424 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 425 | id, _ := r.pushed("q", "Fix the README typo", base, cand) |
| 426 | r.expect(wire.FrameWatchMerge) |
| 427 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 428 | r.checks("q-c", c, wire.ChecksPassed, exit(0), "", "") |
| 429 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 430 | if _, err := r.inst.DB.Writer().Exec("UPDATE runner_sessions SET connected = 0"); err != nil { |
| 431 | t.Fatal(err) |
| 432 | } |
| 433 | r.verdict("q-r", rv, wire.VerdictApprove) |
| 434 | if k, _ := r.client.Task(r.ctx, id); k.Stage != wire.StageReadyToMerge || k.MergeDecision != nil || r.heldKinds(wire.FrameDispatch) != 0 || len(r.mergeCards(id)) != 0 { |
| 435 | t.Fatalf("decided while offline: %+v", k) |
| 436 | } |
| 437 | if _, err := r.inst.DB.Writer().Exec("UPDATE runner_sessions SET connected = 1"); err != nil { |
| 438 | t.Fatal(err) |
| 439 | } |
| 440 | r.syncNow("q-sync") |
| 441 | r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 442 | } |
| 443 | |
| 444 | // TestContractShipsQuestion: unknown shipping asks, with the one ships |
| 445 | // question per project; the answer is policy for later decisions only: |
| 446 | // the open card is not re-decided, the next task merges on its own. |
| 447 | func TestContractShipsQuestion(t *testing.T) { |
| 448 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 449 | base := strings.Repeat("1", 40) |
| 450 | id, _ := r.measured("s", "Swipe back", base, strings.Repeat("2", 40), wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 451 | card := r.mergeCardsWait(id) |
| 452 | if !strings.Contains(card.Question, "I don't know yet whether merging to Sample's main branch ships to users") { |
| 453 | t.Fatalf("card %+v", card) |
| 454 | } |
| 455 | var ships wire.Card |
| 456 | r.wait("ships question", func(c *Conversation) bool { |
| 457 | for _, k := range c.Cards { |
| 458 | if k.Kind == wire.CardMergeShips { |
| 459 | ships = k |
| 460 | return true |
| 461 | } |
| 462 | } |
| 463 | return false |
| 464 | }) |
| 465 | if ships.Question != "Does merging to Sample's main branch ship to users?" || !slices.Equal(ships.Options, []string{"ships", "not_ships"}) { |
| 466 | t.Fatalf("ships %+v", ships) |
| 467 | } |
| 468 | // A second task asking on the same unknown: no second question. |
| 469 | other, _ := r.measured("s1", "Edge swipe on iPad", base, strings.Repeat("5", 40), wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 470 | r.mergeCardsWait(other) |
| 471 | // Only the owner it asked answers it; a stale revision is a conflict. |
| 472 | if _, err := r.client.Answer(r.ctx, ships.ID, wire.ChoiceNotShips, ships.Revision+1); Status(err) != http.StatusConflict { |
| 473 | t.Fatalf("stale: %v", err) |
| 474 | } |
| 475 | if _, err := r.client.Answer(r.ctx, ships.ID, wire.ChoiceNotShips, ships.Revision); err != nil { |
| 476 | t.Fatal(err) |
| 477 | } |
| 478 | if p, _ := r.client.MergePolicy(r.ctx, r.projectOf(id)); p.MainShips != "no" { |
| 479 | t.Fatalf("policy %+v", p) |
| 480 | } |
| 481 | // The open card stays the person's; a sync decides nothing. |
| 482 | r.syncNow("s-sync") |
| 483 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 484 | t.Fatalf("re-decided an open card: %d dispatches", n) |
| 485 | } |
| 486 | // A second task: one question only, and it merges on its own. |
| 487 | r.measured("s2", "Haptics", base, strings.Repeat("3", 40), wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 488 | r.dispatched(wire.SpecialistMerge, base, strings.Repeat("3", 40), 0) |
| 489 | n := 0 |
| 490 | r.conv.Locked(func(c *Conversation) { |
| 491 | for _, k := range c.Cards { |
| 492 | if k.Kind == wire.CardMergeShips { |
| 493 | n++ |
| 494 | } |
| 495 | } |
| 496 | }) |
| 497 | if n != 1 { |
| 498 | t.Fatalf("%d ships questions", n) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // TestContractWaitNeverMerges: Wait for a Mac only waits; Merge stays |
| 503 | // offered, and nothing merges on its own afterwards. |
| 504 | func TestContractWaitNeverMerges(t *testing.T) { |
| 505 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 506 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 507 | id, _ := r.measured("w", "Swipe back", base, cand, wire.ChecksCannotRun, "needs_macos", nil) |
| 508 | card := r.mergeCardsWait(id) |
| 509 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); err != nil { |
| 510 | t.Fatal(err) |
| 511 | } |
| 512 | r.shipsNo(r.projectOf(id)) |
| 513 | r.syncNow("w-sync") |
| 514 | if k, _ := r.client.Task(r.ctx, id); k.Stage != wire.StageReadyToMerge || !k.MergeOffered || r.heldKinds(wire.FrameDispatch) != 0 { |
| 515 | t.Fatalf("after Wait %+v", k) |
| 516 | } |
| 517 | if len(r.mergeCards(id)) != 1 { |
| 518 | t.Fatal("asked again on a sync") |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // TestContractUndoMerge: Undo of Equipo's own merge opens an ordinary |
| 523 | // revert task (Engineering first, through the normal flow), puts the |
| 524 | // project in ask mode, and is refused a second time, for a teammate, and |
| 525 | // for a merge whose commit the hub did not name. |
| 526 | func TestContractUndoMerge(t *testing.T) { |
| 527 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 528 | project := r.project() |
| 529 | r.shipsNo(project) |
| 530 | base, cand, merge := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) |
| 531 | id, _ := r.measured("u", "Swipe back", base, cand, wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 532 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 533 | r.step("u-m", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}, MergeSHA: merge, Into: "main", Strategy: wire.MergeCommit, MergeSource: wire.MergeSourceHub, MergeParents: []string{strings.Repeat("9", 40), cand}}) |
| 534 | receipt, _ := func() (wire.Card, bool) { |
| 535 | var out wire.Card |
| 536 | r.wait("receipt", func(c *Conversation) bool { |
| 537 | for _, k := range c.Cards { |
| 538 | if k.Kind == wire.CardMergeReceipt && k.TaskID == id { |
| 539 | out = k |
| 540 | return true |
| 541 | } |
| 542 | } |
| 543 | return false |
| 544 | }) |
| 545 | return out, true |
| 546 | }() |
| 547 | mate := personToolRig(t, r, "mate@example.com", equipo.RoleOwner) |
| 548 | if _, err := mate.client.UndoMerge(r.ctx, id, receipt.Revision); Status(err) != http.StatusNotFound { |
| 549 | t.Fatalf("teammate undo: %v", err) |
| 550 | } |
| 551 | if _, err := r.client.UndoMerge(r.ctx, id, receipt.Revision+1); Status(err) != http.StatusConflict { |
| 552 | t.Fatalf("undo on a stale receipt: %v", err) |
| 553 | } |
| 554 | out, err := r.client.Answer(r.ctx, receipt.ID, wire.ChoiceUndo, receipt.Revision) |
| 555 | if err != nil { |
| 556 | t.Fatal(err) |
| 557 | } |
| 558 | if !strings.HasPrefix(out.Card.Question, "Reverting it: [Revert eeeeeee in Sample](/tasks/") { |
| 559 | t.Fatalf("receipt after undo %+v", out.Card) |
| 560 | } |
| 561 | _, body := r.expect(wire.FrameDispatch) |
| 562 | eng := body.(*wire.DispatchCommand) |
| 563 | if eng.Specialist != wire.SpecialistEngineering || !strings.Contains(eng.Brief, "git revert -m 1 "+merge) { |
| 564 | t.Fatalf("revert dispatch %+v", eng) |
| 565 | } |
| 566 | if p, _ := r.client.MergePolicy(r.ctx, project); p.Mode != "ask" || !strings.Contains(p.ModeReason, "eeeeeee") { |
| 567 | t.Fatalf("policy after undo %+v", p) |
| 568 | } |
| 569 | if _, err := r.client.UndoMerge(r.ctx, id, out.Card.Revision); Status(err) != http.StatusConflict { |
| 570 | t.Fatalf("undo twice: %v", err) |
| 571 | } |
| 572 | // The revert's review measures whether it reverts exactly that commit. |
| 573 | rev := eng.TaskID |
| 574 | revCand := strings.Repeat("4", 40) |
| 575 | r.step("rv-run", wire.StepReport{TaskID: rev, StepID: eng.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: merge, Family: "claude", Account: "claude-a"}) |
| 576 | r.step("rv-done", wire.StepReport{TaskID: rev, StepID: eng.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: merge, CandidateSHA: revCand, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"}) |
| 577 | r.expect(wire.FrameWatchMerge) |
| 578 | c := r.dispatched(wire.SpecialistChecks, merge, revCand, 0) |
| 579 | r.checks("rv-c", c, wire.ChecksCannotRun, nil, "needs_macos", "") |
| 580 | rv := r.dispatched(wire.SpecialistReview, merge, revCand, 0) |
| 581 | if rv.RevertSHA != merge { |
| 582 | t.Fatalf("review of a revert without its target %+v", rv) |
| 583 | } |
| 584 | f := facts(rv) |
| 585 | f.Revert = &wire.RevertCheck{SHA: merge, OnTop: true, Exact: true} |
| 586 | r.step("rv-r", wire.StepReport{TaskID: rev, StepID: rv.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: merge, CandidateSHA: revCand, Termination: true, Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Change: f}) |
| 587 | card := r.mergeCardsWait(rev) |
| 588 | if !strings.Contains(card.Question, "I'd merge: approved by Review, a small change, 12 lines in one file, nothing ships until a release, but I ask before every merge in this project.") { |
| 589 | t.Fatalf("revert card %+v", card) |
| 590 | } |
| 591 | |
| 592 | // A merge the forge settled from the default branch: no Undo. |
| 593 | r2 := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 594 | r2.shipsNo(r2.project()) |
| 595 | id2, _ := r2.measured("n", "Swipe back", base, cand, wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 596 | m2 := r2.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 597 | r2.step("n-m", wire.StepReport{TaskID: id2, StepID: m2.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}, MergeSHA: merge, Into: "main", Strategy: wire.MergeCommit, MergeSource: wire.MergeSourceReconciled}) |
| 598 | r2.wait("done", func(c *Conversation) bool { return c.Tasks[id2].Stage == wire.StageDone }) |
| 599 | var unverified wire.Card |
| 600 | r2.wait("receipt", func(c *Conversation) bool { |
| 601 | for _, k := range c.Cards { |
| 602 | if k.Kind == wire.CardMergeReceipt && k.TaskID == id2 { |
| 603 | unverified = k |
| 604 | return true |
| 605 | } |
| 606 | } |
| 607 | return false |
| 608 | }) |
| 609 | if len(unverified.Options) != 0 || !strings.Contains(unverified.Question, "so I can't undo it") { |
| 610 | t.Fatalf("unverified receipt %+v", unverified) |
| 611 | } |
| 612 | if _, err := r2.client.UndoMerge(r2.ctx, id2, unverified.Revision); Status(err) != http.StatusConflict { |
| 613 | t.Fatalf("undo of an unverified merge: %v", err) |
| 614 | } |
| 615 | // A person's merge without checks, even with the hub's own answer, |
| 616 | // has no receipt and no Undo: it was their call. |
| 617 | id3, _ := r2.measured("p", "Haptics", base, strings.Repeat("3", 40), wire.ChecksCannotRun, "needs_macos", nil) |
| 618 | card3 := r2.mergeCardsWait(id3) |
| 619 | if _, err := r2.client.Answer(r2.ctx, card3.ID, wire.ChoiceMerge, card3.Revision); err != nil { |
| 620 | t.Fatal(err) |
| 621 | } |
| 622 | m3 := r2.dispatched(wire.SpecialistMerge, base, strings.Repeat("3", 40), 0) |
| 623 | r2.step("p-m", wire.StepReport{TaskID: id3, StepID: m3.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: m3.CandidateSHA, Termination: true, Findings: []wire.Finding{}, MergeSHA: strings.Repeat("d", 40), Into: "main", Strategy: wire.MergeCommit, MergeSource: wire.MergeSourceHub, MergeParents: []string{strings.Repeat("9", 40), m3.CandidateSHA}}) |
| 624 | r2.wait("person's merge done", func(c *Conversation) bool { return c.Tasks[id3].Stage == wire.StageDone }) |
| 625 | if k, _ := r2.client.Task(r2.ctx, id3); k.UndoOffered { |
| 626 | t.Fatal("Undo offered on a person's merge") |
| 627 | } |
| 628 | if _, err := r2.client.UndoMerge(r2.ctx, id3, 1); Status(err) != http.StatusConflict { |
| 629 | t.Fatalf("undo of a person's merge: %v", err) |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // TestContractMergePolicyTool: the coordinator's set_merge_policy applies |
| 634 | // a stricter policy at once; a looser one only makes a card the person |
| 635 | // saves; hold_merge only stops. |
| 636 | func TestContractMergePolicyTool(t *testing.T) { |
| 637 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 638 | project := r.project() |
| 639 | if res := r.call("p-tight", wire.ToolSetMergePolicy, map[string]any{"project": "oficina/sample", "words": "always ask me about Woodstar", "mode": "ask"}); res.IsError || res.Code != "policy_set" { |
| 640 | t.Fatalf("tighter %+v", res) |
| 641 | } |
| 642 | p, _ := r.client.MergePolicy(r.ctx, project) |
| 643 | if p.Mode != "ask" || p.Words != "always ask me about Woodstar" { |
| 644 | t.Fatalf("after tighter %+v", p) |
| 645 | } |
| 646 | if res := r.call("p-loose", wire.ToolSetMergePolicy, map[string]any{"project": "oficina/sample", "words": "for Woodstar, merge when Review approves", "mode": "rules", "main_ships": "no"}); res.IsError || res.Code != "policy_proposed" { |
| 647 | t.Fatalf("looser %+v", res) |
| 648 | } |
| 649 | if again, _ := r.client.MergePolicy(r.ctx, project); again.Mode != "ask" || again.MainShips != "unknown" || again.Revision != p.Revision { |
| 650 | t.Fatalf("a looser policy applied without a tap %+v", again) |
| 651 | } |
| 652 | var confirm wire.Card |
| 653 | r.wait("confirm card", func(c *Conversation) bool { |
| 654 | for _, k := range c.Cards { |
| 655 | if k.Kind == wire.CardMergePolicy { |
| 656 | confirm = k |
| 657 | return true |
| 658 | } |
| 659 | } |
| 660 | return false |
| 661 | }) |
| 662 | if _, err := r.client.Answer(r.ctx, confirm.ID, wire.ChoiceSave, confirm.Revision); err != nil { |
| 663 | t.Fatal(err) |
| 664 | } |
| 665 | if after, _ := r.client.MergePolicy(r.ctx, project); after.Mode != "rules" || after.MainShips != "no" || after.Words != "for Woodstar, merge when Review approves" { |
| 666 | t.Fatalf("saved %+v", after) |
| 667 | } |
| 668 | if res := r.call("p-bad", wire.ToolSetMergePolicy, map[string]any{"project": "oficina/sample", "words": "anything", "max_lines": 5000}); !res.IsError { |
| 669 | t.Fatalf("out of bounds %+v", res) |
| 670 | } |
| 671 | // A member who is not an owner reads the policy and cannot change it. |
| 672 | member := personToolRig(t, r, "member@example.com", equipo.RoleMember) |
| 673 | mp, err := member.client.MergePolicy(r.ctx, project) |
| 674 | if err != nil || mp.Editable { |
| 675 | t.Fatalf("member reads %+v %v", mp, err) |
| 676 | } |
| 677 | if _, err := member.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: "rules", MaxLines: 2000, MaxFiles: 100, MainShips: "no", Revision: mp.Revision}); Status(err) != http.StatusForbidden { |
| 678 | t.Fatalf("member set: %v", err) |
| 679 | } |
| 680 | // The project page shows it, editable for the owner only. |
| 681 | page := r.get(r.inst.Cookie(), "/projects/"+project, 200) |
| 682 | if !strings.Contains(page, "How Equipo merges") || !strings.Contains(page, "data-merge-policy-form") || !strings.Contains(page, "In your words: for Woodstar, merge when Review approves") { |
| 683 | t.Fatalf("project page: %s", page) |
| 684 | } |
| 685 | if mpage := r.get(r.inst.SessionFor(t, "member@example.com"), "/projects/"+project, 200); !strings.Contains(mpage, "How Equipo merges") || strings.Contains(mpage, "data-merge-policy-form") { |
| 686 | t.Fatal("member's project page") |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // TestContractFactsAcceptance: facts are kept only when exactly the |
| 691 | // review attempt's, whole: otherwise the gate asks on facts_present. |
| 692 | func TestContractFactsAcceptance(t *testing.T) { |
| 693 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 694 | r.shipsNo(r.project()) |
| 695 | base := strings.Repeat("1", 40) |
| 696 | for i, bad := range []func(*wire.DispatchCommand) *wire.ChangeFacts{ |
| 697 | func(d *wire.DispatchCommand) *wire.ChangeFacts { f := facts(d); f.Attempt = 2; return f }, |
| 698 | func(d *wire.DispatchCommand) *wire.ChangeFacts { f := facts(d); f.ReviewStep = "s_other"; return f }, |
| 699 | func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 700 | f := facts(d) |
| 701 | f.SHA = strings.Repeat("a", 40) |
| 702 | return f |
| 703 | }, |
| 704 | func(d *wire.DispatchCommand) *wire.ChangeFacts { f := facts(d); f.Complete = false; return f }, |
| 705 | func(d *wire.DispatchCommand) *wire.ChangeFacts { f := facts(d); f.Files = 3; return f }, |
| 706 | func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 707 | f := facts(d) |
| 708 | f.Entries[0].New = "../escape" |
| 709 | return f |
| 710 | }, |
| 711 | func(d *wire.DispatchCommand) *wire.ChangeFacts { |
| 712 | f := facts(d) |
| 713 | f.Repo = "amadan.net/oficina/other" |
| 714 | return f |
| 715 | }, |
| 716 | } { |
| 717 | id, _ := r.measured("x"+itoa(i), "Case "+itoa(i), base, strings.Repeat(string(rune('2'+i)), 40), wire.ChecksCannotRun, "needs_macos", bad) |
| 718 | if card := r.mergeCardsWait(id); !strings.Contains(card.Question, "the runner didn't say what changed") { |
| 719 | t.Errorf("case %d kept: %q", i, card.Question) |
| 720 | } |
| 721 | } |
| 722 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 723 | t.Fatalf("%d merges on refused facts", n) |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // TestContractRosterDenial: a creator demoted from owner can no longer |
| 728 | // have Equipo merge their task, from the card or the task page. |
| 729 | func TestContractRosterDenial(t *testing.T) { |
| 730 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 731 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 732 | id, _ := r.measured("d", "Swipe back", base, cand, wire.ChecksCannotRun, "needs_macos", nil) |
| 733 | card := r.mergeCardsWait(id) |
| 734 | personToolRig(t, r, "second@example.com", equipo.RoleOwner) |
| 735 | if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'member' WHERE address = ?", Owner); err != nil { |
| 736 | t.Fatal(err) |
| 737 | } |
| 738 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); Status(err) != http.StatusForbidden { |
| 739 | t.Fatalf("a demoted creator's Merge: %v", err) |
| 740 | } |
| 741 | if err := r.client.do(r.ctx, "POST", "/api/v1/tasks/"+id+"/merge", nil, nil); Status(err) != http.StatusForbidden { |
| 742 | t.Fatalf("a demoted creator's task-page Merge: %v", err) |
| 743 | } |
| 744 | if n := r.heldKinds(wire.FrameDispatch); n != 0 { |
| 745 | t.Fatal("dispatched for a demoted creator") |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // TestContractCardRefresh: a hold or a stricter policy after the card |
| 750 | // opened rewrites the card's words, with a new revision, so the person |
| 751 | // answers what holds now; an answer from before the change is stale. |
| 752 | func TestContractCardRefresh(t *testing.T) { |
| 753 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 754 | project := r.project() |
| 755 | base := strings.Repeat("1", 40) |
| 756 | id, _ := r.measured("c", "Swipe back", base, strings.Repeat("2", 40), wire.ChecksCannotRun, "needs_macos", func(d *wire.DispatchCommand) *wire.ChangeFacts { return facts(d) }) |
| 757 | before := r.mergeCardsWait(id) |
| 758 | if res := r.call("c-hold", wire.ToolHoldMerge, map[string]string{"task": id, "reason": "the person wants to try it first"}); res.IsError { |
| 759 | t.Fatalf("hold %+v", res) |
| 760 | } |
| 761 | held := r.cardOf(id) |
| 762 | if held.Revision <= before.Revision || !strings.Contains(held.Question, "the person wants to try it first") { |
| 763 | t.Fatalf("after the hold %+v", held) |
| 764 | } |
| 765 | if _, err := r.client.Answer(r.ctx, before.ID, wire.ChoiceMerge, before.Revision); Status(err) != http.StatusConflict { |
| 766 | t.Fatalf("an answer from before the hold: %v", err) |
| 767 | } |
| 768 | p, _ := r.client.MergePolicy(r.ctx, project) |
| 769 | if _, err := r.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: "ask", MaxLines: p.MaxLines, MaxFiles: p.MaxFiles, MainShips: p.MainShips, ExtraSensitive: p.ExtraSensitive, Revision: p.Revision}); err != nil { |
| 770 | t.Fatal(err) |
| 771 | } |
| 772 | asking := r.cardOf(id) |
| 773 | if asking.Revision <= held.Revision || !strings.Contains(asking.Question, "I ask before every merge in this project") { |
| 774 | t.Fatalf("after ask mode %+v", asking) |
| 775 | } |
| 776 | if _, err := r.client.Answer(r.ctx, held.ID, wire.ChoiceMerge, held.Revision); Status(err) != http.StatusConflict { |
| 777 | t.Fatalf("an answer from before the policy change: %v", err) |
| 778 | } |
| 779 | if k, _ := r.client.Task(r.ctx, id); k.MergeDecision == nil || k.MergeDecision.Class != "ask" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 780 | t.Fatalf("refresh acted %+v", k.MergeDecision) |
| 781 | } |
| 782 | } |
| 783 | |