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/personapi"
6 "encoding/json"
7 "fmt"
8 "net/url"
9 "os"
10 "strings"
11 "testing"
12)
13
14func TestPersonAPISharedOperations(t *testing.T) {
15 f := newFixture(t)
16 body := `{"name":"Parity QA","icon":"check-circle","instructions":"Check the supplied evidence.","max_concurrent":1}`
17 form := url.Values{"name": {"Parity QA"}, "icon": {"check-circle"}, "instructions": {"Check the supplied evidence."}, "max_concurrent": {"1"}, "request_id": {"shared-specialist"}}
18 expectHTTP(t, formPost(t, f, owner, "/specialists", form), 303)
19 w := apiRequest(t, f, owner, "POST", "/api/v1/specialists", body, "shared-specialist", "")
20 expectHTTP(t, w, 200)
21 sp := apiValue[personapi.Specialist](t, w)
22 if sp.ID == "" || sp.Revision != 1 {
23 t.Fatal("missing shared specialist result", sp)
24 }
25 if f.count(t, "SELECT count(*) FROM specialists WHERE name='Parity QA'") != 1 {
26 t.Fatal("HTML/API duplicated specialist")
27 }
28 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/specialists/"+sp.ID, body, "edit", `"1"`), 200)
29 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/specialists/"+sp.ID, body, "stale", `"1"`), 409)
30 expectHTTP(t, apiRequest(t, f, owner, "DELETE", "/api/v1/specialists/engineering", `{}`, "required", `"1"`), 403)
31 w = apiRequest(t, f, owner, "POST", "/api/v1/templates/qa/install", `{"version":1}`, "install", "")
32 expectHTTP(t, w, 200)
33 installed := apiValue[personapi.Specialist](t, w)
34 diff := apiRequest(t, f, owner, "GET", "/api/v1/specialists/"+installed.ID+"/template-diff", "", "", "")
35 expectHTTP(t, diff, 200)
36 expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/specialists/"+installed.ID+"/template-update", jsonBody(t, personapi.TemplateUpdateInput{Diff: apiValue[personapi.TemplateDiff](t, diff), Fields: []string{"name"}}), "apply", `"1"`), 200)
37 w = apiRequest(t, f, owner, "POST", "/api/v1/templates/playbook_bug/install", `{"version":1}`, "playbook", "")
38 expectHTTP(t, w, 200)
39 pb := apiValue[personapi.Playbook](t, w)
40 pb.Name = "Shared bug"
41 expectHTTP(t, formPost(t, f, owner, "/playbook-templates/playbook_bug/install", url.Values{"request_id": {"playbook"}, "version": {"1"}}), 303)
42 w = apiRequest(t, f, owner, "PATCH", "/api/v1/playbooks/"+pb.ID, jsonBody(t, pb.PlaybookInput), "playbook-edit", `"1"`)
43 expectHTTP(t, w, 200)
44 if apiValue[personapi.Playbook](t, w).Revision != 2 {
45 t.Fatal("playbook parity failed")
46 }
47 account := apiRequest(t, f, owner, "POST", "/api/v1/accounts", `{"label":"Manual account"}`, "account", "")
48 expectHTTP(t, account, 200)
49 again := apiRequest(t, f, owner, "POST", "/api/v1/accounts", `{"label":"Manual account"}`, "account", "")
50 expectHTTP(t, again, 200)
51 if account.Body.String() != again.Body.String() {
52 t.Fatal("account replay differs")
53 }
54 var accountID int64
55 must(t, f.DB.Writer().QueryRow("SELECT id FROM accounts WHERE label='Manual account'").Scan(&accountID))
56 project := personapi.ProjectFields{AccountID: accountID, Name: "Parity", Repository: "amadan.net/test/parity", DefaultBranch: "main", Kind: "repository", Owners: []personapi.DecisionOwner{{Decision: "work", Address: owner.Address}, {Decision: "merge", Address: owner.Address}}}
57 // Thin v1 replaced POST /api/v1/projects (projects by repository from
58 // the runner's allowlist); the old project model is created by its form.
59 expectHTTP(t, formPost(t, f, owner, "/projects", url.Values{"request_id": {"project"}, "account_id": {fmt.Sprint(accountID)}, "name": {"Parity"}, "repository": {"amadan.net/test/parity"}, "default_branch": {"main"}, "kind": {"repository"}, "owner_work": {owner.Address}, "owner_merge": {owner.Address}}), 303)
60 var pj personapi.Project
61 must(t, f.DB.Writer().QueryRow("SELECT id FROM projects WHERE name='Parity'").Scan(&pj.ID))
62 expectHTTP(t, formPost(t, f, owner, "/projects/"+pj.ID, url.Values{"revision": {"1"}, "request_id": {"project-edit"}, "account_id": {fmt.Sprint(accountID)}, "name": {"Parity"}, "repository": {"amadan.net/test/parity"}, "default_branch": {"main"}, "kind": {"repository"}, "owner_work": {owner.Address}, "owner_merge": {owner.Address}}), 303)
63 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/projects/"+pj.ID, jsonBody(t, project), "project-edit", `"1"`), 200)
64 if f.count(t, "SELECT count(*) FROM audit_events WHERE kind='project.save' AND record_id=?", pj.ID) != 2 {
65 t.Fatal("project replay duplicated audit")
66 }
67
68 settings := apiRequest(t, f, owner, "GET", "/api/v1/projects/"+pj.ID+"/settings", "", "", "")
69 expectHTTP(t, settings, 200)
70 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/projects/"+pj.ID+"/settings", `{"action":"approval","approval_mode":"required"}`, "approval", settings.Header().Get("ETag")), 200)
71 settings = apiRequest(t, f, owner, "GET", "/api/v1/projects/"+pj.ID+"/settings", "", "", "")
72 expectHTTP(t, settings, 200)
73 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/projects/"+pj.ID+"/settings", `{"action":"rules","rules":"Use the team's terminology."}`, "rules", settings.Header().Get("ETag")), 200)
74 f.planConnections(t, 1)
75 var connection string
76 must(t, f.DB.Writer().QueryRow("SELECT id FROM connections WHERE person=? ORDER BY id LIMIT 1", owner.Address).Scan(&connection))
77 w = apiRequest(t, f, owner, "GET", "/api/v1/connections/"+connection, "", "", "")
78 expectHTTP(t, w, 200)
79 c := apiValue[personapi.Connection](t, w)
80 expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1/connections/"+connection, "", "", ""), 404)
81 expectHTTP(t, formPost(t, f, owner, "/projects/"+pj.ID+"/connections", url.Values{"revision": {"1"}, "mode": {"best_fit"}, "connection_id": {connection}}), 303)
82 expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/projects/"+pj.ID+"/connections", jsonBody(t, personapi.ConnectionChoiceInput{ConnectionIDs: []string{connection}, Mode: "best_fit"}), "choices", `"2"`), 200)
83
84 expectHTTP(t, apiRequest(t, f, owner, "PATCH", "/api/v1/connections/"+connection, `{"label":"My account"}`, "label", fmt.Sprintf(`"%d"`, c.Revision)), 200)
85 expectHTTP(t, apiRequest(t, f, owner, "DELETE", "/api/v1/connections/"+connection, `{}`, "revoke-connection", fmt.Sprintf(`"%d"`, c.Revision+1)), 200)
86 if f.count(t, "SELECT count(*) FROM connection_invalidations WHERE connection_id=? AND reason='revoked'", connection) != 1 {
87 t.Fatal("revocation did not request fencing")
88 }
89 expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/onboarding/advance", `{"step":"welcome"}`, "welcome", `"1"`), 200)
90 expectHTTP(t, formPost(t, f, owner, "/onboarding/advance", url.Values{"step": {"welcome"}, "request_id": {"welcome"}, "revision": {"1"}}), 303)
91 accountRunner(t, f)
92 w = apiRequest(t, f, owner, "POST", "/api/v1/account-setups", `{"runner_id":"account-runner","provider":"codex","label":"Personal"}`, "setup", "")
93 expectHTTP(t, w, 200)
94 fields := apiValue[map[string]any](t, w)
95 if len(fields) != 4 {
96 t.Fatal("account setup exposes more than ID, state, mode, command", fields)
97 }
98 setupID := fields["id"].(string)
99 expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1/account-setups/"+setupID, "", "", ""), 404)
100 expectHTTP(t, apiRequest(t, f, owner, "DELETE", "/api/v1/account-setups/"+setupID, `{}`, "cancel", w.Header().Get("ETag")), 200)
101}
102func TestPersonAPISharedPendingInvitation(t *testing.T) {
103 f := newFixture(t)
104 recipient := equipo.Actor{Address: "pending@example.com"}
105 in, secret := issueInvite(t, f, recipient.Address)
106 inviteSession(t, f, recipient)
107 expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/invites/"+in.ID, "", "", ""), 200)
108 expectHTTP(t, apiRequest(t, f, recipient, "GET", "/api/v1/invites/"+in.ID, "", "", ""), 200)
109 for _, path := range []string{"/people", "/tasks", "/conversation", "/accounts", "/runners", "/teams"} {
110 expectHTTP(t, apiRequest(t, f, recipient, "GET", "/api/v1"+path, "", "", ""), 403)
111 }
112 expectHTTP(t, apiRequest(t, f, recipient, "POST", "/api/v1/invites/"+in.ID+"/accept", jsonBody(t, personapi.AcceptInviteInput{Secret: secret}), "accept", `"1"`), 200)
113 expectHTTP(t, apiRequest(t, f, recipient, "POST", "/api/v1/invites/"+in.ID+"/accept", jsonBody(t, personapi.AcceptInviteInput{Secret: secret}), "accept", `"1"`), 200)
114 expectHTTP(t, apiRequest(t, f, recipient, "GET", "/api/v1/people", "", "", ""), 200)
115}
116func TestPersonAPINativeAnswers(t *testing.T) {
117 f, _, _, _, q := nativeConversationFixture(t)
118 apiRouter(t, f, nil)
119 cred := approvedDevice(t, f)
120 path := "/api/v1/questions/" + q.ID + "/answer"
121 body := `{"kind":"native","behavior":"allow","updated_input":{"answers":{"Which?":"Blue"}}}`
122 expectHTTP(t, deviceRequest(t, f, "notification-action", "POST", path, body, "unauth", `"1"`), 401)
123 expectHTTP(t, deviceRequest(t, f, cred.Token, "POST", path, body, "stale", `"2"`), 409)
124 expectHTTP(t, deviceRequest(t, f, cred.Token, "POST", path, body, "answer", `"1"`), 200)
125 f.exec(t, "UPDATE questions SET state='invalid' WHERE id=?", q.ID)
126 expectHTTP(t, deviceRequest(t, f, cred.Token, "POST", path, body, "cancelled", `"1"`), 409)
127 f.exec(t, "UPDATE members SET role='member' WHERE address=?", owner.Address)
128 f.exec(t, "UPDATE tasks SET requester=? WHERE id=?", peer.Address, q.TaskID)
129 expectHTTP(t, deviceRequest(t, f, cred.Token, "POST", path, body, "other-recipient", `"1"`), 403)
130}
131func TestPersonAPIPaginationUnknown(t *testing.T) {
132 f := newFixture(t)
133 for _, name := range []string{"One", "Two", "Three"} {
134 _, e := f.Store.CreateSpecialist(ctx, owner, specialistInput(name), equipo.Mutation{RequestID: name})
135 must(t, e)
136 }
137 f.exec(t, "DROP TABLE reservations")
138 w := apiRequest(t, f, owner, "GET", "/api/v1/specialists?limit=1", "", "", "")
139 expectHTTP(t, w, 200)
140 page := apiValue[personapi.Page[personapi.Specialist]](t, w)
141 if len(page.Items) != 1 || page.NextCursor == "" {
142 t.Fatal("unbounded list", page)
143 }
144 if !strings.Contains(w.Body.String(), `"work_count":null`) {
145 t.Fatal("unknown count became zero")
146 }
147 _, e := f.Store.CreateSpecialist(ctx, owner, specialistInput("Inserted"), equipo.Mutation{RequestID: "inserted"})
148 must(t, e)
149 next := apiRequest(t, f, owner, "GET", "/api/v1/specialists?limit=1&cursor="+url.QueryEscape(page.NextCursor), "", "", "")
150 expectHTTP(t, next, 200)
151 if apiValue[personapi.Page[personapi.Specialist]](t, next).Items[0].ID == page.Items[0].ID {
152 t.Fatal("repeated cursor item")
153 }
154 expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1/specialists?cursor="+url.QueryEscape(page.NextCursor), "", "", ""), 410)
155 expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/playbooks?cursor="+url.QueryEscape(page.NextCursor), "", "", ""), 410)
156 expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/specialists?limit=101", "", "", ""), 422)
157 for _, key := range []string{"template_base", "secret", "session_epoch"} {
158 if strings.Contains(w.Body.String(), key) {
159 t.Fatal("private field", key)
160 }
161 }
162}
163func TestPersonAPISchemaCompatibility(t *testing.T) {
164 f := newFixture(t)
165 for _, body := range []string{`{"role":"member","active":true,"session_epoch":0}`, `{"role":"member","role":"owner","active":true}`, `{"role":"member"}`} {
166 expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/roster/"+peer.Address, body, "invalid", `"1"`), 422)
167 }
168 expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", `{"team":{},"projects":{"1":{"rogue":1}}}`, "nested", `"0"`), 422)
169 raw, e := os.ReadFile("testdata/person-api-v1.json")
170 must(t, e)
171 var fixture struct {
172 Device personapi.PersonCredential `json:"device"`
173 AccountSetup personapi.AccountSetup `json:"account_setup"`
174 }
175 must(t, json.Unmarshal(raw, &fixture))
176 if fixture.Device.Audience != "equipo.person.v1" || fixture.AccountSetup.Command == "" {
177 t.Fatal("v1 fixture changed")
178 }
179}
180
181func TestPersonAPISharedConnectionContainment(t *testing.T) {
182 f, _, _, claim := claimTools(t)
183 apiRouter(t, f, nil)
184 id := claim.Brief.Selection.ConnectionID
185 expectHTTP(t, apiRequest(t, f, owner, "DELETE", "/api/v1/connections/"+id, `{}`, "contain", fmt.Sprintf(`"%d"`, claim.Brief.Selection.Revision)), 200)
186 var state, containment string
187 must(t, f.DB.Writer().QueryRow("SELECT state,containment FROM attempts WHERE id=?", claim.Handle.Attempt).Scan(&state, &containment))
188 if state != "stopping" || containment != "pending" {
189 t.Fatal("revocation did not request coordinator containment", state, containment)
190 }
191}
192