| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestFinalFixRequesterAuthority(t *testing.T) { |
| 11 | for _, role := range []string{"owner", "member"} { |
| 12 | t.Run(role, func(t *testing.T) { |
| 13 | f := newFixture(t) |
| 14 | p := proposed(t, f) |
| 15 | f.planConnections(t, p.ProjectID) |
| 16 | f.exec(t, "UPDATE members SET role=? WHERE address=?", role, peer.Address) |
| 17 | mode := equipo.ApprovalRequired |
| 18 | must(t, f.Store.SetApprovalOverride(ctx, owner, p.ProjectID, &mode, equipo.Mutation{RequestID: "required"})) |
| 19 | t.Run("approve", func(t *testing.T) { |
| 20 | _, err := f.Store.ApprovePlan(ctx, peer, p.TaskID, equipo.Mutation{RequestID: "foreign-approve", Revision: 1}) |
| 21 | if !errors.Is(err, equipo.ErrForbidden) { |
| 22 | t.Fatal("non-requester approved", err) |
| 23 | } |
| 24 | }) |
| 25 | t.Run("edit", func(t *testing.T) { |
| 26 | q := p |
| 27 | q.Steps[0].Instructions = "foreign instructions" |
| 28 | _, err := f.Store.EditPlan(ctx, peer, q, equipo.Mutation{RequestID: "foreign-edit", Revision: 1}) |
| 29 | if !errors.Is(err, equipo.ErrForbidden) { |
| 30 | t.Fatal("non-requester edited", err) |
| 31 | } |
| 32 | }) |
| 33 | t.Run("correct", func(t *testing.T) { |
| 34 | _, err := f.Store.CorrectTaskProject(ctx, peer, p.TaskID, p.ProjectID, equipo.Mutation{RequestID: "foreign-correct", Revision: 1}) |
| 35 | if !errors.Is(err, equipo.ErrForbidden) { |
| 36 | t.Fatal("non-requester corrected project", err) |
| 37 | } |
| 38 | }) |
| 39 | t.Run("read", func(t *testing.T) { |
| 40 | var thread string |
| 41 | must(t, f.DB.Writer().QueryRow("SELECT id FROM conversation_threads WHERE task_id=?", p.TaskID).Scan(&thread)) |
| 42 | _, err := f.Store.SendMessage(ctx, peer, thread, "Shared discussion", equipo.Mutation{RequestID: "discussion"}) |
| 43 | must(t, err) |
| 44 | w := f.get(t, peer, "/tasks/"+p.TaskID+"/plan") |
| 45 | expectHTTP(t, w, 200) |
| 46 | if strings.Contains(w.Body.String(), `id="plan-editor"`) || strings.Contains(w.Body.String(), `action="/tasks/`+p.TaskID+`/plan/approve"`) { |
| 47 | t.Fatal("foreign execution controls visible") |
| 48 | } |
| 49 | if !strings.Contains(w.Body.String(), `data-requester-mode="required"`) || !strings.Contains(w.Body.String(), `data-project-mode="immediate"`) { |
| 50 | t.Fatal("requester and project approval modes missing") |
| 51 | } |
| 52 | }) |
| 53 | t.Run("http", func(t *testing.T) { |
| 54 | expectHTTP(t, apiRequest(t, f, peer, "POST", "/api/v1/tasks/"+p.TaskID+"/plan/approve", "{}", "api-foreign", `"1"`), 403) |
| 55 | expectHTTP(t, formPost(t, f, peer, "/tasks/"+p.TaskID+"/plan/approve", actionForm("html-foreign", 1)), 403) |
| 56 | }) |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestFinalFixPlanSelections(t *testing.T) { |
| 62 | for _, operation := range []string{"propose", "edit"} { |
| 63 | for _, value := range []string{"foreign", "unknown-model"} { |
| 64 | t.Run(operation+"/"+value, func(t *testing.T) { |
| 65 | f := newFixture(t) |
| 66 | p := proposed(t, f) |
| 67 | f.planConnections(t, p.ProjectID) |
| 68 | if value == "foreign" { |
| 69 | f.exec(t, "UPDATE connections SET person=? WHERE id='claude'", peer.Address) |
| 70 | p.Steps[0].ConnectionID = "claude" |
| 71 | } else { |
| 72 | p.Steps[0].Model = "not-reported" |
| 73 | } |
| 74 | var err error |
| 75 | if operation == "propose" { |
| 76 | p.TaskID = "" |
| 77 | p.Revision = 0 |
| 78 | _, err = f.Store.ProposePlan(ctx, owner, p, equipo.Mutation{RequestID: "bad-pick"}) |
| 79 | } else { |
| 80 | _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "bad-pick", Revision: 1}) |
| 81 | } |
| 82 | if !errors.Is(err, equipo.ErrInvalid) { |
| 83 | t.Fatal("invalid requester selection persisted", err) |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestFinalFixApprovalWait(t *testing.T) { |
| 91 | for _, started := range []bool{false, true} { |
| 92 | t.Run(map[bool]string{false: "proposed", true: "started"}[started], func(t *testing.T) { |
| 93 | f := newFixture(t) |
| 94 | p := proposed(t, f) |
| 95 | f.planConnections(t, p.ProjectID) |
| 96 | x, _, _ := f.planCore(t, p) |
| 97 | if !started { |
| 98 | f.exec(t, "UPDATE tasks SET state='proposed',job_id=0 WHERE id=?", p.TaskID) |
| 99 | } |
| 100 | must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "wait"})) |
| 101 | must(t, x.Sync(ctx)) |
| 102 | task, err := f.Store.Task(ctx, owner, p.TaskID) |
| 103 | must(t, err) |
| 104 | if task.WaitCode != "plan.awaiting_approval" { |
| 105 | t.Fatal("approval wait mislabelled", task.WaitCode) |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | } |
| 110 | |