Files
magpie/internal/library/found_skill_remove_test.go
T
yetone 2d721f7f3d library, gui: the skills in your agents can be picked and removed together
wongxy on Discord: 资源库技能支持批量删除 (batch delete for the Library's
skills). The library's own skills already have this: Select, then
Remove…, since #1852 (a4385e099, v0.1.1217). The skills under "In your
agents" (the ones an agent has of its own) could only be removed one at
a time. This adds the same for them.

What it does:
- RemoveFoundSkills(names), POST /api/library/skills/remove-found-some.
  One change takes each name out exactly as RemoveFoundSkill does (its
  body is now removeFound, shared by both). Folders and byte copies, the
  shared ~/.agents/skills entry and a folder in the library's own folder
  go to magpie's backups, not the OS trash and not deleted. Links are
  taken away. A folder elsewhere that the skill only links to stays, and
  so does another skill by that name.
- A name that can't be taken out is left as it was and goes in
  Result.Unremoved; the others go all the same. When none can go the
  change fails, the endpoint answers 400, and nothing is touched.
- The "In your agents" heading gets its own Select. Each row gets a
  box, its own buttons go, and a bar has All, Clear, the count, Remove…
  and Done. Remove… asks once, naming the count, the agents, how many
  folders go to the backups, how many are only unlinked and how many
  have a shared entry. A name that wasn't removed stays picked, with its
  error in its row's place, and the status names it.
- Only one Select is on at a time: while either is on, the other is
  dimmed with a title saying why ("Done picking … first"). Ending one
  from the other took its bar away above the pointer and scrolled the
  page.
- Eight new strings in zh, zh-TW, ja and de.

Run:
- TestRemoveFoundSkills: picks notes (a folder plus a byte copy), dev (a
  link to a folder of the user's own), stuck (a read-only folder in
  Gemini's skills) and notes again. Checks that stuck is in Unremoved
  with its files intact, that the others are gone with their backups,
  that dev's own folder stays, that keep (not picked) stays, that
  "none can go" fails and touches nothing, and that no names is refused.
  Broken by hand, it goes red both ways: returning on the first error
  ("permission denied", the change fails), and dropping Unremoved
  ("unremoved: []").
- found-skill-pick-remove.test.cjs in Chromium and WebKit, en, zh,
  zh-TW, ja and de, at 1100 and 420: 11/11 each. Every press checks
  that nothing scrolled and that the pressed button didn't move. It
  covers the one-Select-at-a-time rule both ways, picking 3 and the
  sheet's title and lines, Clear, a partial failure, the 400 when none
  can go, and All then unpicking one. Broken by hand (failed names not
  kept, or unpicked anyway), it goes red: 0/11.
- 80 Library, skill, group, removal, i18n-keys, gui-de/ja/zh-tw and
  click-scroll GUI test files: Chromium 458/458; WebKit 456/458. The
  two reds are group-member-info (en) and library-mcp-tool-switch
  (zh-TW 980), both "locator.click: Timeout 5000ms exceeded". Both fail
  the same way, with different cases each run, at 2c3112969 with this
  change stashed.
- gofmt -l is clean. go vet passes on darwin, GOOS=linux -tags nogui
  and GOOS=windows; go build passes on all three.
- go test -tags nogui ./... under a sandbox HOME, rebased on a069f224c:
  all pass except internal/gateway, which hit the default 10m timeout
  while the GUI suites were running alongside it. Run alone with
  -timeout 25m it passed in 327s.

Not tried against a real agent's skills folder; only the sandbox HOME.

Reported-by: wongxy (Discord)
2026-10-13 11:04:13 +08:00

150 lines
5.4 KiB
Go

package library
import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
)
// #1303 (sxwedo): a skill an agent has of its own could only be brought
// into the library, then removed from it. Removed where it is, it leaves
// every agent that has it, its copies and its shared entry with it, each
// kept with the backups; a folder the agents only linked to, and another
// skill by that name, stay.
func TestRemoveFoundSkill(t *testing.T) {
h := sandbox(t)
claude := filepath.Join(h, ".claude/skills")
skill(t, filepath.Join(claude, "notes"), "notes", "Mine")
skill(t, filepath.Join(h, ".codex/skills/notes"), "notes", "Mine") // a byte copy
gemini := filepath.Join(h, ".gemini/skills/notes")
skill(t, gemini, "notes", "Another")
// the shared folder is Goose's too: it goes with Goose's backups
shared := filepath.Join(h, ".agents/skills/pdf")
skill(t, shared, "pdf", "PDFs")
if err := os.Symlink(shared, filepath.Join(claude, "pdf")); err != nil {
t.Fatal(err)
}
dev := filepath.Join(h, "src/dev")
skill(t, dev, "dev", "Work in progress")
if err := os.Symlink(dev, filepath.Join(claude, "dev")); err != nil {
t.Fatal(err)
}
for _, name := range []string{"notes", "pdf", "dev"} {
ok(t)(RemoveFoundSkill(name))
}
for _, p := range []string{filepath.Join(claude, "notes"), filepath.Join(h, ".codex/skills/notes"), filepath.Join(claude, "pdf"), shared, filepath.Join(claude, "dev")} {
if _, err := os.Lstat(p); !os.IsNotExist(err) {
t.Errorf("%s is still there", p)
}
}
for _, g := range []string{"*/claude/skills/notes/SKILL.md", "*/codex/skills/notes/SKILL.md", "*/*/skills/pdf/SKILL.md"} {
if m, _ := filepath.Glob(filepath.Join(BackupDir(), g)); len(m) != 1 {
t.Errorf("%s isn't kept with the backups: %v", g, m)
}
}
if read(t, filepath.Join(dev, "SKILL.md")) == "" {
t.Error("the folder Claude Code linked to is gone")
}
if read(t, filepath.Join(gemini, "SKILL.md")) == "" {
t.Error("gemini's other notes was touched")
}
v, _ := Read(nil)
var names []string
for _, f := range v.FoundSkills {
names = append(names, f.Name+":"+f.Agents[0])
}
if !slices.Equal(names, []string{"notes:gemini"}) {
t.Errorf("found after: %v", names)
}
if _, err := RemoveFoundSkill("nothing"); err == nil {
t.Error("a skill no agent has was removed")
}
if len(v.Skills) != 0 {
t.Errorf("the library got skills: %+v", v.Skills)
}
}
// wongxy on Discord (资源库技能支持批量删除): the skills the agents have of
// their own go several at once, each as RemoveFoundSkill takes one. One
// that can't be taken out (its agent's folder can't be written) stays, in
// Unremoved, as it was; the others go all the same, with their backups.
// When none can go the change fails and nothing is touched.
func TestRemoveFoundSkills(t *testing.T) {
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
t.Skip("a folder made read-only still lets this user (or root) move what's in it")
}
h := sandbox(t)
claude := filepath.Join(h, ".claude/skills")
skill(t, filepath.Join(claude, "notes"), "notes", "Mine")
skill(t, filepath.Join(h, ".codex/skills/notes"), "notes", "Mine") // a byte copy
dev := filepath.Join(h, "src/dev")
skill(t, dev, "dev", "Work in progress")
if err := os.Symlink(dev, filepath.Join(claude, "dev")); err != nil {
t.Fatal(err)
}
keep := filepath.Join(claude, "keep")
skill(t, keep, "keep", "Not picked")
// Gemini's skill folder is read-only: it can't be moved, nor its files
// taken out of it
gem := filepath.Join(h, ".gemini/skills")
stuck := filepath.Join(gem, "stuck")
skill(t, stuck, "stuck", "Held")
if err := os.Chmod(stuck, 0o555); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(stuck, 0o755) })
res, err := RemoveFoundSkills([]string{"notes", "stuck", "dev", "notes"})
if err != nil {
t.Fatal(err)
}
if len(res.Unremoved) != 1 || res.Unremoved[0].What != "skill:stuck" || res.Unremoved[0].Error == "" {
t.Fatalf("unremoved: %+v", res.Unremoved)
}
for _, p := range []string{filepath.Join(claude, "notes"), filepath.Join(h, ".codex/skills/notes"), filepath.Join(claude, "dev")} {
if _, err := os.Lstat(p); !os.IsNotExist(err) {
t.Errorf("%s is still there", p)
}
}
for _, g := range []string{"*/claude/skills/notes/SKILL.md", "*/codex/skills/notes/SKILL.md"} {
if m, _ := filepath.Glob(filepath.Join(BackupDir(), g)); len(m) != 1 {
t.Errorf("%s isn't kept with the backups: %v", g, m)
}
}
if read(t, filepath.Join(dev, "SKILL.md")) == "" {
t.Error("the folder Claude Code linked to is gone")
}
if read(t, filepath.Join(gem, "stuck/SKILL.md")) == "" {
t.Error("the skill that couldn't be removed lost its files")
}
found := func() []string {
v, _ := Read(nil)
var names []string
for _, f := range v.FoundSkills {
names = append(names, f.Name+":"+strings.Join(slices.Concat(f.Agents, f.Copies), ","))
}
return names
}
if got := found(); !slices.Equal(got, []string{"keep:claude", "stuck:gemini"}) {
t.Errorf("found after: %v", got)
}
// none can go: the change fails with the reason, and nothing goes
if _, err := RemoveFoundSkills([]string{"stuck", "nothing"}); err == nil {
t.Error("removing none of them didn't fail")
}
if got := found(); !slices.Equal(got, []string{"keep:claude", "stuck:gemini"}) {
t.Errorf("found after a failed change: %v", got)
}
if _, err := RemoveFoundSkills(nil); err == nil {
t.Error("no names wasn't refused")
}
if v, _ := Read(nil); len(v.Skills) != 0 {
t.Errorf("the library got skills: %+v", v.Skills)
}
}