1package skills
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9func TestSplitFrontmatter(t *testing.T) {
10 in := "---\nname: foo\ndescription: bar\n---\nbody text\n"
11 front, body := splitFrontmatter(in)
12 if front != "name: foo\ndescription: bar" {
13 t.Errorf("front = %q", front)
14 }
15 if body != "body text\n" {
16 t.Errorf("body = %q", body)
17 }
18
19 front2, body2 := splitFrontmatter("no frontmatter here")
20 if front2 != "" || body2 != "no frontmatter here" {
21 t.Errorf("expected pass-through, got front=%q body=%q", front2, body2)
22 }
23}
24
25func TestParseFrontmatter(t *testing.T) {
26 front := `name: code-review
27description: "Review a recent change."
28allowed-tools: [read, bash]
29permissions:
30 bash: ["git diff*", "git log*"]
31`
32 s := &Skill{}
33 parseFrontmatter(front, s)
34 if s.Name != "code-review" {
35 t.Errorf("name = %q", s.Name)
36 }
37 if s.Description != "Review a recent change." {
38 t.Errorf("description = %q", s.Description)
39 }
40 if got := s.AllowedTools; len(got) != 2 || got[0] != "read" || got[1] != "bash" {
41 t.Errorf("allowed-tools = %v", got)
42 }
43 if got := s.Permissions["bash"]; len(got) != 2 || got[0] != "git diff*" || got[1] != "git log*" {
44 t.Errorf("permissions[bash] = %v", got)
45 }
46}
47
48func TestDiscoverProjectAndGlobalPriorityAndDedup(t *testing.T) {
49 tmp := t.TempDir()
50 zotHome := filepath.Join(tmp, "home")
51 cwd := filepath.Join(tmp, "proj")
52
53 mk := func(dir, name, desc string) {
54 full := filepath.Join(dir, name)
55 os.MkdirAll(full, 0o755)
56 body := "---\nname: " + name + "\ndescription: " + desc + "\n---\n# " + name + "\n"
57 os.WriteFile(filepath.Join(full, "SKILL.md"), []byte(body), 0o644)
58 }
59
60 // Same skill name in BOTH project and global; project should win.
61 mk(filepath.Join(cwd, ".zot", "skills"), "shared", "project version")
62 mk(filepath.Join(zotHome, "skills"), "shared", "global version")
63 // Unique skill in global only.
64 mk(filepath.Join(zotHome, "skills"), "global-only", "from global")
65
66 skills, errs := Discover(zotHome, cwd, "", true /* includeUser */)
67 if len(errs) > 0 {
68 t.Fatalf("errs: %v", errs)
69 }
70 // Expect the two user skills + every built-in shipped with the
71 // binary (currently the write-zot-extension authoring guide).
72 builtins := loadBuiltins()
73 want := 2 + len(builtins)
74 if len(skills) != want {
75 t.Fatalf("expected %d skills (2 user + %d built-in), got %d (%v)", want, len(builtins), len(skills), skills)
76 }
77 shared := FindByName(skills, "shared")
78 if shared == nil || shared.Description != "project version" {
79 t.Errorf("expected project to win for 'shared', got %v", shared)
80 }
81 if FindByName(skills, "global-only") == nil {
82 t.Errorf("global-only skill missing")
83 }
84 // At least one built-in should have made it through.
85 for _, b := range builtins {
86 if FindByName(skills, b.Name) == nil {
87 t.Errorf("built-in skill %q missing from Discover output", b.Name)
88 }
89 }
90}
91
92func TestVisibleSkillsHidesBuiltins(t *testing.T) {
93 in := []*Skill{
94 {Name: "user-one"},
95 {Name: "built-one", Builtin: true},
96 {Name: "user-two"},
97 }
98 out := VisibleSkills(in)
99 if len(out) != 2 {
100 t.Fatalf("expected 2 visible skills, got %d (%v)", len(out), out)
101 }
102 for _, s := range out {
103 if s.Builtin {
104 t.Errorf("built-in %q leaked into visible set", s.Name)
105 }
106 }
107}
108
109func TestSystemPromptAddendum(t *testing.T) {
110 skills := []*Skill{
111 {Name: "built-a", Description: "Do A.", Builtin: true},
112 {Name: "user-b", Description: "Do B.", Path: "/tmp/skills/user-b/SKILL.md"},
113 }
114 out := SystemPromptAddendum(skills)
115 if !contains(out, "- built-a [builtin]: Do A.\n") {
116 t.Errorf("builtin entry missing or wrong:\n%s", out)
117 }
118 if !contains(out, "- user-b [/tmp/skills/user-b/SKILL.md]: Do B.\n") {
119 t.Errorf("user entry missing path pointer:\n%s", out)
120 }
121}
122
123func contains(s, sub string) bool {
124 return len(s) >= len(sub) && (s == sub || (len(sub) > 0 && stringIndex(s, sub) >= 0))
125}
126
127func stringIndex(s, sub string) int {
128 for i := 0; i+len(sub) <= len(s); i++ {
129 if s[i:i+len(sub)] == sub {
130 return i
131 }
132 }
133 return -1
134}