package thintest import ( "context" "net/http" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // personToolRig is a second team member with their own device, runner // and running turn on the same instance as a. func personToolRig(t *testing.T, a *toolRig, person string, role equipo.Role) *toolRig { t.Helper() return personToolRigAt(t, a, person, role, wire.Protocol1) } // personToolRigAt is personToolRig with a runner speaking protocol. func personToolRigAt(t *testing.T, a *toolRig, person string, role equipo.Role, protocol int) *toolRig { t.Helper() ctx := a.ctx if err := a.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, person, role, true, 0); err != nil { t.Fatal(err) } r := &rig{t: t, ctx: ctx, inst: a.inst, client: NewClient(a.inst.URL, a.inst.DeviceTokenFor(t, person))} if role == equipo.RoleOwner { r.runner = a.inst.EnrolRunnerFor(t, person).Token } snap, err := r.client.Snapshot(ctx) if err != nil { t.Fatal(err) } r.conv = NewConversation(snap) r.follow() if role != equipo.RoleOwner { return &toolRig{rig: r} } var runner *FakeRunner if protocol <= wire.Protocol1 { runner = r.dial(0) } else { f, err := DialRunnerHello(ctx, a.inst.URL, r.runner, primaryAt(wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, protocol)) if err != nil { t.Fatal(err) } t.Cleanup(f.Close) runner = f } hello := wire.Hello{Generation: runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample", "amadan.net/oficina/equipo"}, CreateIn: []string{}, 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{"go"}}}, Connections: []wire.HeldConnection{}, Versions: map[string]string{}} if protocol > wire.Protocol1 { hello = primaryAt(hello, protocol) } if err := runner.Send(ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } r.wait("second runner online", func(c *Conversation) bool { return c.Runner.Online }) sent, err := r.client.Send(ctx, "hello", "k-second") if err != nil { t.Fatal(err) } f, _, err := runner.Expect(ctx, wire.FrameMessage) if err != nil { t.Fatal(err) } if err = runner.Send(ctx, "a-msg", wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { t.Fatal(err) } r.wait("second turn running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning }) return &toolRig{rig: r, runner: runner, turn: sent.Turn.ID} } // TestContractTaskAuthority: tasks are shared within the team (another // member reads them and gets their events), but only the task's creator // may start, retry or cancel it, onto their own runner; another // person's coordinator may read a task and never act on it. Someone no // longer on the team sees nothing. func TestContractTaskAuthority(t *testing.T) { a := newToolRig(t) ctx := a.ctx b := personToolRig(t, a, "second@example.com", equipo.RoleOwner) running := taskID(t, a.call("a1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "A's running task", "brief": "x"})) a.call("a2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) _, body := a.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) a.step("r-a1", wire.StepReport{TaskID: running, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: "1111111111111111111111111111111111111111"}) a.step("r-a2", wire.StepReport{TaskID: running, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) noted := taskID(t, a.call("a3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "A's noted task", "brief": "y"})) // Shared: B reads both, with their events. b.wait("B sees A's tasks", func(c *Conversation) bool { return len(c.Tasks) == 2 }) if k, err := b.client.Task(ctx, running); err != nil || k.Creator != Owner { t.Fatalf("B reads A's task: %+v %v", k, err) } if _, err := b.client.Tasks(ctx); err != nil { t.Fatal(err) } // Not shared: B may not act on A's tasks through the API. if _, err := b.client.RetryTask(ctx, running); Status(err) != http.StatusForbidden { t.Fatalf("B retried A's task: %v", err) } if _, err := b.client.CancelTask(ctx, running); Status(err) != http.StatusForbidden { t.Fatalf("B cancelled A's task: %v", err) } if _, err := b.client.CancelTask(ctx, noted); Status(err) != http.StatusForbidden { t.Fatalf("B cancelled A's noted task: %v", err) } // Nor through their coordinator. if res := b.call("b1", wire.ToolReadTask, map[string]string{"task": running}); res.IsError { t.Fatalf("B's coordinator could not read A's task: %+v", res) } for name, args := range map[string]map[string]any{ wire.ToolStartTask: {"task": noted, "brief": "mine now"}, wire.ToolCancelTask: {"task": running}, wire.ToolAddFinding: {"task": noted, "text": "x", "repo": "oficina/sample", "path": "a.go", "line": 1}, } { if res := b.call("b-"+name, name, args); !res.IsError || res.Code != "not_yours" { t.Fatalf("B's coordinator %s on A's task: %+v", name, res) } } // Nothing reached B's runner, and A's tasks are as they were. for _, h := range b.held { if h.f.Type == wire.FrameDispatch || h.f.Type == wire.FrameCancelStep { t.Fatalf("B's runner got %s", h.f.Type) } } if k, _ := a.client.Task(ctx, noted); k.Stage != wire.StageNoted || len(k.Findings) != 0 { t.Fatalf("A's noted task changed: %+v", k) } if k, _ := a.client.Task(ctx, running); k.Stage == wire.StageCancelled || k.Steps[0].Attempt != 1 { t.Fatalf("A's running task changed: %+v", k.Steps) } // A still may. if acc, err := a.client.RetryTask(ctx, running); err != nil || acc.Attempt != 2 { t.Fatalf("A's retry: %+v %v", acc, err) } if _, err := a.client.CancelTask(ctx, noted); err != nil { t.Fatalf("A's cancel: %v", err) } // Someone removed from the team reads nothing. var revision int64 if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", "second@example.com").Scan(&revision); err != nil { t.Fatal(err) } if err := a.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleOwner, false, revision); err == nil { if _, err := b.client.Task(ctx, running); Status(err) != http.StatusUnauthorized && Status(err) != http.StatusForbidden { t.Fatalf("a removed member read a task: %v", err) } if _, err := b.client.Tasks(ctx); Status(err) != http.StatusUnauthorized && Status(err) != http.StatusForbidden { t.Fatalf("a removed member listed tasks: %v", err) } } else { t.Fatalf("remove: %v", err) } }