Files
magpie/internal/library/carry_hooks_test.go
T
yetone 47d5839397 library: sync and backup files carry the hooks
VincentChan on Discord (v0.1.3005) said part of the Library doesn't reach
his other computer through sync. Checked kind by kind what the library part
of a bundle carries: instructions (each set's text, a linked set's file text,
the agents ticked, each agent's own addition), MCP servers, skills and
subagents all go. Hooks didn't: library.Bundle had no field for them, so a
hook made on one computer never reached the other, by WebDAV/S3/GitHub sync
or a backup file.

- library.Bundle.Hooks (*[]*Hook): name, description, event, matcher,
  command, timeout, agents ticked. Collect always sets it, empty too.
  Applied.Hooks (the entries magpie wrote into each agent here) stays out:
  each computer writes them on the sync Put runs, beside the user's own
  hooks, and records its own entries, so an entry the user edited there
  stays theirs.
- Put replaces the hooks with the bundle's, so one taken out on one
  computer goes from the others. A nil list (a bundle from a magpie before
  this) keeps this computer's.
- check refuses a hook with no name, no command, or named twice. The event
  and agents are carried as they come: an event a newer magpie knows is a
  problem on the agent here, not a refused library.
- davsync: an empty list hashes as before the field, so upgrading with no
  hooks syncs nothing.
- The CLI's backup line counts hooks; the GUI's sync/export/import ticks
  say "instructions, MCP servers, skills, hooks and subagents" in every
  language.
- library.md and sync-backup.md updated, and they now name what stays
  this computer's own: projects, skill groups, CopySkills/SkillHow,
  SharedSkills, MCPHub.

Instructions: two sandbox computers through the fake DAV server carried a
named set and a change to it, a set linked to ~/.agents/AGENTS.md and a
hand edit of that file, both computers having their own text first, an
agent's own addition, and an agent installed after the first sync. All
reached the second computer's CLAUDE.md/AGENTS.md (exploration tests, not
kept). So no instructions change here; what VincentChan saw is not
reproduced yet.

Run (worktree at 0e55a7593; origin/main bf60ea596's two commits touch
none of these files):
- mote: go test -tags nogui ./internal/backup ./internal/davsync
  ./internal/cli ./internal/library ok; go vet -tags nogui for darwin,
  GOOS=linux and GOOS=windows on the four packages ok; gofmt -l clean;
  go test ./internal/gui -run 'TestCoreScriptBudget|Words|I18n|Builtins' ok
- GUI (wsl-run, Linux): i18n-keys, github-sync, s3-sync, sync-*,
  webdav-settings-refresh, library-backups-top, gui-de, gui-ja, gui-zh-tw:
  187/187

Broken by hand: with Collect not setting Hooks, Put ignoring them and the
hash not dropping an empty list (the old code), TestCarryHooks,
TestCarryOldBundleKeepsHooks, TestCarryNewerHooks,
TestBackupFileCarriesHooks, TestSyncHooks and TestNoHooksHashAsBefore
went red.

Not tried: a real WebDAV/S3/GitHub remote between two real computers;
Windows and Linux runs of the Go packages.
2026-11-05 11:29:20 +08:00

142 lines
5.1 KiB
Go

package library
import (
"encoding/json"
"path/filepath"
"strings"
"testing"
)
// A hook goes with the library to another computer, which writes it into
// its agents beside the user's own hooks there; what magpie wrote into the
// first computer's agents stays out of the bundle; taking it out on one
// takes magpie's entry out on the other and leaves the user's.
func TestCarryHooks(t *testing.T) {
sandbox(t)
ok(t)(SaveHook("", Hook{Name: "bark", Description: "Ping my phone", Event: "turn-end", Command: "curl -s https://api.day.app/KEY/done",
Timeout: 10, Agents: []string{"codex", "claude"}}))
b, err := Collect()
if err != nil {
t.Fatal(err)
}
carried := mustJSON(t, b)
var raw struct {
Hooks []map[string]any `json:"hooks"`
}
json.Unmarshal([]byte(carried), &raw)
if len(raw.Hooks) != 1 {
t.Fatalf("hooks carried: %s", carried)
}
if got := raw.Hooks[0]; got["name"] != "bark" || got["event"] != "turn-end" || got["description"] != "Ping my phone" ||
got["command"] != "curl -s https://api.day.app/KEY/done" || got["timeout"] != float64(10) || mustJSON(t, got["agents"]) != `["claude","codex"]` {
t.Errorf("the hook carried: %v", got)
}
if strings.Contains(carried, "applied") || strings.Contains(carried, `"entry"`) {
t.Errorf("what magpie wrote here is carried:\n%s", carried)
}
h2 := sandbox(t)
settings := filepath.Join(h2, ".claude/settings.json")
write(t, settings, `{"model": "opus", "hooks": {"Stop": [{"hooks": [{"type": "command", "command": "say my own"}]}]}}`)
r := putJSON(t, carried)
for _, p := range r.Problems {
if strings.HasPrefix(p.What, "hook") {
t.Errorf("a problem: %+v", p)
}
}
if got := hooksOf(t, settings, "Stop"); len(got) != 2 || !strings.Contains(got[0], "say my own") || !strings.Contains(got[1], "api.day.app") {
t.Errorf("claude's Stop on the second computer: %v", got)
}
if got := hooksOf(t, filepath.Join(h2, ".codex/hooks.json"), "Stop"); len(got) != 1 || !strings.Contains(got[0], "api.day.app") {
t.Errorf("codex's Stop on the second computer: %v", got)
}
// the first computer takes it out
none := []*Hook{}
b.Hooks = &none
putJSON(t, mustJSON(t, b))
if got := hooksOf(t, settings, "Stop"); len(got) != 1 || !strings.Contains(got[0], "say my own") {
t.Errorf("after the hook was taken out, claude's Stop: %v", got)
}
if !strings.Contains(read(t, settings), `"model": "opus"`) {
t.Errorf("the user's settings changed:\n%s", read(t, settings))
}
}
// A bundle from a magpie before hooks were carried has none, and knew
// nothing of this computer's: they stay.
func TestCarryOldBundleKeepsHooks(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "github", Transport: "stdio", Command: "npx", Agents: []string{"claude"}}))
b, err := Collect()
if err != nil || b == nil {
t.Fatal(b, err)
}
b.Hooks = nil
old := mustJSON(t, b)
if strings.Contains(old, `"hooks"`) {
t.Fatalf("not as an older magpie wrote it: %s", old)
}
h := sandbox(t)
ok(t)(SaveHook("", Hook{Name: "mine", Event: "turn-end", Command: "say mine", Agents: []string{"claude"}}))
putJSON(t, old)
l, err := Collect()
if err != nil {
t.Fatal(err)
}
if l.Hooks == nil || len(*l.Hooks) != 1 || (*l.Hooks)[0].Name != "mine" {
t.Fatalf("an older bundle changed the hooks: %s", mustJSON(t, l.Hooks))
}
if got := hooksOf(t, filepath.Join(h, ".claude/settings.json"), "Stop"); len(got) != 1 {
t.Errorf("claude's Stop: %v", got)
}
}
// A hook on an event a newer magpie knows and this one doesn't is carried
// as it comes: the agent it is given to has a problem, and the rest of the
// library comes in. A hook with no name or command, or one named twice,
// refuses the bundle.
func TestCarryNewerHooks(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "github", Transport: "stdio", Command: "npx", Agents: []string{"claude"}}))
b, err := Collect()
if err != nil || b == nil {
t.Fatal(b, err)
}
hooks := []*Hook{
{Name: "later", Event: "turn-later", Command: "say later", Agents: []string{"claude", "newagent"}},
{Name: "done", Event: "turn-end", Command: "say done", Agents: []string{"claude"}},
}
b.Hooks = &hooks
h := sandbox(t)
r := putJSON(t, mustJSON(t, b))
if got := hooksOf(t, filepath.Join(h, ".claude/settings.json"), "Stop"); len(got) != 1 || !strings.Contains(got[0], "say done") {
t.Errorf("claude's Stop: %v", got)
}
found := false
for _, p := range r.Problems {
found = found || p.What == "hook:later"
}
if !found {
t.Errorf("no problem for the event this magpie doesn't know: %+v", r.Problems)
}
l, _ := Collect()
if len(*l.Hooks) != 2 || (*l.Hooks)[1].Name != "later" || mustJSON(t, (*l.Hooks)[1].Agents) != `["claude","newagent"]` {
t.Errorf("not kept as it came: %s", mustJSON(t, l.Hooks))
}
for _, bad := range [][]*Hook{
{{Name: "a b", Event: "turn-end", Command: "x"}},
{{Name: "x", Event: "turn-end", Command: " "}},
{{Name: "x", Event: "turn-end", Command: "x"}, {Name: "x", Event: "prompt", Command: "y"}},
{nil},
} {
b.Hooks = &bad
var in Bundle
json.Unmarshal([]byte(mustJSON(t, b)), &in)
if _, err := Put(&in); err == nil {
t.Errorf("put %s", mustJSON(t, bad))
}
}
}