package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/library" "amadan.net/oficina/equipo/internal/equipo/personapi" "amadan.net/oficina/equipo/internal/taskplan" "amadan.net/rastrillo/rastrillo/mail" "encoding/json" "fmt" "io" "log/slog" "net/http/httptest" "net/url" "strconv" "strings" "testing" ) func apiRequest(t *testing.T, f *fixture, actor equipo.Actor, method, path, body, key, revision string) *httptest.ResponseRecorder { t.Helper() r := httptest.NewRequest(method, path, strings.NewReader(body)) if actor.Address != "" { r.AddCookie(f.Cookies[actor.Address]) } r.Header.Set("Origin", origin) r.Header.Set("Content-Type", "application/json") if key != "" { r.Header.Set("Idempotency-Key", key) } if revision != "" { r.Header.Set("If-Match", revision) } w := httptest.NewRecorder() f.Handler.ServeHTTP(w, r) return w } func apiValue[T any](t *testing.T, w *httptest.ResponseRecorder) T { t.Helper() var out T must(t, json.Unmarshal(w.Body.Bytes(), &out)) return out } func apiRouter(t *testing.T, f *fixture, verifier personapi.VerifyPersonBearer) { t.Helper() if verifier != nil { equipo.WithPersonBearerVerifier(verifier)(f.Store) } logger := slog.New(slog.NewTextHandler(io.Discard, nil)) r, err := equipo.RouterWithStore(f.DB, equipo.Config{Origin: origin, InstanceKey: "plan3-fixture", AllowLoggedMail: true, Owners: []string{owner.Address}}, mail.Logged(logger), logger, f.Store) must(t, err) f.Handler = equipo.Mux(r) } func formPost(t *testing.T, f *fixture, actor equipo.Actor, path string, v url.Values) *httptest.ResponseRecorder { t.Helper() r := httptest.NewRequest("POST", path, strings.NewReader(v.Encode())) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("Origin", origin) r.AddCookie(f.Cookies[actor.Address]) w := httptest.NewRecorder() f.Handler.ServeHTTP(w, r) return w } func actionForm(id string, revision int64) url.Values { return url.Values{"request_id": {id}, "revision": {fmt.Sprint(revision)}} } func planForm(p taskplan.Plan, id string) url.Values { v := actionForm(id, p.Revision) v.Set("kind", string(p.Kind)) v.Set("project_id", fmt.Sprint(p.ProjectID)) v.Set("playbook_id", p.PlaybookID) v.Set("playbook_revision", fmt.Sprint(p.PlaybookRevision)) v.Set("step_count", fmt.Sprint(len(p.Steps))) for i, s := range p.Steps { prefix := fmt.Sprintf("step_%d_", i) for k, value := range map[string]string{"key": s.Key, "specialist": s.Specialist, "action": s.Action, "instructions": s.Instructions, "connection": s.ConnectionID, "model": s.Model, "reason": s.SelectionReason, "depends_on": strings.Join(s.DependsOn, ","), "position": fmt.Sprint(i + 1)} { v.Set(prefix+k, value) } } return v } func proposed(t *testing.T, f *fixture) taskplan.Plan { task := f.createTask(t) p := taskplan.BugPlan(task.ID, task.ProjectID) p.Steps[0].Instructions = "fixture symptom" p.TaskID = "" p.Revision = 0 result, err := f.Store.ProposePlan(ctx, owner, p, equipo.Mutation{RequestID: "proposal"}) must(t, err) if result.TaskID == "" || result.Revision != 1 { t.Fatal("proposal lacks server identity", result) } replay, err := f.Store.ProposePlan(ctx, owner, p, equipo.Mutation{RequestID: "proposal"}) must(t, err) if replay.TaskID != result.TaskID { t.Fatal("proposal replay duplicated task") } if f.count(t, "SELECT count(*) FROM conversation_threads WHERE task_id=?", result.TaskID) != 1 || f.count(t, "SELECT count(*) FROM conversation_cards WHERE resource_id=?", result.TaskID) != 1 || f.count(t, "SELECT count(*) FROM task_outboxes WHERE task_id=?", result.TaskID) != 1 { t.Fatal("proposal incomplete") } page, err := f.Store.Conversation(ctx, owner, "", 50) must(t, err) if len(page.Cards) != 1 || page.Cards[0].ResourceID != result.TaskID { t.Fatal("proposal card is not visible in conversation") } return result } func playbookForm(in library.PlaybookInput, request string, revision int64) url.Values { v := actionForm(request, revision) v.Set("name", in.Name) v.Set("kind", string(in.Kind)) v.Set("template_id", in.Template.ID) v.Set("template_version", strconv.FormatInt(in.Template.Version, 10)) v.Set("step_count", strconv.Itoa(len(in.Steps))) for i, st := range in.Steps { prefix := "step_" + strconv.Itoa(i) + "_" for key, value := range map[string]string{"key": st.Key, "specialist": st.Specialist, "action": st.Action, "instructions": st.Instructions, "position": strconv.Itoa(i + 1), "depends": strings.Join(st.DependsOn, ",")} { v.Set(prefix+key, value) } } for _, id := range in.ProjectIDs { v.Add("project", strconv.FormatInt(id, 10)) } return v } func send(t *testing.T, f *fixture, a equipo.Actor, thread, body, key string) equipo.Message { t.Helper() m, e := f.Store.SendMessage(ctx, a, thread, body, equipo.Mutation{RequestID: key}) must(t, e) return m }