| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 14 | "amadan.net/oficina/equipo/internal/taskplan" |
| 15 | ) |
| 16 | |
| 17 | func playbookInput(name string) library.PlaybookInput { |
| 18 | return library.PlaybookInput{Name: name, Kind: taskplan.Bug, Steps: taskplan.BugPlan("", 1).Steps} |
| 19 | } |
| 20 | func TestPlaybookMandatorySteps(t *testing.T) { |
| 21 | f := newFixture(t) |
| 22 | for _, ref := range []string{"playbook_bug", "playbook_website", "playbook_infra", "playbook_investigation", "playbook_blank"} { |
| 23 | row, err := f.Store.InstallPlaybookTemplate(ctx, owner, equipo.TemplateRef{ID: ref, Version: 1}, equipo.Mutation{RequestID: ref}) |
| 24 | must(t, err) |
| 25 | for _, action := range []string{"review", "checks", "person"} { |
| 26 | t.Run(ref+"/"+action, func(t *testing.T) { |
| 27 | in := library.PlaybookFields(row) |
| 28 | in.Steps = nil |
| 29 | for _, st := range row.Steps { |
| 30 | if st.Action != action { |
| 31 | in.Steps = append(in.Steps, st) |
| 32 | } |
| 33 | } |
| 34 | current, err := f.Store.Playbook(ctx, owner, row.ID) |
| 35 | must(t, err) |
| 36 | if _, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: ref + action, Revision: current.Revision}); err == nil { |
| 37 | t.Fatal("mandatory tail removed") |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 | in := playbookInput("Not a publishing playbook") |
| 43 | in.Kind = taskplan.Recommendation |
| 44 | if _, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "recommendation"}); err == nil { |
| 45 | t.Fatal("recommendation instantiated publishing playbook") |
| 46 | } |
| 47 | } |
| 48 | func TestPlaybookDefaultPrecedence(t *testing.T) { |
| 49 | f := newFixture(t) |
| 50 | task := f.createTask(t) |
| 51 | kind, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Kind"), equipo.Mutation{RequestID: "kind"}) |
| 52 | must(t, err) |
| 53 | wide, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Project"), equipo.Mutation{RequestID: "wide"}) |
| 54 | must(t, err) |
| 55 | exact, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Exact"), equipo.Mutation{RequestID: "exact"}) |
| 56 | must(t, err) |
| 57 | check := func(want string) { |
| 58 | t.Helper() |
| 59 | got, e := f.Store.ResolvePlaybook(ctx, owner, task.ProjectID, taskplan.Bug) |
| 60 | must(t, e) |
| 61 | if got.ID != want { |
| 62 | t.Fatalf("precedence: got %s want %s", got.ID, want) |
| 63 | } |
| 64 | } |
| 65 | check("template:playbook_bug:1") |
| 66 | must(t, f.Store.SetPlaybookDefault(ctx, owner, 0, taskplan.Bug, kind.ID, equipo.Mutation{RequestID: "kind-default"})) |
| 67 | check(kind.ID) |
| 68 | must(t, f.Store.SetPlaybookDefault(ctx, owner, task.ProjectID, "", wide.ID, equipo.Mutation{RequestID: "wide-default"})) |
| 69 | check(wide.ID) |
| 70 | must(t, f.Store.SetPlaybookDefault(ctx, owner, task.ProjectID, taskplan.Bug, exact.ID, equipo.Mutation{RequestID: "exact-default"})) |
| 71 | check(exact.ID) |
| 72 | if err = f.Store.SetPlaybookDefault(ctx, owner, task.ProjectID, taskplan.Bug, wide.ID, equipo.Mutation{RequestID: "stale"}); !errors.Is(err, equipo.ErrConflict) { |
| 73 | t.Fatal("second default accepted without revision", err) |
| 74 | } |
| 75 | if n := f.count(t, "SELECT count(*) FROM playbook_defaults WHERE project_id=? AND kind='bug'", task.ProjectID); n != 1 { |
| 76 | t.Fatal("duplicate specificity", n) |
| 77 | } |
| 78 | must(t, f.Store.SetPlaybookDefault(ctx, owner, task.ProjectID, taskplan.Bug, "", equipo.Mutation{RequestID: "clear", Revision: 1})) |
| 79 | check(wide.ID) |
| 80 | must(t, f.Store.SetPlaybookDefault(ctx, owner, task.ProjectID, "", "", equipo.Mutation{RequestID: "clear-wide", Revision: 1})) |
| 81 | check(kind.ID) |
| 82 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 83 | if err = f.Store.SetPlaybookDefault(ctx, peer, 0, taskplan.Bug, exact.ID, equipo.Mutation{RequestID: "member", Revision: 1}); !errors.Is(err, equipo.ErrForbidden) { |
| 84 | t.Fatal("member controlled default", err) |
| 85 | } |
| 86 | p, err := f.Store.ProposePlan(ctx, owner, taskplan.Plan{Kind: taskplan.Bug, ProjectID: task.ProjectID}, equipo.Mutation{RequestID: "default-proposal"}) |
| 87 | must(t, err) |
| 88 | if p.PlaybookID != kind.ID { |
| 89 | t.Fatal("proposal ignored default") |
| 90 | } |
| 91 | } |
| 92 | func TestPlaybookSnapshot(t *testing.T) { |
| 93 | f := newFixture(t) |
| 94 | task := f.createTask(t) |
| 95 | row, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Snapshot"), equipo.Mutation{RequestID: "save"}) |
| 96 | must(t, err) |
| 97 | plan, err := f.Store.InstantiatePlaybook(ctx, owner, row.ID, task.ID) |
| 98 | must(t, err) |
| 99 | in := library.PlaybookFields(row) |
| 100 | in.Steps[0].Instructions = "Later instructions" |
| 101 | edited, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "edit", Revision: row.Revision}) |
| 102 | must(t, err) |
| 103 | again, err := f.Store.InstantiatePlaybook(ctx, owner, row.ID, task.ID) |
| 104 | must(t, err) |
| 105 | if !reflect.DeepEqual(plan, again) || again.PlaybookRevision != 1 || edited.Revision != 2 { |
| 106 | t.Fatal("snapshot changed after library edit") |
| 107 | } |
| 108 | p, err := f.Store.TaskPlan(ctx, owner, task.ID) |
| 109 | must(t, err) |
| 110 | plan.Revision = p.Revision |
| 111 | p, err = f.Store.EditPlan(ctx, owner, plan, equipo.Mutation{RequestID: "use", Revision: p.Revision}) |
| 112 | must(t, err) |
| 113 | in.Steps[0].Instructions = "Third version" |
| 114 | _, err = f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "edit-again", Revision: edited.Revision}) |
| 115 | must(t, err) |
| 116 | saved, err := f.Store.TaskPlan(ctx, owner, task.ID) |
| 117 | must(t, err) |
| 118 | if !reflect.DeepEqual(p, saved) { |
| 119 | t.Fatal("task plan rewritten") |
| 120 | } |
| 121 | } |
| 122 | func TestPlaybookSuggestionNeedsPerson(t *testing.T) { |
| 123 | f, x, principal, claim := claimTools(t) |
| 124 | result := toolCall(t, x, principal, claim, "suggest", "suggest_playbook", map[string]any{"source_message_ids": []string{claim.Turn.MessageID}, "draft": map[string]any{"name": "Suggested", "instructions": "Investigate first"}}) |
| 125 | if f.count(t, "SELECT count(*) FROM playbooks") != 0 { |
| 126 | t.Fatal("suggestion saved without person") |
| 127 | } |
| 128 | in := playbookInput("Reviewed suggestion") |
| 129 | if _, err := f.Store.SavePlaybookSuggestion(ctx, peer, result.ResourceID, in, equipo.Mutation{RequestID: "steal", Revision: 1}); err == nil { |
| 130 | t.Fatal("another person saved private suggestion") |
| 131 | } |
| 132 | if _, err := f.Store.SavePlaybookSuggestion(ctx, equipo.Actor{Address: "coordinator:" + claim.Binding.ID}, result.ResourceID, in, equipo.Mutation{RequestID: "model", Revision: 1}); err == nil { |
| 133 | t.Fatal("coordinator saved playbook") |
| 134 | } |
| 135 | row, err := f.Store.SavePlaybookSuggestion(ctx, owner, result.ResourceID, in, equipo.Mutation{RequestID: "accept", Revision: 1}) |
| 136 | must(t, err) |
| 137 | if row.Creator != owner.Address || f.count(t, "SELECT count(*) FROM playbooks") != 1 { |
| 138 | t.Fatal("person save missing") |
| 139 | } |
| 140 | retry, err := f.Store.SavePlaybookSuggestion(ctx, owner, result.ResourceID, in, equipo.Mutation{RequestID: "accept", Revision: 1}) |
| 141 | must(t, err) |
| 142 | if retry.ID != row.ID { |
| 143 | t.Fatal("suggestion retry duplicated playbook") |
| 144 | } |
| 145 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", owner.Address) |
| 146 | if _, err = f.Store.SavePlaybookSuggestion(ctx, owner, result.ResourceID, in, equipo.Mutation{RequestID: "accept", Revision: 1}); !errors.Is(err, equipo.ErrForbidden) { |
| 147 | t.Fatal("removed person replayed save", err) |
| 148 | } |
| 149 | } |
| 150 | func TestPlaybookTemplates(t *testing.T) { |
| 151 | f := newFixture(t) |
| 152 | task := f.createTask(t) |
| 153 | for _, id := range []string{"playbook_bug", "playbook_website", "playbook_infra", "playbook_investigation", "playbook_blank"} { |
| 154 | row, err := f.Store.InstallPlaybookTemplate(ctx, owner, equipo.TemplateRef{ID: id, Version: 1}, equipo.Mutation{RequestID: id}) |
| 155 | must(t, err) |
| 156 | in := library.PlaybookFields(row) |
| 157 | in.Name = "Our version" |
| 158 | row, err = f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: id + "edit", Revision: 1}) |
| 159 | must(t, err) |
| 160 | diff, err := f.Store.PlaybookTemplateDiff(ctx, owner, row.ID, row.Template) |
| 161 | must(t, err) |
| 162 | current, err := f.Store.Playbook(ctx, owner, row.ID) |
| 163 | must(t, err) |
| 164 | if current.Name != "Our version" { |
| 165 | t.Fatal("preview changed installed copy") |
| 166 | } |
| 167 | original := diff.Suggested["name"] |
| 168 | diff.Suggested["name"] = json.RawMessage(`"forged"`) |
| 169 | if _, err = f.Store.ApplyPlaybookTemplate(ctx, owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: id + "forged", Revision: row.Revision}); !errors.Is(err, equipo.ErrConflict) { |
| 170 | t.Fatal("forged template diff accepted", err) |
| 171 | } |
| 172 | diff.Suggested["name"] = original |
| 173 | applied, err := f.Store.ApplyPlaybookTemplate(ctx, owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: id + "apply", Revision: row.Revision}) |
| 174 | must(t, err) |
| 175 | if applied.Name == "Our version" || applied.Revision != 3 { |
| 176 | t.Fatal("explicit template update not applied") |
| 177 | } |
| 178 | if _, err = f.Store.ApplyPlaybookTemplate(ctx, owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: id + "stale", Revision: row.Revision}); !errors.Is(err, equipo.ErrConflict) { |
| 179 | t.Fatal("stale diff accepted", err) |
| 180 | } |
| 181 | if id == "playbook_investigation" { |
| 182 | p, err := f.Store.InstantiatePlaybook(ctx, owner, row.ID, task.ID) |
| 183 | must(t, err) |
| 184 | if taskplan.Validate(p) != nil || len(p.Steps) != 2 || p.Steps[0].Action != "investigate" || p.Steps[1].Action != "person" { |
| 185 | t.Fatal("investigation skipped research/person checkpoint", p) |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | in := playbookInput("Missing specialist") |
| 190 | in.Steps[0].Specialist = "absent" |
| 191 | if _, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "missing"}); err == nil { |
| 192 | t.Fatal("missing specialist accepted") |
| 193 | } |
| 194 | sp, err := f.Store.CreateSpecialist(ctx, owner, specialistInput("Archived"), equipo.Mutation{RequestID: "specialist"}) |
| 195 | must(t, err) |
| 196 | must(t, f.Store.DeleteSpecialist(ctx, owner, sp.Key, equipo.Mutation{RequestID: "archive", Revision: 1})) |
| 197 | in.Steps[0].Specialist = sp.Key |
| 198 | if _, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "archived"}); err == nil { |
| 199 | t.Fatal("archived specialist accepted") |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestPlaybookAuthorityAndRollback(t *testing.T) { |
| 204 | f := newFixture(t) |
| 205 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 206 | in := playbookInput("Member playbook") |
| 207 | row, err := f.Store.SavePlaybook(ctx, peer, in, equipo.Mutation{RequestID: "create"}) |
| 208 | must(t, err) |
| 209 | outsider := equipo.Actor{Address: "other@example.com"} |
| 210 | must(t, f.Store.SetMember(ctx, owner, outsider.Address, equipo.RoleMember, true, 0)) |
| 211 | edit := library.PlaybookFields(row) |
| 212 | edit.Name = "Changed" |
| 213 | if _, err = f.Store.SavePlaybook(ctx, outsider, edit, equipo.Mutation{RequestID: "steal", Revision: 1}); !errors.Is(err, equipo.ErrForbidden) { |
| 214 | t.Fatal("noncreator edited", err) |
| 215 | } |
| 216 | f.exec(t, `CREATE TRIGGER reject_playbook_audit BEFORE INSERT ON audit_events WHEN NEW.kind='playbook.save' BEGIN SELECT RAISE(ABORT,'fixture audit failure'); END`) |
| 217 | if _, err = f.Store.SavePlaybook(ctx, peer, edit, equipo.Mutation{RequestID: "rollback", Revision: 1}); err == nil { |
| 218 | t.Fatal("audit failure ignored") |
| 219 | } |
| 220 | unchanged, err := f.Store.Playbook(ctx, owner, row.ID) |
| 221 | must(t, err) |
| 222 | if unchanged.Revision != 1 || unchanged.Name != row.Name || f.count(t, "SELECT count(*) FROM playbook_revisions WHERE id=?", row.ID) != 1 { |
| 223 | t.Fatal("failed audit partially committed") |
| 224 | } |
| 225 | f.exec(t, "DROP TRIGGER reject_playbook_audit") |
| 226 | edited, err := f.Store.SavePlaybook(ctx, peer, edit, equipo.Mutation{RequestID: "edit", Revision: 1}) |
| 227 | must(t, err) |
| 228 | retry, err := f.Store.SavePlaybook(ctx, peer, edit, equipo.Mutation{RequestID: "edit", Revision: 1}) |
| 229 | must(t, err) |
| 230 | if retry.Revision != edited.Revision { |
| 231 | t.Fatal("edit retry advanced revision") |
| 232 | } |
| 233 | if _, err = f.Store.SavePlaybook(ctx, peer, edit, equipo.Mutation{RequestID: "stale", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { |
| 234 | t.Fatal("stale edit accepted", err) |
| 235 | } |
| 236 | edit.Name = "Changed request" |
| 237 | if _, err = f.Store.SavePlaybook(ctx, peer, edit, equipo.Mutation{RequestID: "edit", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { |
| 238 | t.Fatal("changed retry accepted", err) |
| 239 | } |
| 240 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address) |
| 241 | if _, err = f.Store.SavePlaybook(ctx, peer, in, equipo.Mutation{RequestID: "create"}); !errors.Is(err, equipo.ErrForbidden) { |
| 242 | t.Fatal("revoked replay accepted", err) |
| 243 | } |
| 244 | must(t, f.Store.DeletePlaybook(ctx, owner, row.ID, equipo.Mutation{RequestID: "archive", Revision: 2})) |
| 245 | archived, err := f.Store.Playbook(ctx, owner, row.ID) |
| 246 | must(t, err) |
| 247 | if !archived.Archived { |
| 248 | t.Fatal("archive missing") |
| 249 | } |
| 250 | } |
| 251 | func TestPlaybookHTTP(t *testing.T) { |
| 252 | f := newFixture(t) |
| 253 | for _, path := range []string{"/playbooks", "/playbooks/new"} { |
| 254 | w := f.get(t, owner, path) |
| 255 | if w.Code != 200 { |
| 256 | t.Fatal("page unavailable", path, w.Code) |
| 257 | } |
| 258 | } |
| 259 | row, err := f.Store.SavePlaybook(ctx, owner, playbookInput("<script>bad</script>"), equipo.Mutation{RequestID: "create"}) |
| 260 | must(t, err) |
| 261 | w := f.get(t, owner, "/playbooks/"+row.ID+"/edit") |
| 262 | if w.Code != 200 || strings.Contains(w.Body.String(), "<script>bad</script>") || !strings.Contains(w.Body.String(), "step_0_instructions") { |
| 263 | t.Fatal("form missing or unescaped", w.Code, w.Body.String()) |
| 264 | } |
| 265 | form := url.Values{"request_id": {"bad"}, "revision": {"1"}, "creator": {"someone"}} |
| 266 | req := httptest.NewRequest("POST", "/playbooks/"+row.ID+"/edit", strings.NewReader(form.Encode())) |
| 267 | req.Header.Set("Origin", origin) |
| 268 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 269 | req.AddCookie(f.Cookies[owner.Address]) |
| 270 | rec := httptest.NewRecorder() |
| 271 | f.Handler.ServeHTTP(rec, req) |
| 272 | if rec.Code < 400 { |
| 273 | t.Fatal("submitted authority accepted", rec.Code) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestPlaybookPlanProvenance(t *testing.T) { |
| 278 | f := newFixture(t) |
| 279 | task := f.createTask(t) |
| 280 | row, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Pinned playbook"), equipo.Mutation{RequestID: "create"}) |
| 281 | must(t, err) |
| 282 | p, err := f.Store.ProposePlan(ctx, owner, taskplan.Plan{Kind: taskplan.Bug, ProjectID: task.ProjectID, PlaybookID: row.ID}, equipo.Mutation{RequestID: "propose"}) |
| 283 | must(t, err) |
| 284 | in := library.PlaybookFields(row) |
| 285 | in.Name = "Later library name" |
| 286 | in.Steps[0].Instructions = "Later instructions" |
| 287 | _, err = f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "update", Revision: 1}) |
| 288 | must(t, err) |
| 289 | snap, err := f.Store.InstantiatePlaybook(ctx, owner, row.ID, p.TaskID) |
| 290 | must(t, err) |
| 291 | if snap.PlaybookRevision != 1 || snap.Steps[0].Instructions != "" { |
| 292 | t.Fatal("proposal snapshot replaced by later revision") |
| 293 | } |
| 294 | p.Steps[0].Instructions = "Task-specific change" |
| 295 | p, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "edit-plan", Revision: 1}) |
| 296 | must(t, err) |
| 297 | w := f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 298 | if w.Code != 200 || !strings.Contains(w.Body.String(), "Pinned playbook") || !strings.Contains(w.Body.String(), "data-playbook-change=\"reproduce\"") { |
| 299 | t.Fatal("chosen playbook or task changes missing", w.Code) |
| 300 | } |
| 301 | p.PlaybookRevision = 999 |
| 302 | if _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "forged", Revision: p.Revision}); err == nil { |
| 303 | t.Fatal("forged provenance accepted") |
| 304 | } |
| 305 | } |
| 306 | func TestPlaybookCoordinatorDefault(t *testing.T) { |
| 307 | f, x, principal, claim := claimTools(t) |
| 308 | var project int64 |
| 309 | must(t, f.DB.Writer().QueryRow("SELECT id FROM projects LIMIT 1").Scan(&project)) |
| 310 | row, err := f.Store.SavePlaybook(ctx, owner, playbookInput("Coordinator default"), equipo.Mutation{RequestID: "create"}) |
| 311 | must(t, err) |
| 312 | must(t, f.Store.SetPlaybookDefault(ctx, owner, project, taskplan.Bug, row.ID, equipo.Mutation{RequestID: "default"})) |
| 313 | result := toolCall(t, x, principal, claim, "default", "propose_plan", map[string]any{"message_id": claim.Turn.MessageID, "plan": map[string]any{"kind": "bug", "project_id": project, "steps": []any{}}}) |
| 314 | p, err := f.Store.TaskPlan(ctx, owner, result.ResourceID) |
| 315 | must(t, err) |
| 316 | if p.PlaybookID != row.ID || p.PlaybookRevision != 1 { |
| 317 | t.Fatal("coordinator ignored default") |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func TestPlaybookLegacyBugDefault(t *testing.T) { |
| 322 | f := newFixture(t) |
| 323 | base := f.createTask(t) |
| 324 | in := playbookInput("Bug default") |
| 325 | in.Steps[0].Instructions = "Use this reproduction process" |
| 326 | row, err := f.Store.SavePlaybook(ctx, owner, in, equipo.Mutation{RequestID: "create"}) |
| 327 | must(t, err) |
| 328 | must(t, f.Store.SetPlaybookDefault(ctx, owner, base.ProjectID, taskplan.Bug, row.ID, equipo.Mutation{RequestID: "default"})) |
| 329 | task, err := f.Store.CreateBug(ctx, owner, base.ProjectID, "Another bug", "another") |
| 330 | must(t, err) |
| 331 | p, err := f.Store.TaskPlan(ctx, owner, task.ID) |
| 332 | must(t, err) |
| 333 | if p.PlaybookID != row.ID || p.Steps[0].Instructions != in.Steps[0].Instructions { |
| 334 | t.Fatal("bug creation ignored default") |
| 335 | } |
| 336 | } |
| 337 | |