oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package plan3test
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo"
5 "amadan.net/oficina/equipo/internal/equipo/library"
6 "amadan.net/oficina/equipo/internal/equipo/personapi"
7 "amadan.net/oficina/equipo/internal/taskplan"
8 "amadan.net/rastrillo/rastrillo/mail"
9 "encoding/json"
10 "fmt"
11 "io"
12 "log/slog"
13 "net/http/httptest"
14 "net/url"
15 "strconv"
16 "strings"
17 "testing"
18)
19
20func apiRequest(t *testing.T, f *fixture, actor equipo.Actor, method, path, body, key, revision string) *httptest.ResponseRecorder {
21 t.Helper()
22 r := httptest.NewRequest(method, path, strings.NewReader(body))
23 if actor.Address != "" {
24 r.AddCookie(f.Cookies[actor.Address])
25 }
26 r.Header.Set("Origin", origin)
27 r.Header.Set("Content-Type", "application/json")
28 if key != "" {
29 r.Header.Set("Idempotency-Key", key)
30 }
31 if revision != "" {
32 r.Header.Set("If-Match", revision)
33 }
34 w := httptest.NewRecorder()
35 f.Handler.ServeHTTP(w, r)
36 return w
37}
38func apiValue[T any](t *testing.T, w *httptest.ResponseRecorder) T {
39 t.Helper()
40 var out T
41 must(t, json.Unmarshal(w.Body.Bytes(), &out))
42 return out
43}
44func apiRouter(t *testing.T, f *fixture, verifier personapi.VerifyPersonBearer) {
45 t.Helper()
46 if verifier != nil {
47 equipo.WithPersonBearerVerifier(verifier)(f.Store)
48 }
49 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
50 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)
51 must(t, err)
52 f.Handler = equipo.Mux(r)
53}
54func formPost(t *testing.T, f *fixture, actor equipo.Actor, path string, v url.Values) *httptest.ResponseRecorder {
55 t.Helper()
56 r := httptest.NewRequest("POST", path, strings.NewReader(v.Encode()))
57 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
58 r.Header.Set("Origin", origin)
59 r.AddCookie(f.Cookies[actor.Address])
60 w := httptest.NewRecorder()
61 f.Handler.ServeHTTP(w, r)
62 return w
63}
64func actionForm(id string, revision int64) url.Values {
65 return url.Values{"request_id": {id}, "revision": {fmt.Sprint(revision)}}
66}
67func planForm(p taskplan.Plan, id string) url.Values {
68 v := actionForm(id, p.Revision)
69 v.Set("kind", string(p.Kind))
70 v.Set("project_id", fmt.Sprint(p.ProjectID))
71 v.Set("playbook_id", p.PlaybookID)
72 v.Set("playbook_revision", fmt.Sprint(p.PlaybookRevision))
73 v.Set("step_count", fmt.Sprint(len(p.Steps)))
74 for i, s := range p.Steps {
75 prefix := fmt.Sprintf("step_%d_", i)
76 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)} {
77 v.Set(prefix+k, value)
78 }
79 }
80 return v
81}
82func proposed(t *testing.T, f *fixture) taskplan.Plan {
83 task := f.createTask(t)
84 p := taskplan.BugPlan(task.ID, task.ProjectID)
85 p.Steps[0].Instructions = "fixture symptom"
86 p.TaskID = ""
87 p.Revision = 0
88 result, err := f.Store.ProposePlan(ctx, owner, p, equipo.Mutation{RequestID: "proposal"})
89 must(t, err)
90 if result.TaskID == "" || result.Revision != 1 {
91 t.Fatal("proposal lacks server identity", result)
92 }
93 replay, err := f.Store.ProposePlan(ctx, owner, p, equipo.Mutation{RequestID: "proposal"})
94 must(t, err)
95 if replay.TaskID != result.TaskID {
96 t.Fatal("proposal replay duplicated task")
97 }
98 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 {
99 t.Fatal("proposal incomplete")
100 }
101 page, err := f.Store.Conversation(ctx, owner, "", 50)
102 must(t, err)
103 if len(page.Cards) != 1 || page.Cards[0].ResourceID != result.TaskID {
104 t.Fatal("proposal card is not visible in conversation")
105 }
106 return result
107}
108func playbookForm(in library.PlaybookInput, request string, revision int64) url.Values {
109 v := actionForm(request, revision)
110 v.Set("name", in.Name)
111 v.Set("kind", string(in.Kind))
112 v.Set("template_id", in.Template.ID)
113 v.Set("template_version", strconv.FormatInt(in.Template.Version, 10))
114 v.Set("step_count", strconv.Itoa(len(in.Steps)))
115 for i, st := range in.Steps {
116 prefix := "step_" + strconv.Itoa(i) + "_"
117 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, ",")} {
118 v.Set(prefix+key, value)
119 }
120 }
121 for _, id := range in.ProjectIDs {
122 v.Add("project", strconv.FormatInt(id, 10))
123 }
124 return v
125}
126
127func send(t *testing.T, f *fixture, a equipo.Actor, thread, body, key string) equipo.Message {
128 t.Helper()
129 m, e := f.Store.SendMessage(ctx, a, thread, body, equipo.Mutation{RequestID: key})
130 must(t, e)
131 return m
132}
133