package specialist import ( "crypto/sha256" "encoding/hex" "errors" "io/fs" "reflect" "slices" "strings" "testing" ) // golden pins every profile on the menu, in menu order, in literals (not // the package's constants, so a renamed constant shows here too). A // change to any field of a profile, its pack included, is a new profile // id (design-v2) with its own entry, never an edit to an existing one: // grants for the old id do not carry over. Later fields on Profile (the // executable policy, task 13) are pinned here by adding them to these // entries; TestMenuGolden compares whole profiles. var golden = []Profile{ { ID: "research-v1", Capabilities: []string{"web.research"}, Tools: []string{"submit_findings", "keep_image"}, }, { ID: "design-v1", Capabilities: []string{"branch.edit", "page.render"}, Tools: []string{"render_page", "submit_candidate", "ask_person"}, Pack: "de40167d0d843f4f99ecc633b7022ac8631db74c74c98ed5fdbd4ea732d3334a", // Task 13's executable policy: the runner's design_step action // profile, Engineering's native tools, and /render read-only // beyond Engineering's file permissions, nothing else. Policy: Policy{ Action: "design_step", Native: []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"}, ReadOnly: []string{"/render"}, }, }, } // TestMenuGolden: the menu is exactly the pinned profiles. func TestMenuGolden(t *testing.T) { got := Profiles() if len(got) != len(golden) { t.Fatalf("%d profiles, %d pinned", len(got), len(golden)) } for i := range golden { if !reflect.DeepEqual(got[i], golden[i]) { t.Errorf("profile %d is\n %+v\npinned\n %+v\n(a changed profile is a new profile id)", i, got[i], golden[i]) } } } // TestPacksArePinned: each profile with a pack has its embedded file // under packs/ named for its id, whose SHA-256 is the profile's Pack; // PackText serves it only while they agree; no file under packs/ is // left without its profile. func TestPacksArePinned(t *testing.T) { files, err := fs.Glob(packs, "packs/*") if err != nil { t.Fatal(err) } seen := 0 for _, p := range Profiles() { text, ok := p.PackText() if p.Pack == "" { if ok || text != "" { t.Fatalf("%s has no pack but served one", p.ID) } continue } seen++ raw, err := packs.ReadFile("packs/" + p.ID + ".md") if err != nil { t.Fatalf("%s: %v", p.ID, err) } sum := sha256.Sum256(raw) if hex.EncodeToString(sum[:]) != p.Pack { 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) } if !ok || text != string(raw) { t.Fatalf("%s: PackText %v", p.ID, ok) } // A profile whose digest is not its file's gets no pack. p.Pack = strings.Repeat("0", 64) if text, ok := p.PackText(); ok || text != "" { t.Fatalf("%s: served a pack for another digest", p.ID) } } if seen != len(files) { t.Fatalf("%d packs embedded, %d profiles have one: %v", len(files), seen, files) } } func design() Proposal { 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}} } // TestValidateTheDesignProfile: design-v1 is accepted exactly, in any // order; anything else is refused, and web.research beside its // capabilities or tools is refused as exclusive first. func TestValidateTheDesignProfile(t *testing.T) { p := design() p.Capabilities = []string{CapabilityPageRender, CapabilityBranchEdit} p.Tools = []string{ToolAskPerson, ToolRenderPage, ToolSubmitCandidate} got, err := Validate(p) if err != nil { t.Fatal(err) } if !slices.Equal(got.Capabilities, []string{"branch.edit", "page.render"}) || !slices.Equal(got.Tools, []string{"render_page", "submit_candidate", "ask_person"}) { t.Fatalf("cleaned %+v", got) } for name, c := range map[string]struct { edit func(*Proposal) want error }{ "web.research added": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, CapabilityWebResearch) }, ErrExclusive}, "web.research for page.render": {func(p *Proposal) { p.Capabilities = []string{CapabilityBranchEdit, CapabilityWebResearch} }, ErrExclusive}, "web.research alone": {func(p *Proposal) { p.Capabilities = []string{CapabilityWebResearch} }, ErrExclusive}, "research's tools added": {func(p *Proposal) { p.Tools = append(p.Tools, ToolKeepImage) }, ErrNotOnMenu}, "the old submit_render": {func(p *Proposal) { p.Tools = append(p.Tools, "submit_render") }, ErrNotOnMenu}, "a coordinator tool": {func(p *Proposal) { p.Tools = append(p.Tools, "propose_specialist") }, ErrNotOnMenu}, "no render_page": {func(p *Proposal) { p.Tools = []string{ToolSubmitCandidate, ToolAskPerson} }, ErrNotOnMenu}, "a repeated tool": {func(p *Proposal) { p.Tools = append(p.Tools, ToolRenderPage) }, ErrNotOnMenu}, "branch.read for branch.edit": {func(p *Proposal) { p.Capabilities = []string{"branch.read", CapabilityPageRender} }, ErrNotOnMenu}, "a release capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "release.staging") }, ErrNotOnMenu}, "page.render alone": {func(p *Proposal) { p.Capabilities = []string{CapabilityPageRender} }, ErrNotOnMenu}, "research's shape under design": {func(p *Proposal) { *p = research(); p.Profile = ProfileDesign }, ErrNotOnMenu}, "design's shape under research": {func(p *Proposal) { p.Profile = ProfileResearch }, ErrNotOnMenu}, "page.render beside web.research": {func(p *Proposal) { *p = research(); p.Capabilities = append(p.Capabilities, CapabilityPageRender) }, ErrExclusive}, "render_page beside web.research": {func(p *Proposal) { *p = research(); p.Tools = append(p.Tools, ToolRenderPage) }, ErrExclusive}, "a built-in's name": {func(p *Proposal) { p.Name = "Review" }, ErrName}, } { p := design() c.edit(&p) if _, err := Validate(p); !errors.Is(err, c.want) { t.Errorf("%s: %v, want %v", name, err, c.want) } } } // TestMenuKeepsWebResearchApart: checkMenu, which the package runs over // its menu at init, accepts the menu and refuses one where a profile // holding web.research holds anything else (page.render above all), a // repeated id, a capability with no words, or a pack that is not its // file's. func TestMenuKeepsWebResearchApart(t *testing.T) { if err := checkMenu(menu); err != nil { t.Fatal(err) } for name, edit := range map[string]func([]Profile) []Profile{ "web.research with page.render": func(m []Profile) []Profile { m[0].Capabilities = append(m[0].Capabilities, CapabilityPageRender) return m }, "web.research with render_page": func(m []Profile) []Profile { m[0].Tools = append(m[0].Tools, ToolRenderPage) return m }, "design with web.research": func(m []Profile) []Profile { m[1].Capabilities = append(m[1].Capabilities, CapabilityWebResearch) return m }, "a repeated id": func(m []Profile) []Profile { return append(m, m[0]) }, "a capability with no words": func(m []Profile) []Profile { m[1].Capabilities = append(m[1].Capabilities, "repo.admin") return m }, "a pack not its file's": func(m []Profile) []Profile { m[1].Pack = strings.Repeat("0", 64) return m }, "a pack with no file": func(m []Profile) []Profile { m[0].Pack = m[1].Pack return m }, } { m := Profiles() if err := checkMenu(edit(m)); err == nil { t.Errorf("%s: accepted", name) } } } // TestDescribeDesign: the Design capabilities have their copy keys. func TestDescribeDesign(t *testing.T) { for c, key := range map[string]string{ "branch.edit": "chat.specialist.capability.branch.edit", "page.render": "chat.specialist.capability.page.render", "web.research": "chat.specialist.capability.web.research", "branch.read": "", "": "", } { if got := Describe(c); got != key { t.Errorf("Describe(%q) = %q, want %q", c, got, key) } } } // TestPolicyIsACopy: Profiles and Lookup hand out copies of a profile's // executable policy, so a caller cannot widen the menu's. func TestPolicyIsACopy(t *testing.T) { p, ok := Lookup(ProfileDesign) if !ok { t.Fatal("no design-v1") } p.Policy.Native[0], p.Policy.ReadOnly[0] = "WebFetch", "/" again, _ := Lookup(ProfileDesign) if !reflect.DeepEqual(again.Policy, golden[1].Policy) { t.Fatalf("the menu's policy changed through a copy: %+v", again.Policy) } }