Files
magpie/internal/library/claude_plugins_test.go
T
yetone 9afec644cc sync: Claude Code's own plugins go with the library (AXmake110 on X)
GitHub sync and backups carried the skills a Claude plugin brought into
magpie's library, but not the plugins installed in Claude Code itself
through its /plugin marketplaces, so a second computer had none of them.

The library bundle now carries ClaudePlugins: each marketplace's source
(from plugins/known_marketplaces.json and settings.json's
extraKnownMarketplaces, only `source` and `autoUpdate` kept) and the
plugin ids enabledPlugins has on, under CLAUDE_CONFIG_DIR when set.
installLocation, installPath and plugins/{marketplaces,cache} stay, as
Claude Code fetches them again. A marketplace from a local folder or
file, or with a user/token in a URL or request headers, is not carried,
and neither are its plugins. claude-plugins-official itself is not
carried (Claude Code adds it), its plugins are.

On the other computer they are added to settings.json's
extraKnownMarketplaces and enabledPlugins, which Claude Code installs
from at its next interactive start: tried with Claude Code 2.1.319 in a
sandbox CLAUDE_CONFIG_DIR, commit-commands@claude-code-plugins was
cloned and installed from those two settings alone. settings.json is
patched in place (comments and other keys kept), written to a temp file
and renamed in, and the old file is kept with the backups. Claude Code's
own known_marketplaces.json and installed_plugins.json are never
written. Only adding: a marketplace known here under that name, and a
plugin with any value here (false too), stay as they are. An unreadable
settings.json is neither carried as "none" nor written over. A computer
with no such plugins carries nil, so its library hash is as before. The
davsync library part counts it as the user's (not unset, #5642), and the
preview lists each marketplace and plugin.

Run (mote):
- go test -tags nogui ./... -timeout 45m -count=1 at 5d12d65dc (on
  origin/main 442964d70): ok, 101 packages, no FAIL
- rebased on becc1ef15 (bili path change in internal/library), then at
  this head: go test ./internal/library ./internal/davsync
  ./internal/backup . -count=1: ok
- go vet ./internal/library ./internal/davsync ./internal/backup, and
  GOOS=linux / GOOS=windows go vet of library and davsync: ok
- gofmt -l: clean
Broken by hand: carriedClaudePlugins and putClaudePlugins returning nil,
the unset clause and the preview branch removed: TestClaudePluginsCollected,
TestClaudePluginsPut, TestClaudePluginsInLibraryPart and
TestClaudePluginsInPreview red. A first version added x@evil for a
carried marketplace that wasn't portable; TestClaudePluginsPut caught it.
The first full run was red only on TestNoEagerPackageRegexps (a
package-level regexp); now lazyre.New.

Not tried: a real two-computer GitHub sync with a real marketplace;
`claude -p` and `claude plugin list` do not install declared plugins,
only an interactive start does. Not yet: a plugin removed or turned off
on one computer is not removed on the others (add-only). GUI sync labels
unchanged (the preview shows each item by name), so no GUI run.
2026-11-15 02:46:01 +08:00

205 lines
8.5 KiB
Go

package library
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/tidwall/gjson"
)
// claudePluginsSandbox is a sandbox whose Claude Code folder is moved by
// CLAUDE_CONFIG_DIR, as a user may have it.
func claudePluginsSandbox(t *testing.T) string {
t.Helper()
h := sandbox(t)
d := filepath.Join(h, "cc-config")
t.Setenv("CLAUDE_CONFIG_DIR", d)
shared := "Use tabs."
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"claude"}}))
return d
}
// The files as Claude Code 2.1.319 wrote them in a sandbox after
// `/plugin marketplace add` and `/plugin install`: absolute paths of this
// computer's, a marketplace from a local folder, one from a git URL with
// a token in it, and one from GitHub.
const (
ccKnownFixture = `{
"claude-plugins-official": {"source": {"source": "github", "repo": "anthropics/claude-plugins-official"}, "installLocation": "/Users/a/.claude/plugins/marketplaces/claude-plugins-official", "lastUpdated": "2026-11-14T10:00:00.000Z"},
"claude-code-plugins": {"source": {"source": "github", "repo": "anthropics/claude-code"}, "installLocation": "/Users/a/.claude/plugins/marketplaces/claude-code-plugins", "lastUpdated": "2026-11-14T10:00:00.000Z"},
"mine": {"source": {"source": "directory", "path": "/Users/a/src/my-market"}, "installLocation": "/Users/a/src/my-market", "lastUpdated": "2026-11-14T10:00:00.000Z"},
"corp": {"source": {"source": "git", "url": "https://x-token:ghp_SECRET123@git.corp.example/plugins.git"}, "installLocation": "/Users/a/.claude/plugins/marketplaces/corp", "lastUpdated": "2026-11-14T10:00:00.000Z"}
}`
ccInstalledFixture = `{"version": 2, "plugins": {
"commit-commands@claude-code-plugins": [{"scope": "user", "installPath": "/Users/a/.claude/plugins/cache/claude-code-plugins/commit-commands/1.0.0", "version": "1.0.0", "installedAt": "2026-11-14T10:00:00.000Z", "lastUpdated": "2026-11-14T10:00:00.000Z"}]
}}`
ccSettingsFixture = `{
"model": "opus",
"enabledPlugins": {
"commit-commands@claude-code-plugins": true,
"pr-review-toolkit@claude-code-plugins": false,
"frontend-design@claude-plugins-official": true,
"local-tool@mine": true,
"secret-tool@corp": true
},
"extraKnownMarketplaces": {
"team": {"source": {"source": "github", "repo": "acme/claude-plugins"}, "autoUpdate": true}
}
}`
)
// TestClaudePluginsCollected (AXmake110 on X): a sync carries the
// marketplaces Claude Code knows and the plugins turned on, leaving out
// this computer's paths, a local folder's marketplace and a source with a
// sign-in in it.
func TestClaudePluginsCollected(t *testing.T) {
d := claudePluginsSandbox(t)
write(t, filepath.Join(d, "plugins", "known_marketplaces.json"), ccKnownFixture)
write(t, filepath.Join(d, "plugins", "installed_plugins.json"), ccInstalledFixture)
write(t, filepath.Join(d, "settings.json"), ccSettingsFixture)
b, err := Collect()
if err != nil {
t.Fatal(err)
}
if b.ClaudePlugins == nil {
t.Fatal("Claude Code's plugins not carried")
}
j := mustJSON(t, b)
for _, bad := range []string{"/Users/a", "installLocation", "installPath", "ghp_SECRET123", "my-market", "lastUpdated"} {
if strings.Contains(j, bad) {
t.Errorf("carried %q:\n%s", bad, j)
}
}
cp := b.ClaudePlugins
var names []string
for n := range cp.Marketplaces {
names = append(names, n)
}
if len(cp.Marketplaces) != 2 || cp.Marketplaces["claude-code-plugins"] == nil || cp.Marketplaces["team"] == nil {
t.Errorf("marketplaces carried: %v", names)
}
if r := gjson.GetBytes(cp.Marketplaces["claude-code-plugins"], "source.repo").String(); r != "anthropics/claude-code" {
t.Errorf("claude-code-plugins carried as %s", cp.Marketplaces["claude-code-plugins"])
}
if !gjson.GetBytes(cp.Marketplaces["team"], "autoUpdate").Bool() {
t.Errorf("team's autoUpdate lost: %s", cp.Marketplaces["team"])
}
want := []string{"commit-commands@claude-code-plugins", "frontend-design@claude-plugins-official"}
if strings.Join(cp.Enabled, ",") != strings.Join(want, ",") {
t.Errorf("enabled carried: %v, want %v", cp.Enabled, want)
}
}
// TestClaudePluginsNoneOrUnknown: a computer with no Claude Code plugins
// carries none (its library part is as before), and one whose settings
// can't be read carries nothing rather than "none".
func TestClaudePluginsNoneOrUnknown(t *testing.T) {
d := claudePluginsSandbox(t)
write(t, filepath.Join(d, "settings.json"), `{"model": "opus"}`)
write(t, filepath.Join(d, "plugins", "known_marketplaces.json"), `{"claude-plugins-official": {"source": {"source": "github", "repo": "anthropics/claude-plugins-official"}}}`)
if b, err := Collect(); err != nil || b.ClaudePlugins != nil {
t.Fatalf("nothing of the user's carried as %+v (%v)", b.ClaudePlugins, err)
}
write(t, filepath.Join(d, "settings.json"), `{"enabledPlugins": {"commit-commands@claude-code-plugins": tr`)
if b, err := Collect(); err != nil || b.ClaudePlugins != nil {
t.Fatalf("unreadable settings carried as %+v (%v)", b.ClaudePlugins, err)
}
}
// TestClaudePluginsPut: on the other computer the marketplaces and
// plugins are added to settings.json, where Claude Code installs them
// from at its next start. What the user has there stays: their other
// settings, a plugin they turned off, a marketplace they know under that
// name. Claude Code's own files are not written.
func TestClaudePluginsPut(t *testing.T) {
claudePluginsSandbox(t)
in := Bundle{ClaudePlugins: &ClaudePlugins{
Marketplaces: map[string]json.RawMessage{
"claude-code-plugins": json.RawMessage(`{"source":{"source":"github","repo":"anthropics/claude-code"}}`),
"team": json.RawMessage(`{"source":{"source":"github","repo":"acme/claude-plugins"},"autoUpdate":true}`),
"evil": json.RawMessage(`{"source":{"source":"directory","path":"/Users/a/x"}}`),
},
Enabled: []string{"commit-commands@claude-code-plugins", "frontend-design@claude-plugins-official", "off-here@team", "x@evil", "y@nowhere"},
}}
b, err := Collect()
if err != nil {
t.Fatal(err)
}
in.Instructions = b.Instructions
in.Skills = b.Skills
d := os.Getenv("CLAUDE_CONFIG_DIR")
mine := "{\n // mine\n \"model\": \"sonnet\",\n \"enabledPlugins\": {\"off-here@team\": false},\n \"extraKnownMarketplaces\": {\"team\": {\"source\": {\"source\": \"github\", \"repo\": \"me/my-team\"}}}\n}\n"
write(t, filepath.Join(d, "settings.json"), mine)
ok(t)(Put(&in))
got := read(t, filepath.Join(d, "settings.json"))
if !strings.Contains(got, "// mine") {
t.Errorf("settings.json rewritten whole:\n%s", got)
}
j := settingsOf(t, got)
checks := map[string]string{
"model": "sonnet",
"enabledPlugins.off-here@team": "false",
"enabledPlugins.commit-commands@claude-code-plugins": "true",
"enabledPlugins.frontend-design@claude-plugins-official": "true",
"extraKnownMarketplaces.team.source.repo": "me/my-team",
"extraKnownMarketplaces.claude-code-plugins.source.repo": "anthropics/claude-code",
}
for path, want := range checks {
if v := j.Get(gjsonPath(path)).String(); v != want {
t.Errorf("%s = %q, want %q:\n%s", path, v, want, got)
}
}
for _, gone := range []string{"enabledPlugins.x@evil", "enabledPlugins.y@nowhere", "extraKnownMarketplaces.evil"} {
if j.Get(gjsonPath(gone)).Exists() {
t.Errorf("%s added:\n%s", gone, got)
}
}
for _, f := range []string{"known_marketplaces.json", "installed_plugins.json"} {
if _, err := os.Stat(filepath.Join(d, "plugins", f)); err == nil {
t.Errorf("Claude Code's own %s written", f)
}
}
// again: nothing more to add, the file is left as it is
before := got
ok(t)(Put(&in))
if after := read(t, filepath.Join(d, "settings.json")); after != before {
t.Errorf("a second sync changed settings.json:\n%s", after)
}
// a settings.json that can't be read is not written over
broken := `{"model": "sonnet", "enabledPlugins": {`
write(t, filepath.Join(d, "settings.json"), broken)
ok(t)(Put(&in))
if after := read(t, filepath.Join(d, "settings.json")); after != broken {
t.Errorf("unreadable settings.json written over:\n%s", after)
}
}
func settingsOf(t *testing.T, s string) gjson.Result {
t.Helper()
var v any
clean := strings.ReplaceAll(s, "// mine", "")
if err := json.Unmarshal([]byte(clean), &v); err != nil {
t.Fatalf("%v:\n%s", err, s)
}
return gjson.Parse(clean)
}
// gjsonPath escapes the @ and dots of a plugin id inside a dotted path
// written plainly in a test: only the first segment is a key of its own.
func gjsonPath(p string) string {
head, rest, ok := strings.Cut(p, ".")
if !ok {
return gjson.Escape(p)
}
if head == "enabledPlugins" {
return head + "." + gjson.Escape(rest)
}
return p
}