Files
ai-agent/backend/test_plugin_telemetry.py
gabrielvidalandClaude Opus 5.5 2037700317 feat(plugins): retire core Context & skills dashboard into the context-skills plugin (contextcost.read bridge, /dashboards grid)
- GET /api/dashboard → owner-only contextcost.read bridge (plugins/hubcontextcost.py)
- /dashboards becomes DashboardsHome, a card grid of the dashboard plugins' pages
- one-time carry-over of the stat-card order/bench into the plugin's layout.json
- telemetry: /dashboards is dash.home; the plugin page keeps dash.overview

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 13:48:04 +01:00

106 lines
4.6 KiB
Python

"""The ``telemetry.read`` capability — what the App usage plugin reads since
the Usage dashboard left the core (2026-11-25). The counting stays core
(telemetry/); only the page moved."""
from __future__ import annotations
import json
import pathlib
import tempfile
import unittest
from unittest.mock import patch
from fastapi.testclient import TestClient
import main
from plugins.manifest import capability_summary, requires_operator, validate_manifest
from plugins.store import OWNER_UID, PluginStore
from telemetry import views
from toolproxy import access
MANIFEST = {
"schema": 1, "id": "au", "name": "App usage",
"ui": {"pages": [{"id": "usage", "label": "App usage", "nav": "dashboards", "html": "ui/u.html"}]},
"actions": {"usage": {"script": "handlers/u.py"}},
"capabilities": ["telemetry.read"],
}
def _write(root: pathlib.Path) -> pathlib.Path:
root.mkdir(parents=True, exist_ok=True)
(root / "plugin.json").write_text(json.dumps(MANIFEST), encoding="utf-8")
(root / "ui").mkdir(exist_ok=True)
(root / "ui" / "u.html").write_text("<p>u</p>", encoding="utf-8")
(root / "handlers").mkdir(exist_ok=True)
(root / "handlers" / "u.py").write_text("print('{}')\n", encoding="utf-8")
return root
class TestTelemetryRead(unittest.TestCase):
def setUp(self):
self.tc = TestClient(main.app)
self.deps = main.app.state.ai
def _store(self, td):
store = PluginStore(pathlib.Path(td) / "data")
store.install(OWNER_UID, str(_write(pathlib.Path(td) / "src")), is_owner=True, approve=True)
return store
def test_privileged(self):
with tempfile.TemporaryDirectory() as td:
mf = validate_manifest(_write(pathlib.Path(td) / "au"))
self.assertTrue(requires_operator(mf))
self.assertTrue([c for c in capability_summary(mf) if c["kind"] == "capability"][0]["privileged"])
def test_registry_is_the_apps(self):
reg = {f["id"]: f for f in views.registry()}
self.assertEqual(reg["dash.home"]["label"], "Dashboards")
self.assertEqual(reg["split.open"]["kind"], "event")
self.assertTrue(reg["settings.users"]["ownerOnly"])
def test_owner_reads_usage_and_registry(self):
with tempfile.TemporaryDirectory() as td:
tok = access.mint_handler(OWNER_UID, "au", ttl=600)
with patch.object(self.deps, "plugin_store", self._store(td)), \
patch("telemetry.views.usage", return_value={"features": [], "people": []}) as u:
r = self.tc.get("/proxy/tools/plugins/au/hub/telemetry/usage?range=7d&who=guests",
headers={"Authorization": f"Bearer {tok}"})
self.assertEqual(r.status_code, 200, r.text)
self.assertEqual(u.call_args.args[1:], ("7d", "guests"))
self.assertTrue(any(f["id"] == "dash.home" for f in r.json()["registry"]))
def test_bad_range(self):
with tempfile.TemporaryDirectory() as td:
tok = access.mint_handler(OWNER_UID, "au", ttl=600)
with patch.object(self.deps, "plugin_store", self._store(td)):
r = self.tc.get("/proxy/tools/plugins/au/hub/telemetry/usage?range=1y",
headers={"Authorization": f"Bearer {tok}"})
self.assertEqual(r.status_code, 400)
def test_a_guest_never_does(self):
with tempfile.TemporaryDirectory() as td:
tok = access.mint("cel", ttl=600)
with patch.object(self.deps, "plugin_store", self._store(td)), \
patch("telemetry.views.usage") as u:
r = self.tc.get("/proxy/tools/plugins/au/hub/telemetry/usage",
headers={"Authorization": f"Bearer {tok}"})
self.assertEqual(r.status_code, 403)
u.assert_not_called()
def test_plugin_without_the_capability_is_refused(self):
with tempfile.TemporaryDirectory() as td:
store = PluginStore(pathlib.Path(td) / "data")
m = dict(MANIFEST, capabilities=[])
root = _write(pathlib.Path(td) / "src")
(root / "plugin.json").write_text(json.dumps(m), encoding="utf-8")
store.install(OWNER_UID, str(root), is_owner=True, approve=True)
tok = access.mint_handler(OWNER_UID, "au", ttl=600)
with patch.object(self.deps, "plugin_store", store):
r = self.tc.get("/proxy/tools/plugins/au/hub/telemetry/usage",
headers={"Authorization": f"Bearer {tok}"})
self.assertEqual(r.status_code, 403)
if __name__ == "__main__":
unittest.main()