oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "errors"
5 "testing"
6
7 "gorm.io/gorm"
8
9 "amadan.net/oficina/equipo/internal/specialist"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// rosterRig is a store whose roster is the members table, read in the
14// transaction as the instance's own does: inactive or unknown is
15// forbidden, and lookupFails makes every lookup an error.
16func rosterRig(t *testing.T) (*hkRig, *bool) {
17 r := newHKRig(t)
18 fail := new(bool)
19 r.s.authority.Me = func(tx *gorm.DB, address string) (wire.Me, error) {
20 if *fail {
21 return wire.Me{}, errors.New("roster unavailable")
22 }
23 var rows []struct {
24 Active bool
25 Role string
26 }
27 if err := tx.Raw("SELECT active, role FROM members WHERE address = ?", address).Scan(&rows).Error; err != nil {
28 return wire.Me{}, err
29 }
30 if len(rows) != 1 || !rows[0].Active {
31 return wire.Me{}, ErrForbidden
32 }
33 return wire.Me{Address: address, Role: rows[0].Role}, nil
34 }
35 return r, fail
36}
37
38// approvedResearch proposes research-v1 in owner's turn and adds it.
39func approvedResearch(r *hkRig, owner string) specialistRecord {
40 r.t.Helper()
41 p, err := specialist.Validate(specialist.Proposal{Profile: specialist.ProfileResearch, Name: "Research", Brief: "Look up the shop's site.", Capabilities: []string{specialist.CapabilityWebResearch}, Tools: []string{specialist.ToolSubmitFindings, specialist.ToolKeepImage}})
42 r.must(err)
43 var card string
44 r.must(r.tx(func(t *txn) error {
45 res, err := t.proposeSpecialist(turnRecord{ID: "t1", Person: owner, Kind: wire.TurnPerson}, p, projectRecord{})
46 if err != nil || res.IsError {
47 return errors.Join(err, errors.New(res.Text))
48 }
49 var sp specialistRecord
50 if err := t.db.Where("profile = ? AND state = ?", specialist.ProfileResearch, wire.SpecialistProposed).First(&sp).Error; err != nil {
51 return err
52 }
53 card = sp.CardID
54 return nil
55 }))
56 if _, _, err := r.s.Answer(r.ctx, owner, card, wire.AnswerCard{Choice: wire.ChoiceAdd, Revision: 1}); err != nil {
57 r.t.Fatal(err)
58 }
59 var sp specialistRecord
60 r.must(r.s.db.G.Where("card_id = ?", card).First(&sp).Error)
61 return sp
62}
63
64func (r *hkRig) grant() (specialistRecord, grantRecord, error) {
65 var sp specialistRecord
66 var g grantRecord
67 err := r.tx(func(t *txn) error {
68 var err error
69 sp, g, err = t.researchGrant("")
70 return err
71 })
72 return sp, g, err
73}
74
75func (r *hkRig) exec(q string, args ...any) {
76 r.t.Helper()
77 r.must(r.s.db.G.Exec(q, args...).Error)
78}
79
80// TestResearchGrantUse: the grant is usable only while the specialist is
81// active, its one web.research grant is unrevoked at the specialist's
82// revision with nothing beside it, and the approver is still an owner
83// now; any lookup error denies.
84func TestResearchGrantUse(t *testing.T) {
85 r, lookupFails := rosterRig(t)
86 if _, _, err := r.grant(); !errors.Is(err, ErrNoGrant) {
87 t.Fatalf("no specialist yet: %v", err)
88 }
89 sp := approvedResearch(r, hkOwner)
90 got, g, err := r.grant()
91 if err != nil || got.ID != sp.ID || g.Capability != specialist.CapabilityWebResearch || g.Revision != sp.Revision || g.GrantedBy != hkOwner || got.Approver != hkOwner {
92 t.Fatalf("usable grant %+v %+v %v", got, g, err)
93 }
94 denied := func(what string, wantNoGrant bool) {
95 t.Helper()
96 _, _, err := r.grant()
97 if err == nil || wantNoGrant && !errors.Is(err, ErrNoGrant) {
98 t.Fatalf("%s: %v", what, err)
99 }
100 }
101 usable := func(what string) {
102 t.Helper()
103 if _, _, err := r.grant(); err != nil {
104 t.Fatalf("%s: %v", what, err)
105 }
106 }
107 // The approver is made a teammate, then removed: no longer usable.
108 r.exec("UPDATE members SET role = 'member' WHERE address = ?", hkOwner)
109 denied("approver a teammate", true)
110 r.exec("UPDATE members SET role = 'owner', active = 0 WHERE address = ?", hkOwner)
111 denied("approver removed", true)
112 r.exec("DELETE FROM members WHERE address = ?", hkOwner)
113 denied("approver gone", true)
114 r.exec("INSERT INTO members(address, active, role) VALUES (?, 1, 'owner')", hkOwner)
115 usable("approver an owner again")
116 // Another owner does not stand in for the approver.
117 r.exec("UPDATE members SET role = 'member' WHERE address = ?", hkOwner)
118 if _, err := r.s.authority.Me(r.s.db.G, hkMember); err != nil {
119 t.Fatal(err)
120 }
121 denied("another owner remains", true)
122 r.exec("UPDATE members SET role = 'owner' WHERE address = ?", hkOwner)
123 // A roster that cannot be read denies.
124 *lookupFails = true
125 denied("lookup error", false)
126 *lookupFails = false
127 usable("roster back")
128 // The specialist moved on from the grant's revision.
129 r.exec("UPDATE chat_specialists SET revision = revision + 1 WHERE id = ?", sp.ID)
130 denied("revision moved", true)
131 r.exec("UPDATE chat_specialists SET revision = revision - 1 WHERE id = ?", sp.ID)
132 // Something granted beside web.research.
133 r.exec("INSERT INTO chat_capability_grants(id, specialist_id, capability, revision, granted_by, granted_at) VALUES ('g_extra', ?, 'repo.read', ?, ?, ?)", sp.ID, sp.Revision, hkOwner, r.now)
134 denied("another capability beside it", true)
135 r.exec("DELETE FROM chat_capability_grants WHERE id = 'g_extra'")
136 usable("extra gone")
137 // Not active.
138 r.exec("UPDATE chat_specialists SET state = ? WHERE id = ?", wire.SpecialistDeclined, sp.ID)
139 denied("declined", true)
140 r.exec("UPDATE chat_specialists SET state = ? WHERE id = ?", wire.SpecialistActive, sp.ID)
141 // A profile not on the menu.
142 r.exec("UPDATE chat_specialists SET profile = 'research-v0' WHERE id = ?", sp.ID)
143 denied("unknown profile", true)
144 r.exec("UPDATE chat_specialists SET profile = ? WHERE id = ?", specialist.ProfileResearch, sp.ID)
145 usable("restored")
146 // Revoked through the store: denied, with the grant row revoked.
147 if _, err := r.s.RevokeSpecialist(r.ctx, hkOwner, sp.ID, wire.RevokeSpecialist{Revision: sp.Revision}); err != nil {
148 t.Fatal(err)
149 }
150 denied("revoked", true)
151 var live int64
152 r.must(r.s.db.G.Model(&grantRecord{}).Where("revoked_at IS NULL").Count(&live).Error)
153 if live != 0 {
154 t.Fatalf("%d live grants after revoke", live)
155 }
156}
157
158// TestProposeSpecialistNeedsAnOwner: a person who is no longer an owner
159// (read in the call's transaction) proposes nothing, and a connection
160// below protocol 13 gets the update message before anything else.
161func TestProposeSpecialistNeedsAnOwner(t *testing.T) {
162 r, _ := rosterRig(t)
163 args := []byte(`{"profile":"research-v1","name":"Research","brief":"b","capabilities":["web.research"],"tools":["submit_findings","keep_image"]}`)
164 call := &wire.ToolCall{CallID: "t1/1/u", TurnID: "t1", Attempt: 1, Name: wire.ToolProposeSpecialist, Args: args}
165 turn := turnRecord{ID: "t1", Person: hkMember, Kind: wire.TurnPerson}
166 try := func(protocol int) wire.ToolResult {
167 var res wire.ToolResult
168 r.must(r.tx(func(t *txn) error {
169 var err error
170 res, err = t.toolProposeSpecialist(Lease{Protocol: protocol}, turn, call)
171 return err
172 }))
173 return res
174 }
175 r.exec("UPDATE members SET role = 'member' WHERE address = ?", hkMember)
176 if res := try(wire.ProtocolResearch); !res.IsError || res.Code != "not_owner" {
177 t.Fatalf("a teammate proposed: %+v", res)
178 }
179 if res := try(wire.ProtocolPreviewHost); res.Code != "research_unsupported" || res.Text != toolSpecialistOutdated {
180 t.Fatalf("at 11: %+v", res)
181 }
182 r.exec("UPDATE members SET role = 'owner' WHERE address = ?", hkMember)
183 if res := try(wire.ProtocolResearch); res.IsError || res.Code != "proposed" {
184 t.Fatalf("an owner: %+v", res)
185 }
186 var n int64
187 r.must(r.s.db.G.Model(&specialistRecord{}).Count(&n).Error)
188 if n != 1 {
189 t.Fatalf("%d specialists", n)
190 }
191}
192