package core import ( "context" "os" "os/exec" "path/filepath" "reflect" "strings" "testing" "skill-manager/internal/fetch" "skill-manager/internal/lock" "skill-manager/internal/manifest" ) func changes(st SourceStatus) []string { var out []string for _, ch := range st.Changes { out = append(out, ch.Kind.Symbol()+ch.Name) } return out } func lockedRev(t *testing.T, s Store, key string) string { t.Helper() l, err := lock.Load(filepath.Join(s.Dir, LockFile)) if err != nil { t.Fatal(err) } if l.Sources[key] == nil { return "" } return l.Sources[key].Rev() } func check(t *testing.T, s Store, names ...string) []SourceStatus { t.Helper() sts, err := s.Check(context.Background(), names...) if err != nil { t.Fatal(err) } for _, st := range sts { if st.Err != nil { t.Fatalf("%s: %v", st.Source, st.Err) } } return sts } func TestCheckModified(t *testing.T) { ctx := context.Background() s, f := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("tdd", "prototype")}); err != nil { t.Fatal(err) } // Upstream moves without touching our skills: not outdated. f.repos["github:mp/skills"].push("r2", fixture(t, "tdd", "prototype", "grilling:changed")) if sts := check(t, s); sts[0].Outdated() { t.Fatalf("unrelated upstream change reported: %v", changes(sts[0])) } f.repos["github:mp/skills"].push("r3", fixture(t, "tdd:v2", "prototype", "new")) sts := check(t, s) if len(sts) != 1 || sts[0].OldRev != "r1" || sts[0].NewRev != "r3" { t.Fatalf("statuses %+v", sts) } if got := changes(sts[0]); !reflect.DeepEqual(got, []string{"↑tdd"}) { t.Errorf("changes %v, want only tdd modified", got) } d, err := Diff(ctx, sts[0].Changes[0]) if err != nil { t.Fatal(err) } if !strings.Contains(d, "b/tdd/SKILL.md") || !strings.Contains(d, "+v2") { t.Errorf("diff:\n%s", d) } if err := s.Apply(sts); err != nil { t.Fatal(err) } if rev := lockedRev(t, s, "mp/skills"); rev != "r3" { t.Errorf("locked %q, want r3", rev) } if sts := check(t, s); sts[0].Outdated() { t.Errorf("still outdated after apply: %v", changes(sts[0])) } } func TestCheckAllSource(t *testing.T) { ctx := context.Background() s, f := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", All: true}); err != nil { t.Fatal(err) } f.repos["github:other/skills"].push("r2", fixture(t, "grilling", "fresh")) sts := check(t, s) if got, want := changes(sts[0]), []string{"-deslop", "+fresh"}; !reflect.DeepEqual(got, want) { t.Errorf("changes %v, want %v", got, want) } d, err := Diff(ctx, sts[0].Changes[0]) if err != nil { t.Fatal(err) } if !strings.Contains(d, "a/deslop/SKILL.md") { t.Errorf("removal diff:\n%s", d) } if err := s.Apply(sts); err != nil { t.Fatal(err) } if got, want := names(t, s), []string{"other/skills:fresh", "other/skills:grilling"}; !reflect.DeepEqual(got, want) { t.Errorf("installed %v, want %v", got, want) } } func TestCheckHandEdits(t *testing.T) { ctx := context.Background() s, _ := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("tdd")}); err != nil { t.Fatal(err) } // Drop mp and add other by editing skills.json directly. path := filepath.Join(s.Dir, ManifestFile) m, err := manifest.Load(path) if err != nil { t.Fatal(err) } delete(m.Sources, "mp/skills") m.Sources["other/skills"] = &manifest.Source{URL: "github:other/skills", Skills: manifest.Selection{Items: pick("deslop")}} if err := m.Save(path); err != nil { t.Fatal(err) } sts := check(t, s) if len(sts) != 2 { t.Fatalf("statuses %+v", sts) } if got := changes(sts[0]); sts[0].Source != "mp/skills" || !reflect.DeepEqual(got, []string{"-tdd"}) { t.Errorf("mp: %s %v", sts[0].Source, got) } if got := changes(sts[1]); sts[1].OldRev != "" || !reflect.DeepEqual(got, []string{"+deslop"}) { t.Errorf("other: %v", got) } msg := CommitMessage(sts) if !strings.HasPrefix(msg, "skills: remove mp/skills, add other/skills@r1\n") { t.Errorf("commit message:\n%s", msg) } if err := s.Apply(sts); err != nil { t.Fatal(err) } if got, want := names(t, s), []string{"other/skills:deslop"}; !reflect.DeepEqual(got, want) { t.Errorf("installed %v, want %v", got, want) } } func TestCheckNames(t *testing.T) { ctx := context.Background() s, _ := newStore(t) if _, err := s.Add(ctx, AddRequest{URL: "github:mp/skills", Skills: pick("tdd")}); err != nil { t.Fatal(err) } if _, err := s.Add(ctx, AddRequest{URL: "github:other/skills", Skills: pick("deslop")}); err != nil { t.Fatal(err) } for name, want := range map[string]string{"deslop": "other/skills", "mp/skills": "mp/skills"} { if sts := check(t, s, name); len(sts) != 1 || sts[0].Source != want { t.Errorf("Check(%q) = %+v, want only %s", name, sts, want) } } if _, err := s.Check(ctx, "nope"); err == nil { t.Error("want error for an unknown name") } } // TestEndToEnd drives add, check and apply through the real nix against a local git repo. func TestEndToEnd(t *testing.T) { if _, err := exec.LookPath("nix"); err != nil { t.Skip("nix not on PATH") } ctx := context.Background() repo := t.TempDir() write := func(rel, content string) { path := filepath.Join(repo, rel) 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) } } git := func(args ...string) string { out, err := exec.Command("git", append([]string{"-C", repo, "-c", "user.name=t", "-c", "user.email=t@t"}, args...)...).CombinedOutput() if err != nil { t.Fatalf("git %v: %v\n%s", args, err, out) } return strings.TrimSpace(string(out)) } write("skills/a/SKILL.md", "a v1\n") write("skills/b/SKILL.md", "b v1\n") git("init", "-q", "-b", "main") git("add", ".") git("commit", "-q", "-m", "v1") s := Store{Dir: t.TempDir(), Fetcher: fetch.Nix{}} if _, err := s.Add(ctx, AddRequest{Name: "local", URL: "git+file://" + repo, Ref: "main", Skills: pick("a")}); err != nil { t.Fatal(err) } v1 := git("rev-parse", "HEAD") if rev := lockedRev(t, s, "local"); rev != v1 { t.Fatalf("locked %q, want %q", rev, v1) } write("skills/a/SKILL.md", "a v2\n") git("commit", "-q", "-am", "v2") sts := check(t, s) if got := changes(sts[0]); !reflect.DeepEqual(got, []string{"↑a"}) { t.Fatalf("changes %v", got) } d, err := Diff(ctx, sts[0].Changes[0]) if err != nil { t.Fatal(err) } if !strings.Contains(d, "-a v1") || !strings.Contains(d, "+a v2") { t.Errorf("diff:\n%s", d) } if err := s.Apply(sts); err != nil { t.Fatal(err) } if rev, v2 := lockedRev(t, s, "local"), git("rev-parse", "HEAD"); rev != v2 { t.Errorf("locked %q, want %q", rev, v2) } }