Files
magpie/internal/library/skill_from_test.go
T
yetone ce9fc6758a library: on Windows without the right to make symlinks, skills are linked with junctions (#973) — with Developer Mode off and magpie not elevated, os.Symlink fails with "A required privilege is not held by the client": a skill installed from a folder became a detached copy, and bringing in a skill from ~/.agents/skills failed outright. The library's folder links (an agent's link to the library's skill, a folder source, a skill brought in from a link or the shared folder, a carried skill's folder, relink) now go through dirLink, which on that error makes a directory junction instead: one every user may make, followed by every program as a symlink to a folder is, made natively (FSCTL_SET_REPARSE_POINT, no mklink). A target a junction can't reach (a network share) still gets the old copy. Go reports a junction as irregular, not a symlink, so ours, unlink, carry's clearing of the library, a project's place and the CC Switch update now tell a link by linkEntry (symlink or junction) and take it away with os.Remove, leaving what it points at, even when where it points can't be read. On the Windows box with every privilege stripped from the token (CreateRestrictedToken DISABLE_MAX_PRIVILEGE, whoami /priv shows SeChangeNotify only) and an explicit sandbox environment, the library's tests went from 16 failures to none (TestSkillHow, TestProjectSkillsLinked, TestCarry, TestSkillCopyEditReachesTheOthers, TestImportSharedSkillForKimi …); with the privilege they pass too. TestJunctionIsALink (Windows: magpie's own, read through, unlinked without its target, none to a share) is new; the folder fixtures that ignored a failed os.Symlink now use dirLink, and the tests' link checks accept a junction. go test -race ./internal/library/ on the Mac, go vet, windows/linux(nogui) builds pass.
2026-10-06 13:32:03 +08:00

133 lines
5.3 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
)
func repoIn(t *testing.T) map[string]string {
t.Helper()
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
out := map[string]string{}
for _, s := range v.Skills {
out[s.Name] = s.Repo
}
return out
}
// A skill not installed from GitHub is grouped with the repository it
// came from all the same (White Immortal on Discord): the skills CLI's
// lock names it, or the git checkout it's in, whatever the remote's form,
// and never with a user or token the remote had. A skill in ~/.claude kept
// in git (the user's dotfiles) is not from that repository.
func TestSkillRepoTraced(t *testing.T) {
h := sandbox(t)
// npx skills add mattpocock/skills: in ~/.agents/skills, in its lock
skill(t, filepath.Join(h, ".agents/skills/grill-me"), "grill-me", "Grill")
write(t, filepath.Join(h, ".agents/.skill-lock.json"), `{"version":3,"skills":{
"grill-me":{"source":"mattpocock/skills","sourceType":"github","sourceUrl":"https://github.com/mattpocock/skills.git","skillPath":"skills/grill-me/SKILL.md"},
"tdd":{"source":"x","sourceType":"git","sourceUrl":"git@gitlab.com:team/skills.git"}}}`)
os.MkdirAll(filepath.Join(h, ".claude/skills"), 0o755)
dirLink(filepath.Join(h, ".agents/skills/grill-me"), filepath.Join(h, ".claude/skills/grill-me"))
// a clone of the repository, linked from the library
clone := filepath.Join(h, "code/skills")
skill(t, filepath.Join(clone, "skills/write-a-prd"), "write-a-prd", "PRD")
write(t, filepath.Join(clone, ".git/config"), "[core]\n\tbare = false\n[remote \"upstream\"]\n\turl = https://github.com/other/fork.git\n[remote \"origin\"]\r\n\turl = https://me:ghp_secret@github.com/mattpocock/skills.git\r\n")
// a skill that is a clone of its own, moved in
skill(t, filepath.Join(h, ".claude/skills/review"), "review", "Review")
write(t, filepath.Join(h, ".claude/skills/review/.git/config"), "[remote \"origin\"]\n\turl = git@gitlab.com:me/review.git\n")
// ~/.claude in git: the user's dotfiles
write(t, filepath.Join(h, ".claude/.git/config"), "[remote \"origin\"]\n\turl = git@github.com:me/dotfiles.git\n")
skill(t, filepath.Join(h, ".claude/skills/notes"), "notes", "Mine")
ok(t)(ImportSkills([]string{"grill-me", "review", "notes"}))
p, err := ProbeSkills(filepath.Join(clone, "skills/write-a-prd"))
if err != nil || len(p.Candidates) != 1 {
t.Fatalf("probe: %+v %v", p, err)
}
ok(t)(InstallSkills(filepath.Join(clone, "skills/write-a-prd"), []string{p.Candidates[0].Path}, nil))
got := repoIn(t)
want := map[string]string{"grill-me": "mattpocock/skills", "write-a-prd": "mattpocock/skills", "review": "gitlab.com/me/review", "notes": ""}
for n, r := range want {
if got[n] != r {
t.Errorf("%s: repo %q, want %q", n, got[n], r)
}
}
b, _ := os.ReadFile(path())
if strings.Contains(string(b), "ghp_secret") {
t.Error("the remote's token was written down")
}
// the clone moved in kept where it came from
if s := mustLoad(t).skill("review"); s.From != "gitlab.com/me/review" {
t.Errorf("review's From: %q", s.From)
}
}
// The remote forms git takes, each read as the repository.
func TestRemoteRepo(t *testing.T) {
for in, want := range map[string]string{
"https://github.com/mattpocock/skills": "mattpocock/skills",
"https://www.github.com/mattpocock/skills.git/": "mattpocock/skills",
"git@github.com:mattpocock/skills.git": "mattpocock/skills",
"ssh://git@github.com/mattpocock/skills.git": "mattpocock/skills",
"https://user:tok@github.com/a/b/tree/main/x": "a/b",
"git@gitlab.com:group/sub/proj.git": "gitlab.com/group/sub/proj",
"https://tok@codeberg.org/me/s.git": "codeberg.org/me/s",
"mattpocock/skills": "mattpocock/skills",
"/Users/me/skills": "",
"https://github.com/onlyowner": "",
"https://example.com/../etc": "",
} {
if got := remoteRepo(in); got != want {
t.Errorf("%s: %q, want %q", in, got, want)
}
}
}
// Installing from a repository again groups the skills the library has
// already from it (White Immortal: installed before, from that very
// address): those whose SKILL.md is the repository's own. One of the same
// name that differs is someone else's skill, and stays where it was.
func TestInstallAgainNotesFrom(t *testing.T) {
h := sandbox(t)
newFakeGitHub(t)
// the fake repository's skills/pdf/SKILL.md, as installed by hand
write(t, filepath.Join(h, ".claude/skills/pdf/SKILL.md"), "---\nname: pdf\ndescription: PDFs\n---\n")
ok(t)(ImportSkill("pdf"))
if r := repoIn(t)["pdf"]; r != "" {
t.Fatalf("pdf has a repo before: %q", r)
}
if _, err := ProbeSkills("someone/fromagain"); err != nil {
t.Fatal(err)
}
if r := repoIn(t)["pdf"]; r != "someone/fromagain" {
t.Errorf("pdf after installing from its repository again: %q", r)
}
// another's pdf, not the same file: not noted
h = sandbox(t)
write(t, filepath.Join(h, ".claude/skills/pdf/SKILL.md"), "---\nname: pdf\ndescription: My own\n---\n")
ok(t)(ImportSkill("pdf"))
if _, err := ProbeSkills("someone/notmine"); err != nil {
t.Fatal(err)
}
if r := repoIn(t)["pdf"]; r != "" {
t.Errorf("another's pdf grouped with the repository: %q", r)
}
}
func mustLoad(t *testing.T) *Library {
t.Helper()
l, err := load()
if err != nil {
t.Fatal(err)
}
return l
}