Files
ai-agent/backend/test_plugin_telemetry.py
gabrielvidalandClaude Opus 5.5 ecbf0e1eb6 feat(plugins): the App usage dashboard leaves the core for the app-usage plugin
Only the page moves (business/dashboards/pages/Usage.tsx, usagePanels.tsx,
usageAnalysis.ts + test, ~730 lines). The counting stays core: the beacon,
the API middleware, telemetry.db, /api/metrics and /api/telemetry/usage.

- telemetry.read, a new owner-only hub capability: GET
  /proxy/tools/plugins/<id>/hub/telemetry/usage?range=&who= answers what
  /api/telemetry/usage does plus `registry`, the app's feature registry.
- The registry reaches the hub as backend/telemetry/features.json, generated
  from lib/telemetry/features.ts and kept in step by features.test.ts
  (UPDATE_FEATURES=1 rewrites it). telemetry.views.registry() reads it.
- /dashboards/usage redirects to /plugins/app-usage/usage; its registry
  entry (dash.usage, "App usage") follows.

Tests: test_plugin_telemetry.py, features.test.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 18:01:09 +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.usage"]["label"], "App usage")
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.usage" 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()