oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "context"
5 "slices"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/oficina/equipo/internal/specialist"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// Specialists as data (Research spec §6, slice R0): the coordinator
15// proposes Research from the closed menu, one card asks an owner, and
16// only an owner's Add, in one transaction, makes it active with its
17// grant and audit rows.
18
19// researchArgs is a proposal of the research-v1 profile exactly.
20func researchArgs() map[string]any {
21 return map[string]any{"profile": specialist.ProfileResearch, "name": "Research", "brief": "Look up the shop's public pages for its colours, logo and opening hours.", "capabilities": []string{specialist.CapabilityWebResearch}, "tools": []string{specialist.ToolSubmitFindings, specialist.ToolKeepImage}}
22}
23
24// specialistCard waits for the person's specialist card in state.
25func (r *toolRig) specialistCard(state string) wire.Card {
26 r.t.Helper()
27 var card wire.Card
28 r.wait("specialist card "+state, func(c *Conversation) bool {
29 for _, k := range c.Cards {
30 if k.Kind == wire.CardSpecialist && k.State == state {
31 card = k
32 return true
33 }
34 }
35 return false
36 })
37 return card
38}
39
40func (r *toolRig) rows(q string, args ...any) int {
41 r.t.Helper()
42 var n int
43 if err := r.inst.DB.Writer().QueryRow(q, args...).Scan(&n); err != nil {
44 r.t.Fatal(err)
45 }
46 return n
47}
48
49func (r *toolRig) specialistCards() int {
50 r.t.Helper()
51 snap, err := r.client.Snapshot(r.ctx)
52 if err != nil {
53 r.t.Fatal(err)
54 }
55 n := 0
56 for _, c := range snap.Cards {
57 if c.Kind == wire.CardSpecialist {
58 n++
59 }
60 }
61 return n
62}
63
64// TestSpecialistProposeAndAdd: propose -> one card with the profile's
65// capability in plain words and the brief, answers Add and Not now; a
66// replayed call returns the stored result; a second open proposal is
67// refused; Add makes it active with one grant at its revision and the
68// audit rows; then the profile answers already approved.
69func TestSpecialistProposeAndAdd(t *testing.T) {
70 r := newToolRigAt(t, wire.ProtocolCurrent)
71 res := r.call("p1", wire.ToolProposeSpecialist, researchArgs())
72 if res.IsError || res.Code != "proposed" || res.Text == "" {
73 t.Fatalf("propose: %+v", res)
74 }
75 card := r.specialistCard(wire.CardOpen)
76 if !slices.Equal(card.Options, []string{wire.ChoiceAdd, wire.ChoiceNotNow}) || card.Question == "" || card.Name != "Research" || card.TurnID != r.turn {
77 t.Fatalf("card %+v", card)
78 }
79 s := card.Specialist
80 if s == nil || s.Profile != specialist.ProfileResearch || s.State != wire.SpecialistProposed || !strings.Contains(s.Brief, "opening hours") || len(s.Capabilities) != 1 || s.Capabilities[0].ID != specialist.CapabilityWebResearch || s.Capabilities[0].Words == "" || strings.Contains(s.Capabilities[0].Words, "chat.") {
81 t.Fatalf("card's specialist %+v", s)
82 }
83 // The same call id again: the stored result, and nothing new.
84 if again := r.call("p1", wire.ToolProposeSpecialist, researchArgs()); again != res {
85 t.Fatalf("replay %+v vs %+v", again, res)
86 }
87 // Another proposal while one is open is refused.
88 if second := r.call("p2", wire.ToolProposeSpecialist, researchArgs()); !second.IsError || second.Code != "already_proposed" {
89 t.Fatalf("second proposal %+v", second)
90 }
91 if n := r.specialistCards(); n != 1 || r.rows("SELECT count(*) FROM chat_specialists") != 1 {
92 t.Fatalf("%d cards", n)
93 }
94 if r.rows("SELECT count(*) FROM chat_capability_grants") != 0 {
95 t.Fatal("granted before Add")
96 }
97 // The conversation page shows it as simply as the direction card:
98 // the question, the capability in words, the brief, Add and Not now.
99 page := r.get(r.inst.SessionFor(t, Owner), "/conversation", 200)
100 for _, want := range []string{`equipo-card="specialist"`, card.Question, s.Capabilities[0].Words, "opening hours", `data-specialist-choice="add"`, `data-specialist-choice="not_now"`, ">Add<", ">Not now<"} {
101 if !strings.Contains(page, want) {
102 t.Fatalf("the open card lacks %q", want)
103 }
104 }
105 if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAdd, card.Revision); err != nil {
106 t.Fatal(err)
107 }
108 done := r.specialistCard(wire.CardAnswered)
109 if done.Choice != wire.ChoiceAdd || done.Specialist.State != wire.SpecialistActive {
110 t.Fatalf("answered %+v", done)
111 }
112 list, err := r.client.Specialists(r.ctx)
113 if err != nil || len(list.Items) != 1 {
114 t.Fatalf("list %+v %v", list, err)
115 }
116 sp := list.Items[0]
117 if sp.State != wire.SpecialistActive || sp.Approver != Owner || sp.ApprovedAt == nil || sp.ProposedBy != Owner || !slices.Equal(sp.Capabilities, []string{specialist.CapabilityWebResearch}) || !slices.Equal(sp.Tools, []string{specialist.ToolSubmitFindings, specialist.ToolKeepImage}) || sp.ID != s.ID {
118 t.Fatalf("specialist %+v", sp)
119 }
120 if n := r.rows("SELECT count(*) FROM chat_capability_grants WHERE specialist_id = ? AND capability = ? AND revision = ? AND granted_by = ? AND revoked_at IS NULL", sp.ID, specialist.CapabilityWebResearch, sp.Revision, Owner); n != 1 || r.rows("SELECT count(*) FROM chat_capability_grants") != 1 {
121 t.Fatalf("%d grants", n)
122 }
123 if n := r.rows("SELECT count(*) FROM chat_specialist_audit WHERE specialist_id = ? AND action = 'approved' AND actor = ? AND revision = ?", sp.ID, Owner, sp.Revision); n != 1 {
124 t.Fatalf("%d approval audit rows", n)
125 }
126 if n := r.rows("SELECT count(*) FROM chat_specialist_audit WHERE specialist_id = ? AND action = 'proposed'", sp.ID); n != 1 {
127 t.Fatalf("%d proposal audit rows", n)
128 }
129 // Answered: a replay is a conflict, and the profile is approved.
130 if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAdd, card.Revision); Status(err) != 409 {
131 t.Fatalf("replayed answer: %v", err)
132 }
133 if third := r.call("p3", wire.ToolProposeSpecialist, researchArgs()); !third.IsError || third.Code != "already_active" {
134 t.Fatalf("after Add %+v", third)
135 }
136 // Answered, the card says so and offers nothing.
137 if page := r.get(r.inst.SessionFor(t, Owner), "/conversation", 200); !strings.Contains(page, "Added to your team.") || strings.Contains(page, `data-specialist-choice`) {
138 t.Fatal("the answered card")
139 }
140}
141
142// TestSpecialistProposalRefused: anything that is not exactly the
143// research-v1 profile is refused before any card or row.
144func TestSpecialistProposalRefused(t *testing.T) {
145 r := newToolRigAt(t, wire.ProtocolCurrent)
146 for name, c := range map[string]struct {
147 edit func(map[string]any)
148 code string
149 }{
150 "an extra capability": {func(a map[string]any) { a["capabilities"] = []string{"web.research", "repo.read"} }, "exclusive"},
151 "another capability": {func(a map[string]any) { a["capabilities"] = []string{"attachments.read"} }, "not_on_menu"},
152 "an extra tool": {func(a map[string]any) { a["tools"] = []string{"submit_findings", "keep_image", "start_task"} }, "exclusive"},
153 "a missing tool": {func(a map[string]any) { a["tools"] = []string{"submit_findings"} }, "not_on_menu"},
154 "an unknown profile": {func(a map[string]any) { a["profile"] = "release-v1" }, "unknown_profile"},
155 "an over-long brief": {func(a map[string]any) { a["brief"] = strings.Repeat("b", specialist.MaxBrief+1) }, "brief"},
156 "a built-in's name": {func(a map[string]any) { a["name"] = "Engineering" }, "name"},
157 "an unknown field": {func(a map[string]any) { a["model"] = "gpt-6-astra" }, "invalid"},
158 "no tools at all": {func(a map[string]any) { delete(a, "tools") }, "not_on_menu"},
159 "capabilities a string": {func(a map[string]any) { a["capabilities"] = "web.research" }, "invalid"},
160 } {
161 args := researchArgs()
162 c.edit(args)
163 res := r.call("x-"+strings.ReplaceAll(name, " ", "-"), wire.ToolProposeSpecialist, args)
164 if !res.IsError || res.Code != c.code || res.Text == "" {
165 t.Errorf("%s: %+v", name, res)
166 }
167 }
168 if r.specialistCards() != 0 || r.rows("SELECT count(*) FROM chat_specialists") != 0 || r.rows("SELECT count(*) FROM chat_specialist_audit") != 0 {
169 t.Fatal("a refused proposal left something")
170 }
171}
172
173// TestSpecialistNotNow: Not now declines it with an audit row and no
174// grant; the coordinator cannot propose that profile and scope again for
175// 7 days (spec 2026-11-28-dynamic-specialists.md, section 4).
176func TestSpecialistNotNow(t *testing.T) {
177 r := newToolRigAt(t, wire.ProtocolCurrent)
178 r.call("n1", wire.ToolProposeSpecialist, researchArgs())
179 card := r.specialistCard(wire.CardOpen)
180 if _, err := r.client.Answer(r.ctx, card.ID, "maybe", card.Revision); Status(err) != 422 {
181 t.Fatalf("another answer: %v", err)
182 }
183 if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceNotNow, card.Revision); err != nil {
184 t.Fatal(err)
185 }
186 done := r.specialistCard(wire.CardAnswered)
187 if done.Choice != wire.ChoiceNotNow || done.Specialist.State != wire.SpecialistDeclined {
188 t.Fatalf("answered %+v", done)
189 }
190 if r.rows("SELECT count(*) FROM chat_capability_grants") != 0 || r.rows("SELECT count(*) FROM chat_specialist_audit WHERE action = 'declined' AND actor = ?", Owner) != 1 {
191 t.Fatal("Not now granted, or was not audited")
192 }
193 if res := r.call("n2", wire.ToolProposeSpecialist, researchArgs()); !res.IsError || res.Code != "declined_recently" {
194 t.Fatalf("proposed again within 7 days %+v", res)
195 }
196}
197
198// TestSpecialistAnswerAuthority: a stale revision is a conflict; only the
199// card's person answers it (another member's answer finds nothing), and
200// only while they are still an owner, checked in the answer's
201// transaction. Nothing is granted.
202func TestSpecialistAnswerAuthority(t *testing.T) {
203 a := newToolRigAt(t, wire.ProtocolCurrent)
204 ctx := a.ctx
205 b := personToolRigAt(t, a, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent)
206 m := personToolRig(t, a, "member@example.com", equipo.RoleMember)
207 if res := b.call("b1", wire.ToolProposeSpecialist, researchArgs()); res.IsError {
208 t.Fatalf("B's proposal %+v", res)
209 }
210 card := b.specialistCard(wire.CardOpen)
211 for _, rev := range []int64{card.Revision + 1, card.Revision - 1} {
212 if _, err := b.client.Answer(ctx, card.ID, wire.ChoiceAdd, rev); Status(err) != 409 {
213 t.Fatalf("revision %d: %v", rev, err)
214 }
215 }
216 // Someone else's card: not theirs to answer.
217 for _, other := range []*toolRig{a, m} {
218 if _, err := other.client.Answer(ctx, card.ID, wire.ChoiceAdd, card.Revision); Status(err) != 404 {
219 t.Fatalf("another person answered: %v", err)
220 }
221 }
222 // B is made a teammate: their own card no longer takes their Add.
223 var revision int64
224 if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", "second@example.com").Scan(&revision); err != nil {
225 t.Fatal(err)
226 }
227 if err := a.inst.Store.SetMember(context.Background(), equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleMember, true, revision); err != nil {
228 t.Fatal(err)
229 }
230 if _, err := b.client.Answer(ctx, card.ID, wire.ChoiceAdd, card.Revision); Status(err) != 403 {
231 t.Fatalf("a teammate added it: %v", err)
232 }
233 if a.rows("SELECT count(*) FROM chat_capability_grants") != 0 || a.rows("SELECT count(*) FROM chat_specialists WHERE state = ?", wire.SpecialistProposed) != 1 {
234 t.Fatal("granted without an owner")
235 }
236}
237
238// TestSpecialistRevoke: only an owner revokes, at the current revision;
239// the grant is revoked with an audit row, and the profile may be
240// proposed again.
241func TestSpecialistRevoke(t *testing.T) {
242 r := newToolRigAt(t, wire.ProtocolCurrent)
243 m := personToolRig(t, r, "member@example.com", equipo.RoleMember)
244 r.call("r1", wire.ToolProposeSpecialist, researchArgs())
245 card := r.specialistCard(wire.CardOpen)
246 if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAdd, card.Revision); err != nil {
247 t.Fatal(err)
248 }
249 list, _ := m.client.Specialists(r.ctx)
250 if len(list.Items) != 1 || list.Items[0].State != wire.SpecialistActive {
251 t.Fatalf("a teammate reads %+v", list)
252 }
253 sp := list.Items[0]
254 if _, err := m.client.RevokeSpecialist(r.ctx, sp.ID, sp.Revision); Status(err) != 403 {
255 t.Fatalf("a teammate revoked: %v", err)
256 }
257 if _, err := r.client.RevokeSpecialist(r.ctx, sp.ID, sp.Revision-1); Status(err) != 409 {
258 t.Fatalf("a stale revoke: %v", err)
259 }
260 if _, err := r.client.RevokeSpecialist(r.ctx, "sp_nothing", sp.Revision); Status(err) != 404 {
261 t.Fatalf("no such specialist: %v", err)
262 }
263 if r.rows("SELECT count(*) FROM chat_capability_grants WHERE revoked_at IS NULL") != 1 {
264 t.Fatal("a refused revoke changed the grant")
265 }
266 out, err := r.client.RevokeSpecialist(r.ctx, sp.ID, sp.Revision)
267 if err != nil || out.State != wire.SpecialistRevoked || out.RevokedBy != Owner || out.RevokedAt == nil || out.Revision != sp.Revision+1 {
268 t.Fatalf("revoked %+v %v", out, err)
269 }
270 if r.rows("SELECT count(*) FROM chat_capability_grants WHERE revoked_at IS NULL") != 0 || r.rows("SELECT count(*) FROM chat_capability_grants WHERE revoked_by = ?", Owner) != 1 {
271 t.Fatal("the grant is still live")
272 }
273 if r.rows("SELECT count(*) FROM chat_specialist_audit WHERE specialist_id = ? AND action = 'revoked' AND actor = ? AND revision = ?", sp.ID, Owner, out.Revision) != 1 {
274 t.Fatal("no revoke audit row")
275 }
276 if _, err := r.client.RevokeSpecialist(r.ctx, sp.ID, out.Revision); Status(err) != 409 {
277 t.Fatalf("revoked twice: %v", err)
278 }
279 if res := r.call("r2", wire.ToolProposeSpecialist, researchArgs()); res.IsError || res.Code != "proposed" {
280 t.Fatalf("proposed after revoke %+v", res)
281 }
282}
283
284// TestSpecialistOlderRunner: a primary speaking protocol 11 gets the
285// update message for propose_specialist, and nothing is proposed.
286func TestSpecialistOlderRunner(t *testing.T) {
287 r := newToolRigAt(t, wire.ProtocolPreviewHost)
288 res := r.call("o1", wire.ToolProposeSpecialist, researchArgs())
289 if !res.IsError || res.Code != "research_unsupported" || res.Text != "Equipo needs an update before it can research." {
290 t.Fatalf("at 11: %+v", res)
291 }
292 if r.specialistCards() != 0 || r.rows("SELECT count(*) FROM chat_specialists") != 0 {
293 t.Fatal("proposed at 11")
294 }
295}
296