Files
magpie/internal/library/skill_beside_test.go
T
yetone 20482d71ff library: a skill by a name the library has from elsewhere goes in beside it
mora1n on Discord: anthropics/skills' skill-creator and openai/skills'
are two skills by one name, and only the first could be added; the
second was skipped as "another skill called skill-creator, left as it
is". The library keyed being had by name in every place that asked.

Now being had is decided by source (hadSkill): the same source under any
name here, or a skill with no source (taken in from an agent's folder)
by that name with the very same files. Another skill from GitHub or a
git repository whose name is taken goes in beside it as
<name>-<owner> (skill-creator-openai), then owner-repo, repo, a number,
at most 64 bytes (freeSkillName in skill_beside.go). The library's copy
of its SKILL.md gets that name in the front matter's top-level name:
line (nameSkill), so no agent sees two skills by one name; Skill.Called
keeps the name it came with, and the carried bundle carries it. Its hash
is of the renamed copy, and checks (GitHub and git) and updates compare
what they fetch renamed the same way (fetchedHash), so it doesn't read
as edited and an update keeps the name.

The class: probe (Candidate.Have now means this skill; Candidate.As is
the name it has or would get), installFrom (listed and unlisted folder
by that name), the new-skills check (no longer drops a skill because
its name is taken), the market's had check (by name only for a skill
with no repository: none or a folder), carry, the TUI's probe list and
the GUI's probe list ("adds as {name}" with why in its title, "In the
library as {name}"; zh, zh-TW, ja, de; the tag is cut short in its row).

Old data: nothing is renamed. A skill already in the library keeps its
name, folder, SKILL.md and agent links; library.json without "called"
reads as before. Only a skill added from now on can get another name.
Not changed: a folder of the user's own is linked as it is and its
SKILL.md can't be named apart, so one by a taken name is still skipped,
now saying to rename it in its SKILL.md; and an agent's own skills
folder still holds one skill per name (an agent's own skill by the
library's name is settled by Use library / Keep agent's as before).

Run (mote, at c6fd4b37c on origin/main 6a900e0d0, then rebased on
0fe9f97c1, which touches only claudetui, gui and provider):
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build ./..., go build -tags nogui:
  clean.
- go test -tags nogui -count=1 -timeout 45m ./...: 82 packages ok,
  internal/gui red only in TestSettingsSaveKeepsWhatItDoesNotSend
  (SearchAll, #4514, open on main, not this change).
- After the rebase: internal/library and internal/tui ok.
- GUI, both engines: skill-install-beside (new), every skill-*,
  library-*, found-skill-*, remove-found-*, mcp-*, i18n-keys, gui-ja,
  gui-de, gui-zh-tw, font-cascade, inner-scroll-contain, win-fonts:
  1427 tests, 1423 pass. The 4 reds fail the same at origin/main
  3baf3b6df run alone: gui-ja (#4580), i18n-keys "Unmarked" (#4524),
  library-row-remove chromium (#3099).

Red on the old code: the anthropics-then-openai install against
origin/main code fails with "the library already has another skill
called skill-creator, left as it is". Broken by hand: fetchedHash made
to hash the fetched files unrenamed turns
TestSameNameFromAnotherSourceGoesInBeside red (hash mismatch); the
.lib-pick > .lib-tag rule removed turns skill-install-beside red ("the
tag stays in its row") in both widths.

Not tried: real anthropics/skills and openai/skills installs in a
sandbox HOME (the tests serve both repositories' layouts as tarballs;
the fixtures put both at skills/skill-creator. The real openai/skills
keeps it under skills/.system, which a probe of the repository's top
doesn't walk into; that is how probing has been and isn't changed
here); Windows and Linux runs of the GUI.
2026-11-01 02:02:24 +08:00

232 lines
9.4 KiB
Go

package library
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// skillCreators serves anthropics/skills and openai/skills, each with a
// skill-creator of its own, laid out as the two repositories have them:
// anthropics' and openai's in skills/. version is what openai's says it
// is.
func skillCreators(t *testing.T, version *string) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/anthropics/skills/"):
w.Write(tarball(t, map[string]string{
"skills/skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Guide for creating effective skills (Anthropic).\nlicense: Complete terms in LICENSE.txt\n---\n\n# Skill Creator\n",
"skills/skill-creator/LICENSE.txt": "Apache",
}))
case strings.HasPrefix(r.URL.Path, "/openai/skills/"):
w.Write(tarball(t, map[string]string{
"skills/skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Guide for creating effective skills (OpenAI " + *version + ").\nmetadata:\n short-description: Create or update a skill\n---\n\n# Skill Creator\n",
}))
case strings.HasPrefix(r.URL.Path, "/acme/skills/"):
w.Write(tarball(t, map[string]string{
"skill-creator/SKILL.md": "---\nname: skill-creator\ndescription: Acme's own\n---\n",
}))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
old := tarballURL
tarballURL = func(repo, ref string) string { return srv.URL + "/" + repo + "/" + ref }
t.Cleanup(func() { tarballURL = old })
}
// mora1n on Discord: anthropics' skill-creator and openai's are two
// skills by one name, and only the first could be installed; the second
// was "left as it is". Now the second goes in beside the first under a
// name of its own, its source's owner added, and every agent gets both.
// The first keeps its name, folder and links.
func TestSameNameFromAnotherSourceGoesInBeside(t *testing.T) {
h := sandbox(t)
version := "one"
skillCreators(t, &version)
ok(t)(InstallSkills("anthropics/skills", []string{"skills/skill-creator"}, []string{"claude"}))
anthropic := read(t, filepath.Join(skillDir("skill-creator"), "SKILL.md"))
p, err := ProbeSkills("openai/skills")
if err != nil {
t.Fatal(err)
}
if len(p.Candidates) != 1 || p.Candidates[0].Have || p.Candidates[0].As != "skill-creator-openai" {
t.Fatalf("openai's probed as %+v", p.Candidates)
}
r := ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, []string{"claude"}))
if !slices.Equal(r.Installed, []string{"skill-creator-openai"}) || len(r.Skipped) != 0 {
t.Fatalf("installed %v skipped %+v", r.Installed, r.Skipped)
}
l, _ := load()
first, second := l.skill("skill-creator"), l.skill("skill-creator-openai")
if first == nil || first.Source.Repo != "anthropics/skills" || first.Called != "" {
t.Errorf("anthropics' is now %+v", first)
}
if second == nil || second.Source.Repo != "openai/skills" || second.Source.Path != "skills/skill-creator" || second.Called != "skill-creator" {
t.Fatalf("openai's is %+v", second)
}
if got := read(t, filepath.Join(skillDir("skill-creator"), "SKILL.md")); got != anthropic {
t.Errorf("anthropics' SKILL.md changed: %q", got)
}
// the agent knows it by its folder's name: two skill-creators in
// one agent would be one hiding the other
want := "---\nname: skill-creator-openai\ndescription: Guide for creating effective skills (OpenAI one).\nmetadata:\n short-description: Create or update a skill\n---\n\n# Skill Creator\n"
if got := read(t, filepath.Join(skillDir("skill-creator-openai"), "SKILL.md")); got != want {
t.Errorf("openai's SKILL.md is %q", got)
}
for _, n := range []string{"skill-creator", "skill-creator-openai"} {
if !ours(filepath.Join(h, ".claude/skills", n), n) {
t.Errorf("claude wasn't given %s", n)
}
}
// named so, it is what was fetched: not changed here, and a check
// finds the same files at its source
if edited, known := l.editState(second, Targets()); edited || !known {
t.Errorf("openai's reads as edited=%v known=%v", edited, known)
}
if got := second.fetchedHash(filepath.Join(p.root, "skills", "skill-creator")); got == "" || got != second.Hash {
t.Errorf("fetched hash %q, library's %q", got, second.Hash)
}
v, _ := Read(nil)
for _, s := range v.Skills {
if s.Name == "skill-creator-openai" && s.Source != "https://github.com/openai/skills/tree/HEAD/skills/skill-creator" {
t.Errorf("openai's source reads %q", s.Source)
}
}
// installed again, it is had, by the name it has here
r = ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, nil))
if len(r.Installed) != 0 || !slices.Equal(r.Had, []string{"skill-creator-openai"}) {
t.Errorf("again: installed %v had %v", r.Installed, r.Had)
}
if p, _ := ProbeSkills("openai/skills"); !p.Candidates[0].Have || p.Candidates[0].As != "skill-creator-openai" {
t.Errorf("probed again: %+v", p.Candidates)
}
// the market sees which repository's it is
l, _ = load()
if got := l.haveSkill("openai/skills", "skill-creator", "skill-creator"); got != "skill-creator-openai" {
t.Errorf("market has openai's as %q", got)
}
if got := l.haveSkill("acme/skills", "skill-creator", "skill-creator"); got != "" {
t.Errorf("market has acme's as %q", got)
}
// a third from elsewhere gets its own owner's name
if r := ok(t)(InstallMarketSkill("acme/skills", "skill-creator", []string{"claude"})); !slices.Equal(r.Installed, []string{"skill-creator-acme"}) {
t.Errorf("acme's: installed %v skipped %+v", r.Installed, r.Skipped)
}
// an update keeps the name it has here, in its SKILL.md too
version = "two"
ok(t)(UpdateSkill("skill-creator-openai", false))
got := read(t, filepath.Join(skillDir("skill-creator-openai"), "SKILL.md"))
if !strings.Contains(got, "name: skill-creator-openai\n") || !strings.Contains(got, "(OpenAI two)") {
t.Errorf("after update: %q", got)
}
l, _ = load()
if edited, _ := l.editState(l.skill("skill-creator-openai"), Targets()); edited {
t.Error("the update reads as changed here")
}
}
// A skill taken in from an agent's folder has no source to tell it by:
// the very same files by its name are had, not installed twice.
func TestSameFilesByNameAreHad(t *testing.T) {
sandbox(t)
version := "one"
skillCreators(t, &version)
p, err := ProbeSkills("acme/skills")
if err != nil {
t.Fatal(err)
}
if err := copyDir(filepath.Join(p.root, "skill-creator"), skillDir("skill-creator")); err != nil {
t.Fatal(err)
}
if _, err := change(func(l *Library) error {
l.Skills = append(l.Skills, &Skill{Name: "skill-creator", Agents: []string{}})
return nil
}); err != nil {
t.Fatal(err)
}
r := ok(t)(InstallSkills("acme/skills", []string{"skill-creator"}, nil))
if len(r.Installed) != 0 || !slices.Equal(r.Had, []string{"skill-creator"}) {
t.Errorf("installed %v had %v", r.Installed, r.Had)
}
// another one by that name, with no source, is another skill
r = ok(t)(InstallSkills("openai/skills", []string{"skills/skill-creator"}, nil))
if !slices.Equal(r.Installed, []string{"skill-creator-openai"}) {
t.Errorf("openai's: installed %v had %v", r.Installed, r.Had)
}
}
func TestFreeSkillName(t *testing.T) {
sandbox(t)
l := &Library{Skills: []*Skill{{Name: "pdf"}, {Name: "pdf-openai"}, {Name: strings.Repeat("a", 64)}}}
gh := Source{Kind: "github", Repo: "openai/skills"}
for _, c := range []struct {
name string
src Source
want string
}{
{"docx", gh, "docx"},
{"pdf", gh, "pdf-openai-skills"},
{"pdf", Source{Kind: "git", Repo: "git@gitlab.example.com:team/tools.git"}, "pdf-team"},
{"pdf", Source{Kind: "folder", Dir: "/Users/me/work/kit"}, "pdf-work"},
{strings.Repeat("a", 64), gh, strings.Repeat("a", 64-len("-openai")) + "-openai"},
} {
if got := l.freeSkillName(c.name, c.src); got != c.want || !nameRe.MatchString(got) {
t.Errorf("%s from %+v: %q, want %q", c.name, c.src, got, c.want)
}
}
// every other name taken: a number
l.Skills = append(l.Skills, &Skill{Name: "pdf-openai-skills"}, &Skill{Name: "pdf-skills"})
if got := l.freeSkillName("pdf", gh); got != "pdf-openai-2" {
t.Errorf("all taken: %q", got)
}
// a folder in the library's that it doesn't list is taken too
write(t, filepath.Join(skillDir("docx"), "SKILL.md"), "mine")
if got := l.freeSkillName("docx", gh); got != "docx-openai" {
t.Errorf("unlisted folder: %q", got)
}
}
func TestRenamedMeta(t *testing.T) {
for _, c := range []struct{ in, want string }{
{"---\nname: a\ndescription: d\n---\nbody name: a\n", "---\nname: b\ndescription: d\n---\nbody name: a\n"},
{"---\r\nname: \"a\"\r\n---\r\n", "---\r\nname: b\r\n---\r\n"},
// a nested name isn't the skill's
{"---\nmetadata:\n name: a\nname: a\n---\n", "---\nmetadata:\n name: a\nname: b\n---\n"},
{"---\ndescription: d\n---\nname: a\n", "---\ndescription: d\n---\nname: a\n"},
{"# no front matter\nname: a\n", "# no front matter\nname: a\n"},
} {
got, _ := renamedMeta(c.in, "b")
if got != c.want {
t.Errorf("%q: %q, want %q", c.in, got, c.want)
}
}
// a SKILL.md that is a link isn't written through
dir := t.TempDir()
other := filepath.Join(dir, "other.md")
write(t, other, "---\nname: a\n---\n")
if err := os.MkdirAll(filepath.Join(dir, "s"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(other, filepath.Join(dir, "s", "SKILL.md")); err != nil {
t.Skip(err)
}
if err := nameSkill(filepath.Join(dir, "s"), "b"); err != nil {
t.Fatal(err)
}
if got := read(t, other); got != "---\nname: a\n---\n" {
t.Errorf("written through the link: %q", got)
}
}