| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // personToolRig is a second team member with their own device, runner |
| 13 | // and running turn on the same instance as a. |
| 14 | func personToolRig(t *testing.T, a *toolRig, person string, role equipo.Role) *toolRig { |
| 15 | t.Helper() |
| 16 | return personToolRigAt(t, a, person, role, wire.Protocol1) |
| 17 | } |
| 18 | |
| 19 | // personToolRigAt is personToolRig with a runner speaking protocol. |
| 20 | func personToolRigAt(t *testing.T, a *toolRig, person string, role equipo.Role, protocol int) *toolRig { |
| 21 | t.Helper() |
| 22 | ctx := a.ctx |
| 23 | if err := a.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, person, role, true, 0); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | r := &rig{t: t, ctx: ctx, inst: a.inst, client: NewClient(a.inst.URL, a.inst.DeviceTokenFor(t, person))} |
| 27 | if role == equipo.RoleOwner { |
| 28 | r.runner = a.inst.EnrolRunnerFor(t, person).Token |
| 29 | } |
| 30 | snap, err := r.client.Snapshot(ctx) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | r.conv = NewConversation(snap) |
| 35 | r.follow() |
| 36 | if role != equipo.RoleOwner { |
| 37 | return &toolRig{rig: r} |
| 38 | } |
| 39 | var runner *FakeRunner |
| 40 | if protocol <= wire.Protocol1 { |
| 41 | runner = r.dial(0) |
| 42 | } else { |
| 43 | f, err := DialRunnerHello(ctx, a.inst.URL, r.runner, primaryAt(wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, protocol)) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | t.Cleanup(f.Close) |
| 48 | runner = f |
| 49 | } |
| 50 | 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{}} |
| 51 | if protocol > wire.Protocol1 { |
| 52 | hello = primaryAt(hello, protocol) |
| 53 | } |
| 54 | if err := runner.Send(ctx, "h1", wire.FrameHello, hello); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | r.wait("second runner online", func(c *Conversation) bool { return c.Runner.Online }) |
| 58 | sent, err := r.client.Send(ctx, "hello", "k-second") |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | f, _, err := runner.Expect(ctx, wire.FrameMessage) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if err = runner.Send(ctx, "a-msg", wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | r.wait("second turn running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning }) |
| 70 | return &toolRig{rig: r, runner: runner, turn: sent.Turn.ID} |
| 71 | } |
| 72 | |
| 73 | // TestContractTaskAuthority: tasks are shared within the team (another |
| 74 | // member reads them and gets their events), but only the task's creator |
| 75 | // may start, retry or cancel it, onto their own runner; another |
| 76 | // person's coordinator may read a task and never act on it. Someone no |
| 77 | // longer on the team sees nothing. |
| 78 | func TestContractTaskAuthority(t *testing.T) { |
| 79 | a := newToolRig(t) |
| 80 | ctx := a.ctx |
| 81 | b := personToolRig(t, a, "second@example.com", equipo.RoleOwner) |
| 82 | running := taskID(t, a.call("a1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "A's running task", "brief": "x"})) |
| 83 | a.call("a2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) |
| 84 | _, body := a.expect(wire.FrameDispatch) |
| 85 | d := body.(*wire.DispatchCommand) |
| 86 | a.step("r-a1", wire.StepReport{TaskID: running, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: "1111111111111111111111111111111111111111"}) |
| 87 | a.step("r-a2", wire.StepReport{TaskID: running, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 88 | noted := taskID(t, a.call("a3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "A's noted task", "brief": "y"})) |
| 89 | |
| 90 | // Shared: B reads both, with their events. |
| 91 | b.wait("B sees A's tasks", func(c *Conversation) bool { return len(c.Tasks) == 2 }) |
| 92 | if k, err := b.client.Task(ctx, running); err != nil || k.Creator != Owner { |
| 93 | t.Fatalf("B reads A's task: %+v %v", k, err) |
| 94 | } |
| 95 | if _, err := b.client.Tasks(ctx); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | // Not shared: B may not act on A's tasks through the API. |
| 99 | if _, err := b.client.RetryTask(ctx, running); Status(err) != http.StatusForbidden { |
| 100 | t.Fatalf("B retried A's task: %v", err) |
| 101 | } |
| 102 | if _, err := b.client.CancelTask(ctx, running); Status(err) != http.StatusForbidden { |
| 103 | t.Fatalf("B cancelled A's task: %v", err) |
| 104 | } |
| 105 | if _, err := b.client.CancelTask(ctx, noted); Status(err) != http.StatusForbidden { |
| 106 | t.Fatalf("B cancelled A's noted task: %v", err) |
| 107 | } |
| 108 | // Nor through their coordinator. |
| 109 | if res := b.call("b1", wire.ToolReadTask, map[string]string{"task": running}); res.IsError { |
| 110 | t.Fatalf("B's coordinator could not read A's task: %+v", res) |
| 111 | } |
| 112 | for name, args := range map[string]map[string]any{ |
| 113 | wire.ToolStartTask: {"task": noted, "brief": "mine now"}, |
| 114 | wire.ToolCancelTask: {"task": running}, |
| 115 | wire.ToolAddFinding: {"task": noted, "text": "x", "repo": "oficina/sample", "path": "a.go", "line": 1}, |
| 116 | } { |
| 117 | if res := b.call("b-"+name, name, args); !res.IsError || res.Code != "not_yours" { |
| 118 | t.Fatalf("B's coordinator %s on A's task: %+v", name, res) |
| 119 | } |
| 120 | } |
| 121 | // Nothing reached B's runner, and A's tasks are as they were. |
| 122 | for _, h := range b.held { |
| 123 | if h.f.Type == wire.FrameDispatch || h.f.Type == wire.FrameCancelStep { |
| 124 | t.Fatalf("B's runner got %s", h.f.Type) |
| 125 | } |
| 126 | } |
| 127 | if k, _ := a.client.Task(ctx, noted); k.Stage != wire.StageNoted || len(k.Findings) != 0 { |
| 128 | t.Fatalf("A's noted task changed: %+v", k) |
| 129 | } |
| 130 | if k, _ := a.client.Task(ctx, running); k.Stage == wire.StageCancelled || k.Steps[0].Attempt != 1 { |
| 131 | t.Fatalf("A's running task changed: %+v", k.Steps) |
| 132 | } |
| 133 | // A still may. |
| 134 | if acc, err := a.client.RetryTask(ctx, running); err != nil || acc.Attempt != 2 { |
| 135 | t.Fatalf("A's retry: %+v %v", acc, err) |
| 136 | } |
| 137 | if _, err := a.client.CancelTask(ctx, noted); err != nil { |
| 138 | t.Fatalf("A's cancel: %v", err) |
| 139 | } |
| 140 | // Someone removed from the team reads nothing. |
| 141 | var revision int64 |
| 142 | if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", "second@example.com").Scan(&revision); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | if err := a.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleOwner, false, revision); err == nil { |
| 146 | if _, err := b.client.Task(ctx, running); Status(err) != http.StatusUnauthorized && Status(err) != http.StatusForbidden { |
| 147 | t.Fatalf("a removed member read a task: %v", err) |
| 148 | } |
| 149 | if _, err := b.client.Tasks(ctx); Status(err) != http.StatusUnauthorized && Status(err) != http.StatusForbidden { |
| 150 | t.Fatalf("a removed member listed tasks: %v", err) |
| 151 | } |
| 152 | } else { |
| 153 | t.Fatalf("remove: %v", err) |
| 154 | } |
| 155 | } |
| 156 | |