oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "net/http"
5 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// activity sends one step activity line (not durable: no ack).
14func (r *toolRig) activity(id string, a wire.StepActivityReport) {
15 r.t.Helper()
16 if err := r.runner.Send(r.ctx, id, wire.FrameStepActivity, a); err != nil {
17 r.t.Fatal(err)
18 }
19}
20
21// TestContractStepActivity: a running step's activity lines come only
22// from the step person's primary runner, for the current attempt; the
23// newest five are kept on the step and every member gets each line as
24// an activity event; a new attempt starts with none.
25func TestContractStepActivity(t *testing.T) {
26 a := newToolRig(t)
27 ctx := a.ctx
28 b := personToolRig(t, a, "second@example.com", equipo.RoleOwner)
29 id := taskID(t, a.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Swipe back from the edge does nothing", "brief": "x"}))
30 a.call("k2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"})
31 _, body := a.expect(wire.FrameDispatch)
32 d := body.(*wire.DispatchCommand)
33 a.step("r-s1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)})
34
35 // Another person's runner, about A's step: dropped. B's own turn
36 // activity after it shows B's frame was applied first.
37 if err := b.runner.Send(ctx, "b-act", wire.FrameStepActivity, wire.StepActivityReport{TaskID: id, StepID: d.StepID, Attempt: 1, Kind: wire.ActivityRead, Subject: "secret.go"}); err != nil {
38 t.Fatal(err)
39 }
40 if err := b.runner.Send(ctx, "b-turn", wire.FrameActivity, wire.ActivityReport{TurnID: b.turn, Attempt: 1, Kind: wire.ActivityThink}); err != nil {
41 t.Fatal(err)
42 }
43 b.wait("B turn activity", func(c *Conversation) bool { return len(c.Activity[b.turn]) > 0 })
44 // A stale attempt: dropped.
45 a.activity("a-stale", wire.StepActivityReport{TaskID: id, StepID: d.StepID, Attempt: 2, Kind: wire.ActivityRead, Subject: "stale.go"})
46 lines := []wire.StepActivityReport{
47 {Kind: wire.ActivityRead, Subject: "/workspace/panes/swipe.go"},
48 {Kind: wire.ActivitySearch, Subject: "EdgeSwipe"},
49 {Kind: "launch_missiles", Subject: "x"},
50 {Kind: wire.ActivityEdit, Subject: "panes/swipe.go"},
51 {Kind: wire.ActivityRun, Subject: "go test ./panes\nrm -rf /"},
52 {Kind: wire.ActivityRun, Subject: "make \x1b[31mci" + strings.Repeat("x", 300)},
53 {Kind: wire.ActivityEdit, Subject: "panes/swipe_test.go"},
54 }
55 for i, l := range lines {
56 l.TaskID, l.StepID, l.Attempt = id, d.StepID, 1
57 a.activity("a-act-"+itoa(i), l)
58 }
59 for _, r := range []*toolRig{a, b} {
60 r.wait("every line, to every member", func(c *Conversation) bool { return len(c.StepActivity[d.StepID]) == len(lines) })
61 }
62 var got []wire.StepActivity
63 a.conv.Locked(func(c *Conversation) { got = c.StepActivity[d.StepID] })
64 first, last := got[0].Activity, got[len(got)-1].Activity
65 if len(first) != 1 || first[0].Kind != wire.ActivityRead || first[0].Subject != "panes/swipe.go" || first[0].At.IsZero() || got[0].TaskID != id || got[0].Attempt != 1 {
66 t.Fatalf("first line %+v", got[0])
67 }
68 if len(last) != wire.MaxStepActivity || last[0].Kind != wire.ActivityOther || last[2].Subject != "go test ./panes" || strings.ContainsRune(last[3].Subject, 0x1b) || len(last[3].Subject) > wire.MaxActivitySubject || last[4].Subject != "panes/swipe_test.go" {
69 t.Fatalf("kept lines %+v", last)
70 }
71 for _, e := range got {
72 for _, l := range e.Activity {
73 if l.Subject == "secret.go" || l.Subject == "stale.go" {
74 t.Fatalf("a dropped line was kept: %+v", e)
75 }
76 }
77 }
78 k, err := a.client.Task(ctx, id)
79 if err != nil || len(k.Steps[0].Activity) != wire.MaxStepActivity || k.Steps[0].Activity[4].Subject != "panes/swipe_test.go" {
80 t.Fatalf("step activity on the task: %+v %v", k.Steps, err)
81 }
82 // The step keeps its last lines after it ends; a retry starts afresh.
83 a.step("r-s2", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true})
84 if k, _ := a.client.Task(ctx, id); len(k.Steps[0].Activity) != wire.MaxStepActivity {
85 t.Fatalf("ended step lost its lines: %+v", k.Steps[0])
86 }
87 a.activity("a-after", wire.StepActivityReport{TaskID: id, StepID: d.StepID, Attempt: 1, Kind: wire.ActivityRead, Subject: "late.go"})
88 if _, err := a.client.RetryTask(ctx, id); err != nil {
89 t.Fatal(err)
90 }
91 a.expect(wire.FrameDispatch)
92 k, _ = a.client.Task(ctx, id)
93 if len(k.Steps[0].Activity) != 0 || k.Steps[0].Attempt != 2 {
94 t.Fatalf("retry kept the old activity: %+v", k.Steps[0])
95 }
96}
97
98// TestContractTaskSourceAndDuplicates: a task names its creator, the
99// message it was noted from and the coordinator looking into it; a task
100// recorded anyway beside a similar open one names it as a possible
101// duplicate, and only its creator may fold it into that one.
102func TestContractTaskSourceAndDuplicates(t *testing.T) {
103 a := newToolRig(t)
104 ctx := a.ctx
105 b := personToolRig(t, a, "second@example.com", equipo.RoleOwner)
106 first := taskID(t, a.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Swipe back from the edge does nothing", "brief": "On iOS, swiping from the left edge does not go back."}))
107 var source wire.Message
108 a.conv.Locked(func(c *Conversation) {
109 for _, m := range c.Messages {
110 if m.Role == wire.RolePerson && m.TurnID == a.turn {
111 source = m
112 }
113 }
114 })
115 k, err := a.client.Task(ctx, first)
116 if err != nil {
117 t.Fatal(err)
118 }
119 if k.CreatorName != "Owner" || source.ID == "" || k.SourceMessageID != source.ID || k.SourceAt == nil || !k.SourceAt.Equal(source.CreatedAt) {
120 t.Fatalf("creator and source: %q %q %v, message %+v", k.CreatorName, k.SourceMessageID, k.SourceAt, source)
121 }
122 if k.Coordinator == nil || k.Coordinator.Kind != wire.CoordinatorLooking || k.Coordinator.Since.IsZero() || k.DuplicateOf != nil {
123 t.Fatalf("coordinator looking: %+v dup %+v", k.Coordinator, k.DuplicateOf)
124 }
125
126 similar := map[string]any{"repo": "oficina/sample", "title": "Swipe from the edge to go back does nothing", "brief": "Swiping from the left edge does not go back on iOS."}
127 if res := a.call("k2", wire.ToolRecordTask, similar); res.Code == "recorded" {
128 t.Fatalf("a similar task was recorded: %+v", res)
129 }
130 similar["separate"] = true
131 second := taskID(t, a.call("k3", wire.ToolRecordTask, similar))
132 k, _ = a.client.Task(ctx, second)
133 if k.DuplicateOf == nil || k.DuplicateOf.ID != first || k.DuplicateOf.Title != "Swipe back from the edge does nothing" || k.DuplicateOf.Stage != wire.StageNoted {
134 t.Fatalf("duplicate_of: %+v", k.DuplicateOf)
135 }
136 other := taskID(t, a.call("k4", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Rename the coordinator", "brief": "y"}))
137
138 // Only the creator folds a task, into another open task of its project.
139 if _, err := b.client.MarkDuplicate(ctx, second, first); Status(err) != http.StatusForbidden {
140 t.Fatalf("another person folded it: %v", err)
141 }
142 for of, want := range map[string]int{second: 422, "": 422, other: 422, "tknope": 404} {
143 if _, err := a.client.MarkDuplicate(ctx, second, of); Status(err) != want {
144 t.Fatalf("fold into %q: %v, want %d", of, err, want)
145 }
146 }
147 folded, err := a.client.MarkDuplicate(ctx, second, first)
148 if err != nil || folded.Stage != wire.StageCancelled || folded.DuplicateOf == nil || folded.DuplicateOf.ID != first {
149 t.Fatalf("fold: %+v %v", folded, err)
150 }
151 k, _ = a.client.Task(ctx, first)
152 if len(k.Thread) != 1 || k.Thread[0].Author != Owner || !strings.Contains(k.Thread[0].Text, "Swipe from the edge to go back does nothing") || !strings.Contains(k.Thread[0].Text, "on iOS.") || k.Reports != 1 {
153 t.Fatalf("report on the kept task: %+v reports %d", k.Thread, k.Reports)
154 }
155 if _, err := a.client.MarkDuplicate(ctx, second, first); Status(err) != http.StatusConflict {
156 t.Fatalf("fold an ended task: %v", err)
157 }
158 b.wait("B sees the fold", func(c *Conversation) bool { return c.Tasks[second].Stage == wire.StageCancelled })
159}
160
161// TestContractCoordinatorMerging: while Equipo merges a task, the task
162// says the coordinator is merging it, since the merge step started.
163func TestContractCoordinatorMerging(t *testing.T) {
164 r := newToolRigAt(t, wire.ProtocolInjectionDefences)
165 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
166 id := r.judgedTask("a", "Fix the README typo", base, cand, wire.ChecksPassed, "")
167 _, body := r.expect(wire.FrameDispatch)
168 m := body.(*wire.DispatchCommand)
169 r.step("mg-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand})
170 k, _ := r.client.Task(r.ctx, id)
171 started := k.Steps[len(k.Steps)-1].StartedAt
172 if k.Stage != wire.StageMerging || k.Coordinator == nil || k.Coordinator.Kind != wire.CoordinatorMerging || started == nil || !k.Coordinator.Since.Equal(*started) {
173 t.Fatalf("merging: %s %+v started %v steps %d", k.Stage, k.Coordinator, started, len(k.Steps))
174 }
175 r.mergedStep("mg-m", m, strings.Repeat("e", 40))
176 r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone })
177 if k, _ := r.client.Task(r.ctx, id); k.Coordinator != nil {
178 t.Fatalf("done task still busy: %+v", k.Coordinator)
179 }
180}
181
182// TestContractProjectFacts: a project says what the runners' index knows
183// of it (description, stack, whether checks run, the default branch's
184// newest commit) and who is involved; a repository no index knows has no
185// facts, never invented ones.
186func TestContractProjectFacts(t *testing.T) {
187 a := newToolRig(t)
188 ctx := a.ctx
189 head := time.Date(2026, 11, 7, 9, 30, 0, 0, time.UTC)
190 hello := wire.Hello{Generation: a.runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample", "amadan.net/oficina/equipo"}, CreateIn: []string{"oficina"}, Agent: "equipo-test", Catalog: []wire.RepoInfo{{Repo: "amadan.net/oficina/sample", DefaultBranch: "main", Summary: "Woodstar is a reading app.", Names: []string{"sample", "Woodstar"}, Hints: []string{"swift", "swiftui", "unknown"}, UpdatedAt: head, HeadSHA: strings.Repeat("3", 40), HeadSubject: "Trim the compose sheet", ChecksReason: "needs_macos"}}, Connections: []wire.HeldConnection{{LocalID: "claude-a", Person: Owner, Provider: "claude", Kind: "subscription", Label: Owner, State: "healthy"}}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}}
191 if err := a.runner.Send(ctx, "h2", wire.FrameHello, hello); err != nil {
192 t.Fatal(err)
193 }
194 b := personToolRig(t, a, "nora.k@example.com", equipo.RoleOwner)
195 taskID(t, a.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Timeline jumps", "brief": "x"}))
196 taskID(t, b.call("k2", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Images load late", "brief": "y"}))
197 taskID(t, a.call("k3", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Rename the coordinator", "brief": "z"}))
198 list, err := a.client.Projects(ctx)
199 if err != nil {
200 t.Fatal(err)
201 }
202 var sample, equipoP wire.Project
203 for _, p := range list.Items {
204 switch p.Repo {
205 case "amadan.net/oficina/sample":
206 sample = p
207 case "amadan.net/oficina/equipo":
208 equipoP = p
209 }
210 }
211 if sample.Description != "Woodstar is a reading app." || strings.Join(sample.Stack, ",") != "Swift,SwiftUI" || sample.Checks == nil || sample.Checks.Here || sample.Checks.Reason != "needs_macos" {
212 t.Fatalf("sample facts: %+v %+v", sample, sample.Checks)
213 }
214 at := sample.Attachments[0]
215 if at.DefaultBranch != "main" || at.HeadSHA != strings.Repeat("3", 40) || at.HeadSubject != "Trim the compose sheet" || at.HeadAt == nil || !at.HeadAt.Equal(head) {
216 t.Fatalf("attachment head: %+v", at)
217 }
218 if len(sample.People) != 2 || sample.People[0].Address != Owner || sample.People[0].Name != "Owner" || sample.People[1].Name != "Nora" || sample.People[1].Initials != "N" {
219 t.Fatalf("people: %+v", sample.People)
220 }
221 if equipoP.Checks != nil || equipoP.Description != "" || len(equipoP.Stack) != 0 || equipoP.Attachments[0].HeadSHA != "" {
222 t.Fatalf("invented facts: %+v", equipoP)
223 }
224 // A gate this runner can run: checks run here.
225 hello.Catalog[0].ChecksReason, hello.Catalog[0].Gate, hello.Catalog[0].Hints = "", "make ci", []string{"go"}
226 if err := a.runner.Send(ctx, "h3", wire.FrameHello, hello); err != nil {
227 t.Fatal(err)
228 }
229 deadline := time.Now().Add(5 * time.Second)
230 for {
231 list, _ = a.client.Projects(ctx)
232 var c *wire.ProjectChecks
233 for _, p := range list.Items {
234 if p.Repo == "amadan.net/oficina/sample" {
235 c = p.Checks
236 }
237 }
238 if c != nil && c.Here && c.Gate == "make ci" && c.Reason == "" {
239 break
240 }
241 if time.Now().After(deadline) {
242 t.Fatalf("checks here: %+v", c)
243 }
244 time.Sleep(20 * time.Millisecond)
245 }
246}
247