Files
ai-agent/backend/test_services_plugin_host.py
gabrielvidalandClaude Opus 5.5 ea29553d8b feat(services): the Services catalogue moves to the services live plugin
The core keeps the services index (names, README line, GOAL.md, last commit)
that service: tags, goals, artefacts and file chips need, and the service
page's shell (README, CLAUDE.md, commits, conversations, memories). Compose /
Traefik parsing, containers, URLs, auth chain, env keys and the file browser
(/api/service-file) leave services/catalog.py (667 → 186 lines) for the
services plugin in ai-agent-plugins.

Two plugin extension points make it work: provides.service.cards (the host
runs the plugin's action and merges whitelisted card fields into
/api/services and /api/service, services/enrich.py) and the services.read
capability (AIA_SERVICES_DIR for the owner's handler runs). ui.slots now
render (PluginSlots, service.detail with {slug}), and every plugin frame gets
the app's theme tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 16:41:37 +01:00

136 lines
5.4 KiB
Python

"""The core's half of the Services catalogue plugin (2026-11-25): the slim
index, the ``provides: service.cards`` merge, the ``services.read``
capability (the services dir for the owner's handlers only)."""
from __future__ import annotations
import json
import pathlib
import tempfile
import unittest
from types import SimpleNamespace
from unittest.mock import patch
from plugins.manifest import (ManifestError, capability_fingerprint, capability_summary,
requires_operator, validate_manifest)
from plugins.runner import run_script
from plugins.store import OWNER_UID, PluginStore
from services import catalog, enrich
CARDS = r'''
import json, os, sys
sys.stdin.read()
print(json.dumps({"services": {"web": {
"urls": ["https://web.example"], "containerCount": 2, "containers": ["a", "b"],
"auth": "auth", "profiles": ["dev"], "icon": "x.png", "routerCount": 1,
"homepageDescription": "From homepage", "evil": "<nope>",
"dir": os.environ.get("AIA_SERVICES_DIR", "")}}}))
'''
ENV = r'''
import json, os, sys
sys.stdin.read()
print(json.dumps({"dir": os.environ.get("AIA_SERVICES_DIR")}))
'''
MANIFEST = {
"schema": 1, "id": "services", "name": "Services",
"actions": {"cards": {"script": "handlers/cards.py"}, "env": {"script": "handlers/env.py"}},
"provides": {"service.cards": "cards"},
"capabilities": ["services.read"],
}
def _write(root: pathlib.Path, manifest: dict) -> pathlib.Path:
(root / "handlers").mkdir(parents=True, exist_ok=True)
(root / "plugin.json").write_text(json.dumps(manifest))
(root / "handlers" / "cards.py").write_text(CARDS)
(root / "handlers" / "env.py").write_text(ENV)
return root
class ManifestTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = pathlib.Path(self.tmp.name)
def tearDown(self):
self.tmp.cleanup()
def test_provides_and_capability(self):
mf = validate_manifest(_write(self.root / "p", MANIFEST))
self.assertEqual(mf["provides"], {"service.cards": "cards"})
self.assertTrue(requires_operator(mf))
kinds = {c["kind"] for c in capability_summary(mf)}
self.assertIn("provides", kinds)
bare = validate_manifest(_write(self.root / "q", {**MANIFEST, "provides": {}}))
self.assertNotEqual(capability_fingerprint(mf), capability_fingerprint(bare))
def test_provides_rejects_unknown_and_undeclared(self):
for prov in ({"project.cards": "cards"}, {"service.cards": "nope"}):
with self.assertRaises(ManifestError):
validate_manifest(_write(self.root / "r", {**MANIFEST, "provides": prov}))
class HostTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = pathlib.Path(self.tmp.name)
self.svc = self.root / "services"
(self.svc / "web").mkdir(parents=True)
(self.svc / "web" / "README.md").write_text("# web\n\n")
(self.svc / "api").mkdir()
(self.svc / "api" / "README.md").write_text("# api\n\nThe API.\n")
self.store = PluginStore(self.root / "data")
self.pdir = _write(self.root / "plugin", MANIFEST)
self.deps = SimpleNamespace(plugin_store=self.store, users=SimpleNamespace(get=lambda u: None))
enrich._cache.clear()
self.p = patch.object(catalog, "SERVICES_DIR", self.svc.resolve())
self.p.start()
def tearDown(self):
self.p.stop()
enrich._cache.clear()
self.tmp.cleanup()
def test_services_dir_only_for_the_owner(self):
self.store.install(OWNER_UID, str(self.pdir), is_owner=True, approve=True)
rec = self.store.get_raw(OWNER_UID, "services")
out = run_script(self.deps, self.store, OWNER_UID, rec, "handlers/env.py")
self.assertEqual(out["dir"], str(self.svc.resolve()))
out = run_script(self.deps, self.store, "dom", rec, "handlers/env.py")
self.assertIsNone(out["dir"])
def test_no_plugin_bare_cards(self):
cards = enrich.enrich(self.deps, catalog.list_services())
api = next(c for c in cards if c["dir"] == "api")
self.assertEqual(api["description"], "The API.")
self.assertNotIn("urls", api)
def test_cards_merged_whitelisted(self):
self.store.install(OWNER_UID, str(self.pdir), is_owner=True, approve=True)
cards = {c["dir"]: c for c in enrich.enrich(self.deps, catalog.list_services())}
web = cards["web"]
self.assertEqual(web["urls"], ["https://web.example"])
self.assertEqual(web["containerCount"], 2)
self.assertEqual(web["auth"], "auth")
self.assertEqual(web["description"], "From homepage") # README had no prose line
self.assertNotIn("evil", web)
self.assertEqual(web["dir"], "web") # a plugin can't rename a card
self.assertNotIn("urls", cards["api"])
def test_disabled_plugin_ignored(self):
self.store.install(OWNER_UID, str(self.pdir), is_owner=True, approve=True)
self.store.update(OWNER_UID, "services", enabled=False)
cards = {c["dir"]: c for c in enrich.enrich(self.deps, catalog.list_services())}
self.assertNotIn("urls", cards["web"])
def test_detail_shell(self):
d = catalog.service_detail("api")
self.assertEqual(d["readme"], "# api\n\nThe API.\n")
self.assertIn("commits", d)
self.assertIsNone(catalog.service_detail("../etc"))
if __name__ == "__main__":
unittest.main()