package specialist import ( "errors" "slices" "strings" "testing" ) func research() Proposal { return Proposal{Profile: ProfileResearch, Name: "Research", Brief: "Look up the shop's public pages for colours, logo and hours.", Capabilities: []string{CapabilityWebResearch}, Tools: []string{ToolSubmitFindings, ToolKeepImage}} } // TestValidateTheResearchProfile: the closed profile is accepted exactly, // in any order, with its name and brief cleaned; anything that is not // exactly the profile's capabilities and tools is refused, and so is // web.research beside anything else. func TestValidateTheResearchProfile(t *testing.T) { p := research() p.Name, p.Brief = " Research ", "\n"+p.Brief+"\n" p.Tools = []string{ToolKeepImage, ToolSubmitFindings} got, err := Validate(p) if err != nil { t.Fatal(err) } if got.Name != "Research" || got.Brief != research().Brief || !slices.Equal(got.Capabilities, []string{CapabilityWebResearch}) || !slices.Equal(got.Tools, []string{ToolSubmitFindings, ToolKeepImage}) { t.Fatalf("cleaned %+v", got) } for name, c := range map[string]struct { edit func(*Proposal) want error }{ "an unknown profile": {func(p *Proposal) { p.Profile = "research-v2" }, ErrUnknownProfile}, "no profile": {func(p *Proposal) { p.Profile = "" }, ErrUnknownProfile}, "an extra capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "repo.read") }, ErrExclusive}, "a release capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "release.staging") }, ErrExclusive}, "another capability alone": {func(p *Proposal) { p.Capabilities = []string{"attachments.read"} }, ErrNotOnMenu}, "no capability": {func(p *Proposal) { p.Capabilities = nil }, ErrNotOnMenu}, "a repeated capability": {func(p *Proposal) { p.Capabilities = []string{CapabilityWebResearch, CapabilityWebResearch} }, ErrNotOnMenu}, "an extra tool": {func(p *Proposal) { p.Tools = append(p.Tools, "start_task") }, ErrExclusive}, "a native tool": {func(p *Proposal) { p.Tools = append(p.Tools, "Bash") }, ErrExclusive}, "a missing tool": {func(p *Proposal) { p.Tools = []string{ToolSubmitFindings} }, ErrNotOnMenu}, "no tools": {func(p *Proposal) { p.Tools = nil }, ErrNotOnMenu}, "a repeated tool": {func(p *Proposal) { p.Tools = []string{ToolSubmitFindings, ToolKeepImage, ToolKeepImage} }, ErrExclusive}, "an over-long brief": {func(p *Proposal) { p.Brief = strings.Repeat("a", MaxBrief+1) }, ErrBrief}, "an empty brief": {func(p *Proposal) { p.Brief = " \n " }, ErrBrief}, "a brief with a NUL": {func(p *Proposal) { p.Brief = "a\x00b" }, ErrBrief}, "a brief not UTF-8": {func(p *Proposal) { p.Brief = "a\xffb" }, ErrBrief}, "an empty name": {func(p *Proposal) { p.Name = " " }, ErrName}, "a long name": {func(p *Proposal) { p.Name = strings.Repeat("r", MaxName+1) }, ErrName}, "a name with markup": {func(p *Proposal) { p.Name = "Research" }, ErrName}, "a name with a newline": {func(p *Proposal) { p.Name = "Re\nsearch" }, ErrName}, "a built-in's name": {func(p *Proposal) { p.Name = "engineering" }, ErrName}, } { p := research() c.edit(&p) if _, err := Validate(p); !errors.Is(err, c.want) { t.Errorf("%s: %v, want %v", name, err, c.want) } } // A brief of exactly 2 KB and a name in other scripts are fine. p = research() p.Brief, p.Name = strings.Repeat("a", MaxBrief), "Investigación" if _, err := Validate(p); err != nil { t.Fatal(err) } } // TestMenuIsClosed: every profile on the menu keeps web.research apart // from everything else, and its tools are its own; a profile is a copy, // so a caller cannot widen the menu. func TestMenuIsClosed(t *testing.T) { if len(Profiles()) == 0 { t.Fatal("no profiles") } for _, p := range Profiles() { if slices.Contains(p.Capabilities, CapabilityWebResearch) && (len(p.Capabilities) != 1 || !slices.Equal(p.Tools, []string{ToolSubmitFindings, ToolKeepImage})) { t.Fatalf("%s mixes web.research: %+v", p.ID, p) } for _, c := range p.Capabilities { if Describe(c) == "" { t.Fatalf("%s: no key for %s", p.ID, c) } } } p, ok := Lookup(ProfileResearch) if !ok { t.Fatal("research-v1 missing") } p.Capabilities[0], p.Tools[0] = "repo.write", "start_task" again, _ := Lookup(ProfileResearch) if again.Capabilities[0] != CapabilityWebResearch || again.Tools[0] != ToolSubmitFindings { t.Fatalf("the menu changed through a lookup: %+v", again) } if _, ok := Lookup("release-v1"); ok { t.Fatal("a profile that is not built yet") } }