Files
magpie/internal/library/command_import_test.go
T
yetone ec1a1a7a55 library: import the slash commands the agents already have
miantiao_me on X (after v0.1.4189): 「之前在 Pi 定义的可以导入吗?」. Library ›
Slash commands now has a "Find in agents" card at the top. It reads the
command files each agent already has, from the same places magpie writes
them (Pi prompts/*.md, Claude commands/*.md, OpenCode command/ and
commands/, Gemini and Qwen .toml, Qwen .md, Droid), and lists them to
import, the way Subagents' import does.

- Parsing: frontmatter description plus body; for TOML, description and
  prompt. Arguments go back to magpie's form: Claude's $ARGUMENTS[n] and
  $n (0-based) become $n+1, Gemini/Qwen {{args}} becomes $ARGUMENTS,
  Pi's $@ becomes $ARGUMENTS (${@:N} is left as written).
- Files magpie wrote itself are skipped. The same content in several
  agents is one command; a name with different bodies per agent shows
  "Differs by agent" and one of them is picked; a name the library has
  with other content is "Name taken" and can't be picked.
- An imported command keeps its source agents ticked, and magpie adopts
  each original file byte for byte (Command.Taken): sync writes the same
  bytes at the same path, so the file is never doubled or rewritten.
  After an edit the agent gets magpie's render at that path; a rename
  drops Taken. Taken never leaves the machine (Collect strips it, Put
  keeps the local one).
- API: POST /api/library/commands/probe, and commands/import through
  the library change route. internal/cli's parity test names the probe
  as stage 3, like subagents/probe.
- Docs: docs/subsystems/library.md has the new row.

Run at 039e25752 (on origin/main dd89f1255), on mote:
- go test ./internal/library/ ./internal/cli/: ok. ./internal/gui/: only
  TestCoreScriptBudget red (app.js 10 bytes over), red on main, #6733;
  this commit doesn't touch app.js.
- gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui ./...: ok
  (at fb01ae2bb, base 028b07b4b; the two later main commits are Windows
  hook code in internal/gui).
- GUI: library-commands-import (new: chromium + webkit, en/zh/zh-TW/ja/de,
  420 and 1100 wide, no sideways scroll, press stays in place),
  library-commands, library-subagents-import, i18n-keys, i18n-en-no-cjk:
  64 of 64.
Earlier, at 1a70f31b6 (base 6af327e2b):
- go test -timeout 45m ./...: every package ok except internal/cli's
  TestEveryRouteHasACommandOrIsNamed, which named the missing probe route;
  fixed here and green since.
- Whole GUI suite (--test-concurrency=8, mote at load 17 to 44 with
  another session's go suite and GUI runs): 26887 pass, 1005 fail in 109
  files, none of them library-commands. The 109 files run alone at
  99719df19 and at origin/main 5bd41301b: 1145/996 vs 1144/997, the
  same tests red apart from language/width variants that flip between
  runs. Recorded on #6709.
Tests use a sandbox HOME with real Pi prompt files from Pi's repo
(internal/library/testdata/pi-prompts). Broken by hand: with syncCommands'
Taken branch off, all four import tests go red ("has a slash command of
its own"); with Put's Taken keep off, TestCommandImportNotCarried goes red.
Not tried: a real Pi install on a live HOME (by rule), or Windows/Linux
runs of the Go tests.
2026-11-15 12:59:12 +08:00

296 lines
10 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// piPrompts puts Pi's own prompt files (testdata/pi-prompts, from Pi's
// repository .pi/prompts) in the sandbox's ~/.pi/agent/prompts, and says
// each one's bytes by path.
func piPrompts(t *testing.T, h string) map[string]string {
t.Helper()
ents, err := os.ReadDir("testdata/pi-prompts")
if err != nil {
t.Fatal(err)
}
m := map[string]string{}
for _, e := range ents {
b, err := os.ReadFile(filepath.Join("testdata/pi-prompts", e.Name()))
if err != nil {
t.Fatal(err)
}
f := filepath.Join(h, ".pi/agent/prompts", e.Name())
write(t, f, string(b))
m[f] = string(b)
}
return m
}
func probeCommands(t *testing.T) *CommandProbe {
t.Helper()
p, err := ProbeCommands()
if err != nil {
t.Fatal(err)
}
return p
}
func candidate(p *CommandProbe, name string) []CommandCandidate {
var r []CommandCandidate
for _, c := range p.Candidates {
if c.Name == name {
r = append(r, c)
}
}
return r
}
func candidatePaths(p *CommandProbe) []string {
var r []string
for _, c := range p.Candidates {
r = append(r, c.Path)
}
return r
}
// The prompts the user made in Pi are taken into the library as they
// are: Pi keeps its files byte for byte, magpie takes them as its own,
// nothing is doubled, and Pi's $@ reads as $ARGUMENTS.
func TestCommandImportPiPrompts(t *testing.T) {
h := sandbox(t)
files := commandAgents(t, h, "pr")
pi := piPrompts(t, h)
p := probeCommands(t)
if len(p.Candidates) != len(pi) || len(p.Left) != 0 {
t.Fatalf("found: %+v", p)
}
pr := candidate(p, "pr")
if len(pr) != 1 {
t.Fatalf("pr: %+v", pr)
}
if pr[0].Description != "Review PRs from URLs with structured issue and code analysis" ||
!strings.HasPrefix(pr[0].Prompt, "You are given one or more GitHub PR URLs: $ARGUMENTS\n") ||
strings.Contains(pr[0].Prompt, "$@") || pr[0].State != "" || pr[0].Clash ||
!slices.Equal(pr[0].Agents, []string{"pi"}) || pr[0].Files["pi"] != files["pi"] {
t.Fatalf("pr: %+v", pr[0])
}
res := ok(t)(ImportCommands(candidatePaths(p)))
if res.Commands == nil || len(res.Commands.Added) != len(pi) || len(res.Commands.Joined) != 0 {
t.Fatalf("imported: %+v", res.Commands)
}
for f, b := range pi {
if got := read(t, f); got != b {
t.Errorf("%s was rewritten:\n%s", f, got)
}
}
ents, _ := os.ReadDir(filepath.Join(h, ".pi/agent/prompts"))
if len(ents) != len(pi) {
t.Errorf("Pi's prompts after the import: %d files, want %d", len(ents), len(pi))
}
for id, f := range files {
if id != "pi" {
if _, err := os.Stat(f); !os.IsNotExist(err) {
t.Errorf("%s was given pr: %v", id, err)
}
}
}
l, _ := load()
c := l.command("pr")
if c == nil || !slices.Equal(c.Agents, []string{"pi"}) || !strings.Contains(c.Prompt, "$ARGUMENTS") {
t.Fatalf("library's pr: %+v", c)
}
if !slices.ContainsFunc(l.Applied["pi"].Commands, func(f SubagentFile) bool { return f.Name == "pr" && f.Path == files["pi"] }) {
t.Errorf("Pi's pr isn't magpie's: %+v", l.Applied["pi"].Commands)
}
// found again: they are magpie's now
if p := probeCommands(t); len(p.Candidates) != 0 {
t.Errorf("found again: %+v", p.Candidates)
}
// ticked for Claude Code too: Claude's file says it as Claude does,
// Pi's stays the user's bytes
ok(t)(CommandAgentsSet("pr", []string{"claude", "pi"}))
if got := read(t, files["claude"]); !strings.Contains(got, "PR URLs: $ARGUMENTS\n") {
t.Errorf("claude's pr:\n%s", got)
}
if read(t, files["pi"]) != pi[files["pi"]] {
t.Errorf("Pi's pr was rewritten:\n%s", read(t, files["pi"]))
}
// edited here: Pi's file says the new prompt, in Pi's place
c.Prompt = "Review $ARGUMENTS."
c.Agents = []string{"claude", "pi"}
ok(t)(SaveCommand("pr", *c))
if got := read(t, files["pi"]); got != "---\ndescription: \"Review PRs from URLs with structured issue and code analysis\"\n---\n\nReview $ARGUMENTS.\n" {
t.Errorf("Pi's pr after an edit:\n%s", got)
}
if v, _ := load(); v.command("pr").Taken["pi"] == nil {
t.Error("an edit lost where pr came from")
}
// renamed: the new name's file is written in Pi's folder, the old goes
c.Name = "review"
ok(t)(SaveCommand("pr", *c))
if v, _ := load(); v.command("review").Taken != nil {
t.Errorf("a rename kept the old file: %+v", v.command("review").Taken)
}
if _, err := os.Stat(files["pi"]); !os.IsNotExist(err) {
t.Errorf("pr.md stayed after the rename: %v", err)
}
if got := read(t, filepath.Join(h, ".pi/agent/prompts/review.md")); !strings.Contains(got, "Review $ARGUMENTS.") {
t.Errorf("review.md: %q", got)
}
}
// Each agent's way of saying the words typed in reads back as magpie's.
func TestCommandImportArgs(t *testing.T) {
h := sandbox(t)
commandAgents(t, h, "x")
write(t, filepath.Join(h, ".claude/commands/fix.md"), "---\ndescription: Fix it\nallowed-tools: Bash\n---\nFix issue $ARGUMENTS[0] in $1, not \\$0 or \\$2.\n")
write(t, filepath.Join(h, ".claude/commands/all.md"), "Do $ARGUMENTS and $0.\r\n")
write(t, filepath.Join(h, ".gemini/commands/gem.toml"), "description = \"Gem\"\nprompt = \"\"\"\nRun {{args}} now.\n\"\"\"\n")
write(t, filepath.Join(h, ".qwen/commands/qw.toml"), "prompt = \"Q {{args}}\"\n")
write(t, filepath.Join(h, ".qwen/commands/qm.md"), "---\ndescription: QM\n---\nQ {{args}} md\n")
write(t, filepath.Join(h, ".config/opencode/command/oc.md"), "---\ndescription: OC\nagent: build\n---\nOC $ARGUMENTS\n")
write(t, filepath.Join(h, ".factory/commands/dr.md"), "Droid $ARGUMENTS\n")
write(t, filepath.Join(h, ".pi/agent/prompts/sub.md"), "Pi ${@:2} and $1\n")
want := map[string]string{
"fix": "Fix issue $1 in $2, not $0 or \\$2.",
"all": "Do $ARGUMENTS and $1.",
"gem": "Run $ARGUMENTS now.",
"qw": "Q $ARGUMENTS",
"qm": "Q $ARGUMENTS md",
"oc": "OC $ARGUMENTS",
"dr": "Droid $ARGUMENTS",
"sub": "Pi ${@:2} and $1",
}
p := probeCommands(t)
if len(p.Candidates) != len(want) {
t.Fatalf("found: %+v", p.Candidates)
}
for _, c := range p.Candidates {
if c.Prompt != want[c.Name] {
t.Errorf("%s: %q, want %q", c.Name, c.Prompt, want[c.Name])
}
}
if c := candidate(p, "gem"); len(c) != 1 || c[0].Description != "Gem" {
t.Errorf("gem: %+v", c)
}
// every one taken, every file as it was
before := map[string]string{}
for _, c := range p.Candidates {
for _, f := range c.Files {
before[f] = read(t, f)
}
}
ok(t)(ImportCommands(candidatePaths(p)))
for f, b := range before {
if got := read(t, f); got != b {
t.Errorf("%s was rewritten:\n%q\nwas\n%q", f, got, b)
}
}
// OpenCode's command/ file stays where it is: no second one in commands/
if _, err := os.Stat(filepath.Join(h, ".config/opencode/commands/oc.md")); !os.IsNotExist(err) {
t.Errorf("oc doubled in commands/: %v", err)
}
if p := probeCommands(t); len(p.Candidates) != 0 {
t.Errorf("found again: %+v", p.Candidates)
}
}
// One name in several agents: saying the same, one command for them all;
// saying different things, one of them to pick. Against the library: the
// same command joins, another one is left alone.
func TestCommandImportByName(t *testing.T) {
h := sandbox(t)
files := commandAgents(t, h, "rv")
write(t, files["claude"], "---\ndescription: Review\n---\nReview $ARGUMENTS.\n")
write(t, files["pi"], "---\ndescription: Review\n---\n\nReview $@.\n")
write(t, files["droid"], "Something else.\n")
write(t, filepath.Join(h, ".claude/commands/lib.md"), "Library's\n")
write(t, filepath.Join(h, ".claude/commands/own.md"), "Not the library's\n")
write(t, filepath.Join(h, ".claude/commands/Bad.md"), "x\n")
write(t, filepath.Join(h, ".claude/commands/empty.md"), "---\ndescription: d\n---\n")
ok(t)(SaveCommand("", Command{Name: "lib", Prompt: "Library's", Agents: []string{"opencode"}}))
ok(t)(SaveCommand("", Command{Name: "own", Prompt: "The library's", Agents: []string{"opencode"}}))
p := probeCommands(t)
rv := candidate(p, "rv")
if len(rv) != 2 || !slices.Equal(rv[0].Agents, []string{"claude", "pi"}) || rv[0].Files["pi"] != files["pi"] ||
!slices.Equal(rv[1].Agents, []string{"droid"}) || !rv[0].Clash || !rv[1].Clash {
t.Fatalf("rv: %+v", rv)
}
if c := candidate(p, "lib"); len(c) != 1 || c[0].State != "same" {
t.Errorf("lib: %+v", c)
}
if c := candidate(p, "own"); len(c) != 1 || c[0].State != "taken" {
t.Errorf("own: %+v", c)
}
if len(p.Left) != 2 || p.Left[0].Path != filepath.Join(h, ".claude/commands/Bad.md") || p.Left[1].Path != filepath.Join(h, ".claude/commands/empty.md") {
t.Errorf("left: %+v", p.Left)
}
if _, err := ImportCommands([]string{rv[0].Path, rv[1].Path}); err == nil {
t.Error("two rv taken")
}
if _, err := ImportCommands([]string{candidate(p, "own")[0].Path}); err == nil {
t.Error("own taken over the library's")
}
if _, err := ImportCommands([]string{filepath.Join(h, "nothing.md")}); err == nil {
t.Error("a path with no command taken")
}
res := ok(t)(ImportCommands([]string{rv[0].Path, candidate(p, "lib")[0].Path}))
if !slices.Equal(res.Commands.Added, []string{"rv"}) || !slices.Equal(res.Commands.Joined, []string{"lib"}) {
t.Errorf("imported: %+v", res.Commands)
}
l, _ := load()
if c := l.command("rv"); c == nil || !slices.Equal(c.Agents, []string{"claude", "pi"}) || c.Prompt != "Review $ARGUMENTS." {
t.Errorf("rv: %+v", c)
}
if c := l.command("lib"); c == nil || !slices.Equal(c.Agents, []string{"claude", "opencode"}) {
t.Errorf("lib: %+v", c)
}
if read(t, files["droid"]) != "Something else.\n" || read(t, filepath.Join(h, ".claude/commands/own.md")) != "Not the library's\n" {
t.Error("a file not imported was written over")
}
if read(t, files["pi"]) != "---\ndescription: Review\n---\n\nReview $@.\n" || read(t, filepath.Join(h, ".claude/commands/lib.md")) != "Library's\n" {
t.Error("an imported file was rewritten")
}
// the library's view never shows where they came from
for _, c := range View0(t).Commands {
if c.Taken != nil {
t.Errorf("%s shows %+v", c.Name, c.Taken)
}
}
}
// Where a command came from is this computer's: never carried, and a
// bundle put here keeps it.
func TestCommandImportNotCarried(t *testing.T) {
h := sandbox(t)
files := commandAgents(t, h, "pr")
pi := piPrompts(t, h)
ok(t)(ImportCommands([]string{files["pi"]}))
b, err := Collect()
if err != nil {
t.Fatal(err)
}
for _, c := range *b.Commands {
if c.Taken != nil {
t.Fatalf("carried: %+v", c.Taken)
}
}
ok(t)(Put(b))
l, _ := load()
if c := l.command("pr"); c == nil || c.Taken["pi"] == nil {
t.Fatalf("put lost where pr came from: %+v", c)
}
if read(t, files["pi"]) != pi[files["pi"]] {
t.Errorf("Pi's pr after put:\n%s", read(t, files["pi"]))
}
}