61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package vendor
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/m3tam3re/agent-lib/internal/lockfile"
|
||
|
|
)
|
||
|
|
|
||
|
|
func labelsWorkDir(t *testing.T, labelsJSON string) string {
|
||
|
|
t.Helper()
|
||
|
|
dir := t.TempDir()
|
||
|
|
write := func(rel, content string) {
|
||
|
|
p := filepath.Join(dir, rel)
|
||
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
write("skills/alpha/SKILL.md", "---\nname: alpha\ndescription: a\n---\n")
|
||
|
|
write("agents/beta.md", "---\ndescription: b\n---\n")
|
||
|
|
write("mcp/websearch.yaml", "websearch: {}\n")
|
||
|
|
if labelsJSON != "" {
|
||
|
|
write("agent-lib.labels.json", labelsJSON)
|
||
|
|
}
|
||
|
|
return dir
|
||
|
|
}
|
||
|
|
|
||
|
|
func labelErrors(v Validation) []string {
|
||
|
|
var out []string
|
||
|
|
for _, e := range v.Errors {
|
||
|
|
if e.Rule == "labels" {
|
||
|
|
out = append(out, e.Message)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestValidateLabels(t *testing.T) {
|
||
|
|
lf := lockfile.New()
|
||
|
|
if errs := labelErrors(Validate(labelsWorkDir(t, ""), lf)); len(errs) != 0 {
|
||
|
|
t.Errorf("no labels file is fine, got %v", errs)
|
||
|
|
}
|
||
|
|
ok := `{"version":1,"labels":{"sales":["alpha","websearch"],"dev":["beta"]}}`
|
||
|
|
if errs := labelErrors(Validate(labelsWorkDir(t, ok), lf)); len(errs) != 0 {
|
||
|
|
t.Errorf("valid file reported errors: %v", errs)
|
||
|
|
}
|
||
|
|
unknown := `{"version":1,"labels":{"sales":["alpha","gamma"]}}`
|
||
|
|
errs := labelErrors(Validate(labelsWorkDir(t, unknown), lf))
|
||
|
|
if len(errs) != 1 || !strings.Contains(errs[0], `"gamma"`) {
|
||
|
|
t.Errorf("unknown name must be reported once, got %v", errs)
|
||
|
|
}
|
||
|
|
bad := `{"version":1,"labels":{"Sales":["alpha"]}}`
|
||
|
|
if errs := labelErrors(Validate(labelsWorkDir(t, bad), lf)); len(errs) != 1 {
|
||
|
|
t.Errorf("malformed label must be reported, got %v", errs)
|
||
|
|
}
|
||
|
|
}
|