package core import ( "context" "errors" "net/url" "os" "path/filepath" "reflect" "strings" "sync" "testing" "skill-manager/internal/fetch" "skill-manager/internal/lock" "skill-manager/internal/manifest" ) // fakeRepo maps revs to fixture directories; head is what an unpinned fetch gets. type fakeRepo struct { head string revs map[string]string } func (r *fakeRepo) push(rev, dir string) { r.revs[rev] = dir r.head = rev } // fakeFetcher serves fakeRepos keyed by flake ref without its query. type fakeFetcher struct { mu sync.Mutex repos map[string]*fakeRepo calls []string } func (f *fakeFetcher) Prefetch(_ context.Context, ref string) (fetch.Result, error) { f.mu.Lock() defer f.mu.Unlock() f.calls = append(f.calls, ref) base, query, _ := strings.Cut(ref, "?") repo, ok := f.repos[base] if !ok { return fetch.Result{}, errors.New("unknown source " + base) } rev := repo.head q, _ := url.ParseQuery(query) if pinned := q.Get("rev"); pinned != "" { rev = pinned } dir, ok := repo.revs[rev] if !ok { return fetch.Result{}, errors.New("unknown rev " + rev) } return fetch.Result{StorePath: dir, Locked: map[string]any{"rev": rev, "narHash": "sha256-" + rev}}, nil } // fixture builds a repo with skills//SKILL.md for each "name" or "name:content". func fixture(t *testing.T, skills ...string) string { t.Helper() root := t.TempDir() for _, s := range skills { name, content, _ := strings.Cut(s, ":") dir := filepath.Join(root, "skills", name) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0o644); err != nil { t.Fatal(err) } } return root } func newStore(t *testing.T) (Store, *fakeFetcher) { f := &fakeFetcher{repos: map[string]*fakeRepo{ "github:mp/skills": {head: "r1", revs: map[string]string{"r1": fixture(t, "tdd", "prototype", "grilling")}}, "github:other/skills": {head: "r1", revs: map[string]string{"r1": fixture(t, "grilling", "deslop:d")}}, }} return Store{Dir: t.TempDir(), Fetcher: f}, f } func names(t *testing.T, s Store) []string { t.Helper() list, err := s.List() if err != nil { t.Fatal(err) } var out []string for _, i := range list { out = append(out, i.Source+":"+i.Name) } return out } func pick(names ...string) []manifest.Skill { var out []manifest.Skill for _, n := range names { name, as, _ := strings.Cut(n, "=") out = append(out, manifest.Skill{Name: name, As: as}) } return out } func TestSourceKey(t *testing.T) { cases := map[string]string{ "github:mattpocock/skills": "mattpocock/skills", "github:mattpocock/skills/main": "mattpocock/skills", "github:mattpocock/skills?ref=main": "mattpocock/skills", "git+ssh://git@git.example.com/jo/skills.git": "jo/skills", "git+https://host/group/sub/repo?ref=x": "sub/repo", } for in, want := range cases { if got := SourceKey(in); got != want { t.Errorf("SourceKey(%q) = %q, want %q", in, got, want) } } } func TestAdd(t *testing.T) { ctx := context.Background() s, f := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Ref: "main", Skills: pick("tdd")}); err != nil { t.Fatal(err) } if f.calls[0] != "github:mp/skills?ref=main" { t.Errorf("first fetch = %q", f.calls[0]) } // Adding to an existing source keeps its pin even if upstream moved. f.repos["github:mp/skills"].push("r2", fixture(t, "tdd:new", "prototype", "grilling")) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("prototype")}); err != nil { t.Fatal(err) } if f.calls[1] != "github:mp/skills?rev=r1" { t.Errorf("second fetch = %q, want pinned to r1", f.calls[1]) } if got, want := names(t, s), []string{"mp/skills:prototype", "mp/skills:tdd"}; !reflect.DeepEqual(got, want) { t.Errorf("installed %v, want %v", got, want) } l, err := lock.Load(filepath.Join(s.Dir, LockFile)) if err != nil { t.Fatal(err) } if rev := l.Sources["mp/skills"].Rev(); rev != "r1" { t.Errorf("locked rev %q, want r1", rev) } } func TestAddErrors(t *testing.T) { ctx := context.Background() s, _ := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("grilling")}); err != nil { t.Fatal(err) } _, err := s.Add(ctx, AddRequest{URL: "github:other/skills"}) if sel, ok := errors.AsType[*NoSelectionError](err); !ok || available(sel.Available) != "deslop, grilling" { t.Errorf("no selection: got %v", err) } if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", Skills: pick("nope")}); err == nil { t.Error("want error for a missing skill") } if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", Skills: pick("grilling")}); err == nil { t.Error("want collision error") } if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("grilling")}); err == nil { t.Error("want error for adding the same skill twice") } // Failed adds must leave nothing behind; an alias resolves the collision. if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", Skills: pick("grilling=grilling-o")}); err != nil { t.Fatal(err) } if got, want := names(t, s), []string{"mp/skills:grilling", "other/skills:grilling-o"}; !reflect.DeepEqual(got, want) { t.Errorf("installed %v, want %v", got, want) } } func TestRemove(t *testing.T) { ctx := context.Background() s, _ := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("tdd", "prototype")}); err != nil { t.Fatal(err) } if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", All: true}); err != nil { t.Fatal(err) } if _, err := s.Remove("deslop"); err == nil { t.Error("want error removing one skill from a * source") } if _, err := s.Remove("tdd"); err != nil { t.Fatal(err) } if _, err := s.Remove("prototype"); err != nil { t.Fatal(err) } if err := s.RemoveSource("other/skills"); err != nil { t.Fatal(err) } m, err := manifest.Load(filepath.Join(s.Dir, ManifestFile)) if err != nil { t.Fatal(err) } if len(m.Sources) != 0 || len(names(t, s)) != 0 { t.Errorf("want everything removed, manifest has %v", m.Sources) } }