Files
ai-agent/backend/test_plugin_page_links.py
gabrielvidalandClaude Opus 5.5 ce6382f03b feat(plugins): the core names no plugin page — each page brings its own links
The core still hard-coded links into plugin pages (Files → Goals / Skills /
Agents / Forms cards and drawer links, Dashboards → Performance, Settings →
What's new and its editor, the home Analytics icon, skill/agent pills) and
~25 old addresses that forwarded to them (/goals, /skills, /dashboards/plan,
/whats-new, /memories/<slug>…). On a hub without those plugins (raw-hub)
they opened 404 pages. Now every link to a plugin page comes from its
manifest:

- bottom-bar drawers: every `files` / `dashboards` plugin page joins its
  section's drawer (past five slots into the stash), key
  `<section>/p:<plugin>:<page>`; saved slots of the core's old links map to
  the plugins' (SUB_RENAMED);
- Files landing: the plugins' Files pages are its cards; Files panel: the
  hard-coded Goals row is gone;
- What's new + editor leave the settings registry (the plugin's own
  `nav: "settings"` pages);
- new optional page fields (written only when set — `ui` is in the approval
  fingerprint): `perm` (the page reaches its holders only), `header: "home"`
  (a button in the home page's head — Analytics), `tag` (the tag kind whose
  pills open the page at <page>/<slug> — skill, agent), `feature` (the App
  usage id its visits count under, so a former core page keeps its history;
  else plugin.<plugin>.<page>, in the frontend and telemetry/views.registry);
- the old addresses and their redirect components are dropped; the owner's
  usage rings, the burn gauge and the Stats redirect link nowhere into a
  plugin; the plan-usage and /tmp-full pushes open /dashboards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 18:14:14 +01:00

141 lines
6.2 KiB
Python

"""A plugin page's own links (2026-11-27): the core names no plugin page, so
a page claims what it adds beyond its nav entry —
* ``perm`` shown only to callers holding that permission (the What's new
editor: ``users.manage``);
* ``header`` a button in the home page's head (Analytics: ``home``);
* ``tag`` the tag kind whose pills open it at ``<page>/<slug>``
(skills-agents: ``skill``, ``agent``);
* ``feature`` the App usage id its visits count under (an id it had as a
core page keeps its history) — ``telemetry.views.plugin_features``.
"""
from __future__ import annotations
import json
import pathlib
import sys
import tempfile
import types
import unittest
from unittest import mock
BACKEND = pathlib.Path(__file__).resolve().parent
if str(BACKEND) not in sys.path:
sys.path.insert(0, str(BACKEND))
from plugins import routes as plugin_routes # noqa: E402
from plugins.manifest import ManifestError, validate_manifest # noqa: E402
from telemetry import views # noqa: E402
from users.caller import Caller # noqa: E402
FILES = {"ui/p.html": "<p>x</p>"}
def _write(root: pathlib.Path, pages: list[dict]) -> pathlib.Path:
root.mkdir(parents=True, exist_ok=True)
mf = {"schema": 1, "id": root.name, "name": root.name.title(), "version": "1.0.0",
"description": "d", "ui": {"pages": pages}}
(root / "plugin.json").write_text(json.dumps(mf), encoding="utf-8")
for rel, content in FILES.items():
(root / rel).parent.mkdir(parents=True, exist_ok=True)
(root / rel).write_text(content, encoding="utf-8")
return root
class PageFieldsTest(unittest.TestCase):
def setUp(self):
self.td = tempfile.TemporaryDirectory()
self.root = pathlib.Path(self.td.name)
def tearDown(self):
self.td.cleanup()
def test_fields_written_only_when_set(self):
mf = validate_manifest(_write(self.root / "links", [
{"id": "editor", "nav": "settings", "group": "workspace", "perm": "users.manage",
"feature": "settings.whatsNew", "html": "ui/p.html"},
{"id": "skills", "nav": "files", "tag": "skill", "subpath": True, "html": "ui/p.html"},
{"id": "analytics", "header": "home", "feature": "dash.conversations", "html": "ui/p.html"},
{"id": "plain", "html": "ui/p.html"},
]))
editor, skills, analytics, plain = mf["ui"]["pages"]
self.assertEqual((editor["perm"], editor["feature"]), ("users.manage", "settings.whatsNew"))
self.assertEqual(skills["tag"], "skill")
self.assertEqual((analytics["header"], analytics["feature"]), ("home", "dash.conversations"))
# an unchanged manifest keeps its approval fingerprint: no new keys
for k in ("perm", "header", "tag", "feature"):
self.assertNotIn(k, plain)
def test_bad_values_refused(self):
for i, page in enumerate([
{"id": "p", "perm": "no.such.permission"},
{"id": "p", "header": "sidebar"},
{"id": "p", "tag": "project", "subpath": True}, # the core routes projects itself
{"id": "p", "tag": "skill"}, # a pill opens <page>/<slug>
{"id": "p", "feature": "Not An Id"},
]):
with self.subTest(page=page), self.assertRaises(ManifestError):
validate_manifest(_write(self.root / f"bad{i}", [{**page, "html": "ui/p.html"}]))
class _Store:
def __init__(self, recs):
self.recs = recs
def managed_plugins(self):
return []
def all_installed(self):
return [("owner", r) for r in self.recs]
def _rec(pid: str, pages: list[dict]) -> dict:
return {"id": pid, "name": pid.title(), "enabled": True,
"manifest": {"id": pid, "name": pid.title(), "ui": {"pages": pages}}}
class PermFilterTest(unittest.TestCase):
RECS = [_rec("whats-new", [
{"id": "news", "label": "What's new", "nav": "settings", "group": "you", "html": "ui/n.html"},
{"id": "editor", "label": "What's new editor", "nav": "settings", "group": "workspace",
"perm": "users.manage", "html": "ui/e.html"},
])]
def _pages(self, caller):
deps = types.SimpleNamespace(plugin_store=_Store(self.RECS))
with mock.patch("plugins.resources.registry") as reg:
reg.return_value.descriptors.return_value = []
ui = plugin_routes._ui_descriptors(deps, caller, self.RECS)
return {p["id"]: p["to"] for p in ui["pages"]}
def test_the_editor_is_the_permission_holders(self):
owner = Caller(id="owner", name="Owner", role="owner")
self.assertEqual(self._pages(owner), {"news": "/settings/p/whats-new/news",
"editor": "/settings/p/whats-new/editor"})
guest = Caller(id="cel", name="Cel", role="guest", perms=frozenset({"plugins.use"}))
self.assertEqual(self._pages(guest), {"news": "/settings/p/whats-new/news"})
class PluginFeaturesTest(unittest.TestCase):
def test_a_page_counts_under_its_old_id_or_its_own(self):
deps = types.SimpleNamespace(plugin_store=_Store([
_rec("ask-form", [{"id": "forms", "label": "Forms", "nav": "files", "feature": "page.forms"}]),
_rec("demo", [{"id": "board", "label": "Board", "nav": "dashboards"},
{"id": "cb", "label": "Callback", "nav": "none"}]),
_rec("whats-new", [{"id": "editor", "label": "What's new editor", "nav": "settings",
"perm": "users.manage", "feature": "settings.whatsNew"}]),
]))
feats = {f["id"]: f for f in views.plugin_features(deps)}
self.assertEqual(set(feats), {"page.forms", "plugin.demo.board", "settings.whatsNew"})
self.assertEqual((feats["page.forms"]["area"], feats["page.forms"]["kind"]), ("Lab", "view"))
self.assertEqual(feats["plugin.demo.board"]["area"], "Dashboards")
self.assertTrue(feats["settings.whatsNew"]["ownerOnly"])
# the full registry: the app's own features, then the plugins' new ones
reg = [f["id"] for f in views.registry(deps)]
self.assertIn("page.home", reg)
self.assertEqual(reg.count("plugin.demo.board"), 1)
if __name__ == "__main__":
unittest.main()