package thintest import ( "context" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // Uncombine is Undo on a task Equipo combined into another. func (c *Client) Uncombine(ctx context.Context, id string) (out wire.Task, err error) { return out, c.do(ctx, "POST", "/api/v1/tasks/"+id+"/uncombine", nil, &out) } // forgetJudgements drops the "recorded as separate" pairs, as for tasks // filed before Equipo looked for duplicates (staging's swipe-back pair). func (r *toolRig) forgetJudgements() { r.t.Helper() if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_task_distinct"); err != nil { r.t.Fatal(err) } } // forgetPair drops one "recorded as separate" pair. func (r *toolRig) forgetPair(a, b string) { r.t.Helper() 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 { r.t.Fatal(err) } } func (r *toolRig) tidy() int { r.t.Helper() n, err := r.inst.Store.Chat().TidyDuplicates(r.ctx) if err != nil { r.t.Fatal(err) } return n } // TestContractCombineKeepsDecided: staging's Woodstar pair, 2026-11-11. // One person's two look-alike tasks, both Ready to merge and waiting on // a Mac. The person answered Wait for a Mac on the newer one; the older // one's merge card is still open. Equipo combined the decided one into // the older one. A person's decision outranks age: the decided task is // never closed, and the older one, a decision still open on it, is only // named as its duplicate. Both cards stay as they were. func TestContractCombineKeepsDecided(t *testing.T) { r := newToolRig(t) ctx := r.ctx card := func(task string) (out wire.Card, ok bool) { r.conv.Locked(func(c *Conversation) { for _, k := range c.Cards { if k.Kind == wire.CardMerge && k.TaskID == task { out, ok = k, true } } }) return out, ok } open := func(task string) func(*Conversation) bool { return func(c *Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardMerge && k.TaskID == task && k.State == wire.CardOpen { return true } } return false } } 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") r.wait("older merge card", open(older)) newer := r.judgedTask("dk2", "Swipe back from the edge doesn't work", strings.Repeat("9", 40), strings.Repeat("a", 40), wire.ChecksCannotRun, "needs_macos") r.wait("newer merge card", open(newer)) 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)} { if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET created_at = ? WHERE id = ?", at, id); err != nil { t.Fatal(err) } } c, _ := card(newer) if out, err := r.client.Answer(ctx, c.ID, wire.ChoiceWaitMac, c.Revision); err != nil || out.Card.State != wire.CardAnswered { t.Fatalf("wait for a Mac: %+v %v", out, err) } r.forgetPair(older, newer) if n := r.tidy(); n != 0 { t.Fatalf("combined %d: a task with a decision still open on it, or the decided one, was closed", n) } if k, _ := r.client.Task(ctx, newer); k.Folded != nil || k.Stage != wire.StageReadyToMerge || len(k.Combined) != 0 { t.Fatalf("the decided task: stage %s, folded %+v, combined %+v", k.Stage, k.Folded, k.Combined) } if k, _ := r.client.Task(ctx, older); k.Folded != nil || k.Stage != wire.StageReadyToMerge || k.DuplicateOf == nil || k.DuplicateOf.ID != newer { t.Fatalf("the undecided task: stage %s, folded %+v, named %+v", k.Stage, k.Folded, k.DuplicateOf) } if c, ok := card(older); !ok || c.State != wire.CardOpen { t.Fatalf("the open merge card was lost: %+v", c) } if c, ok := card(newer); !ok || c.State != wire.CardAnswered || c.Choice != wire.ChoiceWaitMac { t.Fatalf("the answered merge card changed: %+v", c) } } // TestContractCombineDuplicates: Paul, staging ("Can Equipo itself tidy // up the duplicates?"). Two tasks one person filed that read very much // alike are combined by Equipo: the one furthest along keeps the work and // gains the other's report, findings and thread; the other is cancelled // without merging, and the person gets a quiet receipt with Undo. Undo // restores it exactly (stage, findings, thread) and the pair is never // combined again. A teammate's look-alike is only named, a task at work // is left alone, and a test person's tasks are listed to itself only. func TestContractCombineDuplicates(t *testing.T) { r := newToolRig(t) ctx := r.ctx 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."})) 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}) 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})) r.call("d4", wire.ToolAddFinding, map[string]any{"task": fold, "text": "Same pane host.", "repo": "oficina/sample", "path": "panes/host.go", "line": 9}) if _, err := r.client.PostThread(ctx, fold, "Only on the phone.", "n1"); err != nil { t.Fatal(err) } // Recorded as separate by the coordinator: never combined on its own. if n := r.tidy(); n != 0 { t.Fatalf("combined a pair the coordinator kept apart: %d", n) } before, _ := r.client.Task(ctx, fold) keptBefore, _ := r.client.Task(ctx, keep) if before.Stage != wire.StageInvestigated || before.DuplicateOf == nil || before.DuplicateOf.ID != keep || before.DuplicateOf.Creator != Owner { t.Fatalf("separate record: %+v", before.DuplicateOf) } // Filed before Equipo looked: combined on the next look. r.forgetJudgements() if n := r.tidy(); n != 1 { t.Fatalf("combined %d", n) } folded, _ := r.client.Task(ctx, fold) kept, _ := r.client.Task(ctx, keep) if folded.Stage != wire.StageCancelled || folded.Folded == nil || folded.Folded.Into.ID != keep || folded.Merged != nil { t.Fatalf("folded task: %+v", folded) } if len(kept.Combined) != 1 || kept.Combined[0].ID != fold || kept.Stage != keptBefore.Stage { t.Fatalf("kept task: combined %+v stage %s", kept.Combined, kept.Stage) } text := func(k wire.Task) string { var b strings.Builder for _, f := range k.Findings { b.WriteString(f.Text + "|") } for _, e := range k.Thread { b.WriteString(e.Text + "|") } return b.String() } 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."} { if !strings.Contains(text(kept), want) { t.Fatalf("kept task lacks %q: %s", want, text(kept)) } } var card wire.Card r.wait("combined receipt", func(c *Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardCombined && k.TaskID == fold && k.State == wire.CardOpen { card = k return true } } return false }) 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”") { t.Fatalf("receipt: %q", card.Question) } // Only its creator undoes it. member := personToolRig(t, r, "member@example.com", equipo.RoleMember) if _, err := member.client.Uncombine(ctx, fold); Status(err) != 403 { t.Fatalf("member undo: %v", err) } // Undo restores it exactly, from the receipt. if _, err := r.client.Answer(ctx, card.ID, wire.ChoiceUndo, card.Revision); err != nil { t.Fatal(err) } after, _ := r.client.Task(ctx, fold) keptAfter, _ := r.client.Task(ctx, keep) if after.Stage != before.Stage || after.Folded != nil || text(after) != text(before) || len(after.Findings) != len(before.Findings) { t.Fatalf("undo: stage %s (was %s), %q (was %q)", after.Stage, before.Stage, text(after), text(before)) } if text(keptAfter) != text(keptBefore) || len(keptAfter.Combined) != 0 { t.Fatalf("kept task after undo: %q (was %q)", text(keptAfter), text(keptBefore)) } if n := r.tidy(); n != 0 { t.Fatal("combined again after Undo") } // A task at work is not combined, even into one further along: keep // moves on to In review; busy has Engineering at work. r.call("d5s", wire.ToolStartTask, map[string]string{"task": keep, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) kd := body.(*wire.DispatchCommand) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) r.step("d5-run", wire.StepReport{TaskID: keep, StepID: kd.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) r.step("d5-done", wire.StepReport{TaskID: keep, StepID: kd.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true}) r.dispatched(wire.SpecialistChecks, base, cand, 0) 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})) r.call("d6", wire.ToolStartTask, map[string]string{"task": busy, "brief": "go"}) _, body = r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) r.step("d6-run", wire.StepReport{TaskID: busy, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) r.forgetPair(busy, keep) r.tidy() if k, _ := r.client.Task(ctx, busy); k.Folded != nil || k.Stage == wire.StageCancelled { t.Fatalf("a task at work was combined: %+v", k.Folded) } // A teammate's look-alike is only named. b := personToolRig(t, r, "second@example.com", equipo.RoleOwner) 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})) r.forgetPair(theirs, keep) r.tidy() if k, _ := r.client.Task(ctx, theirs); k.Folded != nil || k.DuplicateOf == nil { t.Fatalf("a teammate's look-alike: folded %+v, named %+v", k.Folded, k.DuplicateOf) } // Never into another person's task, automatically or by hand, and the // other task is left exactly as it was. keepNow, _ := r.client.Task(ctx, keep) if len(keepNow.Combined) != 0 || text(keepNow) != text(keptBefore) { t.Fatalf("a teammate's look-alike changed the owner's task: %+v %q", keepNow.Combined, text(keepNow)) } theirsBefore, _ := b.client.Task(ctx, theirs) mineToo := taskID(t, r.call("d9", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Crash on launch after the update", "brief": "Again."})) if _, err := r.client.MarkDuplicate(ctx, mineToo, theirs); Status(err) != 403 { t.Fatalf("fold into another person's task: %v", err) } if after, _ := b.client.Task(ctx, theirs); text(after) != text(theirsBefore) || after.Stage != theirsBefore.Stage { t.Fatal("a refused fold changed the other person's task") } // An open card on the task kept does not keep them apart (the // coordinator, 2026-11-11, on staging's Woodstar pair: the survivor // waits on its merge card): the other is combined into it and the // card stays open. A decision still to make on the task that would // close does keep them apart, and so does a creator no longer on the // team. cardOn := func(c *Conversation, task string) bool { for _, k := range c.Cards { if k.Kind == wire.CardMerge && k.TaskID == task && k.State == wire.CardOpen { return true } } return false } mergeCard := func(task string) (open bool) { r.conv.Locked(func(c *Conversation) { open = cardOn(c, task) }) return open } ready := r.judgedTask("dc", "Timeline jumps when images load", strings.Repeat("3", 40), strings.Repeat("4", 40), wire.ChecksCannotRun, "needs_macos") r.wait("merge card", func(c *Conversation) bool { return cardOn(c, ready) }) 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})) r.forgetPair(again, ready) if n := r.tidy(); n != 1 { t.Fatalf("an open merge card on the task kept blocked the combine: combined %d", n) } if k, _ := r.client.Task(ctx, again); k.Folded == nil || k.Folded.Into.ID != ready || k.Stage != wire.StageCancelled { t.Fatalf("not combined into the task with the merge card: %+v", k.Folded) } if k, _ := r.client.Task(ctx, ready); k.Stage != wire.StageReadyToMerge || !mergeCard(ready) { t.Fatalf("the task kept lost its merge card or moved: %s", k.Stage) } later := r.judgedTask("dd", "Timeline jumps when the images load late", strings.Repeat("5", 40), strings.Repeat("6", 40), wire.ChecksCannotRun, "needs_macos") r.wait("second merge card", func(c *Conversation) bool { return cardOn(c, later) }) r.forgetPair(later, ready) r.tidy() if k, _ := r.client.Task(ctx, later); k.Folded != nil || k.DuplicateOf == nil || k.DuplicateOf.ID != ready { t.Fatalf("closed a task with a decision still open: folded %+v, named %+v", k.Folded, k.DuplicateOf) } one := taskID(t, b.call("d11", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Compose sheet keyboard covers the send button", "brief": "x"})) 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})) m, err := r.inst.Store.Member(context.Background(), "second@example.com") if err != nil { t.Fatal(err) } if err := r.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleOwner, false, m.Revision); err != nil { t.Fatal(err) } r.forgetPair(one, two) r.tidy() for _, id := range []string{one, two} { if k, _ := r.client.Task(ctx, id); k.Folded != nil || len(k.Combined) != 0 { t.Fatalf("combined the tasks of someone no longer on the team: %s", id) } } // A test person's tasks are its own: listed to itself only, and never // paired with anyone else's. tester := personToolRig(t, r, "suite@staging.invalid", equipo.RoleOwner) 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})) r.forgetPair(own, keep) r.tidy() if k, _ := tester.client.Task(ctx, own); k.DuplicateOf != nil || k.Folded != nil { t.Fatalf("test person's task paired: %+v", k.DuplicateOf) } list, _ := r.client.Tasks(ctx) for _, k := range list.Items { if k.ID == own { t.Fatal("a test person's task is listed to the owner") } } mine, _ := tester.client.Tasks(ctx) found := false for _, k := range mine.Items { found = found || k.ID == own } if !found { t.Fatal("a test person does not see its own task") } }