Files
magpie/internal/library/skill_how_test.go
T
yetone 3c7ceb68f4 library, gui, cli: skills can be given to the agents as copies rather than links, for every agent or one (#896, ADabbler) — the Library linked each skill into the agents (a copy only where it couldn't link: Windows without the right to make links, WSL, Claude Desktop), and some want each agent to have a folder of its own. Skills › under the list, "Give skills as: Links | Copies" sets the library's way (library.json copySkills), and By agent gives an agent its own way (skillHow: Library's way / Links / Copies; Claude Desktop and WSL only ever take copies and have no switch). A copy is the whole skill folder with the .magpie-library marker, made by the same swap as before (.name.magpie-next, the old one set aside), so it is known for magpie's: taking a skill away removes magpie's copy and never a folder of the user's by that name. Switching makes each enabled skill again in place: a link becomes a copy; a copy becomes a link (made beside it and swapped in; a copy edited by hand is kept with the backups first). Where a link can't be made (Windows without the right) it stays a copy, as before. A copy follows the library's skill on every sync (at start, and after any change in the Library) as before; one that differs now (the library's folder edited by hand) is marked "Copy out of date" on the skill, saying in which agents, and a click copies it again. CLI: magpie library skill how link|copy [agent], magpie library skill how default <agent>. en/zh/ja/de. TestSkillHow (temp HOME: links at first, bad ways refused, copies marked, Read's how and copySkills, behind after a hand edit and cleared by a sync, codex its own way, an edited copy kept in the backups, back to the library's way, the user's own same-name folder and own skill untouched both ways, a copy removed with its skill) fails without the change; TestSkillCopyFollowsTheLibrary and TestSkillCopySwapSurvivesAnUndeletableFile now ask for copies rather than leave a copy where links are given, which is made a link again now. skill-how.test.cjs (WebKit, en and zh: the bar, Links/Copies posted with the note and status, the out-of-date tag with its agents and a click syncing without opening the skill, By agent's sheet with Claude Desktop's copies-only row and no switch, an agent's way posted and back to the library's, By agent (1), no select, no scroll, no left border) fails without it. Every Library and skill GUI test and click-scroll pass in WebKit. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 19:46:55 +08:00

143 lines
4.4 KiB
Go

package library
import (
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// #896: skills are given as links or as copies, for every agent or one;
// a change of way makes each again, a copy behind the library's is said
// and made again by a sync, and the user's own folders are never touched.
func TestSkillHow(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
// the user's own skill, which the library has nothing to do with
skill(t, filepath.Join(h, ".claude/skills/mine"), "mine", "Mine")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude", "codex"}))
cl, cx := filepath.Join(h, ".claude/skills/pdf"), filepath.Join(h, ".codex/skills/pdf")
if !isLink(t, cl) || !isLink(t, cx) {
t.Fatal("not linked at first")
}
if _, err := SetSkillHow("", "both"); err == nil {
t.Error("an unknown way was taken")
}
if _, err := SetSkillHow("", ""); err == nil {
t.Error("the library was given no way")
}
// copies for every agent
ok(t)(SetSkillHow("", HowCopy))
for _, p := range []string{cl, cx} {
if isLink(t, p) {
t.Fatalf("%s is still a link", p)
}
if read(t, filepath.Join(p, "SKILL.md")) != read(t, filepath.Join(src, "pdf/SKILL.md")) {
t.Errorf("%s: not a copy of the skill", p)
}
if !ours(p, "pdf") {
t.Errorf("%s: not marked magpie's", p)
}
}
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if !v.CopySkills {
t.Error("the page doesn't say copies")
}
for _, a := range v.Agents {
if (a.ID == "claude" || a.ID == "codex") && (a.How != HowCopy || a.HowOwn) {
t.Errorf("%s: how %q own %v", a.ID, a.How, a.HowOwn)
}
}
if len(v.Skills) != 1 || v.Skills[0].Behind != nil {
t.Fatalf("behind at once: %+v", v.Skills)
}
// the library's skill changes (a folder edited by hand): the copies
// are behind till a sync makes them again
write(t, filepath.Join(src, "pdf/SKILL.md"), "---\nname: pdf\ndescription: Changed\n---\n")
v, _ = Read(nil)
if b := v.Skills[0].Behind; !slices.Equal(b, []string{"claude", "codex"}) {
t.Fatalf("behind: %v", b)
}
ok(t)(Sync())
if !strings.Contains(read(t, filepath.Join(cl, "SKILL.md")), "Changed") {
t.Error("the sync didn't copy it again")
}
if v, _ = Read(nil); v.Skills[0].Behind != nil {
t.Errorf("still behind: %v", v.Skills[0].Behind)
}
// one agent its own way
ok(t)(SetSkillHow("codex", HowLink))
if !isLink(t, cx) || isLink(t, cl) {
t.Fatal("codex isn't a link again, or claude's copy went")
}
v, _ = Read(nil)
for _, a := range v.Agents {
if a.ID == "codex" && (a.How != HowLink || !a.HowOwn) {
t.Errorf("codex: how %q own %v", a.How, a.HowOwn)
}
}
if _, err := SetSkillHow("no such/agent", HowCopy); err == nil {
t.Error("a malformed agent id was given a way")
}
// an edit made in claude's copy isn't lost when it is a link again:
// it is kept with the backups
write(t, filepath.Join(cl, "notes.md"), "my notes\n")
ok(t)(SetSkillHow("", HowLink))
if !isLink(t, cl) {
t.Fatal("claude isn't a link again")
}
var kept []string
filepath.WalkDir(BackupDir(), func(p string, d fs.DirEntry, err error) error {
if err == nil && d.Name() == "notes.md" {
kept = append(kept, p)
}
return nil
})
if len(kept) != 1 || read(t, kept[0]) != "my notes\n" {
t.Errorf("the edited copy wasn't kept: %v", kept)
}
// codex goes the library's way again
ok(t)(SetSkillHow("codex", HowCopy))
ok(t)(SetSkillHow("codex", ""))
if !isLink(t, cx) {
t.Error("codex didn't go back to the library's way")
}
// a skill of the user's by the library's name stands, whichever way
ok(t)(SkillAgents("pdf", []string{"codex"}))
skill(t, cl, "pdf", "The user's own")
for _, how := range []string{HowCopy, HowLink} {
ok(t)(SetSkillHow("", how))
if isLink(t, cl) || ours(cl, "pdf") || !strings.Contains(read(t, filepath.Join(cl, "SKILL.md")), "The user's own") {
t.Fatalf("%s: the user's own folder was touched", how)
}
}
if !strings.Contains(read(t, filepath.Join(h, ".claude/skills/mine/SKILL.md")), "Mine") {
t.Error("the user's own skill was touched")
}
// a copy is taken away when the skill is
ok(t)(SetSkillHow("", HowCopy))
if isLink(t, cx) {
t.Fatal("codex isn't a copy")
}
ok(t)(RemoveSkill("pdf"))
if _, err := os.Lstat(cx); !os.IsNotExist(err) {
t.Error("codex's copy is still there")
}
if _, err := os.Stat(filepath.Join(cl, "SKILL.md")); err != nil {
t.Error("the user's own pdf went with the library's")
}
}