oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package specialist
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "io/fs"
8 "reflect"
9 "slices"
10 "strings"
11 "testing"
12)
13
14// golden pins every profile on the menu, in menu order, in literals (not
15// the package's constants, so a renamed constant shows here too). A
16// change to any field of a profile, its pack included, is a new profile
17// id (design-v2) with its own entry, never an edit to an existing one:
18// grants for the old id do not carry over. Later fields on Profile (the
19// executable policy, task 13) are pinned here by adding them to these
20// entries; TestMenuGolden compares whole profiles.
21var golden = []Profile{
22 {
23 ID: "research-v1",
24 Capabilities: []string{"web.research"},
25 Tools: []string{"submit_findings", "keep_image"},
26 },
27 {
28 ID: "design-v1",
29 Capabilities: []string{"branch.edit", "page.render"},
30 Tools: []string{"render_page", "submit_candidate", "ask_person"},
31 Pack: "de40167d0d843f4f99ecc633b7022ac8631db74c74c98ed5fdbd4ea732d3334a",
32 // Task 13's executable policy: the runner's design_step action
33 // profile, Engineering's native tools, and /render read-only
34 // beyond Engineering's file permissions, nothing else.
35 Policy: Policy{
36 Action: "design_step",
37 Native: []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"},
38 ReadOnly: []string{"/render"},
39 },
40 },
41}
42
43// TestMenuGolden: the menu is exactly the pinned profiles.
44func TestMenuGolden(t *testing.T) {
45 got := Profiles()
46 if len(got) != len(golden) {
47 t.Fatalf("%d profiles, %d pinned", len(got), len(golden))
48 }
49 for i := range golden {
50 if !reflect.DeepEqual(got[i], golden[i]) {
51 t.Errorf("profile %d is\n %+v\npinned\n %+v\n(a changed profile is a new profile id)", i, got[i], golden[i])
52 }
53 }
54}
55
56// TestPacksArePinned: each profile with a pack has its embedded file
57// under packs/ named for its id, whose SHA-256 is the profile's Pack;
58// PackText serves it only while they agree; no file under packs/ is
59// left without its profile.
60func TestPacksArePinned(t *testing.T) {
61 files, err := fs.Glob(packs, "packs/*")
62 if err != nil {
63 t.Fatal(err)
64 }
65 seen := 0
66 for _, p := range Profiles() {
67 text, ok := p.PackText()
68 if p.Pack == "" {
69 if ok || text != "" {
70 t.Fatalf("%s has no pack but served one", p.ID)
71 }
72 continue
73 }
74 seen++
75 raw, err := packs.ReadFile("packs/" + p.ID + ".md")
76 if err != nil {
77 t.Fatalf("%s: %v", p.ID, err)
78 }
79 sum := sha256.Sum256(raw)
80 if hex.EncodeToString(sum[:]) != p.Pack {
81 t.Fatalf("%s: the pack's digest is %x, the profile says %s (a changed pack is a new profile id)", p.ID, sum, p.Pack)
82 }
83 if !ok || text != string(raw) {
84 t.Fatalf("%s: PackText %v", p.ID, ok)
85 }
86 // A profile whose digest is not its file's gets no pack.
87 p.Pack = strings.Repeat("0", 64)
88 if text, ok := p.PackText(); ok || text != "" {
89 t.Fatalf("%s: served a pack for another digest", p.ID)
90 }
91 }
92 if seen != len(files) {
93 t.Fatalf("%d packs embedded, %d profiles have one: %v", len(files), seen, files)
94 }
95}
96
97func design() Proposal {
98 return Proposal{Profile: ProfileDesign, Name: "Design", Brief: "Overhaul the shop's theme: two or three directions to choose from.", Capabilities: []string{CapabilityBranchEdit, CapabilityPageRender}, Tools: []string{ToolRenderPage, ToolSubmitCandidate, ToolAskPerson}}
99}
100
101// TestValidateTheDesignProfile: design-v1 is accepted exactly, in any
102// order; anything else is refused, and web.research beside its
103// capabilities or tools is refused as exclusive first.
104func TestValidateTheDesignProfile(t *testing.T) {
105 p := design()
106 p.Capabilities = []string{CapabilityPageRender, CapabilityBranchEdit}
107 p.Tools = []string{ToolAskPerson, ToolRenderPage, ToolSubmitCandidate}
108 got, err := Validate(p)
109 if err != nil {
110 t.Fatal(err)
111 }
112 if !slices.Equal(got.Capabilities, []string{"branch.edit", "page.render"}) || !slices.Equal(got.Tools, []string{"render_page", "submit_candidate", "ask_person"}) {
113 t.Fatalf("cleaned %+v", got)
114 }
115 for name, c := range map[string]struct {
116 edit func(*Proposal)
117 want error
118 }{
119 "web.research added": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, CapabilityWebResearch) }, ErrExclusive},
120 "web.research for page.render": {func(p *Proposal) { p.Capabilities = []string{CapabilityBranchEdit, CapabilityWebResearch} }, ErrExclusive},
121 "web.research alone": {func(p *Proposal) { p.Capabilities = []string{CapabilityWebResearch} }, ErrExclusive},
122 "research's tools added": {func(p *Proposal) { p.Tools = append(p.Tools, ToolKeepImage) }, ErrNotOnMenu},
123 "the old submit_render": {func(p *Proposal) { p.Tools = append(p.Tools, "submit_render") }, ErrNotOnMenu},
124 "a coordinator tool": {func(p *Proposal) { p.Tools = append(p.Tools, "propose_specialist") }, ErrNotOnMenu},
125 "no render_page": {func(p *Proposal) { p.Tools = []string{ToolSubmitCandidate, ToolAskPerson} }, ErrNotOnMenu},
126 "a repeated tool": {func(p *Proposal) { p.Tools = append(p.Tools, ToolRenderPage) }, ErrNotOnMenu},
127 "branch.read for branch.edit": {func(p *Proposal) { p.Capabilities = []string{"branch.read", CapabilityPageRender} }, ErrNotOnMenu},
128 "a release capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "release.staging") }, ErrNotOnMenu},
129 "page.render alone": {func(p *Proposal) { p.Capabilities = []string{CapabilityPageRender} }, ErrNotOnMenu},
130 "research's shape under design": {func(p *Proposal) { *p = research(); p.Profile = ProfileDesign }, ErrNotOnMenu},
131 "design's shape under research": {func(p *Proposal) { p.Profile = ProfileResearch }, ErrNotOnMenu},
132 "page.render beside web.research": {func(p *Proposal) { *p = research(); p.Capabilities = append(p.Capabilities, CapabilityPageRender) }, ErrExclusive},
133 "render_page beside web.research": {func(p *Proposal) { *p = research(); p.Tools = append(p.Tools, ToolRenderPage) }, ErrExclusive},
134 "a built-in's name": {func(p *Proposal) { p.Name = "Review" }, ErrName},
135 } {
136 p := design()
137 c.edit(&p)
138 if _, err := Validate(p); !errors.Is(err, c.want) {
139 t.Errorf("%s: %v, want %v", name, err, c.want)
140 }
141 }
142}
143
144// TestMenuKeepsWebResearchApart: checkMenu, which the package runs over
145// its menu at init, accepts the menu and refuses one where a profile
146// holding web.research holds anything else (page.render above all), a
147// repeated id, a capability with no words, or a pack that is not its
148// file's.
149func TestMenuKeepsWebResearchApart(t *testing.T) {
150 if err := checkMenu(menu); err != nil {
151 t.Fatal(err)
152 }
153 for name, edit := range map[string]func([]Profile) []Profile{
154 "web.research with page.render": func(m []Profile) []Profile {
155 m[0].Capabilities = append(m[0].Capabilities, CapabilityPageRender)
156 return m
157 },
158 "web.research with render_page": func(m []Profile) []Profile {
159 m[0].Tools = append(m[0].Tools, ToolRenderPage)
160 return m
161 },
162 "design with web.research": func(m []Profile) []Profile {
163 m[1].Capabilities = append(m[1].Capabilities, CapabilityWebResearch)
164 return m
165 },
166 "a repeated id": func(m []Profile) []Profile {
167 return append(m, m[0])
168 },
169 "a capability with no words": func(m []Profile) []Profile {
170 m[1].Capabilities = append(m[1].Capabilities, "repo.admin")
171 return m
172 },
173 "a pack not its file's": func(m []Profile) []Profile {
174 m[1].Pack = strings.Repeat("0", 64)
175 return m
176 },
177 "a pack with no file": func(m []Profile) []Profile {
178 m[0].Pack = m[1].Pack
179 return m
180 },
181 } {
182 m := Profiles()
183 if err := checkMenu(edit(m)); err == nil {
184 t.Errorf("%s: accepted", name)
185 }
186 }
187}
188
189// TestDescribeDesign: the Design capabilities have their copy keys.
190func TestDescribeDesign(t *testing.T) {
191 for c, key := range map[string]string{
192 "branch.edit": "chat.specialist.capability.branch.edit",
193 "page.render": "chat.specialist.capability.page.render",
194 "web.research": "chat.specialist.capability.web.research",
195 "branch.read": "",
196 "": "",
197 } {
198 if got := Describe(c); got != key {
199 t.Errorf("Describe(%q) = %q, want %q", c, got, key)
200 }
201 }
202}
203
204// TestPolicyIsACopy: Profiles and Lookup hand out copies of a profile's
205// executable policy, so a caller cannot widen the menu's.
206func TestPolicyIsACopy(t *testing.T) {
207 p, ok := Lookup(ProfileDesign)
208 if !ok {
209 t.Fatal("no design-v1")
210 }
211 p.Policy.Native[0], p.Policy.ReadOnly[0] = "WebFetch", "/"
212 again, _ := Lookup(ProfileDesign)
213 if !reflect.DeepEqual(again.Policy, golden[1].Policy) {
214 t.Fatalf("the menu's policy changed through a copy: %+v", again.Policy)
215 }
216}
217