package thintest import ( "net/http" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // activity sends one step activity line (not durable: no ack). func (r *toolRig) activity(id string, a wire.StepActivityReport) { r.t.Helper() if err := r.runner.Send(r.ctx, id, wire.FrameStepActivity, a); err != nil { r.t.Fatal(err) } } // TestContractStepActivity: a running step's activity lines come only // from the step person's primary runner, for the current attempt; the // newest five are kept on the step and every member gets each line as // an activity event; a new attempt starts with none. func TestContractStepActivity(t *testing.T) { a := newToolRig(t) ctx := a.ctx b := personToolRig(t, a, "second@example.com", equipo.RoleOwner) id := taskID(t, a.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Swipe back from the edge does nothing", "brief": "x"})) a.call("k2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"}) _, body := a.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) a.step("r-s1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) // Another person's runner, about A's step: dropped. B's own turn // activity after it shows B's frame was applied first. 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 { t.Fatal(err) } if err := b.runner.Send(ctx, "b-turn", wire.FrameActivity, wire.ActivityReport{TurnID: b.turn, Attempt: 1, Kind: wire.ActivityThink}); err != nil { t.Fatal(err) } b.wait("B turn activity", func(c *Conversation) bool { return len(c.Activity[b.turn]) > 0 }) // A stale attempt: dropped. a.activity("a-stale", wire.StepActivityReport{TaskID: id, StepID: d.StepID, Attempt: 2, Kind: wire.ActivityRead, Subject: "stale.go"}) lines := []wire.StepActivityReport{ {Kind: wire.ActivityRead, Subject: "/workspace/panes/swipe.go"}, {Kind: wire.ActivitySearch, Subject: "EdgeSwipe"}, {Kind: "launch_missiles", Subject: "x"}, {Kind: wire.ActivityEdit, Subject: "panes/swipe.go"}, {Kind: wire.ActivityRun, Subject: "go test ./panes\nrm -rf /"}, {Kind: wire.ActivityRun, Subject: "make \x1b[31mci" + strings.Repeat("x", 300)}, {Kind: wire.ActivityEdit, Subject: "panes/swipe_test.go"}, } for i, l := range lines { l.TaskID, l.StepID, l.Attempt = id, d.StepID, 1 a.activity("a-act-"+itoa(i), l) } for _, r := range []*toolRig{a, b} { r.wait("every line, to every member", func(c *Conversation) bool { return len(c.StepActivity[d.StepID]) == len(lines) }) } var got []wire.StepActivity a.conv.Locked(func(c *Conversation) { got = c.StepActivity[d.StepID] }) first, last := got[0].Activity, got[len(got)-1].Activity 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 { t.Fatalf("first line %+v", got[0]) } 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" { t.Fatalf("kept lines %+v", last) } for _, e := range got { for _, l := range e.Activity { if l.Subject == "secret.go" || l.Subject == "stale.go" { t.Fatalf("a dropped line was kept: %+v", e) } } } k, err := a.client.Task(ctx, id) if err != nil || len(k.Steps[0].Activity) != wire.MaxStepActivity || k.Steps[0].Activity[4].Subject != "panes/swipe_test.go" { t.Fatalf("step activity on the task: %+v %v", k.Steps, err) } // The step keeps its last lines after it ends; a retry starts afresh. a.step("r-s2", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) if k, _ := a.client.Task(ctx, id); len(k.Steps[0].Activity) != wire.MaxStepActivity { t.Fatalf("ended step lost its lines: %+v", k.Steps[0]) } a.activity("a-after", wire.StepActivityReport{TaskID: id, StepID: d.StepID, Attempt: 1, Kind: wire.ActivityRead, Subject: "late.go"}) if _, err := a.client.RetryTask(ctx, id); err != nil { t.Fatal(err) } a.expect(wire.FrameDispatch) k, _ = a.client.Task(ctx, id) if len(k.Steps[0].Activity) != 0 || k.Steps[0].Attempt != 2 { t.Fatalf("retry kept the old activity: %+v", k.Steps[0]) } } // TestContractTaskSourceAndDuplicates: a task names its creator, the // message it was noted from and the coordinator looking into it; a task // recorded anyway beside a similar open one names it as a possible // duplicate, and only its creator may fold it into that one. func TestContractTaskSourceAndDuplicates(t *testing.T) { a := newToolRig(t) ctx := a.ctx b := personToolRig(t, a, "second@example.com", equipo.RoleOwner) 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."})) var source wire.Message a.conv.Locked(func(c *Conversation) { for _, m := range c.Messages { if m.Role == wire.RolePerson && m.TurnID == a.turn { source = m } } }) k, err := a.client.Task(ctx, first) if err != nil { t.Fatal(err) } if k.CreatorName != "Owner" || source.ID == "" || k.SourceMessageID != source.ID || k.SourceAt == nil || !k.SourceAt.Equal(source.CreatedAt) { t.Fatalf("creator and source: %q %q %v, message %+v", k.CreatorName, k.SourceMessageID, k.SourceAt, source) } if k.Coordinator == nil || k.Coordinator.Kind != wire.CoordinatorLooking || k.Coordinator.Since.IsZero() || k.DuplicateOf != nil { t.Fatalf("coordinator looking: %+v dup %+v", k.Coordinator, k.DuplicateOf) } 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."} if res := a.call("k2", wire.ToolRecordTask, similar); res.Code == "recorded" { t.Fatalf("a similar task was recorded: %+v", res) } similar["separate"] = true second := taskID(t, a.call("k3", wire.ToolRecordTask, similar)) k, _ = a.client.Task(ctx, second) if k.DuplicateOf == nil || k.DuplicateOf.ID != first || k.DuplicateOf.Title != "Swipe back from the edge does nothing" || k.DuplicateOf.Stage != wire.StageNoted { t.Fatalf("duplicate_of: %+v", k.DuplicateOf) } other := taskID(t, a.call("k4", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Rename the coordinator", "brief": "y"})) // Only the creator folds a task, into another open task of its project. if _, err := b.client.MarkDuplicate(ctx, second, first); Status(err) != http.StatusForbidden { t.Fatalf("another person folded it: %v", err) } for of, want := range map[string]int{second: 422, "": 422, other: 422, "tknope": 404} { if _, err := a.client.MarkDuplicate(ctx, second, of); Status(err) != want { t.Fatalf("fold into %q: %v, want %d", of, err, want) } } folded, err := a.client.MarkDuplicate(ctx, second, first) if err != nil || folded.Stage != wire.StageCancelled || folded.DuplicateOf == nil || folded.DuplicateOf.ID != first { t.Fatalf("fold: %+v %v", folded, err) } k, _ = a.client.Task(ctx, first) 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 { t.Fatalf("report on the kept task: %+v reports %d", k.Thread, k.Reports) } if _, err := a.client.MarkDuplicate(ctx, second, first); Status(err) != http.StatusConflict { t.Fatalf("fold an ended task: %v", err) } b.wait("B sees the fold", func(c *Conversation) bool { return c.Tasks[second].Stage == wire.StageCancelled }) } // TestContractCoordinatorMerging: while Equipo merges a task, the task // says the coordinator is merging it, since the merge step started. func TestContractCoordinatorMerging(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("a", "Fix the README typo", base, cand, wire.ChecksPassed, "") _, body := r.expect(wire.FrameDispatch) m := body.(*wire.DispatchCommand) r.step("mg-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) k, _ := r.client.Task(r.ctx, id) started := k.Steps[len(k.Steps)-1].StartedAt if k.Stage != wire.StageMerging || k.Coordinator == nil || k.Coordinator.Kind != wire.CoordinatorMerging || started == nil || !k.Coordinator.Since.Equal(*started) { t.Fatalf("merging: %s %+v started %v steps %d", k.Stage, k.Coordinator, started, len(k.Steps)) } r.mergedStep("mg-m", m, strings.Repeat("e", 40)) r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) if k, _ := r.client.Task(r.ctx, id); k.Coordinator != nil { t.Fatalf("done task still busy: %+v", k.Coordinator) } } // TestContractProjectFacts: a project says what the runners' index knows // of it (description, stack, whether checks run, the default branch's // newest commit) and who is involved; a repository no index knows has no // facts, never invented ones. func TestContractProjectFacts(t *testing.T) { a := newToolRig(t) ctx := a.ctx head := time.Date(2026, 11, 7, 9, 30, 0, 0, time.UTC) 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"}} if err := a.runner.Send(ctx, "h2", wire.FrameHello, hello); err != nil { t.Fatal(err) } b := personToolRig(t, a, "nora.k@example.com", equipo.RoleOwner) taskID(t, a.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Timeline jumps", "brief": "x"})) taskID(t, b.call("k2", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Images load late", "brief": "y"})) taskID(t, a.call("k3", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Rename the coordinator", "brief": "z"})) list, err := a.client.Projects(ctx) if err != nil { t.Fatal(err) } var sample, equipoP wire.Project for _, p := range list.Items { switch p.Repo { case "amadan.net/oficina/sample": sample = p case "amadan.net/oficina/equipo": equipoP = p } } 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" { t.Fatalf("sample facts: %+v %+v", sample, sample.Checks) } at := sample.Attachments[0] if at.DefaultBranch != "main" || at.HeadSHA != strings.Repeat("3", 40) || at.HeadSubject != "Trim the compose sheet" || at.HeadAt == nil || !at.HeadAt.Equal(head) { t.Fatalf("attachment head: %+v", at) } 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" { t.Fatalf("people: %+v", sample.People) } if equipoP.Checks != nil || equipoP.Description != "" || len(equipoP.Stack) != 0 || equipoP.Attachments[0].HeadSHA != "" { t.Fatalf("invented facts: %+v", equipoP) } // A gate this runner can run: checks run here. hello.Catalog[0].ChecksReason, hello.Catalog[0].Gate, hello.Catalog[0].Hints = "", "make ci", []string{"go"} if err := a.runner.Send(ctx, "h3", wire.FrameHello, hello); err != nil { t.Fatal(err) } deadline := time.Now().Add(5 * time.Second) for { list, _ = a.client.Projects(ctx) var c *wire.ProjectChecks for _, p := range list.Items { if p.Repo == "amadan.net/oficina/sample" { c = p.Checks } } if c != nil && c.Here && c.Gate == "make ci" && c.Reason == "" { break } if time.Now().After(deadline) { t.Fatalf("checks here: %+v", c) } time.Sleep(20 * time.Millisecond) } }