Files
magpie/internal/cli/library_handover_test.go
T
yetone da8039368f library: skills kept in ~/.agents/skills can be handed over to the library, and then turned off per agent
tnt1223 on Discord asked for the skills in ~/.agents/skills to move into
magpie's library folder and be managed there ("统一移入 magpie 的资源库文件
夹,交给 magpie 管理"), since a skill there can't be turned off for one agent
("不能决定某个 skill 是否自由开启"). Codex, Gemini CLI and the other readers
of that folder read it themselves, whatever the library ticks. Goose, Kimi
Code and Cindy use it as their own folder.

HandOverSkills (POST /api/library/skills/hand-over with names) does the
hand-over only when the user asks for it:
- A folder kept in ~/.agents/skills moves into library/skills/<name>
  (moveIn). It is renamed, or, across disks, copied. A copy has to read
  back the same (sameTree) before the original is set aside with the
  backups. On any failure the folder stays where it was and the library's
  link stays as it was.
- An entry there that only links to a folder elsewhere is removed, and
  the library links straight to that folder. The folder itself stays.
- The user's own links to the skill in agents' folders are removed too,
  since they would point at nothing. The library gives those agents its
  own link.
- Every agent here that read the skill there (readsSharedHere, plus the
  agent of each such link) is ticked. So each agent keeps the skill, now
  from the library, until the user unticks it or turns it off here.
- A skill found there that isn't in the library yet is imported first.
- A name that can't be handed over is listed in Result.Unmoved, and the
  other names are handed over all the same.
HandBackSkill moves the folder back and links the library to it again.

"Kept there" is now sharedEntry: the skill's own entry in the shared
folder, by its name, or, for a library link into it under another
folder's name, by that folder's name. Everything that read the old
by-name check follows it:
- SkillView.Always, plus the new Shared and SharedElsewhere fields;
- syncSkills' in-shared check;
- the shared folder's own target, which no longer gets a second link
  beside such an entry. TestSharedUnderAnotherName covers this, and it
  fails without the guard.

Where:
- GUI, Library > Skills: a row kept there has "Manage in magpie"
  ("交给 magpie 管理") in its source line. With more than one such row,
  the skills heading has "Manage {n} in magpie". Each asks first, in the
  page, and says:
  - where the folder goes, or that only the link goes;
  - which agents keep the skill, ticked;
  - how to put it back.
  Cancel sends nothing. The strings are in en, zh, zh-TW, ja and de.
- CLI: `magpie library skill hand-over <name…>|--all` and
  `magpie library skill hand-back <name>`.

Existing users: nothing changes until they click. A library.json gains
no new key.

Run (mote, at 58e926e5d/d4b7c0586 rebased on 3142a5559 unless said):
- gofmt -l: clean.
- go vet on darwin, GOOS=linux -tags nogui and GOOS=windows, for
  library, cli and gui. GOOS=linux and GOOS=windows builds.
- go test -tags nogui ./... -timeout 45m, at the pre-rebase base
  a14f5c9c9: 76 packages ok. The one red was internal/gui's
  TestCoreScriptBudget (1531446 > 1530861), which is red on main since
  a4e929212 (#4133). This change adds no core script.
- Windows (win-run, xiaomi-gpu): TestHandOver*, TestSharedUnderAnotherName
  and TestSharedAgentsSkills pass. TestHandOverFailureKeepsOriginal
  skips there, because a read-only folder can still be written.
- GUI: skill-handover.test.cjs in Chromium and WebKit, at 860 and 400px,
  in five languages, plus the heading's all-at-once case. It ran with the
  library skill files and i18n-keys: 84/84.
- Whole GUI suite, on the Windows box (mote was running another
  session's full suite). Nine reds, all in pages this change doesn't
  touch:
  - webkit: agents' sign-ins read, or not at all;
  - disconnect previews use the full body width (chromium and webkit,
    en and zh);
  - webkit zh: an agent's model list;
  - webkit en/ja 1100: an agent's own settings are in its opened row;
  - webkit: a token on a new custom provider with no Balance URL.
  These are NOT yet compared with main. The local disk filled up before
  the run's summary could be read.

- At the head rebased on 4d9058a98 (WSL magpie-desktop): go vet
  -tags nogui on library and cli; TestHandOver*,
  TestSharedUnderAnotherName, TestSharedAgentsSkills and
  TestLibrarySkillHandOver pass; skill-handover and i18n-keys pass 28/28.
  i18n-keys was red there first: the hand-over tooltip "Move these {n}
  out of ~/.agents/skills…" said the usage plugin's word "{n} out".
  It now reads "Move these {n} from ~/.agents/skills…".

Broken by hand:
- Without the ticking, the HandOver tests fail.
- Without syncSkills' guard, TestSharedUnderAnotherName fails ("goose
  was given it twice").
(At 400px the first layout squeezed the path to nothing. The button
now shrinks first, and the test checks the path's width. That check
wasn't run against the old CSS.)

Not covered:
- Another computer whose skill is still in its own ~/.agents/skills gets
  the library's copy from a sync, beside its own, until it is handed over
  there too.
- The skills CLI's lock (npx skills update) may put the entry back.
- No hand-back button in the GUI; the sheet names the CLI command.
- Tried in a sandbox HOME only, never on a real ~/.agents.
2026-10-29 15:52:42 +08:00

58 lines
2.1 KiB
Go

package cli
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/library"
)
// magpie library skill hand-over / hand-back (tnt1223): a skill kept in
// ~/.agents/skills moves into the library's folder and back again; --all
// takes the library's skills kept there, and nothing else.
func TestLibrarySkillHandOver(t *testing.T) {
h := t.TempDir()
for k, v := range map[string]string{"HOME": h, "USERPROFILE": h, "XDG_CONFIG_HOME": filepath.Join(h, ".config"), "CLAUDE_CONFIG_DIR": "", "CODEX_HOME": "", "APPDATA": "", "LOCALAPPDATA": "", "PATH": filepath.Join(h, "bin")} {
t.Setenv(k, v)
}
shared := filepath.Join(h, ".agents/skills", "orca")
if err := os.MkdirAll(shared, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(shared, "SKILL.md"), []byte("---\nname: orca\ndescription: Orca\n---\n"), 0o644); err != nil {
t.Fatal(err)
}
// found, not brought in: --all is the library's alone
if err := libraryCmd([]string{"library", "skill", "hand-over", "--all"}); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(shared, "SKILL.md")); err != nil {
t.Fatal("--all moved a skill the library doesn't have")
}
if err := libraryCmd([]string{"library", "skill", "hand-over", "--all", "--yes"}); err == nil {
t.Error("an unknown flag was taken")
}
if _, err := library.ImportSkill("orca"); err != nil {
t.Fatal(err)
}
if err := libraryCmd([]string{"library", "skill", "hand-over", "--all"}); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(shared); !os.IsNotExist(err) {
t.Fatalf("still in ~/.agents/skills: %v", err)
}
if fi, err := os.Lstat(library.SkillPath("orca")); err != nil || !fi.IsDir() {
t.Fatalf("not a folder in the library: %v", err)
}
if err := libraryCmd([]string{"library", "skill", "hand-back", "orca"}); err != nil {
t.Fatal(err)
}
if fi, err := os.Lstat(shared); err != nil || !fi.IsDir() {
t.Fatalf("not back in ~/.agents/skills: %v", err)
}
if err := libraryCmd([]string{"library", "skill", "hand-over", "nope"}); err == nil {
t.Error("a skill that isn't there was handed over")
}
}