| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // Exploration tasks (spec 2026-11-12-equipo-exploration-tasks.md): |
| 13 | // mockups, prototypes and spikes end at a preview and the person's |
| 14 | // choice of direction, never a merge. |
| 15 | |
| 16 | var baseX, candX = strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 17 | |
| 18 | // previewsOn sends the primary's hello again saying it can publish |
| 19 | // previews (the first hello never says so). |
| 20 | func (r *toolRig) previewsOn() { |
| 21 | r.t.Helper() |
| 22 | h := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent) |
| 23 | h.Capabilities.Previews = true |
| 24 | h.Capabilities.PreviewHost = &wire.PreviewHost{Kind: wire.PreviewHostDevbox, Domain: "docs-t1.example.ts.net"} |
| 25 | if err := r.runner.Send(r.ctx, "h-previews", wire.FrameHello, h); err != nil { |
| 26 | r.t.Fatal(err) |
| 27 | } |
| 28 | r.client.Snapshot(r.ctx) |
| 29 | } |
| 30 | |
| 31 | // explored records an exploration, starts it, and reports its |
| 32 | // Engineering step pushed on cand; it returns the task and the step's |
| 33 | // dispatch. |
| 34 | func (r *toolRig) explored(key, title, cand string) (string, *wire.DispatchCommand) { |
| 35 | r.t.Helper() |
| 36 | id := taskID(r.t, r.call(key+"-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": title, "brief": "Two clickable mockups.", "separate": true, "kind": wire.KindExplore})) |
| 37 | if res := r.call(key+"-start", wire.ToolStartTask, map[string]string{"task": id, "brief": "Make them."}); res.IsError { |
| 38 | r.t.Fatalf("start: %+v", res) |
| 39 | } |
| 40 | d := r.nextDispatch() |
| 41 | r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX}) |
| 42 | r.step(key+"-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: cand, Pushed: true, Termination: true}) |
| 43 | r.expect(wire.FrameWatchMerge) |
| 44 | return id, d |
| 45 | } |
| 46 | |
| 47 | // approved has Review approve cand, saying whether it has a preview |
| 48 | // config; it returns Review's dispatch. |
| 49 | func (r *toolRig) approved(key, cand string, has bool) *wire.DispatchCommand { |
| 50 | r.t.Helper() |
| 51 | rv := r.dispatched(wire.SpecialistReview, baseX, cand, 0) |
| 52 | r.step(key+"-r", wire.StepReport{TaskID: rv.TaskID, StepID: rv.StepID, Attempt: rv.Attempt, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: cand, Termination: true, Family: "codex", Account: "codex-a", Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Summary: "Read it.", HasPreview: has}) |
| 53 | return rv |
| 54 | } |
| 55 | |
| 56 | // directionCard waits for the task's open direction card. |
| 57 | func (r *toolRig) directionCard(id string) wire.Card { |
| 58 | r.t.Helper() |
| 59 | var card wire.Card |
| 60 | r.wait("direction card", func(c *Conversation) bool { |
| 61 | for _, k := range c.Cards { |
| 62 | if k.Kind == wire.CardDirection && k.TaskID == id && k.State == wire.CardOpen { |
| 63 | card = k |
| 64 | return true |
| 65 | } |
| 66 | } |
| 67 | return false |
| 68 | }) |
| 69 | return card |
| 70 | } |
| 71 | |
| 72 | func (r *toolRig) noMerge() { |
| 73 | r.t.Helper() |
| 74 | if n := r.commandsLike(wire.FrameDispatch, `"specialist":"merge"`); n != 0 { |
| 75 | r.t.Fatalf("%d merge dispatches", n) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (r *toolRig) commandsLike(kind, like string) int { |
| 80 | r.t.Helper() |
| 81 | var n int |
| 82 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ? AND body LIKE ?", kind, "%"+like+"%").Scan(&n); err != nil { |
| 83 | r.t.Fatal(err) |
| 84 | } |
| 85 | return n |
| 86 | } |
| 87 | |
| 88 | // TestExploreFlow: the coordinator records an exploration; Engineering's |
| 89 | // brief asks for the preview config; Review comes next, no checks, told |
| 90 | // it reads an exploration; its approval schedules the preview and, at |
| 91 | // once, the direction card; the deployed preview fills the card's line |
| 92 | // in; nothing is ever merged. |
| 93 | func TestExploreFlow(t *testing.T) { |
| 94 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 95 | r.previewsOn() |
| 96 | id, d := r.explored("x", "Barber app mockups", candX) |
| 97 | if !strings.Contains(d.Brief, "release/preview.json") || !strings.Contains(d.Brief, "never merged") { |
| 98 | t.Fatalf("Engineering's brief: %q", d.Brief) |
| 99 | } |
| 100 | if k := r.task(id); k.Kind != wire.KindExplore { |
| 101 | t.Fatalf("kind %q", k.Kind) |
| 102 | } |
| 103 | rv := r.approved("x", candX, true) |
| 104 | if !strings.Contains(rv.Brief, "exploration, never merged") { |
| 105 | t.Fatalf("Review's brief: %q", rv.Brief) |
| 106 | } |
| 107 | pv := r.previewDispatch(candX) |
| 108 | if pv == nil { |
| 109 | t.Fatal("no preview") |
| 110 | } |
| 111 | card := r.directionCard(id) |
| 112 | if !strings.Contains(card.Question, "Barber app mockups") || strings.Join(card.Options, ",") != "build,again,drop" { |
| 113 | t.Fatalf("card %+v", card) |
| 114 | } |
| 115 | k := r.task(id) |
| 116 | if k.NeedsYou != wire.NeedsDirection || k.Stage != wire.StageInReview || k.Direction == nil || k.Direction.PreviewURL != "" || k.Direction.PreviewsOff { |
| 117 | t.Fatalf("waiting %+v %+v", k.NeedsYou, k.Direction) |
| 118 | } |
| 119 | if r.commandsLike(wire.FrameDispatch, `"specialist":"checks"`) != 0 { |
| 120 | t.Fatal("an exploration got checks") |
| 121 | } |
| 122 | // The preview deploys: its link is the card's line. |
| 123 | if refused := r.lease(r.runner, "x-pv-start", wire.LeaseCheck{StepID: pv.StepID, Attempt: pv.Attempt}); refused != "" { |
| 124 | t.Fatalf("start refused %q", refused) |
| 125 | } |
| 126 | r.built("x-pv-built", pv, digestA) |
| 127 | if refused := r.lease(r.runner, "x-pv-pub", wire.LeaseCheck{StepID: pv.StepID, Attempt: pv.Attempt, Purpose: wire.LeasePurposePreview, Digest: digestA}); refused != "" { |
| 128 | t.Fatalf("publish refused %q", refused) |
| 129 | } |
| 130 | expires := time.Now().Add(7 * 24 * time.Hour) |
| 131 | link := "https://tmp-" + id + ".bds.oncarlos.com/" |
| 132 | r.previewDone("x-pv-done", pv, wire.PreviewResult{Phase: wire.PreviewDone, App: "tmp-" + id, URL: link, Version: "p-" + pv.StepID + "-1", Digest: digestA, ExpiresAt: &expires, Outcome: wire.PreviewDeployed}) |
| 133 | if k := r.task(id); k.Direction == nil || k.Direction.PreviewURL != link || k.NeedsYou != wire.NeedsDirection { |
| 134 | t.Fatalf("after the preview %+v", k.Direction) |
| 135 | } |
| 136 | page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 137 | if list := r.get(r.inst.Cookie(), "/tasks", 200); !strings.Contains(list, "Exploration") { |
| 138 | t.Fatal("no Exploration badge on the task list") |
| 139 | } |
| 140 | for _, want := range []string{"Preview: tmp-" + id + ".bds.oncarlos.com", `href="/tasks/` + id + `/preview"`, "data-direction-choice", "data-direction-note"} { |
| 141 | if !strings.Contains(page, want) && !strings.Contains(r.get(r.inst.Cookie(), "/conversation", 200), want) { |
| 142 | t.Fatalf("pages lack %q", want) |
| 143 | } |
| 144 | } |
| 145 | r.syncNow("x-sync") |
| 146 | r.noMerge() |
| 147 | } |
| 148 | |
| 149 | // toDirection takes an exploration to its direction card (previews off: |
| 150 | // a primary at 10 without the capability). |
| 151 | func (r *toolRig) toDirection(key, title string) (string, wire.Card) { |
| 152 | r.t.Helper() |
| 153 | id, _ := r.explored(key, title, candX) |
| 154 | r.approved(key, candX, true) |
| 155 | return id, r.directionCard(id) |
| 156 | } |
| 157 | |
| 158 | // TestExploreBuildThisOne: one transaction makes a linked change task, |
| 159 | // from the default branch, its brief naming the explored branch and |
| 160 | // commit, started at once; the exploration is done; a replayed answer |
| 161 | // changes nothing. |
| 162 | func TestExploreBuildThisOne(t *testing.T) { |
| 163 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 164 | id, card := r.toDirection("b", "Barber app mockups") |
| 165 | explore := r.task(id) |
| 166 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceBuild, card.Revision); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | d := r.nextDispatch() |
| 170 | k := r.task(id) |
| 171 | if k.Stage != wire.StageDone || k.BuiltAs == nil || k.BuiltAs.ID != d.TaskID { |
| 172 | t.Fatalf("exploration after Build %+v %+v", k.Stage, k.BuiltAs) |
| 173 | } |
| 174 | built := r.task(d.TaskID) |
| 175 | if built.Kind != "" || built.ExploredFrom == nil || built.ExploredFrom.ID != id || built.Title != "Build: Barber app mockups" || built.Stage != wire.StageInProgress { |
| 176 | t.Fatalf("built %+v", built) |
| 177 | } |
| 178 | if d.Specialist != wire.SpecialistEngineering || d.BaseSHA != "" || !strings.Contains(d.Brief, explore.Branch) || !strings.Contains(d.Brief, candX[:7]) || strings.Contains(d.Brief, "release/preview.json") { |
| 179 | t.Fatalf("its Engineering: base %q brief %q", d.BaseSHA, d.Brief) |
| 180 | } |
| 181 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceBuild, card.Revision); Status(err) != 409 { |
| 182 | t.Fatalf("a replayed answer: %v", err) |
| 183 | } |
| 184 | if n := r.commandsLike(wire.FrameDispatch, `"title":"Build: Barber app mockups"`); n != 1 { |
| 185 | t.Fatalf("%d builds", n) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // TestExploreBuildNote: Build this may carry the person's note (protocol |
| 190 | // 36: the option they want), bounded as Try again's is: past |
| 191 | // wire.MaxDirectionNote, with a NUL, or blank, it is refused and nothing |
| 192 | // is built; within it, Build goes ahead. |
| 193 | func TestExploreBuildNote(t *testing.T) { |
| 194 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 195 | id, card := r.toDirection("b", "Barber app mockups") |
| 196 | for name, note := range map[string]string{"too long": strings.Repeat("a", wire.MaxDirectionNote+1), "a NUL": "The bold\x00 grid.", "blank": " "} { |
| 197 | if _, err := r.client.AnswerNote(r.ctx, card.ID, wire.ChoiceBuild, note, card.Revision); Status(err) != 422 { |
| 198 | t.Fatalf("a note %s: %v", name, err) |
| 199 | } |
| 200 | } |
| 201 | if k := r.task(id); k.BuiltAs != nil || k.Stage == wire.StageDone { |
| 202 | t.Fatalf("a refused note built %+v", k.BuiltAs) |
| 203 | } |
| 204 | if _, err := r.client.AnswerNote(r.ctx, card.ID, wire.ChoiceBuild, strings.Repeat("a", wire.MaxDirectionNote), card.Revision); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | d := r.nextDispatch() |
| 208 | if k := r.task(id); k.Stage != wire.StageDone || k.BuiltAs == nil || k.BuiltAs.ID != d.TaskID { |
| 209 | t.Fatalf("Build with a note: %+v %+v", k.Stage, k.BuiltAs) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // TestExploreTryAgain: Try again needs the person's note and sends one |
| 214 | // more round with it (brief and resumed-session feedback); its new |
| 215 | // candidate is asked about afresh. |
| 216 | func TestExploreTryAgain(t *testing.T) { |
| 217 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 218 | id, card := r.toDirection("a", "Barber app mockups") |
| 219 | if _, err := r.client.AnswerNote(r.ctx, card.ID, wire.ChoiceAgain, "", card.Revision); Status(err) != 422 { |
| 220 | t.Fatalf("no note: %v", err) |
| 221 | } |
| 222 | if _, err := r.client.AnswerNote(r.ctx, card.ID, wire.ChoiceAgain, "Make the booking button bigger.", card.Revision); err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | d := r.dispatched(wire.SpecialistEngineering, candX, "", 1) |
| 226 | if strings.Contains(d.Brief, "Make the booking button bigger.") { |
| 227 | t.Fatal("person note in coordinator brief", d.Brief) |
| 228 | } |
| 229 | for _, s := range []string{d.Feedback} { |
| 230 | if !strings.Contains(s, "Make the booking button bigger.") { |
| 231 | t.Fatalf("the note is missing: %q", s) |
| 232 | } |
| 233 | } |
| 234 | if !strings.Contains(d.Feedback, "release/preview.json") { |
| 235 | t.Fatalf("a resumed session misses the exploration's rule: %q", d.Feedback) |
| 236 | } |
| 237 | cand2 := strings.Repeat("3", 40) |
| 238 | r.step("a2", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: candX, CandidateSHA: cand2, Pushed: true, Termination: true}) |
| 239 | r.expect(wire.FrameWatchMerge) |
| 240 | rv := r.dispatched(wire.SpecialistReview, baseX, cand2, 1) |
| 241 | r.verdict("a2-r", rv, wire.VerdictApprove) |
| 242 | again := r.directionCard(id) |
| 243 | if again.ID == card.ID { |
| 244 | t.Fatal("the same card") |
| 245 | } |
| 246 | r.noMerge() |
| 247 | } |
| 248 | |
| 249 | // TestExploreDropIt: the exploration is cancelled; nothing is sent. |
| 250 | func TestExploreDropIt(t *testing.T) { |
| 251 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 252 | id, card := r.toDirection("d", "Barber app mockups") |
| 253 | before := r.commandsLike(wire.FrameDispatch, "") |
| 254 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceDrop, card.Revision); err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | if k := r.task(id); k.Stage != wire.StageCancelled || r.commandsLike(wire.FrameDispatch, "") != before { |
| 258 | t.Fatalf("dropped %+v", k.Stage) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // changeAtReady is a change approved with checks passed, left at Ready to |
| 263 | // merge with its merge card (the project asks before every merge). |
| 264 | func (r *toolRig) changeAtReady(key, title string) (string, wire.Card) { |
| 265 | r.t.Helper() |
| 266 | r.asksBeforeMerging() |
| 267 | id := r.judgedTask(key, title, baseX, candX, wire.ChecksPassed, "") |
| 268 | return id, r.mergeCardsWait(id) |
| 269 | } |
| 270 | |
| 271 | // TestExploreNeverMerges: once an exploration, nothing merges it: the |
| 272 | // task page's Merge, the withdrawn merge card, a merge Retry. |
| 273 | func TestExploreNeverMerges(t *testing.T) { |
| 274 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 275 | id, mc := r.changeAtReady("n", "Barber app mockups") |
| 276 | if _, err := r.client.SetKind(r.ctx, id, wire.KindExplore, r.task(id).Revision); err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | if _, err := r.client.Answer(r.ctx, mc.ID, wire.ChoiceMerge, mc.Revision); Status(err) != 409 { |
| 280 | t.Fatalf("the withdrawn merge card: %v", err) |
| 281 | } |
| 282 | if _, err := r.client.MergeTask(r.ctx, id); err == nil { |
| 283 | t.Fatal("the task page's Merge merged an exploration") |
| 284 | } |
| 285 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); strings.Contains(page, "data-task-merge") { |
| 286 | t.Fatal("Merge offered on an exploration") |
| 287 | } |
| 288 | // Even forced to Ready to merge, the gate and startMerge refuse it. |
| 289 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET stage = ?, merge_asked = '' WHERE id = ?", wire.StageReadyToMerge, id); err != nil { |
| 290 | t.Fatal(err) |
| 291 | } |
| 292 | if _, err := r.client.MergeTask(r.ctx, id); err == nil { |
| 293 | t.Fatal("a forced exploration merged") |
| 294 | } |
| 295 | r.syncNow("n-sync") |
| 296 | r.noMerge() |
| 297 | } |
| 298 | |
| 299 | // TestExploreKindSwitch: Paul's switch, only at rest, at the task's |
| 300 | // revision, by its creator. A change at Ready to merge becomes an |
| 301 | // exploration: its merge card goes and the direction is asked. Back to a |
| 302 | // change, an exploration's approval no longer counts: checks, then a |
| 303 | // change Review. A round trip on the same commit asks the direction again. |
| 304 | func TestExploreKindSwitch(t *testing.T) { |
| 305 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 306 | id, card := r.toDirection("k", "Barber app mockups") |
| 307 | // Another owner never switches it, at rest and at its revision: only |
| 308 | // its creator. |
| 309 | other := personToolRig(t, r, "second@example.com", equipo.RoleOwner) |
| 310 | if _, err := other.client.SetKind(r.ctx, id, wire.KindChange, r.task(id).Revision); Status(err) != 403 { |
| 311 | t.Fatalf("another owner: %v", err) |
| 312 | } |
| 313 | k := r.task(id) |
| 314 | if _, err := r.client.SetKind(r.ctx, id, wire.KindChange, k.Revision-1); Status(err) != 409 { |
| 315 | t.Fatalf("stale revision: %v", err) |
| 316 | } |
| 317 | if _, err := r.client.SetKind(r.ctx, id, "merge", k.Revision); Status(err) != 422 { |
| 318 | t.Fatalf("unknown kind: %v", err) |
| 319 | } |
| 320 | if _, err := r.client.SetKind(r.ctx, id, wire.KindChange, k.Revision); err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | c := r.dispatched(wire.SpecialistChecks, baseX, candX, 0) |
| 324 | if k := r.task(id); k.Kind != "" || k.NeedsYou != "" { |
| 325 | t.Fatalf("a change again %+v", k.NeedsYou) |
| 326 | } |
| 327 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceBuild, card.Revision); Status(err) != 409 { |
| 328 | t.Fatalf("the withdrawn direction card: %v", err) |
| 329 | } |
| 330 | // Not at rest: the checks are running. |
| 331 | if _, err := r.client.SetKind(r.ctx, id, wire.KindExplore, r.task(id).Revision); Status(err) != 409 { |
| 332 | t.Fatalf("switched while busy: %v", err) |
| 333 | } |
| 334 | r.checks("k-c2", c, wire.ChecksPassed, exit(0), "", "ok\n") |
| 335 | rv := r.dispatched(wire.SpecialistReview, baseX, candX, 0) |
| 336 | if strings.Contains(rv.Brief, "exploration") { |
| 337 | t.Fatalf("a change's Review told it reads an exploration: %q", rv.Brief) |
| 338 | } |
| 339 | r.verdict("k-r2", rv, wire.VerdictChanges) |
| 340 | r.dispatched(wire.SpecialistEngineering, candX, "", 1) |
| 341 | } |
| 342 | |
| 343 | // TestExploreRoundTrip: explore, change and explore again on the same |
| 344 | // approved commit (a change Review counts for an exploration) asks the |
| 345 | // direction afresh. |
| 346 | func TestExploreRoundTrip(t *testing.T) { |
| 347 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 348 | id, mc := r.changeAtReady("t", "Barber app mockups") |
| 349 | if _, err := r.client.SetKind(r.ctx, id, wire.KindExplore, r.task(id).Revision); err != nil { |
| 350 | t.Fatal(err) |
| 351 | } |
| 352 | first := r.directionCard(id) |
| 353 | if k := r.task(id); k.NeedsYou != wire.NeedsDirection || k.Stage != wire.StageInReview { |
| 354 | t.Fatalf("as an exploration %+v %+v", k.NeedsYou, k.Stage) |
| 355 | } |
| 356 | if c := r.cardOf(id); c.ID == mc.ID && c.State == wire.CardOpen { |
| 357 | t.Fatal("the merge card stayed open") |
| 358 | } |
| 359 | if _, err := r.client.SetKind(r.ctx, id, wire.KindChange, r.task(id).Revision); err != nil { |
| 360 | t.Fatal(err) |
| 361 | } |
| 362 | if _, err := r.client.SetKind(r.ctx, id, wire.KindExplore, r.task(id).Revision); err != nil { |
| 363 | t.Fatal(err) |
| 364 | } |
| 365 | second := r.directionCard(id) |
| 366 | if second.ID == first.ID { |
| 367 | t.Fatal("not asked afresh") |
| 368 | } |
| 369 | r.noMerge() |
| 370 | } |
| 371 | |
| 372 | // TestExplorePreviewsOff: a primary at 10 without previews gets no |
| 373 | // preview step; the card says previews aren't set up and links the |
| 374 | // branch. A primary at 9 says nothing about previews: no preview this |
| 375 | // time, unless one deployed. |
| 376 | func TestExplorePreviewsOff(t *testing.T) { |
| 377 | t.Run("off", func(t *testing.T) { |
| 378 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 379 | id, _ := r.toDirection("o", "Barber app mockups") |
| 380 | if pv := r.previewDispatch(candX); pv != nil { |
| 381 | t.Fatalf("a preview with previews off: %+v", pv) |
| 382 | } |
| 383 | k := r.task(id) |
| 384 | if k.Direction == nil || !k.Direction.PreviewsOff || k.Direction.BranchURL == "" { |
| 385 | t.Fatalf("direction %+v", k.Direction) |
| 386 | } |
| 387 | page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 388 | if !strings.Contains(page, "data-direction-branch") || !strings.Contains(page, `href="/conversation/settings/machines#previews"`) || !strings.Contains(page, "Pair a preview host to open previews in your browser.") { |
| 389 | t.Fatalf("no pairing and branch links: %s", page) |
| 390 | } |
| 391 | }) |
| 392 | t.Run("protocol 9", func(t *testing.T) { |
| 393 | r := newToolRigAt(t, wire.ProtocolPreview) |
| 394 | id, _ := r.toDirection("p", "Barber app mockups") |
| 395 | if k := r.task(id); k.Direction == nil || k.Direction.PreviewsOff { |
| 396 | t.Fatalf("direction %+v", k.Direction) |
| 397 | } |
| 398 | }) |
| 399 | t.Run("on later", func(t *testing.T) { |
| 400 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 401 | _, _ = r.toDirection("l", "Barber app mockups") |
| 402 | if pv := r.previewDispatch(candX); pv != nil { |
| 403 | t.Fatal("previewed before the capability") |
| 404 | } |
| 405 | r.previewsOn() |
| 406 | if pv := r.previewDispatch(candX); pv == nil { |
| 407 | t.Fatal("no preview once previews are on") |
| 408 | } |
| 409 | r.previewsOn() |
| 410 | if pv := r.previewDispatch(candX); pv != nil { |
| 411 | t.Fatal("a second preview") |
| 412 | } |
| 413 | }) |
| 414 | } |
| 415 | |
| 416 | // TestExploreOnce: the start-up switch of a change recorded before |
| 417 | // explorations existed, its merge failed at Ready to merge: once, through |
| 418 | // the same switch, raising the direction; Paul switching it back is never |
| 419 | // undone; an ended task only writes the row; a busy one writes nothing. |
| 420 | func TestExploreOnce(t *testing.T) { |
| 421 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 422 | id := r.judgedTask("o", "Barber app mockups", baseX, candX, wire.ChecksPassed, "") |
| 423 | m := r.dispatched(wire.SpecialistMerge, baseX, candX, 0) |
| 424 | r.step("o-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX, CandidateSHA: candX}) |
| 425 | // While the merge runs: transient, nothing written. |
| 426 | if switched, err := r.inst.Store.Chat().ExploreOnce(r.ctx, id); err != nil || switched { |
| 427 | t.Fatalf("while merging: %t %v", switched, err) |
| 428 | } |
| 429 | r.step("o-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: baseX, CandidateSHA: candX, Termination: true}) |
| 430 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 431 | if switched, err := r.inst.Store.Chat().ExploreOnce(r.ctx, id); err != nil || !switched { |
| 432 | t.Fatalf("switch: %t %v", switched, err) |
| 433 | } |
| 434 | r.directionCard(id) |
| 435 | if k := r.task(id); k.Kind != wire.KindExplore || k.NeedsYou != wire.NeedsDirection || k.Stage != wire.StageInReview { |
| 436 | t.Fatalf("after the switch %+v %+v %+v", k.Kind, k.NeedsYou, k.Stage) |
| 437 | } |
| 438 | if _, err := r.client.SetKind(r.ctx, id, wire.KindChange, r.task(id).Revision); err != nil { |
| 439 | t.Fatal(err) |
| 440 | } |
| 441 | if switched, err := r.inst.Store.Chat().ExploreOnce(r.ctx, id); err != nil || switched { |
| 442 | t.Fatalf("again: %t %v", switched, err) |
| 443 | } |
| 444 | if k := r.task(id); k.Kind != "" { |
| 445 | t.Fatal("Paul's switch back was undone") |
| 446 | } |
| 447 | // Missing: the row only. |
| 448 | if switched, err := r.inst.Store.Chat().ExploreOnce(r.ctx, "tkmissing"); err != nil || switched { |
| 449 | t.Fatalf("missing: %t %v", switched, err) |
| 450 | } |
| 451 | var rows int |
| 452 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_housekeeping WHERE detector = 'kind_switch' AND finding_key IN (?, ?)", "explore:"+id, "explore:tkmissing").Scan(&rows); err != nil || rows != 2 { |
| 453 | t.Fatalf("rows %d %v", rows, err) |
| 454 | } |
| 455 | // Only the merge it had as a change, before the switch. |
| 456 | if n := r.commandsLike(wire.FrameDispatch, `"specialist":"merge"`); n != 1 { |
| 457 | t.Fatalf("%d merge dispatches", n) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // TestExploreKindsNotDuplicates: an exploration and a change that read |
| 462 | // alike are never similar for record_task, never combined or linked by |
| 463 | // housekeeping, never folded by the person API. |
| 464 | func TestExploreKindsNotDuplicates(t *testing.T) { |
| 465 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 466 | change := taskID(t, r.call("c-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Barber booking page mockups", "brief": "Two clickable barber booking page mockups."})) |
| 467 | res := r.call("e-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Barber booking page mockups", "brief": "Two clickable barber booking page mockups.", "kind": wire.KindExplore}) |
| 468 | if res.Code != "recorded" { |
| 469 | t.Fatalf("an exploration read as a change's duplicate: %+v", res) |
| 470 | } |
| 471 | explore := taskID(t, res) |
| 472 | if r.tidy() != 0 { |
| 473 | t.Fatal("housekeeping combined an exploration with a change") |
| 474 | } |
| 475 | if k := r.task(explore); k.DuplicateOf != nil || k.Stage == wire.StageCancelled { |
| 476 | t.Fatalf("linked or folded %+v", k.DuplicateOf) |
| 477 | } |
| 478 | if _, err := r.client.MarkDuplicate(r.ctx, explore, change); err == nil { |
| 479 | t.Fatal("folded an exploration into a change") |
| 480 | } |
| 481 | if k := r.task(change); k.Reports != 0 || k.Stage == wire.StageCancelled { |
| 482 | t.Fatalf("the change was touched %+v", k) |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // TestExploreDirectionStays: triage, re-triage and the stall sweep leave |
| 487 | // an exploration's direction to its person. |
| 488 | func TestExploreDirectionStays(t *testing.T) { |
| 489 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 490 | id, _ := r.toDirection("s", "Barber app mockups") |
| 491 | dispatches := r.commands(wire.FrameDispatch) |
| 492 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 493 | t.Fatal(err) |
| 494 | } |
| 495 | r.clock(time.Now().UTC().Add(2 * time.Hour)) |
| 496 | r.tick() |
| 497 | if k := r.task(id); k.NeedsYou != wire.NeedsDirection || k.Triage != nil || r.commands(wire.FrameDispatch) != dispatches { |
| 498 | t.Fatalf("after triage %+v %+v", k.NeedsYou, k.Triage) |
| 499 | } |
| 500 | if needs, _ := r.client.NeedsYou(r.ctx); len(needs.Items) != 1 || needs.Items[0].ID != id { |
| 501 | t.Fatalf("Needs you %+v", needs.Items) |
| 502 | } |
| 503 | } |
| 504 | |