| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo" |
| 8 | "amadan.net/oficina/equipo/internal/taskplan" |
| 9 | ) |
| 10 | |
| 11 | func TestConversationScriptlessPlanGuards(t *testing.T) { |
| 12 | f := newFixture(t) |
| 13 | p := proposed(t, f) |
| 14 | // Read-only steps may be reordered until claimed. |
| 15 | p.Steps = append([]taskplan.Step{{Key: "inspect", Specialist: "engineering", Action: "investigate", Instructions: "Read current code"}}, p.Steps...) |
| 16 | v := planForm(p, "add") |
| 17 | expectHTTP(t, formPost(t, f, owner, "/tasks/"+p.TaskID+"/plan", v), 303) |
| 18 | p, err := f.Store.TaskPlan(ctx, owner, p.TaskID) |
| 19 | must(t, err) |
| 20 | p.Steps[0], p.Steps[1] = p.Steps[1], p.Steps[0] |
| 21 | expectHTTP(t, formPost(t, f, owner, "/tasks/"+p.TaskID+"/plan", planForm(p, "reorder")), 303) |
| 22 | got, err := f.Store.TaskPlan(ctx, owner, p.TaskID) |
| 23 | must(t, err) |
| 24 | if got.Steps[0].Key != "reproduce" || got.Steps[1].Key != "inspect" { |
| 25 | t.Fatal("unstarted reorder ignored") |
| 26 | } |
| 27 | // The server rejects submitted changes to a claimed step even if readonly is removed. |
| 28 | f2 := newFixture(t) |
| 29 | p = proposed(t, f2) |
| 30 | x, _, scope := f2.planCore(t, p) |
| 31 | _, err = x.Core.Claim(ctx, scope, "reproduce", "runner", "workspace") |
| 32 | must(t, err) |
| 33 | p.Steps[0].Instructions = "forged started instructions" |
| 34 | expectHTTP(t, formPost(t, f2, owner, "/tasks/"+p.TaskID+"/plan", planForm(p, "locked")), 422) |
| 35 | w := f2.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 36 | expectHTTP(t, w, 200) |
| 37 | if !strings.Contains(w.Body.String(), "This step has started") || !strings.Contains(w.Body.String(), "readonly") { |
| 38 | t.Fatal("started step is not visibly locked") |
| 39 | } |
| 40 | } |
| 41 | func TestConversationScriptlessSettings(t *testing.T) { |
| 42 | f, x, _, b := coordinatorFixture(t) |
| 43 | _ = x |
| 44 | liveFixture(t, f) |
| 45 | w := f.get(t, owner, "/settings/coordinator") |
| 46 | expectHTTP(t, w, 200) |
| 47 | if !strings.Contains(w.Body.String(), `<p>claude</p>`) || !strings.Contains(w.Body.String(), `name="calls" value="24"`) { |
| 48 | t.Fatal("coordinator settings missing", w.Body.String()) |
| 49 | } |
| 50 | v := actionForm("binding", b.Revision) |
| 51 | v.Set("action", "bind") |
| 52 | v.Set("connection_id", "codex") |
| 53 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 422) |
| 54 | got, err := f.Store.Coordinator(ctx, owner) |
| 55 | must(t, err) |
| 56 | if got.ConnectionID != "claude" { |
| 57 | t.Fatal("manual binding changed automatic coordinator") |
| 58 | } |
| 59 | v = actionForm("reset", got.Revision) |
| 60 | v.Set("action", "reset") |
| 61 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 303) |
| 62 | v = actionForm("limits-ui", 1) |
| 63 | v.Set("action", "limits") |
| 64 | v.Set("calls", "12") |
| 65 | v.Set("input_tokens", "100000") |
| 66 | v.Set("output_tokens", "20000") |
| 67 | v.Set("seconds", "5000") |
| 68 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 303) |
| 69 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 303) |
| 70 | v.Set("request_id", "stale-limit") |
| 71 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 409) |
| 72 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 73 | v.Set("revision", "0") |
| 74 | expectHTTP(t, formPost(t, f, peer, "/settings/coordinator", v), 403) |
| 75 | w = f.get(t, peer, "/settings/coordinator") |
| 76 | expectHTTP(t, w, 200) |
| 77 | if !strings.Contains(w.Body.String(), "Ask a team owner to set coordinator limits.") || strings.Contains(w.Body.String(), `name="calls"`) { |
| 78 | t.Fatal("missing budget setup or owner gate") |
| 79 | } |
| 80 | settings, err := f.Store.PersonalSettings(ctx, owner) |
| 81 | must(t, err) |
| 82 | v = actionForm("landing", settings.Revision) |
| 83 | v.Set("action", "landing") |
| 84 | v.Set("landing", "work") |
| 85 | expectHTTP(t, formPost(t, f, owner, "/settings/coordinator", v), 303) |
| 86 | if w = f.get(t, owner, "/"); w.Header().Get("Location") != "/work" { |
| 87 | t.Fatal("landing setting ignored") |
| 88 | } |
| 89 | var task equipo.Task |
| 90 | must(t, f.DB.G.First(&task).Error) |
| 91 | path := "/tasks/" + task.ID + "/plan/settings" |
| 92 | v = actionForm("project-approval", 0) |
| 93 | v.Set("scope", "project") |
| 94 | v.Set("mode", "required") |
| 95 | expectHTTP(t, formPost(t, f, owner, path, v), 303) |
| 96 | v = actionForm("personal-approval", 0) |
| 97 | v.Set("scope", "personal") |
| 98 | v.Set("mode", "immediate") |
| 99 | expectHTTP(t, formPost(t, f, peer, path, v), 303) |
| 100 | mode, err := f.Store.PlanApproval(ctx, peer, task.ProjectID) |
| 101 | must(t, err) |
| 102 | if mode != equipo.ApprovalImmediate { |
| 103 | t.Fatal("personal override ignored") |
| 104 | } |
| 105 | v = actionForm("inherit", 1) |
| 106 | v.Set("scope", "personal") |
| 107 | v.Set("mode", "") |
| 108 | expectHTTP(t, formPost(t, f, peer, path, v), 303) |
| 109 | mode, err = f.Store.PlanApproval(ctx, peer, task.ProjectID) |
| 110 | must(t, err) |
| 111 | if mode != equipo.ApprovalRequired { |
| 112 | t.Fatal("inherit ignored") |
| 113 | } |
| 114 | v = actionForm("owner-only", 1) |
| 115 | v.Set("scope", "project") |
| 116 | v.Set("mode", "immediate") |
| 117 | expectHTTP(t, formPost(t, f, peer, path, v), 403) |
| 118 | } |
| 119 | |
| 120 | func TestConversationLiveQuestionExpiry(t *testing.T) { |
| 121 | f, x, principal, c := claimTools(t) |
| 122 | liveFixture(t, f) |
| 123 | n := &conversationNotifier{} |
| 124 | f.Store.SetJobNotifier(n) |
| 125 | toolCall(t, x, principal, c, "expiry", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "Expires later", "options": []any{}}) |
| 126 | if len(n.deadlines) == 0 { |
| 127 | t.Fatal("clarification expiry has no scheduled invalidation") |
| 128 | } |
| 129 | for _, topic := range n.deadlines { |
| 130 | if !strings.HasPrefix(topic, "conversation/") { |
| 131 | t.Fatal("question deadline lacks private topic") |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestConversationScriptlessMissingPlan(t *testing.T) { |
| 137 | f := newFixture(t) |
| 138 | expectHTTP(t, f.get(t, owner, "/tasks/absent/plan"), 404) |
| 139 | } |
| 140 | |
| 141 | func TestConversationScriptlessConnectionChoices(t *testing.T) { |
| 142 | f := newFixture(t) |
| 143 | p := proposed(t, f) |
| 144 | f.planConnections(t, p.ProjectID) |
| 145 | w := f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 146 | expectHTTP(t, w, 200) |
| 147 | if !strings.Contains(w.Body.String(), `select rst-input name="step_0_connection"`) || !strings.Contains(w.Body.String(), "claude / fixture-model") { |
| 148 | t.Fatal("plan asks for an opaque connection identifier") |
| 149 | } |
| 150 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["codex"]' WHERE person=?`, owner.Address) |
| 151 | w = f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 152 | expectHTTP(t, w, 200) |
| 153 | if strings.Contains(w.Body.String(), `option value="claude"`) { |
| 154 | t.Fatal("plan offered an excluded connection") |
| 155 | } |
| 156 | } |
| 157 | |