Files
magpie/internal/library/skill_file_test.go
T
yetone 2a19043afc library: a skill's files are read as Markdown and edited in the page (refeccd)
refeccd on Discord kept skills in ~/.agents/skills, handed them over to
magpie ("let magpie manage"), and then had nowhere to edit them: the
Library's skill view showed SKILL.md as raw text with no way to change
it. There never was an edit path; this adds one.

- GET /api/library/skill takes `file` and answers library.ReadSkillFile:
  the file (SKILL.md by default), its text, the skill's folder and its
  text files (UTF-8, no NUL, up to 1 MB each, 200 listed, SKILL.md first;
  links, hidden folders and what installing writes left out).
- POST /api/library/skills/save {name, file, text, was} is
  library.SaveSkillFile, through change(): it writes only a regular
  file already in the skill's library folder (no .., no absolute path,
  no link, no folder that is a link out of the skill, nothing under
  node_modules/.venv/…), refuses when the file no longer holds `was`
  (the text the page opened), keeps CRLF line ends, and writes with
  edit.WriteAtomic. The sync after it reaches every agent: a linked
  one reads the library's folder, a copy is made again. A copy edited in
  an agent before the save is older, so takeEdits sets it aside in the
  backups. A GitHub skill saved here reads as Changed here, so an update
  asks first.
- The page (viewSkill in plugins/library/client.js) draws a Markdown
  file with the Plugins page's own renderer (plugins.js exports it as
  window.mdRender, 27 bytes of core script) and its front matter as
  rows; other files as text. Edit swaps in a text box; Save or ⌘S/Ctrl+S
  sends it; Cancel asks when there are changes. Other files are the
  app's menu under the name (no native select). New code is in the
  library plugin, so the core script budget is not raised.
- SkillText is gone (its only caller was the route). One new string, in
  all five languages. docs/subsystems/library.md says all this.

Run on mote at the change rebased on 9335e23d3:
- go test -count=1 -timeout 45m ./internal/library ./internal/gui
  ./internal/cli: library ok. gui: TestCoreScriptBudget passes; the only
  red is TestMainWindowCorners/macOS_26_SDK (#3949, mote's macOS 27).
  cli: the only red is TestEveryRouteHasACommandOrIsNamed on GET
  /api/context/reply (#5026, from 0872bc2a0). Both are red on main and
  filed.
- GOOS=linux and GOOS=windows go vet -tags nogui on the three packages;
  gofmt -l clean.
- skill-edit.test.cjs: 22/22 (chromium and webkit; en, zh, zh-TW, ja,
  de; 860px and 400px). It checks the drawn Markdown and front matter,
  no sideways scroll, ⌘S posting the exact body with `was`, the status,
  the file menu, Cancel sending nothing, Save on a second file, no click
  scrolling the page, no left-border stripe or gradient. i18n-keys ok.
- Whole GUI suite (a new API call), on the Linux box (wsl-run; mote had
  two other full suites): ~4460 of its results in at the push, not
  finished. Its reds are all WebKit and none is in a file this touches.
  The ones near it, run alone at this change and at origin/main
  bc10a6198: click-scroll, found-skill-pick-remove and account-eye fail
  the same on both (#4385, #4097/#5013, #4244).

Again at the pushed head (rebased on b967b1620; its commits change
app.js and the core budget), on the Linux box: TestCoreScriptBudget ok,
go test ./internal/library ok, gofmt -l clean, skill-edit.test.cjs and
i18n-keys 26/26.

Broken by hand: without the `was` check, without the linked-folder check
and without the CRLF handling, TestSkillFileSaveRefusesAChangedFile,
TestSkillFileStaysInTheSkill and TestSkillFileKeepsLineEnds each went red.
skill-edit.test.cjs on the old assets: 0/11 in chromium (.lib-mdview
never appears).

Not tried: refeccd's own skills (the fixture is a made-up handover from
~/.agents/skills, with a copy agent and a link agent); the real app
window (the GUI tests fake the API); the TUI and CLI, which have no
editor; making, renaming or removing a file from the page; Windows.
2026-11-03 16:33:03 +08:00

148 lines
5.3 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
// refeccd on Discord: skills kept in ~/.agents/skills, handed over to
// magpie, could no longer be edited where they had been. A file saved
// from the page goes into the library's folder, reaches the agent linked
// to it and the one given a copy, and stays through the syncs after.
func TestSavedSkillFileReachesEveryAgent(t *testing.T) {
h := sandbox(t)
noGoose(t, h)
shared := filepath.Join(h, ".agents/skills")
skill(t, filepath.Join(shared, "writer"), "writer", "Write well")
write(t, filepath.Join(shared, "writer/refs/style.md"), "# Style\n")
write(t, filepath.Join(shared, "writer/logo.png"), "\x89PNG\r\n\x1a\n\x00\x00")
ok(t)(ImportSkill("writer"))
ok(t)(HandOverSkills([]string{"writer"}))
ok(t)(SetSkillHow("codex", HowCopy))
ok(t)(SetSkillHow("gemini", HowLink))
cx, gm := filepath.Join(h, ".codex/skills/writer"), filepath.Join(h, ".gemini/skills/writer")
if !ours(cx, "writer") || isLink(t, cx) || !isLink(t, gm) {
t.Fatal("codex hasn't a copy, or gemini a link")
}
f, err := ReadSkillFile("writer", "")
if err != nil {
t.Fatal(err)
}
if f.File != "SKILL.md" || !strings.Contains(f.Text, "description: Write well") {
t.Fatalf("read: %+v", f)
}
if want := []string{"SKILL.md", "refs/style.md", "scripts/run.sh"}; !slices.Equal(f.Files, want) {
t.Fatalf("files %v, want %v (the picture left out)", f.Files, want)
}
time.Sleep(20 * time.Millisecond)
edited := strings.Replace(f.Text, "Write well", "Write well, briefly", 1)
ok(t)(SaveSkillFile("writer", "SKILL.md", edited, f.Text))
for range 2 { // the save's sync, and the one of the next start
for _, p := range []string{skillDir("writer"), cx, gm} {
if s := read(t, filepath.Join(p, "SKILL.md")); s != edited {
t.Fatalf("%s:\n%s", p, s)
}
}
if !ours(cx, "writer") {
t.Fatal("codex's copy isn't magpie's any more")
}
ok(t)(Sync())
}
// another file of it
g, err := ReadSkillFile("writer", "refs/style.md")
if err != nil || g.Text != "# Style\n" {
t.Fatalf("refs/style.md: %+v %v", g, err)
}
ok(t)(SaveSkillFile("writer", "refs/style.md", "# Style\n\nShort.\n", g.Text))
if s := read(t, filepath.Join(cx, "refs/style.md")); s != "# Style\n\nShort.\n" {
t.Fatalf("codex's refs/style.md: %q", s)
}
}
// What the page opened has changed since (an agent's edit taken in, an
// update): the save writes nothing over it.
func TestSkillFileSaveRefusesAChangedFile(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude"}))
f, err := ReadSkillFile("pdf", "SKILL.md")
if err != nil {
t.Fatal(err)
}
now := strings.Replace(f.Text, "Read PDFs", "Changed meanwhile", 1)
write(t, filepath.Join(skillDir("pdf"), "SKILL.md"), now)
if _, err := SaveSkillFile("pdf", "SKILL.md", "mine", f.Text); err == nil || !strings.Contains(err.Error(), "changed since") {
t.Fatalf("saved over a changed file: %v", err)
}
if s := read(t, filepath.Join(skillDir("pdf"), "SKILL.md")); s != now {
t.Fatalf("written over: %q", s)
}
if _, err := SaveSkillFile("nope", "SKILL.md", "x", ""); err == nil {
t.Fatal("saved a skill the library hasn't")
}
}
// Only the skill's own files: no way out of its folder, through .. or a
// link, and none of what magpie leaves aside.
func TestSkillFileStaysInTheSkill(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "src/skills")
skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs")
write(t, filepath.Join(h, "secret.txt"), "key=1\n")
write(t, filepath.Join(h, "outside/notes.md"), "out\n")
ok(t)(InstallSkills(src, []string{"pdf"}, []string{"claude"}))
dir := realDir(skillDir("pdf"))
symlink(t, filepath.Join(h, "secret.txt"), filepath.Join(dir, "link.txt"))
symlink(t, filepath.Join(h, "outside"), filepath.Join(dir, "out"))
write(t, filepath.Join(dir, "node_modules/x/README.md"), "dep\n")
for _, bad := range []string{"../../secret.txt", "/etc/hosts", "link.txt", "out/notes.md", "node_modules/x/README.md", ".magpie", "scripts", ""} {
if _, err := SaveSkillFile("pdf", bad, "x", ""); err == nil {
t.Errorf("saved %q", bad)
}
if bad != "" {
if _, err := ReadSkillFile("pdf", bad); err == nil {
t.Errorf("read %q", bad)
}
}
}
if s := read(t, filepath.Join(h, "secret.txt")); s != "key=1\n" {
t.Fatalf("written outside: %q", s)
}
f, err := ReadSkillFile("pdf", "")
if err != nil {
t.Fatal(err)
}
if want := []string{"SKILL.md", "scripts/run.sh"}; !slices.Equal(f.Files, want) {
t.Fatalf("files %v, want %v", f.Files, want)
}
}
// A file written with Windows line ends keeps them: the page's text has
// \n alone.
func TestSkillFileKeepsLineEnds(t *testing.T) {
h := sandbox(t)
src := filepath.Join(h, "src/skills")
write(t, filepath.Join(src, "crlf/SKILL.md"), "---\r\nname: crlf\r\ndescription: CRLF\r\n---\r\n")
ok(t)(InstallSkills(src, []string{"crlf"}, []string{"claude"}))
f, err := ReadSkillFile("crlf", "")
if err != nil {
t.Fatal(err)
}
page := strings.ReplaceAll(f.Text, "\r\n", "\n") + "More.\n"
ok(t)(SaveSkillFile("crlf", "SKILL.md", page, f.Text))
if s := read(t, filepath.Join(skillDir("crlf"), "SKILL.md")); s != f.Text+"More.\r\n" {
t.Fatalf("%q", s)
}
if _, err := os.Stat(filepath.Join(skillDir("crlf"), "SKILL.md")); err != nil {
t.Fatal(err)
}
}