| 1 | package headless |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 13 | "amadan.net/oficina/equipo/internal/runner/forge/forgetest" |
| 14 | "amadan.net/oficina/equipo/internal/thintest" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // mergedOnForge checks Equipo's merge on the local forge: main is the |
| 19 | // merge commit the task records, its second parent the judged candidate, |
| 20 | // and the task branch is gone. |
| 21 | func mergedOnForge(t *testing.T, bare string, k wire.Task) { |
| 22 | t.Helper() |
| 23 | if k.Merged == nil { |
| 24 | t.Fatalf("task %s not merged by Equipo", k.ID) |
| 25 | } |
| 26 | main := forgeHead(t, bare, "main") |
| 27 | out, err := exec.Command("git", "--git-dir="+bare, "rev-list", "--parents", "-n", "1", k.Merged.SHA).Output() |
| 28 | parents := strings.Fields(string(out)) |
| 29 | if err != nil || len(parents) != 3 || parents[2] != k.CandidateSHA { |
| 30 | t.Fatalf("merge %s parents %v (candidate %s): %v", k.Merged.SHA, parents, k.CandidateSHA, err) |
| 31 | } |
| 32 | if exec.Command("git", "--git-dir="+bare, "merge-base", "--is-ancestor", k.Merged.SHA, main).Run() != nil { |
| 33 | t.Fatalf("main %s does not hold the merge %s", main, k.Merged.SHA) |
| 34 | } |
| 35 | if head := forgeHead(t, bare, k.Branch); head != "" { |
| 36 | t.Fatalf("the task branch is still there at %s", head) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // mergedLine waits for the coordinator's line about the merge and checks |
| 41 | // it is the outcome only (no SHA, no path). |
| 42 | func (s *session) mergedLine(t *testing.T, id, want string) { |
| 43 | t.Helper() |
| 44 | s.wait(t, "the merged line", time.Minute, func(c *thintest.Conversation) bool { |
| 45 | for _, m := range c.Replies() { |
| 46 | if m.Text == want { |
| 47 | return true |
| 48 | } |
| 49 | } |
| 50 | return false |
| 51 | }) |
| 52 | s.conv.Locked(func(c *thintest.Conversation) { |
| 53 | k := c.Tasks[id] |
| 54 | for _, m := range c.Replies() { |
| 55 | if strings.Contains(m.Text, "/tasks/"+id) && k.Merged != nil && (strings.Contains(m.Text, k.Merged.SHA[:7]) || strings.Contains(m.Text, ".md")) { |
| 56 | t.Errorf("a SHA or path in chat: %q", m.Text) |
| 57 | } |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | // mergeCard waits for the task's merge card: its open one when it has |
| 63 | // one (a merge that conflicted leaves its answered card behind). |
| 64 | func (s *session) mergeCard(t *testing.T, id string) wire.Card { |
| 65 | t.Helper() |
| 66 | var card wire.Card |
| 67 | s.wait(t, "merge card", 5*time.Minute, func(c *thintest.Conversation) bool { |
| 68 | found := false |
| 69 | for _, k := range c.Cards { |
| 70 | if k.Kind == wire.CardMerge && k.TaskID == id && (!found || k.State == wire.CardOpen) { |
| 71 | card, found = k, true |
| 72 | } |
| 73 | } |
| 74 | return found |
| 75 | }) |
| 76 | return card |
| 77 | } |
| 78 | |
| 79 | // offlineMacChecks lets the task's checks wait for the offline Mac |
| 80 | // (env.OfflineMac), then end not run (runner_offline) past the offline |
| 81 | // limit, as they do when the Mac stays away. |
| 82 | func (s *session) offlineMacChecks(t *testing.T, id string) { |
| 83 | t.Helper() |
| 84 | s.wait(t, "checks waiting for the Mac", 2*time.Minute, func(c *thintest.Conversation) bool { |
| 85 | for _, st := range c.Tasks[id].Steps { |
| 86 | if st.Specialist == wire.SpecialistChecks && st.Placement == wire.PlacementWaiting { |
| 87 | return true |
| 88 | } |
| 89 | } |
| 90 | return false |
| 91 | }) |
| 92 | s.env.PastOfflineLimit(t) |
| 93 | } |
| 94 | |
| 95 | // 4c: checks that could not run (an iOS change whose Mac stayed offline; |
| 96 | // with no Mac at all it waits for one, 4_checks_honest) are no pass, so |
| 97 | // Equipo asks once with a card instead of merging. While it waits the |
| 98 | // task branch moves and comes back (readiness follows it, Astra round 1). |
| 99 | // Wait leaves it at Ready to merge, and a person may still merge it on |
| 100 | // Amadan (the watch sees it). On the next such task Merge merges that |
| 101 | // SHA, labelled without checks, and the coordinator says so. |
| 102 | func scenarioChecksCard(t *testing.T, env *Env) { |
| 103 | if env.Live { |
| 104 | t.Skip("local only: the fake CLI on the local forge") |
| 105 | } |
| 106 | s := open(t, env) |
| 107 | s.keep() |
| 108 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 109 | bare := env.ForgePath("amadan.net/oficina/sample") |
| 110 | defer env.OfflineMac(t)() |
| 111 | sent := s.send(t, "tool:record_task:oficina/sample|iOS edge swipe|Swipe back does nothing on iOS.\ntool:start_task:$task|edit:ios/App.swift|edgeSwipe = false|edgeSwipe = true") |
| 112 | id := s.taskOf(t, sent.Turn.ID) |
| 113 | s.offlineMacChecks(t, id) |
| 114 | card := s.mergeCard(t, id) |
| 115 | k, _ := env.Client.Task(s.ctx, id) |
| 116 | if k.Stage != wire.StageReadyToMerge || k.Checks != wire.ChecksCannotRun || k.NeedsYou != wire.NeedsMerge || card.Question != "Merge iOS edge swipe? I'd wait: I don't know yet whether merging to Sample's main branch ships to users." || card.State != wire.CardOpen { |
| 117 | t.Fatalf("asking %+v card %+v", k, card) |
| 118 | } |
| 119 | tip := forgeHead(t, bare, "main") |
| 120 | // The branch moves on the forge and back: not ready, then ready. |
| 121 | other, err := exec.Command("git", "--git-dir="+bare, "commit-tree", k.CandidateSHA+"^{tree}", "-p", k.CandidateSHA, "-m", "someone else").Output() |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/"+k.Branch, strings.TrimSpace(string(other))).CombinedOutput(); err != nil { |
| 126 | t.Fatalf("push over: %v %s", err, out) |
| 127 | } |
| 128 | s.wait(t, "branch moved", time.Minute, func(c *thintest.Conversation) bool { |
| 129 | m := c.Tasks[id] |
| 130 | return m.NeedsYou == wire.NeedsBranchMoved && m.Stage == wire.StageInReview && m.Verdict == "" && m.Checks == "" |
| 131 | }) |
| 132 | if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/"+k.Branch, k.CandidateSHA).CombinedOutput(); err != nil { |
| 133 | t.Fatalf("push back: %v %s", err, out) |
| 134 | } |
| 135 | s.wait(t, "ready again", time.Minute, func(c *thintest.Conversation) bool { |
| 136 | m := c.Tasks[id] |
| 137 | return m.Stage == wire.StageReadyToMerge && m.NeedsYou == wire.NeedsMerge && m.Verdict == wire.VerdictApprove |
| 138 | }) |
| 139 | // The move made the card's old revision stale (Astra 4c round 1). |
| 140 | if _, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); thintest.Status(err) != 409 { |
| 141 | t.Fatalf("an answer from before the move: %v", err) |
| 142 | } |
| 143 | s.wait(t, "refreshed card", time.Minute, func(c *thintest.Conversation) bool { return c.Cards[card.ID].Revision > card.Revision }) |
| 144 | s.conv.Locked(func(c *thintest.Conversation) { card = c.Cards[card.ID] }) |
| 145 | // Wait: nothing merges. |
| 146 | if out, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); err != nil || out.Card.Choice != wire.ChoiceWaitMac { |
| 147 | t.Fatalf("wait %+v %v", out, err) |
| 148 | } |
| 149 | time.Sleep(2 * time.Second) |
| 150 | if k, _ := env.Client.Task(s.ctx, id); k.Stage != wire.StageReadyToMerge || forgeHead(t, bare, "main") != tip { |
| 151 | t.Fatalf("after wait %+v", k) |
| 152 | } |
| 153 | // The person merges it on Amadan: done (the watch sees it). |
| 154 | if res, err := forgetest.Merge(context.Background(), "git", bare, k.Branch, forge.MergeRequest{ExpectSHA: k.CandidateSHA, Strategy: wire.MergeCommit, KeepBranch: true}); err != nil || !res.Merged { |
| 155 | t.Fatalf("person's merge %+v %v", res, err) |
| 156 | } |
| 157 | s.wait(t, "done by the person", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 158 | if k, _ := env.Client.Task(s.ctx, id); k.Merged != nil { |
| 159 | t.Fatalf("a person's merge recorded as Equipo's: %+v", k.Merged) |
| 160 | } |
| 161 | |
| 162 | // The next one: Merge. |
| 163 | sent = s.send(t, "tool:record_task:oficina/sample|iPad edge swipe|Swipe back does nothing on iPad.|separate\ntool:start_task:$task|edit:ios/App.swift|import SwiftUI|import SwiftUI // iPad") |
| 164 | id2 := s.taskOf(t, sent.Turn.ID) |
| 165 | s.offlineMacChecks(t, id2) |
| 166 | card2 := s.mergeCard(t, id2) |
| 167 | asked := time.Now() |
| 168 | if out, err := env.Client.Answer(s.ctx, card2.ID, wire.ChoiceMerge, card2.Revision); err != nil || out.Card.Choice != wire.ChoiceMerge { |
| 169 | t.Fatalf("merge %+v %v", out, err) |
| 170 | } |
| 171 | s.wait(t, "merged", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id2].Stage == wire.StageDone }) |
| 172 | k2, _ := env.Client.Task(s.ctx, id2) |
| 173 | if k2.Merged == nil || !k2.Merged.WithoutChecks || k2.Checks != wire.ChecksCannotRun { |
| 174 | t.Fatalf("merged without checks %+v", k2) |
| 175 | } |
| 176 | mergedOnForge(t, bare, k2) |
| 177 | if swift := forgeShow(t, bare, "main:ios/App.swift"); !strings.Contains(swift, "import SwiftUI // iPad") || !strings.Contains(swift, "edgeSwipe = true") { |
| 178 | t.Fatalf("main ios/App.swift: %s", swift) |
| 179 | } |
| 180 | s.mergedLine(t, id2, "Merged [iPad edge swipe](/tasks/"+id2+") into Sample without checks.") |
| 181 | |
| 182 | // Merge authority: the owner says Woodstar's main is not a release, and |
| 183 | // the next small iOS change, measured by the real runner's review, |
| 184 | // merges on its own with a receipt whose Undo the hub's answer allows. |
| 185 | var ships wire.Card |
| 186 | s.wait(t, "ships question", time.Minute, func(c *thintest.Conversation) bool { |
| 187 | for _, k := range c.Cards { |
| 188 | if k.Kind == wire.CardMergeShips { |
| 189 | ships = k |
| 190 | return true |
| 191 | } |
| 192 | } |
| 193 | return false |
| 194 | }) |
| 195 | if _, err := env.Client.Answer(s.ctx, ships.ID, wire.ChoiceNotShips, ships.Revision); err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | sent = s.send(t, "tool:record_task:oficina/sample|Haptics on iOS|No haptic on a swipe back.|separate\ntool:start_task:$task|edit:ios/App.swift|edgeSwipe = true|edgeSwipe = true // haptic") |
| 199 | id3 := s.taskOf(t, sent.Turn.ID) |
| 200 | s.offlineMacChecks(t, id3) |
| 201 | s.wait(t, "merged on its own", 2*time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id3].Stage == wire.StageDone }) |
| 202 | k3, _ := env.Client.Task(s.ctx, id3) |
| 203 | if k3.Merged == nil || !k3.Merged.WithoutChecks || !k3.UndoOffered || k3.MergeDecision == nil || k3.MergeDecision.Class != "act" { |
| 204 | t.Fatalf("Equipo's own merge %+v %+v", k3.Merged, k3.MergeDecision) |
| 205 | } |
| 206 | mergedOnForge(t, bare, k3) |
| 207 | s.wait(t, "receipt", time.Minute, func(c *thintest.Conversation) bool { |
| 208 | for _, k := range c.Cards { |
| 209 | if k.Kind == wire.CardMergeReceipt && k.TaskID == id3 && len(k.Options) == 1 && k.Options[0] == wire.ChoiceUndo { |
| 210 | return true |
| 211 | } |
| 212 | } |
| 213 | return false |
| 214 | }) |
| 215 | Witness[t.Name()] = map[string]any{"wait_task": id, "merge_task": id2, "merged": k2.Merged, "card_to_done_s": time.Since(asked).Seconds(), "own_merge_task": id3} |
| 216 | } |
| 217 | |
| 218 | // 4c: the default branch moves under an approved change and the merge |
| 219 | // conflicts: Engineering gets one rebase round on the new tip (the |
| 220 | // conflict marked in the file), its new candidate is checked and reviewed |
| 221 | // afresh from the new base, and that SHA is merged. |
| 222 | func scenarioRebase(t *testing.T, env *Env) { |
| 223 | if env.Live { |
| 224 | t.Skip("local only: the fake CLI on the local forge") |
| 225 | } |
| 226 | s := open(t, env) |
| 227 | s.keep() |
| 228 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 229 | bare := env.ForgePath("amadan.net/oficina/sample") |
| 230 | sent := s.send(t, "tool:record_task:oficina/sample|Fix the README typo|The README says teh panes.\ntool:start_task:$task|reviewsleep:3000 edit:README.md|teh panes|the panes") |
| 231 | id := s.taskOf(t, sent.Turn.ID) |
| 232 | // While review reads the change, someone changes the same line on main. |
| 233 | s.wait(t, "reviewing", 3*time.Minute, func(c *thintest.Conversation) bool { |
| 234 | for _, st := range c.Tasks[id].Steps { |
| 235 | if st.Specialist == wire.SpecialistReview && st.State == wire.StepRunning { |
| 236 | return true |
| 237 | } |
| 238 | } |
| 239 | return false |
| 240 | }) |
| 241 | work := t.TempDir() |
| 242 | gitRun(t, work, "clone", "-q", bare, "w") |
| 243 | readme := work + "/w/README.md" |
| 244 | raw, _ := os.ReadFile(readme) |
| 245 | os.WriteFile(readme, []byte(strings.Replace(string(raw), "It has a typo: teh panes.", "It has a typo: teh sliding panes.", 1)), 0600) |
| 246 | os.WriteFile(work+"/w/NEWS", []byte("news\n"), 0600) |
| 247 | gitRun(t, work+"/w", "add", "-A") |
| 248 | gitRun(t, work+"/w", "commit", "-q", "-m", "main moves") |
| 249 | gitRun(t, work+"/w", "push", "-q", "origin", "main") |
| 250 | moved := forgeHead(t, bare, "main") |
| 251 | // Ready to merge says Needs you "merge" for a moment before Equipo's |
| 252 | // merge step starts: only Done or a real decision ends the wait. |
| 253 | s.wait(t, "merged after the rebase", 3*time.Minute, func(c *thintest.Conversation) bool { |
| 254 | k := c.Tasks[id] |
| 255 | return k.Stage == wire.StageDone || k.NeedsYou != "" && k.NeedsYou != wire.NeedsMerge |
| 256 | }) |
| 257 | k, _ := env.Client.Task(s.ctx, id) |
| 258 | var kinds []string |
| 259 | var rebase wire.Step |
| 260 | for _, st := range k.Steps { |
| 261 | kinds = append(kinds, st.Specialist+":"+firstOf(st.Checks, st.Verdict, st.Reason, st.State)) |
| 262 | if st.Rebase { |
| 263 | rebase = st |
| 264 | } |
| 265 | } |
| 266 | if want := []string{"engineering:done", "checks:passed", "review:approve", "merge:merge_conflict", "engineering:done", "checks:passed", "review:approve", "merge:done"}; !slices.Equal(kinds, want) || k.Stage != wire.StageDone { |
| 267 | t.Fatalf("steps %v, want %v (%s %s)", kinds, want, k.Stage, k.NeedsYou) |
| 268 | } |
| 269 | if rebase.BaseSHA != moved || k.BaseSHA != moved || rebase.CandidateSHA != k.CandidateSHA { |
| 270 | t.Fatalf("rebase %+v on %s, task base %s", rebase, moved, k.BaseSHA) |
| 271 | } |
| 272 | if parent := strings.TrimSpace(forgeShow(t, bare, "-s", "--format=%P", k.CandidateSHA)); parent != moved { |
| 273 | t.Fatalf("rebased candidate's parent %s, want the moved main %s", parent, moved) |
| 274 | } |
| 275 | mergedOnForge(t, bare, k) |
| 276 | if r := forgeShow(t, bare, "main:README.md"); !strings.Contains(r, "It has a typo: the panes.") || strings.Contains(r, "<<<<<<<") || forgeShow(t, bare, "main:NEWS") != "news\n" { |
| 277 | t.Fatalf("main after the rebase: %s", r) |
| 278 | } |
| 279 | s.mergedLine(t, id, "Merged [Fix the README typo](/tasks/"+id+") into Sample.") |
| 280 | Witness[t.Name()] = map[string]any{"task": id, "steps": kinds, "merged": k.Merged} |
| 281 | } |
| 282 | |
| 283 | // 4c: the same problem reported again goes on the open task: record_task |
| 284 | // returns it as similar and records nothing, and add_report attaches the |
| 285 | // new report (the task's thread, a task card in this turn). |
| 286 | func scenarioDuplicate(t *testing.T, env *Env) { |
| 287 | if env.Live { |
| 288 | t.Skip("local only: the fake CLI") |
| 289 | } |
| 290 | s := open(t, env) |
| 291 | s.keep() |
| 292 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 293 | first := s.send(t, "tool:record_task:oficina/sample|iOS edge swipe to go back doesn't work|Swiping from the left edge on iPhone does nothing.") |
| 294 | id := s.taskOf(t, first.Turn.ID) |
| 295 | s.waitTurn(t, first.Turn.ID, wire.TurnReplied) |
| 296 | again := s.send(t, "tool:record_task:oficina/sample|Swipe back doesn't work|On the iPhone, swiping back from the edge does nothing.\ntool:add_report:$similar|Swipe back still does nothing on my iPhone.") |
| 297 | s.waitTurn(t, again.Turn.ID, wire.TurnReplied) |
| 298 | if got := s.taskOf(t, again.Turn.ID); got != id { |
| 299 | t.Fatalf("the second report shows task %s, want %s", got, id) |
| 300 | } |
| 301 | list, err := env.Client.Tasks(s.ctx) |
| 302 | if err != nil || len(list.Items) != 1 { |
| 303 | t.Fatalf("tasks %+v %v", list, err) |
| 304 | } |
| 305 | k, _ := env.Client.Task(s.ctx, id) |
| 306 | if k.Reports != 1 || len(k.Thread) != 1 || k.Thread[0].Text != "Swipe back still does nothing on my iPhone." { |
| 307 | t.Fatalf("report %+v", k) |
| 308 | } |
| 309 | reply := s.reply(t, again.Turn.ID) |
| 310 | if !strings.Contains(reply.Text, `"code":"similar"`) && !strings.Contains(reply.Text, `"similar":[`) { |
| 311 | t.Fatalf("record_task did not return the similar task: %s", reply.Text) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // 4c witness (staging only, the suite person on a repository with its own |
| 316 | // passing checks): one message becomes a task that reaches Ready to merge |
| 317 | // and is merged by Equipo into the default branch, and the coordinator |
| 318 | // says so plainly. EQUIPO_STAGING_MERGE_WITNESS is the person's message. |
| 319 | func scenarioMergeWitness(t *testing.T, env *Env) { |
| 320 | text := os.Getenv("EQUIPO_STAGING_MERGE_WITNESS") |
| 321 | if !env.Live || text == "" { |
| 322 | t.Skip("staging only, with EQUIPO_STAGING_MERGE_WITNESS") |
| 323 | } |
| 324 | s := open(t, env) |
| 325 | s.keep() |
| 326 | s.wait(t, "runner online", time.Minute, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 327 | start := time.Now() |
| 328 | sent := s.send(t, text) |
| 329 | id := s.taskOf(t, sent.Turn.ID) |
| 330 | seen := map[string]float64{} |
| 331 | s.wait(t, "merged or needs you", 40*time.Minute, func(c *thintest.Conversation) bool { |
| 332 | k := c.Tasks[id] |
| 333 | if _, ok := seen[k.Stage]; !ok && k.Stage != "" { |
| 334 | seen[k.Stage] = time.Since(start).Seconds() |
| 335 | } |
| 336 | return k.Stage == wire.StageDone || k.Stage == wire.StageCancelled || k.NeedsYou != "" && k.NeedsYou != wire.NeedsMerge |
| 337 | }) |
| 338 | k, _ := env.Client.Task(s.ctx, id) |
| 339 | checks, review := oneSHA(t, k) |
| 340 | witnessTask(t, k, checks, review, start) |
| 341 | w := Witness[t.Name()].(map[string]any) |
| 342 | w["stages_s"], w["merged"] = seen, k.Merged |
| 343 | t.Logf("task %s: %s needs_you=%q checks %s verdict %s on %s merged %+v; stages %v", id, k.Stage, k.NeedsYou, k.Checks, k.Verdict, k.CandidateSHA, k.Merged, seen) |
| 344 | if k.Stage != wire.StageDone || k.Merged == nil { |
| 345 | t.Fatalf("not merged: %s %s", k.Stage, k.NeedsYou) |
| 346 | } |
| 347 | var line string |
| 348 | s.wait(t, "the merged line", time.Minute, func(c *thintest.Conversation) bool { |
| 349 | for _, m := range c.Replies() { |
| 350 | if strings.HasPrefix(m.Text, "Merged [") && strings.Contains(m.Text, "/tasks/"+id) { |
| 351 | line = m.Text |
| 352 | return true |
| 353 | } |
| 354 | } |
| 355 | return false |
| 356 | }) |
| 357 | w["line"] = line |
| 358 | if strings.Contains(line, k.Merged.SHA[:7]) { |
| 359 | t.Fatalf("a SHA in chat: %q", line) |
| 360 | } |
| 361 | } |
| 362 | |