Files
magpie/internal/davsync/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

82 lines
2.6 KiB
Go

package davsync
import (
"context"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/library"
)
// A hook made on one computer is written into the other's agents by its
// next sync, and taken out of them when the first takes it out.
func TestSyncHooks(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "correct horse", Keys: true, Agents: true}
now := func(t *testing.T) {
t.Helper()
if err := Now(ctx); err != nil {
t.Fatal(err)
}
if v := Status(); v.Error != "" {
t.Fatalf("sync: %+v", v)
}
}
a, b := newComputer(t), newComputer(t)
a.use(t)
os.MkdirAll(filepath.Join(string(a), ".claude"), 0o755)
if _, err := library.SaveHook("", library.Hook{Name: "done", Event: "turn-end", Command: "echo done", Agents: []string{"claude"}}); err != nil {
t.Fatal(err)
}
Configure(cfg)
now(t)
b.use(t)
os.MkdirAll(filepath.Join(string(b), ".claude"), 0o755)
Configure(cfg)
now(t)
settings := filepath.Join(string(b), ".claude", "settings.json")
if s, _ := os.ReadFile(settings); !strings.Contains(string(s), "echo done") {
t.Fatalf("b's Claude Code wasn't given the hook: %q", s)
}
puts := fake.puts
now(t)
if fake.puts != puts {
t.Fatalf("b pushed back the hooks it brought in (%d puts)", fake.puts-puts)
}
a.use(t)
if _, err := library.RemoveHooks([]string{"done"}); err != nil {
t.Fatal(err)
}
now(t)
b.use(t)
now(t)
if s, _ := os.ReadFile(settings); strings.Contains(string(s), "echo done") {
t.Errorf("the hook is still in b's Claude Code after a took it out: %q", s)
}
}
// A library with no hooks hashes as it did before they were carried, so a
// magpie that starts carrying them doesn't see a change to sync.
func TestNoHooksHashAsBefore(t *testing.T) {
none := []*library.Hook{}
with := backup.Bundle{Library: &library.Bundle{MCP: []*library.Server{}, Skills: []*library.CarriedSkill{}, Hooks: &none}}
before := backup.Bundle{Library: &library.Bundle{MCP: []*library.Server{}, Skills: []*library.CarriedSkill{}}}
if hashes(with)["library"] != hashes(before)["library"] {
t.Error("an empty list of hooks changed the library's hash")
}
one := []*library.Hook{{Name: "done", Event: "turn-end", Command: "echo done"}}
with.Library.Hooks = &one
if hashes(with)["library"] == hashes(before)["library"] {
t.Error("a hook didn't change the library's hash")
}
}