Files
ai-agent/backend/test_plugin_overlays.py
gabrielvidalandClaude Opus 5.5 775e2c2728 feat(plugins): overlays — a plugin draws over a page; the pets leave the core for it
New plugin point ui.overlays (+ ui.prefs): a transparent sandboxed frame
over the conversation page or the home page, fed only the page's box
outlines, its conversations' colour / state / mood, lifecycle stamps and
new-message events — never text. Clicks pass through except inside the
small boxes the frame reports (capped: 16 boxes, 240 px, 15 % of the
frame; a quiet frame or a 60 s press loses the pointer). Per-person
preferences live in the settings blob (pluginPrefs), drawn on the plugin's
page for owner and guests. One eye switch, Plugin overlays; the Animations
switch hides them.

technical/pets/* and the pet-pet dependency removed; the hamsters are the
pets plugin (ai-agent-plugins). Settings v6 moves settings.pet into
pluginPrefs.pets. Gabriel's picks: forms pets-as-an-overlay-plugin_wl2w72,
how-the-pet-moves-on-the-page_6k4gry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 17:47:41 +01:00

141 lines
6.6 KiB
Python

"""``ui.overlays`` + ``ui.prefs``: the overlay point plugins draw over a page
with (the Pets plugin, 2026-11-25) — docs/plugins.md → "Overlays"."""
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 ManifestError, capability_fingerprint, capability_summary, validate_manifest # noqa: E402
from plugins.store import OWNER_UID, PluginStore # noqa: E402
OWNER_H = {"X-Internal-Auth": os.environ["INTERNAL_API_TOKEN"]}
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")
(root / "ui").mkdir(exist_ok=True)
(root / "ui" / "o.html").write_text("<p>pet</p>", encoding="utf-8")
return root
PREFS = [
{"key": "enabled", "type": "switch", "label": "Pets", "default": True},
{"key": "size", "type": "range", "label": "Size", "min": 28, "max": 96, "step": 2, "default": 46, "unit": "px"},
{"key": "color", "type": "color", "label": "Your hamster", "default": "#F4B66A", "swatches": ["#bdb6b0", "nope"]},
]
MANIFEST = {
"schema": 1, "id": "pets", "name": "Pets",
"ui": {
"overlays": [
{"id": "conv", "where": "conversation", "html": "ui/o.html", "pointer": "hits", "cursor": True},
{"id": "home", "where": "home", "html": "ui/o.html"},
],
"prefs": PREFS,
},
"managed": {"defaultForGuests": True},
}
def _mf(td: str, manifest: dict, name: str = "p") -> dict:
return validate_manifest(_write(pathlib.Path(td) / name, manifest))
class TestManifest(unittest.TestCase):
def test_overlays_and_prefs(self):
with tempfile.TemporaryDirectory() as td:
mf = _mf(td, MANIFEST)
ov = mf["ui"]["overlays"]
self.assertEqual(ov[0], {"id": "conv", "where": "conversation", "html": "ui/o.html",
"pointer": "hits", "cursor": True})
# Unset keys aren't written: the fingerprint of a plain overlay can't move.
self.assertEqual(ov[1], {"id": "home", "where": "home", "html": "ui/o.html", "pointer": "none"})
prefs = {p["key"]: p for p in mf["ui"]["prefs"]}
self.assertEqual(prefs["size"]["default"], 46.0)
self.assertEqual(prefs["color"]["default"], "#f4b66a")
self.assertEqual(prefs["color"]["swatches"], ["#bdb6b0"])
def test_refusals(self):
bad = [
{"overlays": [{"id": "x", "where": "sidebar", "html": "ui/o.html"}]},
{"overlays": [{"id": "x", "where": "home", "html": "ui/o.html"},
{"id": "y", "where": "home", "html": "ui/o.html"}]},
{"overlays": [{"id": "x", "where": "home", "html": "ui/o.html", "pointer": "all"}]},
{"overlays": [{"id": "x", "where": "home", "html": "../o.html"}]},
{"prefs": [{"key": "s", "type": "range", "min": 10, "max": 5, "default": 7}]},
{"prefs": [{"key": "c", "type": "color", "default": "red"}]},
{"prefs": [{"key": "m", "type": "select", "options": ["a"], "default": "b"}]},
{"prefs": [{"key": "f", "type": "file"}]},
]
with tempfile.TemporaryDirectory() as td:
for i, ui in enumerate(bad):
with self.subTest(ui=ui), self.assertRaises(ManifestError):
_mf(td, {**MANIFEST, "ui": ui}, f"b{i}")
def test_prefs_are_not_a_capability(self):
with tempfile.TemporaryDirectory() as td:
a = _mf(td, MANIFEST, "a")
b = _mf(td, {**MANIFEST, "ui": {**MANIFEST["ui"], "prefs": PREFS[:1]}}, "b")
c = _mf(td, {**MANIFEST, "ui": {**MANIFEST["ui"], "overlays": MANIFEST["ui"]["overlays"][:1]}}, "c")
self.assertEqual(capability_fingerprint(a), capability_fingerprint(b))
self.assertNotEqual(capability_fingerprint(a), capability_fingerprint(c))
def test_the_consent_card_says_what_it_sees(self):
with tempfile.TemporaryDirectory() as td:
labels = [c["label"] for c in capability_summary(_mf(td, MANIFEST)) if c["kind"] == "ui"]
self.assertTrue(any("conversation pages" in l and "never its text" in l
and "takes clicks" in l and "pointer" in l for l in labels))
self.assertTrue(any("home page" in l and "clicks go through" in l for l in labels))
class TestRoutes(unittest.TestCase):
def setUp(self):
self.tc = TestClient(main.app, headers=OWNER_H)
def test_descriptors_and_document(self):
deps = main.app.state.ai
with tempfile.TemporaryDirectory() as td:
store = PluginStore(pathlib.Path(td) / "data")
store.install(OWNER_UID, str(_write(pathlib.Path(td) / "src", MANIFEST)), is_owner=True, approve=True)
with patch.object(deps, "plugin_store", store):
ui = self.tc.get("/api/plugins").json()["ui"]
doc = self.tc.get("/api/plugins/pets/ui/overlay/conv")
missing = self.tc.get("/api/plugins/pets/ui/overlay/nope")
self.assertEqual([(o["where"], o["pluginId"]) for o in ui["overlays"]],
[("conversation", "pets"), ("home", "pets")])
self.assertEqual([p["key"] for p in ui["prefs"]["pets"]], ["enabled", "size", "color"])
self.assertEqual(doc.status_code, 200)
self.assertEqual(doc.text, "<p>pet</p>")
self.assertEqual(missing.status_code, 404)
def test_a_disabled_plugin_draws_nothing(self):
deps = main.app.state.ai
with tempfile.TemporaryDirectory() as td:
store = PluginStore(pathlib.Path(td) / "data")
store.install(OWNER_UID, str(_write(pathlib.Path(td) / "src", MANIFEST)), is_owner=True, approve=True)
store.update(OWNER_UID, "pets", enabled=False)
with patch.object(deps, "plugin_store", store):
ui = self.tc.get("/api/plugins").json()["ui"]
doc = self.tc.get("/api/plugins/pets/ui/overlay/conv")
self.assertEqual(ui["overlays"], [])
self.assertEqual(doc.status_code, 404)
if __name__ == "__main__":
unittest.main()