84 lines
3.7 KiB
Python
84 lines
3.7 KiB
Python
"""``home.read`` (2026-11-29) — ``GET …/hub/home``: the caller's own home
|
|
page as data (every plugin's ``home.tiles`` + the running and waiting counts),
|
|
what the android-push plugin's live notification draws."""
|
|
|
|
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.manifest import capability_summary, requires_operator, validate_manifest # noqa: E402
|
|
from plugins.store import OWNER_UID, PluginStore # noqa: E402
|
|
from toolproxy import access # noqa: E402
|
|
|
|
MF = {"schema": 1, "id": "live", "name": "Live", "capabilities": ["home.read"]}
|
|
|
|
|
|
def _install(td, manifest) -> PluginStore:
|
|
src = pathlib.Path(td) / "src"
|
|
src.mkdir(parents=True, exist_ok=True)
|
|
(src / "plugin.json").write_text(json.dumps(manifest), encoding="utf-8")
|
|
store = PluginStore(pathlib.Path(td) / "data")
|
|
store.install(OWNER_UID, str(src), is_owner=True, approve=True)
|
|
return store
|
|
|
|
|
|
def _h(pid="live"):
|
|
return {"Authorization": f"Bearer {access.mint_handler(OWNER_UID, pid, ttl=600)}"}
|
|
|
|
|
|
class HomeBridge(unittest.TestCase):
|
|
def test_capability_is_not_operator_only(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
d = pathlib.Path(td)
|
|
(d / "plugin.json").write_text(json.dumps(MF), encoding="utf-8")
|
|
mf = validate_manifest(d)
|
|
self.assertFalse(requires_operator(mf))
|
|
item = next(c for c in capability_summary(mf) if c["kind"] == "capability")
|
|
self.assertIn("home page", item["label"])
|
|
|
|
def test_tiles_and_counts(self):
|
|
deps = main.app.state.ai
|
|
tc = TestClient(main.app)
|
|
tiles = [{"id": "week:personal", "pluginId": "plan-usage", "kind": "arc", "label": "Week", "pct": 61.0}]
|
|
cards = [{"sessionId": "a", "meta": {"state": "running"}},
|
|
{"sessionId": "b", "meta": {"state": "running", "archived": True}},
|
|
{"sessionId": "c", "meta": {"state": "finished"}}]
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = _install(td, MF)
|
|
with patch.object(deps, "plugin_store", store), \
|
|
patch("plugins.home_tiles.tiles_for", return_value=tiles) as tf, \
|
|
patch("conversations.catalog._conversation_cards", return_value=cards), \
|
|
patch("plugins.waiting.waiting_for", return_value=[{"id": "f1"}, {"id": "f2"}]):
|
|
r = tc.get("/proxy/tools/plugins/live/hub/home?profileScope=all&tzOffset=5000", headers=_h())
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertEqual(r.json(), {"tiles": tiles, "running": 1, "waiting": 2})
|
|
self.assertEqual(tf.call_args.args[2:], ("all", "all", "", 960))
|
|
|
|
def test_needs_the_capability(self):
|
|
deps = main.app.state.ai
|
|
tc = TestClient(main.app)
|
|
with tempfile.TemporaryDirectory() as td:
|
|
store = _install(td, {**MF, "id": "nocap", "capabilities": []})
|
|
with patch.object(deps, "plugin_store", store):
|
|
r = tc.get("/proxy/tools/plugins/nocap/hub/home", headers=_h("nocap"))
|
|
self.assertEqual(r.status_code, 403)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|