Files
magpie/internal/library/shared_skills_test.go
T
yetone 7547dfb3c9 library: a skill in the library's own folder is never copied onto itself, is shown and brought in where it is, and one kept in ~/.agents/skills isn't linked a second time into the agents that read that folder (#595)
"Add from folder" pointed at a skill already in ~/.config/magpie/library/skills emptied every file of it: InstallSkills' symlink failed (it was there) and its fallback copied the folder onto itself, O_TRUNC first. A folder from elsewhere by a name the library's folder already had was copied into it the same way. Such a folder is now refused ("…is in the library's folder already: bring it in from the skills found there"), anything by that name in the library's folder is never written into, the copy fallback runs only when the symlink failed for another reason, and copyDir refuses a destination that is its source or inside it.

A skill put in the library's folder by hand, not in library.json, was shown nowhere: foundSkills now scans the library's folder too, first, and such a one gets FoundSkill.Library (magpie's links to it in the agents count as the agents having it). Bringing it in lists it where it is: nothing moved or linked, Source nil for a folder of its own, {folder, its real path} for a link. The found row says "in the library's folder, not listed" with its path, and Bring in that it is listed where it is (en, zh).

Codex, Gemini CLI and the others that read ~/.agents/skills themselves got a ~/.codex/skills/X link to a library skill whose real folder is in ~/.agents/skills, and saw it twice. A user's own entry there (not magpie's link) that is the very folder the library's is now counts as the skill being in the shared folder: no link of their own is made, magpie's made before are taken away, and a byte copy of their own is kept aside with the backups as before. Nothing in ~/.agents/skills is moved or removed. SkillView.Always names the agents that read that folder, and their chips are lit, disabled, and say "Codex reads ~/.agents/skills itself, where this skill is kept — it has it whatever is ticked here" (en, zh); a click on another chip or All neither sends nor darkens them.

A skill's kind and "linked from" are now what its library entry is, not what was written when it came in: a linked folder moved in by hand is the library's own, shown without "linked from", and its Remove says the folder goes to the backups, as removeSkill does (which now also counts a junction as a link, as the page does).

TestAddFromLibraryFolderKeepsIt (the skill's folder and the library's skills folder as the source, another folder by that name, copyDir onto and into itself: bytes unchanged), TestFoundInLibraryFolder, TestSharedSkillNotLinkedTwice and TestSkillKindIsWhatTheFolderIs are new and fail without the change (the first with SKILL.md and run.sh emptied); TestSharedAgentsSkills now asserts Codex and Gemini CLI get no second link (Pi keeps its) and are Always. library-own-folder.test.cjs (WebKit, en and zh) is new and fails without the change but for its Remove case; library-all, library-items, library-loading, library-row-open-scroll, library-toggle-status, click-scroll, import-all-skills, own-skill, every-skill, remove-all-skills, by-agent and market-have pass in WebKit. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 02:03:12 +08:00

184 lines
7.1 KiB
Go

package library
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"testing"
)
func symlink(t *testing.T, to, at string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(at), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(to, at); err != nil {
t.Skipf("no symlinks here: %v", err)
}
}
// #227: the user-wide ~/.agents/skills is found, links to it (symlinks,
// junctions on Windows) are the same skill, and bringing one in leaves the
// shared folder as it is.
func TestSharedAgentsSkills(t *testing.T) {
h := sandbox(t)
// no agent here whose own folder the shared one is (Goose's)
os.RemoveAll(filepath.Join(h, ".config/goose"))
shared := filepath.Join(h, ".agents/skills")
// a skill kept elsewhere (D:\aimer-skills\…) linked into the shared folder
ext := filepath.Join(h, "aimer-skills/grilling")
skill(t, ext, "grilling", "Grill")
symlink(t, ext, filepath.Join(shared, "grilling"))
// real folders in the shared folder
skill(t, filepath.Join(shared, "orchestration"), "orchestration", "Orchestrate")
skill(t, filepath.Join(shared, "orca-cli"), "orca-cli", "Orca")
// agents' links to it: absolute, relative, and to a link
symlink(t, filepath.Join(shared, "orchestration"), filepath.Join(h, ".pi/agent/skills/orchestration"))
symlink(t, filepath.Join("..", "..", ".agents", "skills", "orchestration"), filepath.Join(h, ".codex/skills/orchestration"))
symlink(t, filepath.Join(shared, "grilling"), filepath.Join(h, ".gemini/skills/grilling"))
// a byte copy of its own
skill(t, filepath.Join(h, ".codex/skills/orca-cli"), "orca-cli", "Orca")
// not a skill, and a hidden one
write(t, filepath.Join(shared, "notes/README.md"), "")
skill(t, filepath.Join(shared, ".hidden"), "hidden", "")
found := func() map[string]FoundSkill {
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
m := map[string]FoundSkill{}
for _, f := range v.FoundSkills {
m[f.Name] = f
}
return m
}
byName := found()
if len(byName) != 3 {
t.Fatalf("found: %+v", byName)
}
if f := byName["grilling"]; f.Shared != filepath.Join(shared, "grilling") || f.Link != realDir(ext) || !slices.Equal(f.Agents, []string{"gemini"}) || len(f.Others) != 0 {
t.Errorf("grilling: %+v", f)
}
f := byName["orchestration"]
agents := slices.Sorted(slices.Values(f.Agents))
if f.Shared == "" || f.Link != "" || !slices.Equal(agents, []string{"codex", "pi"}) || len(f.Others) != 0 {
t.Errorf("orchestration is one skill in the shared folder, pi and codex: %+v", f)
}
if f := byName["orca-cli"]; f.Shared == "" || len(f.Agents) != 0 || len(f.Others) != 0 || !slices.Equal(f.Copies, []string{"codex"}) {
t.Errorf("orca-cli: %+v", f)
}
ok(t)(ImportSkill("orchestration"))
ok(t)(ImportSkill("grilling"))
ok(t)(ImportSkill("orca-cli"))
// the shared folder is as it was
for _, n := range []string{"orchestration", "orca-cli"} {
if fi, err := os.Lstat(filepath.Join(shared, n)); err != nil || !fi.IsDir() {
t.Errorf("%s left the shared folder: %v", n, err)
}
if _, err := os.Stat(filepath.Join(shared, n, "scripts/run.sh")); err != nil {
t.Errorf("%s's files: %v", n, err)
}
}
if !linked(filepath.Join(shared, "grilling")) {
t.Error("the shared folder's link went")
}
if _, err := os.Stat(filepath.Join(ext, "SKILL.md")); err != nil {
t.Error("the folder the shared link pointed to went")
}
// the library links to them
for n, want := range map[string]string{"orchestration": filepath.Join(shared, "orchestration"), "grilling": ext, "orca-cli": filepath.Join(shared, "orca-cli")} {
if !linked(skillDir(n)) || realDir(skillDir(n)) != realDir(want) {
t.Errorf("the library's %s isn't a link to %s", n, want)
}
}
// the agents that had it have the library's; Codex and Gemini CLI read
// ~/.agents/skills, where all three are kept, so a link in their own
// folders would be each skill twice (#595): they get none, and Codex's
// byte copy of its own is kept aside with the backups
if !ours(filepath.Join(h, ".pi/agent/skills/orchestration"), "orchestration") {
t.Error("pi's orchestration isn't the library's")
}
for _, d := range []string{".codex/skills/orchestration", ".gemini/skills/grilling", ".codex/skills/orca-cli"} {
if _, err := os.Lstat(filepath.Join(h, d)); !os.IsNotExist(err) {
t.Errorf("%s is there, besides the shared folder's: %v", d, err)
}
}
if m, _ := filepath.Glob(filepath.Join(BackupDir(), "*", "codex", "skills", "orca-cli", "SKILL.md")); len(m) != 1 {
t.Errorf("codex's own copy isn't kept aside: %v", m)
}
v, _ := Read(nil)
for _, s := range v.Skills {
if s.Kind != "folder" {
t.Errorf("%s: %+v", s.Name, s)
}
// every agent reading the shared folder has it, ticked or not
for _, id := range []string{"codex", "gemini"} {
if !slices.Contains(s.Always, id) {
t.Errorf("%s: %s isn't said to have it always: %v", s.Name, id, s.Always)
}
}
if slices.Contains(s.Always, "pi") || slices.Contains(s.Always, "claude") {
t.Errorf("%s: always %v", s.Name, s.Always)
}
}
if byName := found(); len(byName) != 0 {
t.Errorf("still found: %+v", byName)
}
}
func TestRealDir(t *testing.T) {
d := t.TempDir()
skill(t, filepath.Join(d, "a/real"), "real", "")
symlink(t, filepath.Join(d, "a/real"), filepath.Join(d, "b/abs"))
symlink(t, filepath.Join("..", "b", "abs"), filepath.Join(d, "c/rel"))
symlink(t, filepath.Join(d, "c"), filepath.Join(d, "dir"))
want, _ := filepath.EvalSymlinks(filepath.Join(d, "a/real"))
for _, p := range []string{"a/real", "b/abs", "c/rel", "dir/rel", "dir/rel/scripts/.."} {
if got := realDir(filepath.Join(d, p)); got != want {
t.Errorf("%s: %s, want %s", p, got, want)
}
}
if got := realDir(filepath.Join(d, "dir/rel/scripts")); got != filepath.Join(want, "scripts") {
t.Errorf("inside: %s", got)
}
// a loop, and what isn't there: the path as it is
symlink(t, filepath.Join(d, "loop2"), filepath.Join(d, "loop1"))
symlink(t, filepath.Join(d, "loop1"), filepath.Join(d, "loop2"))
for _, p := range []string{filepath.Join(d, "loop1"), filepath.Join(d, "gone/x")} {
if got := realDir(p); got != p {
t.Errorf("%s: %s", p, got)
}
}
if linked(filepath.Join(d, "a/real")) || !linked(filepath.Join(d, "c/rel")) {
t.Error("isLink")
}
if !within(filepath.Join(d, "a", "x"), filepath.Join(d, "a")) || within(filepath.Join(d, "ab"), filepath.Join(d, "a")) || within(d, filepath.Join(d, "a")) {
t.Error("within")
}
}
// A junction (what Windows makes without the right to symlink, and what
// the issue's ZCode and Pi folders were) is a link to the folder it points to.
func TestJunction(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("junctions are Windows'")
}
d := t.TempDir()
skill(t, filepath.Join(d, "shared/orchestration"), "orchestration", "")
j := filepath.Join(d, "zcode", "orchestration")
os.MkdirAll(filepath.Dir(j), 0o755)
if out, err := exec.Command("cmd", "/c", "mklink", "/J", j, filepath.Join(d, "shared", "orchestration")).CombinedOutput(); err != nil {
t.Skipf("mklink /J: %v %s", err, out)
}
if !linked(j) {
t.Error("a junction isn't a link")
}
if realDir(j) != realDir(filepath.Join(d, "shared", "orchestration")) {
t.Errorf("%s != %s", realDir(j), realDir(filepath.Join(d, "shared", "orchestration")))
}
}