| 1 | package specialist |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func research() Proposal { |
| 11 | 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}} |
| 12 | } |
| 13 | |
| 14 | // TestValidateTheResearchProfile: the closed profile is accepted exactly, |
| 15 | // in any order, with its name and brief cleaned; anything that is not |
| 16 | // exactly the profile's capabilities and tools is refused, and so is |
| 17 | // web.research beside anything else. |
| 18 | func TestValidateTheResearchProfile(t *testing.T) { |
| 19 | p := research() |
| 20 | p.Name, p.Brief = " Research ", "\n"+p.Brief+"\n" |
| 21 | p.Tools = []string{ToolKeepImage, ToolSubmitFindings} |
| 22 | got, err := Validate(p) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if got.Name != "Research" || got.Brief != research().Brief || !slices.Equal(got.Capabilities, []string{CapabilityWebResearch}) || !slices.Equal(got.Tools, []string{ToolSubmitFindings, ToolKeepImage}) { |
| 27 | t.Fatalf("cleaned %+v", got) |
| 28 | } |
| 29 | for name, c := range map[string]struct { |
| 30 | edit func(*Proposal) |
| 31 | want error |
| 32 | }{ |
| 33 | "an unknown profile": {func(p *Proposal) { p.Profile = "research-v2" }, ErrUnknownProfile}, |
| 34 | "no profile": {func(p *Proposal) { p.Profile = "" }, ErrUnknownProfile}, |
| 35 | "an extra capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "repo.read") }, ErrExclusive}, |
| 36 | "a release capability": {func(p *Proposal) { p.Capabilities = append(p.Capabilities, "release.staging") }, ErrExclusive}, |
| 37 | "another capability alone": {func(p *Proposal) { p.Capabilities = []string{"attachments.read"} }, ErrNotOnMenu}, |
| 38 | "no capability": {func(p *Proposal) { p.Capabilities = nil }, ErrNotOnMenu}, |
| 39 | "a repeated capability": {func(p *Proposal) { p.Capabilities = []string{CapabilityWebResearch, CapabilityWebResearch} }, ErrNotOnMenu}, |
| 40 | "an extra tool": {func(p *Proposal) { p.Tools = append(p.Tools, "start_task") }, ErrExclusive}, |
| 41 | "a native tool": {func(p *Proposal) { p.Tools = append(p.Tools, "Bash") }, ErrExclusive}, |
| 42 | "a missing tool": {func(p *Proposal) { p.Tools = []string{ToolSubmitFindings} }, ErrNotOnMenu}, |
| 43 | "no tools": {func(p *Proposal) { p.Tools = nil }, ErrNotOnMenu}, |
| 44 | "a repeated tool": {func(p *Proposal) { p.Tools = []string{ToolSubmitFindings, ToolKeepImage, ToolKeepImage} }, ErrExclusive}, |
| 45 | "an over-long brief": {func(p *Proposal) { p.Brief = strings.Repeat("a", MaxBrief+1) }, ErrBrief}, |
| 46 | "an empty brief": {func(p *Proposal) { p.Brief = " \n " }, ErrBrief}, |
| 47 | "a brief with a NUL": {func(p *Proposal) { p.Brief = "a\x00b" }, ErrBrief}, |
| 48 | "a brief not UTF-8": {func(p *Proposal) { p.Brief = "a\xffb" }, ErrBrief}, |
| 49 | "an empty name": {func(p *Proposal) { p.Name = " " }, ErrName}, |
| 50 | "a long name": {func(p *Proposal) { p.Name = strings.Repeat("r", MaxName+1) }, ErrName}, |
| 51 | "a name with markup": {func(p *Proposal) { p.Name = "<b>Research</b>" }, ErrName}, |
| 52 | "a name with a newline": {func(p *Proposal) { p.Name = "Re\nsearch" }, ErrName}, |
| 53 | "a built-in's name": {func(p *Proposal) { p.Name = "engineering" }, ErrName}, |
| 54 | } { |
| 55 | p := research() |
| 56 | c.edit(&p) |
| 57 | if _, err := Validate(p); !errors.Is(err, c.want) { |
| 58 | t.Errorf("%s: %v, want %v", name, err, c.want) |
| 59 | } |
| 60 | } |
| 61 | // A brief of exactly 2 KB and a name in other scripts are fine. |
| 62 | p = research() |
| 63 | p.Brief, p.Name = strings.Repeat("a", MaxBrief), "Investigación" |
| 64 | if _, err := Validate(p); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // TestMenuIsClosed: every profile on the menu keeps web.research apart |
| 70 | // from everything else, and its tools are its own; a profile is a copy, |
| 71 | // so a caller cannot widen the menu. |
| 72 | func TestMenuIsClosed(t *testing.T) { |
| 73 | if len(Profiles()) == 0 { |
| 74 | t.Fatal("no profiles") |
| 75 | } |
| 76 | for _, p := range Profiles() { |
| 77 | if slices.Contains(p.Capabilities, CapabilityWebResearch) && (len(p.Capabilities) != 1 || !slices.Equal(p.Tools, []string{ToolSubmitFindings, ToolKeepImage})) { |
| 78 | t.Fatalf("%s mixes web.research: %+v", p.ID, p) |
| 79 | } |
| 80 | for _, c := range p.Capabilities { |
| 81 | if Describe(c) == "" { |
| 82 | t.Fatalf("%s: no key for %s", p.ID, c) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | p, ok := Lookup(ProfileResearch) |
| 87 | if !ok { |
| 88 | t.Fatal("research-v1 missing") |
| 89 | } |
| 90 | p.Capabilities[0], p.Tools[0] = "repo.write", "start_task" |
| 91 | again, _ := Lookup(ProfileResearch) |
| 92 | if again.Capabilities[0] != CapabilityWebResearch || again.Tools[0] != ToolSubmitFindings { |
| 93 | t.Fatalf("the menu changed through a lookup: %+v", again) |
| 94 | } |
| 95 | if _, ok := Lookup("release-v1"); ok { |
| 96 | t.Fatal("a profile that is not built yet") |
| 97 | } |
| 98 | } |
| 99 | |