| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Uncombine is Undo on a task Equipo combined into another. |
| 14 | func (c *Client) Uncombine(ctx context.Context, id string) (out wire.Task, err error) { |
| 15 | return out, c.do(ctx, "POST", "/api/v1/tasks/"+id+"/uncombine", nil, &out) |
| 16 | } |
| 17 | |
| 18 | // forgetJudgements drops the "recorded as separate" pairs, as for tasks |
| 19 | // filed before Equipo looked for duplicates (staging's swipe-back pair). |
| 20 | func (r *toolRig) forgetJudgements() { |
| 21 | r.t.Helper() |
| 22 | if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_task_distinct"); err != nil { |
| 23 | r.t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // forgetPair drops one "recorded as separate" pair. |
| 28 | func (r *toolRig) forgetPair(a, b string) { |
| 29 | r.t.Helper() |
| 30 | if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_task_distinct WHERE (a = ? AND b = ?) OR (a = ? AND b = ?)", a, b, b, a); err != nil { |
| 31 | r.t.Fatal(err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func (r *toolRig) tidy() int { |
| 36 | r.t.Helper() |
| 37 | n, err := r.inst.Store.Chat().TidyDuplicates(r.ctx) |
| 38 | if err != nil { |
| 39 | r.t.Fatal(err) |
| 40 | } |
| 41 | return n |
| 42 | } |
| 43 | |
| 44 | // TestContractCombineKeepsDecided: staging's Woodstar pair, 2026-11-11. |
| 45 | // One person's two look-alike tasks, both Ready to merge and waiting on |
| 46 | // a Mac. The person answered Wait for a Mac on the newer one; the older |
| 47 | // one's merge card is still open. Equipo combined the decided one into |
| 48 | // the older one. A person's decision outranks age: the decided task is |
| 49 | // never closed, and the older one, a decision still open on it, is only |
| 50 | // named as its duplicate. Both cards stay as they were. |
| 51 | func TestContractCombineKeepsDecided(t *testing.T) { |
| 52 | r := newToolRig(t) |
| 53 | ctx := r.ctx |
| 54 | card := func(task string) (out wire.Card, ok bool) { |
| 55 | r.conv.Locked(func(c *Conversation) { |
| 56 | for _, k := range c.Cards { |
| 57 | if k.Kind == wire.CardMerge && k.TaskID == task { |
| 58 | out, ok = k, true |
| 59 | } |
| 60 | } |
| 61 | }) |
| 62 | return out, ok |
| 63 | } |
| 64 | open := func(task string) func(*Conversation) bool { |
| 65 | return func(c *Conversation) bool { |
| 66 | for _, k := range c.Cards { |
| 67 | if k.Kind == wire.CardMerge && k.TaskID == task && k.State == wire.CardOpen { |
| 68 | return true |
| 69 | } |
| 70 | } |
| 71 | return false |
| 72 | } |
| 73 | } |
| 74 | older := r.judgedTask("dk1", "Swipe from the edge to go back doesn't work", strings.Repeat("7", 40), strings.Repeat("8", 40), wire.ChecksCannotRun, "needs_macos") |
| 75 | r.wait("older merge card", open(older)) |
| 76 | newer := r.judgedTask("dk2", "Swipe back from the edge doesn't work", strings.Repeat("9", 40), strings.Repeat("a", 40), wire.ChecksCannotRun, "needs_macos") |
| 77 | r.wait("newer merge card", open(newer)) |
| 78 | for id, at := range map[string]time.Time{older: time.Date(2026, 11, 7, 18, 39, 0, 0, time.UTC), newer: time.Date(2026, 11, 7, 21, 40, 0, 0, time.UTC)} { |
| 79 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET created_at = ? WHERE id = ?", at, id); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | } |
| 83 | c, _ := card(newer) |
| 84 | if out, err := r.client.Answer(ctx, c.ID, wire.ChoiceWaitMac, c.Revision); err != nil || out.Card.State != wire.CardAnswered { |
| 85 | t.Fatalf("wait for a Mac: %+v %v", out, err) |
| 86 | } |
| 87 | r.forgetPair(older, newer) |
| 88 | if n := r.tidy(); n != 0 { |
| 89 | t.Fatalf("combined %d: a task with a decision still open on it, or the decided one, was closed", n) |
| 90 | } |
| 91 | if k, _ := r.client.Task(ctx, newer); k.Folded != nil || k.Stage != wire.StageReadyToMerge || len(k.Combined) != 0 { |
| 92 | t.Fatalf("the decided task: stage %s, folded %+v, combined %+v", k.Stage, k.Folded, k.Combined) |
| 93 | } |
| 94 | if k, _ := r.client.Task(ctx, older); k.Folded != nil || k.Stage != wire.StageReadyToMerge || k.DuplicateOf == nil || k.DuplicateOf.ID != newer { |
| 95 | t.Fatalf("the undecided task: stage %s, folded %+v, named %+v", k.Stage, k.Folded, k.DuplicateOf) |
| 96 | } |
| 97 | if c, ok := card(older); !ok || c.State != wire.CardOpen { |
| 98 | t.Fatalf("the open merge card was lost: %+v", c) |
| 99 | } |
| 100 | if c, ok := card(newer); !ok || c.State != wire.CardAnswered || c.Choice != wire.ChoiceWaitMac { |
| 101 | t.Fatalf("the answered merge card changed: %+v", c) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // TestContractCombineDuplicates: Paul, staging ("Can Equipo itself tidy |
| 106 | // up the duplicates?"). Two tasks one person filed that read very much |
| 107 | // alike are combined by Equipo: the one furthest along keeps the work and |
| 108 | // gains the other's report, findings and thread; the other is cancelled |
| 109 | // without merging, and the person gets a quiet receipt with Undo. Undo |
| 110 | // restores it exactly (stage, findings, thread) and the pair is never |
| 111 | // combined again. A teammate's look-alike is only named, a task at work |
| 112 | // is left alone, and a test person's tasks are listed to itself only. |
| 113 | func TestContractCombineDuplicates(t *testing.T) { |
| 114 | r := newToolRig(t) |
| 115 | ctx := r.ctx |
| 116 | keep := taskID(t, r.call("d1", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Swipe back from the edge doesn't work", "brief": "On the Account pane."})) |
| 117 | r.call("d2", wire.ToolAddFinding, map[string]any{"task": keep, "text": "The pane takes the edge swipe.", "repo": "oficina/sample", "path": "panes/swipe.go", "line": 4}) |
| 118 | fold := taskID(t, r.call("d3", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Swipe from the edge to go back doesn't work", "brief": "Nothing happens on a swipe.", "separate": true})) |
| 119 | r.call("d4", wire.ToolAddFinding, map[string]any{"task": fold, "text": "Same pane host.", "repo": "oficina/sample", "path": "panes/host.go", "line": 9}) |
| 120 | if _, err := r.client.PostThread(ctx, fold, "Only on the phone.", "n1"); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | // Recorded as separate by the coordinator: never combined on its own. |
| 124 | if n := r.tidy(); n != 0 { |
| 125 | t.Fatalf("combined a pair the coordinator kept apart: %d", n) |
| 126 | } |
| 127 | before, _ := r.client.Task(ctx, fold) |
| 128 | keptBefore, _ := r.client.Task(ctx, keep) |
| 129 | if before.Stage != wire.StageInvestigated || before.DuplicateOf == nil || before.DuplicateOf.ID != keep || before.DuplicateOf.Creator != Owner { |
| 130 | t.Fatalf("separate record: %+v", before.DuplicateOf) |
| 131 | } |
| 132 | |
| 133 | // Filed before Equipo looked: combined on the next look. |
| 134 | r.forgetJudgements() |
| 135 | if n := r.tidy(); n != 1 { |
| 136 | t.Fatalf("combined %d", n) |
| 137 | } |
| 138 | folded, _ := r.client.Task(ctx, fold) |
| 139 | kept, _ := r.client.Task(ctx, keep) |
| 140 | if folded.Stage != wire.StageCancelled || folded.Folded == nil || folded.Folded.Into.ID != keep || folded.Merged != nil { |
| 141 | t.Fatalf("folded task: %+v", folded) |
| 142 | } |
| 143 | if len(kept.Combined) != 1 || kept.Combined[0].ID != fold || kept.Stage != keptBefore.Stage { |
| 144 | t.Fatalf("kept task: combined %+v stage %s", kept.Combined, kept.Stage) |
| 145 | } |
| 146 | text := func(k wire.Task) string { |
| 147 | var b strings.Builder |
| 148 | for _, f := range k.Findings { |
| 149 | b.WriteString(f.Text + "|") |
| 150 | } |
| 151 | for _, e := range k.Thread { |
| 152 | b.WriteString(e.Text + "|") |
| 153 | } |
| 154 | return b.String() |
| 155 | } |
| 156 | for _, want := range []string{"Same pane host.", "Only on the phone.", "Swipe from the edge to go back doesn't work", "The pane takes the edge swipe."} { |
| 157 | if !strings.Contains(text(kept), want) { |
| 158 | t.Fatalf("kept task lacks %q: %s", want, text(kept)) |
| 159 | } |
| 160 | } |
| 161 | var card wire.Card |
| 162 | r.wait("combined receipt", func(c *Conversation) bool { |
| 163 | for _, k := range c.Cards { |
| 164 | if k.Kind == wire.CardCombined && k.TaskID == fold && k.State == wire.CardOpen { |
| 165 | card = k |
| 166 | return true |
| 167 | } |
| 168 | } |
| 169 | return false |
| 170 | }) |
| 171 | if !strings.Contains(card.Question, "Combined “Swipe from the edge to go back doesn't work” into “Swipe back from the edge doesn't work”") { |
| 172 | t.Fatalf("receipt: %q", card.Question) |
| 173 | } |
| 174 | // Only its creator undoes it. |
| 175 | member := personToolRig(t, r, "member@example.com", equipo.RoleMember) |
| 176 | if _, err := member.client.Uncombine(ctx, fold); Status(err) != 403 { |
| 177 | t.Fatalf("member undo: %v", err) |
| 178 | } |
| 179 | |
| 180 | // Undo restores it exactly, from the receipt. |
| 181 | if _, err := r.client.Answer(ctx, card.ID, wire.ChoiceUndo, card.Revision); err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | after, _ := r.client.Task(ctx, fold) |
| 185 | keptAfter, _ := r.client.Task(ctx, keep) |
| 186 | if after.Stage != before.Stage || after.Folded != nil || text(after) != text(before) || len(after.Findings) != len(before.Findings) { |
| 187 | t.Fatalf("undo: stage %s (was %s), %q (was %q)", after.Stage, before.Stage, text(after), text(before)) |
| 188 | } |
| 189 | if text(keptAfter) != text(keptBefore) || len(keptAfter.Combined) != 0 { |
| 190 | t.Fatalf("kept task after undo: %q (was %q)", text(keptAfter), text(keptBefore)) |
| 191 | } |
| 192 | if n := r.tidy(); n != 0 { |
| 193 | t.Fatal("combined again after Undo") |
| 194 | } |
| 195 | |
| 196 | // A task at work is not combined, even into one further along: keep |
| 197 | // moves on to In review; busy has Engineering at work. |
| 198 | r.call("d5s", wire.ToolStartTask, map[string]string{"task": keep, "brief": "go"}) |
| 199 | _, body := r.expect(wire.FrameDispatch) |
| 200 | kd := body.(*wire.DispatchCommand) |
| 201 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 202 | r.step("d5-run", wire.StepReport{TaskID: keep, StepID: kd.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) |
| 203 | r.step("d5-done", wire.StepReport{TaskID: keep, StepID: kd.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true}) |
| 204 | r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 205 | busy := taskID(t, r.call("d5", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Edge swipe back doesn't work on the pane", "brief": "Swipe back.", "separate": true})) |
| 206 | r.call("d6", wire.ToolStartTask, map[string]string{"task": busy, "brief": "go"}) |
| 207 | _, body = r.expect(wire.FrameDispatch) |
| 208 | d := body.(*wire.DispatchCommand) |
| 209 | r.step("d6-run", wire.StepReport{TaskID: busy, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) |
| 210 | r.forgetPair(busy, keep) |
| 211 | r.tidy() |
| 212 | if k, _ := r.client.Task(ctx, busy); k.Folded != nil || k.Stage == wire.StageCancelled { |
| 213 | t.Fatalf("a task at work was combined: %+v", k.Folded) |
| 214 | } |
| 215 | // A teammate's look-alike is only named. |
| 216 | b := personToolRig(t, r, "second@example.com", equipo.RoleOwner) |
| 217 | theirs := taskID(t, b.call("d7", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Swipe back from the pane edge doesn't work", "brief": "Mine too.", "separate": true})) |
| 218 | r.forgetPair(theirs, keep) |
| 219 | r.tidy() |
| 220 | if k, _ := r.client.Task(ctx, theirs); k.Folded != nil || k.DuplicateOf == nil { |
| 221 | t.Fatalf("a teammate's look-alike: folded %+v, named %+v", k.Folded, k.DuplicateOf) |
| 222 | } |
| 223 | |
| 224 | // Never into another person's task, automatically or by hand, and the |
| 225 | // other task is left exactly as it was. |
| 226 | keepNow, _ := r.client.Task(ctx, keep) |
| 227 | if len(keepNow.Combined) != 0 || text(keepNow) != text(keptBefore) { |
| 228 | t.Fatalf("a teammate's look-alike changed the owner's task: %+v %q", keepNow.Combined, text(keepNow)) |
| 229 | } |
| 230 | theirsBefore, _ := b.client.Task(ctx, theirs) |
| 231 | mineToo := taskID(t, r.call("d9", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Crash on launch after the update", "brief": "Again."})) |
| 232 | if _, err := r.client.MarkDuplicate(ctx, mineToo, theirs); Status(err) != 403 { |
| 233 | t.Fatalf("fold into another person's task: %v", err) |
| 234 | } |
| 235 | if after, _ := b.client.Task(ctx, theirs); text(after) != text(theirsBefore) || after.Stage != theirsBefore.Stage { |
| 236 | t.Fatal("a refused fold changed the other person's task") |
| 237 | } |
| 238 | |
| 239 | // An open card on the task kept does not keep them apart (the |
| 240 | // coordinator, 2026-11-11, on staging's Woodstar pair: the survivor |
| 241 | // waits on its merge card): the other is combined into it and the |
| 242 | // card stays open. A decision still to make on the task that would |
| 243 | // close does keep them apart, and so does a creator no longer on the |
| 244 | // team. |
| 245 | cardOn := func(c *Conversation, task string) bool { |
| 246 | for _, k := range c.Cards { |
| 247 | if k.Kind == wire.CardMerge && k.TaskID == task && k.State == wire.CardOpen { |
| 248 | return true |
| 249 | } |
| 250 | } |
| 251 | return false |
| 252 | } |
| 253 | mergeCard := func(task string) (open bool) { |
| 254 | r.conv.Locked(func(c *Conversation) { open = cardOn(c, task) }) |
| 255 | return open |
| 256 | } |
| 257 | ready := r.judgedTask("dc", "Timeline jumps when images load", strings.Repeat("3", 40), strings.Repeat("4", 40), wire.ChecksCannotRun, "needs_macos") |
| 258 | r.wait("merge card", func(c *Conversation) bool { return cardOn(c, ready) }) |
| 259 | again := taskID(t, r.call("d10", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Timeline jumps when the images load", "brief": "Again.", "separate": true})) |
| 260 | r.forgetPair(again, ready) |
| 261 | if n := r.tidy(); n != 1 { |
| 262 | t.Fatalf("an open merge card on the task kept blocked the combine: combined %d", n) |
| 263 | } |
| 264 | if k, _ := r.client.Task(ctx, again); k.Folded == nil || k.Folded.Into.ID != ready || k.Stage != wire.StageCancelled { |
| 265 | t.Fatalf("not combined into the task with the merge card: %+v", k.Folded) |
| 266 | } |
| 267 | if k, _ := r.client.Task(ctx, ready); k.Stage != wire.StageReadyToMerge || !mergeCard(ready) { |
| 268 | t.Fatalf("the task kept lost its merge card or moved: %s", k.Stage) |
| 269 | } |
| 270 | later := r.judgedTask("dd", "Timeline jumps when the images load late", strings.Repeat("5", 40), strings.Repeat("6", 40), wire.ChecksCannotRun, "needs_macos") |
| 271 | r.wait("second merge card", func(c *Conversation) bool { return cardOn(c, later) }) |
| 272 | r.forgetPair(later, ready) |
| 273 | r.tidy() |
| 274 | if k, _ := r.client.Task(ctx, later); k.Folded != nil || k.DuplicateOf == nil || k.DuplicateOf.ID != ready { |
| 275 | t.Fatalf("closed a task with a decision still open: folded %+v, named %+v", k.Folded, k.DuplicateOf) |
| 276 | } |
| 277 | one := taskID(t, b.call("d11", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Compose sheet keyboard covers the send button", "brief": "x"})) |
| 278 | two := taskID(t, b.call("d12", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Keyboard covers the compose sheet send button", "brief": "y", "separate": true})) |
| 279 | m, err := r.inst.Store.Member(context.Background(), "second@example.com") |
| 280 | if err != nil { |
| 281 | t.Fatal(err) |
| 282 | } |
| 283 | if err := r.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleOwner, false, m.Revision); err != nil { |
| 284 | t.Fatal(err) |
| 285 | } |
| 286 | r.forgetPair(one, two) |
| 287 | r.tidy() |
| 288 | for _, id := range []string{one, two} { |
| 289 | if k, _ := r.client.Task(ctx, id); k.Folded != nil || len(k.Combined) != 0 { |
| 290 | t.Fatalf("combined the tasks of someone no longer on the team: %s", id) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // A test person's tasks are its own: listed to itself only, and never |
| 295 | // paired with anyone else's. |
| 296 | tester := personToolRig(t, r, "suite@staging.invalid", equipo.RoleOwner) |
| 297 | own := taskID(t, tester.call("d8", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Swipe back from the edge doesn't work", "brief": "Witness.", "separate": true})) |
| 298 | r.forgetPair(own, keep) |
| 299 | r.tidy() |
| 300 | if k, _ := tester.client.Task(ctx, own); k.DuplicateOf != nil || k.Folded != nil { |
| 301 | t.Fatalf("test person's task paired: %+v", k.DuplicateOf) |
| 302 | } |
| 303 | list, _ := r.client.Tasks(ctx) |
| 304 | for _, k := range list.Items { |
| 305 | if k.ID == own { |
| 306 | t.Fatal("a test person's task is listed to the owner") |
| 307 | } |
| 308 | } |
| 309 | mine, _ := tester.client.Tasks(ctx) |
| 310 | found := false |
| 311 | for _, k := range mine.Items { |
| 312 | found = found || k.ID == own |
| 313 | } |
| 314 | if !found { |
| 315 | t.Fatal("a test person does not see its own task") |
| 316 | } |
| 317 | } |
| 318 | |