Files
skm/internal/core/import_test.go
T
johannesandClaude Opus 5.5 65a96ada8e feat: skm, pin agent skills from git repos via Nix
CLI (add, remove, list, outdated, update, import) over a skills.json manifest and a skills.lock.json pinned with nix flake prefetch, plus a home-manager module that links each locked skill via builtins.fetchTree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:04:50 +02:00

113 lines
3.9 KiB
Go

package core
import (
"context"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"skill-manager/internal/manifest"
)
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestImport(t *testing.T) {
ctx := context.Background()
s, _ := newStore(t)
// Already managed before the import: kept, not duplicated.
if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("prototype")}); err != nil {
t.Fatal(err)
}
vercel := filepath.Join(t.TempDir(), ".skill-lock.json")
writeFile(t, vercel, `{"version": 3, "skills": {
"tdd": {"source": "mp/skills", "sourceType": "github", "skillPath": "skills/tdd/SKILL.md"},
"prototype": {"source": "mp/skills", "sourceType": "github", "skillPath": "skills/prototype/SKILL.md"},
"gone": {"source": "mp/skills", "sourceType": "github", "skillPath": "skills/gone/SKILL.md"},
"deslop": {"source": "other/skills", "sourceType": "github", "skillPath": "skills/deslop/SKILL.md"},
"grill-o": {"source": "other/skills", "sourceType": "github", "skillPath": "skills/grilling/SKILL.md"},
"x": {"source": "x", "sourceType": "local", "skillPath": "x/SKILL.md"}
}}`)
local := t.TempDir()
writeFile(t, filepath.Join(local, "tdd", "SKILL.md"), "")
writeFile(t, filepath.Join(local, "deslop", "SKILL.md"), "my edits")
writeFile(t, filepath.Join(local, "c4-model", "SKILL.md"), "")
if err := os.MkdirAll(filepath.Join(local, "synced"), 0o755); err != nil {
t.Fatal(err)
}
rep, err := s.Import(ctx, vercel, local)
if err != nil {
t.Fatal(err)
}
var got []string
for _, imp := range rep.Imported {
got = append(got, imp.Name+"="+[]string{"same", "differs", "nolocal"}[imp.Status])
}
want := []string{"prototype=nolocal", "tdd=same", "deslop=differs", "grill-o=nolocal"}
if !reflect.DeepEqual(got, want) {
t.Errorf("imported %v, want %v", got, want)
}
if len(rep.Skipped) != 2 || !strings.HasPrefix(rep.Skipped[0], "x:") || !strings.HasPrefix(rep.Skipped[1], "gone:") {
t.Errorf("skipped %v", rep.Skipped)
}
if !reflect.DeepEqual(rep.Untracked, []string{"c4-model"}) {
t.Errorf("untracked %v", rep.Untracked)
}
m, err := manifest.Load(filepath.Join(s.Dir, ManifestFile))
if err != nil {
t.Fatal(err)
}
if got, want := m.Sources["other/skills"].Skills.Items, pick("deslop", "grilling=grill-o"); !reflect.DeepEqual(got, want) {
t.Errorf("other/skills items %v, want %v", got, want)
}
if got, want := m.Sources["mp/skills"].Skills.Items, pick("prototype", "tdd"); !reflect.DeepEqual(got, want) {
t.Errorf("mp/skills items %v, want %v", got, want)
}
// Running it again changes nothing.
if _, err := s.Import(ctx, vercel, local); err != nil {
t.Fatal(err)
}
if got := names(t, s); len(got) != 4 {
t.Errorf("after re-import: %v", got)
}
}
func TestDuplicateNames(t *testing.T) {
ctx := context.Background()
root := t.TempDir()
writeFile(t, filepath.Join(root, "a", "skills", "pr", "SKILL.md"), "")
writeFile(t, filepath.Join(root, "b", "skills", "pr", "SKILL.md"), "")
f := &fakeFetcher{repos: map[string]*fakeRepo{"github:c/plugins": {head: "r1", revs: map[string]string{"r1": root}}}}
s := Store{Dir: t.TempDir(), Fetcher: f}
if _, err := s.Add(ctx, AddRequest{URL: "github:c/plugins", Skills: pick("pr")}); err == nil || !strings.Contains(err.Error(), "several skills") {
t.Errorf("ambiguous name: %v", err)
}
if _, err := s.Add(ctx, AddRequest{URL: "github:c/plugins", All: true}); err == nil {
t.Error(`want error for "*" over duplicate names`)
}
if _, err := s.Add(ctx, AddRequest{URL: "github:c/plugins", Skills: []manifest.Skill{{Path: "b/skills/pr"}}}); err != nil {
t.Fatal(err)
}
list, err := s.List()
if err != nil {
t.Fatal(err)
}
if len(list) != 1 || list[0].Name != "pr" || list[0].Path != "b/skills/pr" {
t.Errorf("installed %+v", list)
}
}