/health's repoPlugins (workers only): <cwd>/.claude-plugin/marketplace.json
(name, path, repo, plugins) and which <plugin>@<marketplace> are installed /
enabled for the default account — read off files, cached 30 s, no CLI, no
token. POST /repo-plugins {enable, disable} runs claude plugin marketplace
add <cwd>/ (when unknown), install|enable / disable --scope user with that
account's env; only the cwd marketplace's ids, never an uninstall; 409 on
the host sidecar.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
5.1 KiB
Python
124 lines
5.1 KiB
Python
"""repo_plugins: the cwd repo's own Claude Code plugins, read and applied."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
import repo_plugins
|
|
|
|
|
|
def _repo(tmp_path, name="orus", plugins=None):
|
|
repo = tmp_path / "orus-monorepo"
|
|
(repo / ".claude-plugin").mkdir(parents=True)
|
|
(repo / ".claude-plugin" / "marketplace.json").write_text(json.dumps({
|
|
"name": name, "owner": {"name": "Orus"},
|
|
"plugins": plugins if plugins is not None else [
|
|
{"name": "work-context", "source": "./.claude/plugins/work-context",
|
|
"description": "Orus work context"},
|
|
{"name": "../evil", "source": "./x"}],
|
|
}))
|
|
return repo
|
|
|
|
|
|
def _cfg(tmp_path, enabled=None, installed=(), known=False, extra=False):
|
|
cfg = tmp_path / ".claude"
|
|
(cfg / "plugins").mkdir(parents=True)
|
|
settings = {"enabledPlugins": enabled or {}}
|
|
if extra:
|
|
settings["extraKnownMarketplaces"] = {"orus": {"source": {"source": "directory",
|
|
"path": "/x"}}}
|
|
(cfg / "settings.json").write_text(json.dumps(settings))
|
|
(cfg / "plugins" / "installed_plugins.json").write_text(json.dumps(
|
|
{"version": 2, "plugins": {p: [{"scope": "user"}] for p in installed}}))
|
|
if known:
|
|
(cfg / "plugins" / "known_marketplaces.json").write_text(json.dumps({"orus": {}}))
|
|
return cfg
|
|
|
|
|
|
def test_no_marketplace(tmp_path):
|
|
st = repo_plugins.read(tmp_path, _cfg(tmp_path))
|
|
assert st["marketplace"] is None and st["plugins"] == {}
|
|
|
|
|
|
def test_reads_marketplace_and_state(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
cfg = _cfg(tmp_path, enabled={"work-context@orus": True},
|
|
installed=["work-context@orus"], extra=True)
|
|
st = repo_plugins.read(repo, cfg)
|
|
assert st["marketplace"]["name"] == "orus"
|
|
assert st["marketplace"]["repo"] == "orus-monorepo"
|
|
assert [p["name"] for p in st["marketplace"]["plugins"]] == ["work-context"]
|
|
assert st["known"] is True
|
|
assert st["plugins"] == {"work-context@orus": {"installed": True, "enabled": True}}
|
|
|
|
|
|
def test_apply_adds_marketplace_then_installs(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
cfg = _cfg(tmp_path)
|
|
calls = []
|
|
|
|
def cli(args):
|
|
calls.append(args)
|
|
if args[:3] == ["plugin", "marketplace", "add"]:
|
|
(cfg / "plugins" / "known_marketplaces.json").write_text('{"orus": {}}')
|
|
if args[:2] == ["plugin", "install"]:
|
|
(cfg / "settings.json").write_text('{"enabledPlugins": {"work-context@orus": true}}')
|
|
(cfg / "plugins" / "installed_plugins.json").write_text(
|
|
'{"plugins": {"work-context@orus": [{}]}}')
|
|
return None
|
|
|
|
out = repo_plugins.apply(["work-context@orus"], [], cwd=str(repo), cfg=cfg, cli=cli)
|
|
assert calls == [["plugin", "marketplace", "add", f"{repo}/"],
|
|
["plugin", "install", "work-context@orus", "--scope", "user"]]
|
|
assert out["results"] == {"work-context@orus": {"ok": True}}
|
|
assert out["repoPlugins"]["plugins"]["work-context@orus"]["enabled"] is True
|
|
|
|
|
|
def test_apply_enables_an_installed_one_and_disables(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
calls = []
|
|
cfg = _cfg(tmp_path, installed=["work-context@orus"], known=True)
|
|
repo_plugins.apply(["work-context@orus"], [], cwd=str(repo), cfg=cfg,
|
|
cli=lambda a: calls.append(a))
|
|
assert calls == [["plugin", "enable", "work-context@orus", "--scope", "user"]]
|
|
calls.clear()
|
|
cfg2 = tmp_path / "b"
|
|
cfg2.mkdir()
|
|
cfg2 = _cfg(cfg2, enabled={"work-context@orus": True},
|
|
installed=["work-context@orus"], known=True)
|
|
repo_plugins.apply([], ["work-context@orus"], cwd=str(repo), cfg=cfg2,
|
|
cli=lambda a: calls.append(a))
|
|
assert calls == [["plugin", "disable", "work-context@orus", "--scope", "user"]]
|
|
|
|
|
|
def test_apply_does_nothing_when_already_so(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
cfg = _cfg(tmp_path, enabled={"work-context@orus": True},
|
|
installed=["work-context@orus"], extra=True)
|
|
calls = []
|
|
out = repo_plugins.apply(["work-context@orus"], [], cwd=str(repo), cfg=cfg,
|
|
cli=lambda a: calls.append(a))
|
|
assert calls == [] and out["results"]["work-context@orus"]["ok"]
|
|
|
|
|
|
def test_apply_reports_cli_errors(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
cfg = _cfg(tmp_path)
|
|
out = repo_plugins.apply(["work-context@orus"], [], cwd=str(repo), cfg=cfg,
|
|
cli=lambda a: "boom")
|
|
assert out["results"]["work-context@orus"] == {"ok": False,
|
|
"error": "marketplace add: boom"}
|
|
|
|
|
|
def test_apply_refuses_foreign_ids(tmp_path):
|
|
repo = _repo(tmp_path)
|
|
with pytest.raises(HTTPException) as e:
|
|
repo_plugins.apply(["other@elsewhere"], [], cwd=str(repo),
|
|
cfg=_cfg(tmp_path), cli=lambda a: None)
|
|
assert e.value.status_code == 400
|
|
with pytest.raises(HTTPException) as e:
|
|
repo_plugins.apply(["x@y"], [], cwd=str(tmp_path / "none"),
|
|
cfg=tmp_path / ".claude", cli=lambda a: None)
|
|
assert e.value.status_code == 409
|