-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskills_test.go
More file actions
257 lines (224 loc) · 6.86 KB
/
skills_test.go
File metadata and controls
257 lines (224 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package iteragent_test
import (
"os"
"path/filepath"
"strings"
"testing"
iteragent "github.com/GrayCodeAI/iteragent"
)
// ---------------------------------------------------------------------------
// parseSkillFile (via LoadSkills + FormatForPrompt as integration)
// ---------------------------------------------------------------------------
func writeSkillFile(t *testing.T, dir, skillName, content string) {
t.Helper()
skillDir := filepath.Join(dir, skillName)
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestLoadSkills_Empty(t *testing.T) {
ss, err := iteragent.LoadSkills([]string{t.TempDir()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(ss.Skills) != 0 {
t.Errorf("expected 0 skills, got %d", len(ss.Skills))
}
}
func TestLoadSkills_MissingDir(t *testing.T) {
ss, err := iteragent.LoadSkills([]string{"/nonexistent/path"})
if err != nil {
t.Fatalf("expected graceful skip for missing dir, got error: %v", err)
}
if len(ss.Skills) != 0 {
t.Errorf("expected 0 skills, got %d", len(ss.Skills))
}
}
func TestLoadSkills_BasicSkill(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "evolve", `---
name: evolve
description: Safely modify your own source code
---
# Evolve skill
Do stuff here.
`)
ss, err := iteragent.LoadSkills([]string{dir})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(ss.Skills) != 1 {
t.Fatalf("expected 1 skill, got %d", len(ss.Skills))
}
s := ss.Skills[0]
if s.Name != "evolve" {
t.Errorf("expected name 'evolve', got %q", s.Name)
}
if s.Description != "Safely modify your own source code" {
t.Errorf("unexpected description: %q", s.Description)
}
if !strings.Contains(s.Content, "Do stuff here") {
t.Errorf("expected body in Content, got %q", s.Content)
}
}
func TestLoadSkills_FallbackSkillMd(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "myskill")
os.MkdirAll(skillDir, 0o755)
// Use skill.md (lowercase) instead of SKILL.md
os.WriteFile(filepath.Join(skillDir, "skill.md"), []byte(`---
name: myskill
description: a test skill
---
body text`), 0o644)
ss, _ := iteragent.LoadSkills([]string{dir})
if len(ss.Skills) != 1 || ss.Skills[0].Name != "myskill" {
t.Errorf("expected skill from skill.md fallback, got %+v", ss.Skills)
}
}
func TestLoadSkills_FallbackNameFromDir(t *testing.T) {
dir := t.TempDir()
// Skill file with no frontmatter name — should fall back to directory name.
writeSkillFile(t, dir, "autoname", "No frontmatter here, just body text.")
ss, _ := iteragent.LoadSkills([]string{dir})
if len(ss.Skills) != 1 || ss.Skills[0].Name != "autoname" {
t.Errorf("expected skill named 'autoname' from dir, got %+v", ss.Skills)
}
}
func TestLoadSkills_Deduplication_LastWins(t *testing.T) {
dir1 := t.TempDir()
dir2 := t.TempDir()
// Same skill name in two dirs — dir2 (later) should win.
writeSkillFile(t, dir1, "myskill", `---
name: myskill
description: version from dir1
---
dir1 body`)
writeSkillFile(t, dir2, "myskill", `---
name: myskill
description: version from dir2
---
dir2 body`)
ss, _ := iteragent.LoadSkills([]string{dir1, dir2})
if len(ss.Skills) != 1 {
t.Fatalf("expected 1 skill after dedup, got %d", len(ss.Skills))
}
if ss.Skills[0].Description != "version from dir2" {
t.Errorf("expected dir2 to win dedup, got description %q", ss.Skills[0].Description)
}
}
func TestLoadSkills_Deduplication_PreservesOrder(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "aardvark", `---
name: aardvark
description: first
---`)
writeSkillFile(t, dir, "zebra", `---
name: zebra
description: last
---`)
ss, _ := iteragent.LoadSkills([]string{dir})
if len(ss.Skills) != 2 {
t.Fatalf("expected 2 skills, got %d", len(ss.Skills))
}
// Both should be present.
names := map[string]bool{}
for _, s := range ss.Skills {
names[s.Name] = true
}
if !names["aardvark"] || !names["zebra"] {
t.Errorf("missing skills: %v", names)
}
}
func TestLoadSkills_SkillGet(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "research", `---
name: research
description: Research skill
---
body`)
ss, _ := iteragent.LoadSkills([]string{dir})
s := ss.Get("research")
if s == nil {
t.Fatal("Get('research') returned nil")
}
if s.Name != "research" {
t.Errorf("expected name 'research', got %q", s.Name)
}
}
func TestLoadSkills_SkillGetMissing(t *testing.T) {
ss := iteragent.SkillSetEmpty()
if ss.Get("nonexistent") != nil {
t.Error("Get on empty set should return nil")
}
}
// ---------------------------------------------------------------------------
// FormatForPrompt
// ---------------------------------------------------------------------------
func TestFormatForPrompt_Empty(t *testing.T) {
ss := iteragent.SkillSetEmpty()
if ss.FormatForPrompt() != "" {
t.Error("empty skill set should return empty prompt")
}
}
func TestFormatForPrompt_ContainsSkillNames(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "evolve", `---
name: evolve
description: Evolution skill
---`)
writeSkillFile(t, dir, "research", `---
name: research
description: Research skill
---`)
ss, _ := iteragent.LoadSkills([]string{dir})
prompt := ss.FormatForPrompt()
if !strings.Contains(prompt, "evolve") {
t.Error("prompt should contain 'evolve'")
}
if !strings.Contains(prompt, "research") {
t.Error("prompt should contain 'research'")
}
if !strings.Contains(prompt, "<available_skills>") {
t.Error("prompt should have <available_skills> tag")
}
if !strings.Contains(prompt, "read_file") {
t.Error("prompt should mention read_file tool")
}
}
// ---------------------------------------------------------------------------
// parseSkillFile edge cases
// directly tested via LoadSkills by reading files we write
// ---------------------------------------------------------------------------
func TestLoadSkills_QuotedFrontmatterValues(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "quoted", `---
name: "quoted skill"
description: 'single quoted desc'
---
body`)
ss, _ := iteragent.LoadSkills([]string{dir})
if len(ss.Skills) != 1 {
t.Fatalf("expected 1 skill, got %d", len(ss.Skills))
}
if ss.Skills[0].Name != "quoted skill" {
t.Errorf("expected name 'quoted skill', got %q", ss.Skills[0].Name)
}
if ss.Skills[0].Description != "single quoted desc" {
t.Errorf("expected desc 'single quoted desc', got %q", ss.Skills[0].Description)
}
}
func TestLoadSkills_NoFrontmatterBody(t *testing.T) {
dir := t.TempDir()
writeSkillFile(t, dir, "plain", "# Just a plain markdown file\n\nNo frontmatter.")
ss, _ := iteragent.LoadSkills([]string{dir})
if len(ss.Skills) != 1 {
t.Fatalf("expected 1 skill, got %d", len(ss.Skills))
}
if !strings.Contains(ss.Skills[0].Content, "plain markdown") {
t.Errorf("full content should be body when no frontmatter, got %q", ss.Skills[0].Content)
}
}