package headless import ( "context" "os" "os/exec" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner/forge" "amadan.net/oficina/equipo/internal/runner/forge/forgetest" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // mergedOnForge checks Equipo's merge on the local forge: main is the // merge commit the task records, its second parent the judged candidate, // and the task branch is gone. func mergedOnForge(t *testing.T, bare string, k wire.Task) { t.Helper() if k.Merged == nil { t.Fatalf("task %s not merged by Equipo", k.ID) } main := forgeHead(t, bare, "main") out, err := exec.Command("git", "--git-dir="+bare, "rev-list", "--parents", "-n", "1", k.Merged.SHA).Output() parents := strings.Fields(string(out)) if err != nil || len(parents) != 3 || parents[2] != k.CandidateSHA { t.Fatalf("merge %s parents %v (candidate %s): %v", k.Merged.SHA, parents, k.CandidateSHA, err) } if exec.Command("git", "--git-dir="+bare, "merge-base", "--is-ancestor", k.Merged.SHA, main).Run() != nil { t.Fatalf("main %s does not hold the merge %s", main, k.Merged.SHA) } if head := forgeHead(t, bare, k.Branch); head != "" { t.Fatalf("the task branch is still there at %s", head) } } // mergedLine waits for the coordinator's line about the merge and checks // it is the outcome only (no SHA, no path). func (s *session) mergedLine(t *testing.T, id, want string) { t.Helper() s.wait(t, "the merged line", time.Minute, func(c *thintest.Conversation) bool { for _, m := range c.Replies() { if m.Text == want { return true } } return false }) s.conv.Locked(func(c *thintest.Conversation) { k := c.Tasks[id] for _, m := range c.Replies() { if strings.Contains(m.Text, "/tasks/"+id) && k.Merged != nil && (strings.Contains(m.Text, k.Merged.SHA[:7]) || strings.Contains(m.Text, ".md")) { t.Errorf("a SHA or path in chat: %q", m.Text) } } }) } // mergeCard waits for the task's merge card: its open one when it has // one (a merge that conflicted leaves its answered card behind). func (s *session) mergeCard(t *testing.T, id string) wire.Card { t.Helper() var card wire.Card s.wait(t, "merge card", 5*time.Minute, func(c *thintest.Conversation) bool { found := false for _, k := range c.Cards { if k.Kind == wire.CardMerge && k.TaskID == id && (!found || k.State == wire.CardOpen) { card, found = k, true } } return found }) return card } // offlineMacChecks lets the task's checks wait for the offline Mac // (env.OfflineMac), then end not run (runner_offline) past the offline // limit, as they do when the Mac stays away. func (s *session) offlineMacChecks(t *testing.T, id string) { t.Helper() s.wait(t, "checks waiting for the Mac", 2*time.Minute, func(c *thintest.Conversation) bool { for _, st := range c.Tasks[id].Steps { if st.Specialist == wire.SpecialistChecks && st.Placement == wire.PlacementWaiting { return true } } return false }) s.env.PastOfflineLimit(t) } // 4c: checks that could not run (an iOS change whose Mac stayed offline; // with no Mac at all it waits for one, 4_checks_honest) are no pass, so // Equipo asks once with a card instead of merging. While it waits the // task branch moves and comes back (readiness follows it, Astra round 1). // Wait leaves it at Ready to merge, and a person may still merge it on // Amadan (the watch sees it). On the next such task Merge merges that // SHA, labelled without checks, and the coordinator says so. func scenarioChecksCard(t *testing.T, env *Env) { if env.Live { t.Skip("local only: the fake CLI on the local forge") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) bare := env.ForgePath("amadan.net/oficina/sample") defer env.OfflineMac(t)() 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") id := s.taskOf(t, sent.Turn.ID) s.offlineMacChecks(t, id) card := s.mergeCard(t, id) k, _ := env.Client.Task(s.ctx, id) 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 { t.Fatalf("asking %+v card %+v", k, card) } tip := forgeHead(t, bare, "main") // The branch moves on the forge and back: not ready, then ready. other, err := exec.Command("git", "--git-dir="+bare, "commit-tree", k.CandidateSHA+"^{tree}", "-p", k.CandidateSHA, "-m", "someone else").Output() if err != nil { t.Fatal(err) } if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/"+k.Branch, strings.TrimSpace(string(other))).CombinedOutput(); err != nil { t.Fatalf("push over: %v %s", err, out) } s.wait(t, "branch moved", time.Minute, func(c *thintest.Conversation) bool { m := c.Tasks[id] return m.NeedsYou == wire.NeedsBranchMoved && m.Stage == wire.StageInReview && m.Verdict == "" && m.Checks == "" }) if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/"+k.Branch, k.CandidateSHA).CombinedOutput(); err != nil { t.Fatalf("push back: %v %s", err, out) } s.wait(t, "ready again", time.Minute, func(c *thintest.Conversation) bool { m := c.Tasks[id] return m.Stage == wire.StageReadyToMerge && m.NeedsYou == wire.NeedsMerge && m.Verdict == wire.VerdictApprove }) // The move made the card's old revision stale (Astra 4c round 1). if _, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); thintest.Status(err) != 409 { t.Fatalf("an answer from before the move: %v", err) } s.wait(t, "refreshed card", time.Minute, func(c *thintest.Conversation) bool { return c.Cards[card.ID].Revision > card.Revision }) s.conv.Locked(func(c *thintest.Conversation) { card = c.Cards[card.ID] }) // Wait: nothing merges. if out, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceWaitMac, card.Revision); err != nil || out.Card.Choice != wire.ChoiceWaitMac { t.Fatalf("wait %+v %v", out, err) } time.Sleep(2 * time.Second) if k, _ := env.Client.Task(s.ctx, id); k.Stage != wire.StageReadyToMerge || forgeHead(t, bare, "main") != tip { t.Fatalf("after wait %+v", k) } // The person merges it on Amadan: done (the watch sees it). 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 { t.Fatalf("person's merge %+v %v", res, err) } s.wait(t, "done by the person", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) if k, _ := env.Client.Task(s.ctx, id); k.Merged != nil { t.Fatalf("a person's merge recorded as Equipo's: %+v", k.Merged) } // The next one: Merge. 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") id2 := s.taskOf(t, sent.Turn.ID) s.offlineMacChecks(t, id2) card2 := s.mergeCard(t, id2) asked := time.Now() if out, err := env.Client.Answer(s.ctx, card2.ID, wire.ChoiceMerge, card2.Revision); err != nil || out.Card.Choice != wire.ChoiceMerge { t.Fatalf("merge %+v %v", out, err) } s.wait(t, "merged", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id2].Stage == wire.StageDone }) k2, _ := env.Client.Task(s.ctx, id2) if k2.Merged == nil || !k2.Merged.WithoutChecks || k2.Checks != wire.ChecksCannotRun { t.Fatalf("merged without checks %+v", k2) } mergedOnForge(t, bare, k2) if swift := forgeShow(t, bare, "main:ios/App.swift"); !strings.Contains(swift, "import SwiftUI // iPad") || !strings.Contains(swift, "edgeSwipe = true") { t.Fatalf("main ios/App.swift: %s", swift) } s.mergedLine(t, id2, "Merged [iPad edge swipe](/tasks/"+id2+") into Sample without checks.") // Merge authority: the owner says Woodstar's main is not a release, and // the next small iOS change, measured by the real runner's review, // merges on its own with a receipt whose Undo the hub's answer allows. var ships wire.Card s.wait(t, "ships question", time.Minute, func(c *thintest.Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardMergeShips { ships = k return true } } return false }) if _, err := env.Client.Answer(s.ctx, ships.ID, wire.ChoiceNotShips, ships.Revision); err != nil { t.Fatal(err) } 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") id3 := s.taskOf(t, sent.Turn.ID) s.offlineMacChecks(t, id3) s.wait(t, "merged on its own", 2*time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id3].Stage == wire.StageDone }) k3, _ := env.Client.Task(s.ctx, id3) if k3.Merged == nil || !k3.Merged.WithoutChecks || !k3.UndoOffered || k3.MergeDecision == nil || k3.MergeDecision.Class != "act" { t.Fatalf("Equipo's own merge %+v %+v", k3.Merged, k3.MergeDecision) } mergedOnForge(t, bare, k3) s.wait(t, "receipt", time.Minute, func(c *thintest.Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardMergeReceipt && k.TaskID == id3 && len(k.Options) == 1 && k.Options[0] == wire.ChoiceUndo { return true } } return false }) 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} } // 4c: the default branch moves under an approved change and the merge // conflicts: Engineering gets one rebase round on the new tip (the // conflict marked in the file), its new candidate is checked and reviewed // afresh from the new base, and that SHA is merged. func scenarioRebase(t *testing.T, env *Env) { if env.Live { t.Skip("local only: the fake CLI on the local forge") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) bare := env.ForgePath("amadan.net/oficina/sample") 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") id := s.taskOf(t, sent.Turn.ID) // While review reads the change, someone changes the same line on main. s.wait(t, "reviewing", 3*time.Minute, func(c *thintest.Conversation) bool { for _, st := range c.Tasks[id].Steps { if st.Specialist == wire.SpecialistReview && st.State == wire.StepRunning { return true } } return false }) work := t.TempDir() gitRun(t, work, "clone", "-q", bare, "w") readme := work + "/w/README.md" raw, _ := os.ReadFile(readme) os.WriteFile(readme, []byte(strings.Replace(string(raw), "It has a typo: teh panes.", "It has a typo: teh sliding panes.", 1)), 0600) os.WriteFile(work+"/w/NEWS", []byte("news\n"), 0600) gitRun(t, work+"/w", "add", "-A") gitRun(t, work+"/w", "commit", "-q", "-m", "main moves") gitRun(t, work+"/w", "push", "-q", "origin", "main") moved := forgeHead(t, bare, "main") // Ready to merge says Needs you "merge" for a moment before Equipo's // merge step starts: only Done or a real decision ends the wait. s.wait(t, "merged after the rebase", 3*time.Minute, func(c *thintest.Conversation) bool { k := c.Tasks[id] return k.Stage == wire.StageDone || k.NeedsYou != "" && k.NeedsYou != wire.NeedsMerge }) k, _ := env.Client.Task(s.ctx, id) var kinds []string var rebase wire.Step for _, st := range k.Steps { kinds = append(kinds, st.Specialist+":"+firstOf(st.Checks, st.Verdict, st.Reason, st.State)) if st.Rebase { rebase = st } } 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 { t.Fatalf("steps %v, want %v (%s %s)", kinds, want, k.Stage, k.NeedsYou) } if rebase.BaseSHA != moved || k.BaseSHA != moved || rebase.CandidateSHA != k.CandidateSHA { t.Fatalf("rebase %+v on %s, task base %s", rebase, moved, k.BaseSHA) } if parent := strings.TrimSpace(forgeShow(t, bare, "-s", "--format=%P", k.CandidateSHA)); parent != moved { t.Fatalf("rebased candidate's parent %s, want the moved main %s", parent, moved) } mergedOnForge(t, bare, k) 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" { t.Fatalf("main after the rebase: %s", r) } s.mergedLine(t, id, "Merged [Fix the README typo](/tasks/"+id+") into Sample.") Witness[t.Name()] = map[string]any{"task": id, "steps": kinds, "merged": k.Merged} } // 4c: the same problem reported again goes on the open task: record_task // returns it as similar and records nothing, and add_report attaches the // new report (the task's thread, a task card in this turn). func scenarioDuplicate(t *testing.T, env *Env) { if env.Live { t.Skip("local only: the fake CLI") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) 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.") id := s.taskOf(t, first.Turn.ID) s.waitTurn(t, first.Turn.ID, wire.TurnReplied) 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.") s.waitTurn(t, again.Turn.ID, wire.TurnReplied) if got := s.taskOf(t, again.Turn.ID); got != id { t.Fatalf("the second report shows task %s, want %s", got, id) } list, err := env.Client.Tasks(s.ctx) if err != nil || len(list.Items) != 1 { t.Fatalf("tasks %+v %v", list, err) } k, _ := env.Client.Task(s.ctx, id) if k.Reports != 1 || len(k.Thread) != 1 || k.Thread[0].Text != "Swipe back still does nothing on my iPhone." { t.Fatalf("report %+v", k) } reply := s.reply(t, again.Turn.ID) if !strings.Contains(reply.Text, `"code":"similar"`) && !strings.Contains(reply.Text, `"similar":[`) { t.Fatalf("record_task did not return the similar task: %s", reply.Text) } } // 4c witness (staging only, the suite person on a repository with its own // passing checks): one message becomes a task that reaches Ready to merge // and is merged by Equipo into the default branch, and the coordinator // says so plainly. EQUIPO_STAGING_MERGE_WITNESS is the person's message. func scenarioMergeWitness(t *testing.T, env *Env) { text := os.Getenv("EQUIPO_STAGING_MERGE_WITNESS") if !env.Live || text == "" { t.Skip("staging only, with EQUIPO_STAGING_MERGE_WITNESS") } s := open(t, env) s.keep() s.wait(t, "runner online", time.Minute, func(c *thintest.Conversation) bool { return c.Runner.Online }) start := time.Now() sent := s.send(t, text) id := s.taskOf(t, sent.Turn.ID) seen := map[string]float64{} s.wait(t, "merged or needs you", 40*time.Minute, func(c *thintest.Conversation) bool { k := c.Tasks[id] if _, ok := seen[k.Stage]; !ok && k.Stage != "" { seen[k.Stage] = time.Since(start).Seconds() } return k.Stage == wire.StageDone || k.Stage == wire.StageCancelled || k.NeedsYou != "" && k.NeedsYou != wire.NeedsMerge }) k, _ := env.Client.Task(s.ctx, id) checks, review := oneSHA(t, k) witnessTask(t, k, checks, review, start) w := Witness[t.Name()].(map[string]any) w["stages_s"], w["merged"] = seen, k.Merged 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) if k.Stage != wire.StageDone || k.Merged == nil { t.Fatalf("not merged: %s %s", k.Stage, k.NeedsYou) } var line string s.wait(t, "the merged line", time.Minute, func(c *thintest.Conversation) bool { for _, m := range c.Replies() { if strings.HasPrefix(m.Text, "Merged [") && strings.Contains(m.Text, "/tasks/"+id) { line = m.Text return true } } return false }) w["line"] = line if strings.Contains(line, k.Merged.SHA[:7]) { t.Fatalf("a SHA in chat: %q", line) } }