Orca's layout in the app's own look (form plugins-page-rework_rhce3j): a category sidebar with counts (a chip row on a phone), search, All / Installed / Available, a sort (Installed first, Name, Recently updated — per person), and cover cards or compact rows (the grid / list toggle is kept). - New manifest field `category` (backend/plugins/categories.py), strict: an app picks dev, data, agents, chat, scheduling, media or other (the default); a provider / harness / translation sits under its kind's shelf. Not in the approval fingerprint. Every row of GET /api/plugins carries the resolved category; a record from before the field reads its source's plugin.json. - Translations works as soon as the translation kind exists; an empty category is hidden. - ?cat= / ?show= addresses; the old ?tab= / ?kind= links still land. - Docs: docs/plugins.md → "Categories and the Plugins page", CLAUDE.md. Deploy this before installing ai-agent-plugins' plugin-categories branch: an older core refuses the unknown `category` key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
5.6 KiB
Python
122 lines
5.6 KiB
Python
"""A plugin's ``category`` (plugins/categories.py): checked against its kind,
|
|
defaulted, and resolved on every row of ``GET /api/plugins``."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
TMP = pathlib.Path(tempfile.mkdtemp())
|
|
for k, v in {"DB_PATH": TMP / "x.db", "TRANSCRIPTS_DIR": TMP / "t", "WORKSPACE": TMP, "REPO_DIR": TMP,
|
|
"META_PATH": TMP / "meta.json", "USERS_PATH": TMP / "users.json", "AUTH_PATH": TMP / "auth.json",
|
|
"PROJECTS_DIR": TMP / "projects", "INTERNAL_API_TOKEN": "tok",
|
|
"SIDECAR_TOKEN": "", "PI_TRANSCRIPTS_DIR": ""}.items():
|
|
os.environ.setdefault(k, str(v))
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import main # noqa: E402
|
|
from plugins.categories import ( # noqa: E402
|
|
APP_CATEGORIES, CATEGORIES, KIND_CATEGORY, category_of, default_for, parse_category,
|
|
)
|
|
from plugins.manifest import ManifestError, validate_manifest # noqa: E402
|
|
from plugins.store import OWNER_UID, PluginStore # noqa: E402
|
|
|
|
OWNER_H = {"X-Internal-Auth": os.environ["INTERNAL_API_TOKEN"]}
|
|
BASE = {"schema": 1, "id": "shelfy", "name": "Shelfy", "version": "1.0.0", "description": "On a shelf"}
|
|
|
|
|
|
def _write(root: pathlib.Path, manifest: dict) -> pathlib.Path:
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
(root / "plugin.json").write_text(json.dumps(manifest), encoding="utf-8")
|
|
return root
|
|
|
|
|
|
class TestParseCategory(unittest.TestCase):
|
|
def _p(self, raw):
|
|
errors: list[str] = []
|
|
return parse_category(raw, errors), errors
|
|
|
|
def test_app_categories_and_default(self):
|
|
for cat in APP_CATEGORIES:
|
|
self.assertEqual(self._p({"category": cat}), (cat, []))
|
|
self.assertEqual(self._p({}), ("other", []))
|
|
|
|
def test_kinds_default_to_their_own_shelf(self):
|
|
for kind, cat in KIND_CATEGORY.items():
|
|
self.assertEqual(self._p({"kind": kind}), (cat, []))
|
|
self.assertEqual(self._p({"kind": kind, "category": cat}), (cat, []))
|
|
self.assertEqual(default_for(kind), cat)
|
|
self.assertEqual(default_for(None), "other")
|
|
|
|
def test_strict(self):
|
|
for raw in ({"category": "Dev"}, {"category": "games"}, {"category": 3}, {"category": ""},
|
|
{"kind": "provider", "category": "dev"}, {"kind": "harness", "category": "providers"},
|
|
{"category": "providers"}, {"category": "translations"}):
|
|
cat, errors = self._p(raw)
|
|
self.assertEqual(len(errors), 1, raw)
|
|
self.assertIn(cat, CATEGORIES)
|
|
|
|
def test_a_translation_kind_works_once_it_exists(self):
|
|
# the translation kind (another branch) needs no change here
|
|
self.assertEqual(self._p({"kind": "translation"}), ("translations", []))
|
|
|
|
|
|
class TestManifestCategory(unittest.TestCase):
|
|
def test_validated_and_defaulted(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
mf = validate_manifest(_write(pathlib.Path(td) / "a", {**BASE, "category": "media"}))
|
|
self.assertEqual(mf["category"], "media")
|
|
mf = validate_manifest(_write(pathlib.Path(td) / "b", BASE))
|
|
self.assertEqual(mf["category"], "other")
|
|
with self.assertRaises(ManifestError) as e:
|
|
validate_manifest(_write(pathlib.Path(td) / "c", {**BASE, "category": "nope"}))
|
|
self.assertIn("category must be one of", str(e.exception))
|
|
|
|
|
|
class TestCategoryOf(unittest.TestCase):
|
|
def test_manifest_then_source_then_kind(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
src = _write(pathlib.Path(td) / "s", {**BASE, "category": "chat"})
|
|
self.assertEqual(category_of({"category": "data"}, str(src)), "data")
|
|
# a manifest validated before the field: read off its source
|
|
self.assertEqual(category_of({"kind": "app"}, str(src)), "chat")
|
|
self.assertEqual(category_of({"kind": "provider"}, None), "providers")
|
|
self.assertEqual(category_of({}, str(pathlib.Path(td) / "gone")), "other")
|
|
# a source that declares a bad one falls back to the default
|
|
bad = _write(pathlib.Path(td) / "b", {**BASE, "category": "zzz"})
|
|
self.assertEqual(category_of({}, str(bad)), "other")
|
|
|
|
|
|
class TestRows(unittest.TestCase):
|
|
def test_installed_and_catalog_rows_carry_it(self):
|
|
tc = TestClient(main.app, headers=OWNER_H)
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = PluginStore(pathlib.Path(td) / "data")
|
|
store.install(OWNER_UID, str(_write(pathlib.Path(td) / "i", {**BASE, "category": "scheduling"})),
|
|
is_owner=True, approve=True)
|
|
store.promote_catalog(str(_write(pathlib.Path(td) / "c", {**BASE, "id": "cat-one"})))
|
|
with patch.object(main.app.state.ai, "plugin_store", store):
|
|
d = tc.get("/api/plugins").json()
|
|
self.assertEqual(d["installed"][0]["category"], "scheduling")
|
|
self.assertEqual({c["id"]: c["category"] for c in d["catalog"]}, {"cat-one": "other"})
|
|
|
|
def test_a_record_from_before_the_field_reads_its_source(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = PluginStore(pathlib.Path(td) / "data")
|
|
src = _write(pathlib.Path(td) / "i", {**BASE, "category": "dev"})
|
|
store.install(OWNER_UID, str(src), is_owner=True, approve=True)
|
|
with store._lock:
|
|
store._state["users"][OWNER_UID]["shelfy"]["manifest"].pop("category")
|
|
[pub] = store.list_for(OWNER_UID)
|
|
self.assertEqual(pub["category"], "dev")
|
|
self.assertFalse(pub["needsApproval"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|