Backend foundation for the plugin platform (SPEC §11), Phases 1-3.
Phase 1 — SDK + loader:
- app/plugins/types.py: frozen Plugin/UiManifest/Slot/Page/JobSpec contract.
- app/plugins/sdk.py: sdk(name) facade — the only app.* import allowed for plugins
(scoped session, plugin.{name}.* publish, blob, outbox, settings, SSRF http_get, KV, enqueue).
- app/plugins/loader.py: entry-point discovery (webmail.plugins), hook forwarding
(API + worker), router mounting at /api/plugins/{name}, ui-manifest aggregation.
- plugin_kv core table (migration 0013) + GET /api/plugins/ui-manifest.
Phase 2 — non-removable core plugins + DB-enforced privacy:
- builtin flag (calendar/contacts will be builtin=True; uninstall refuses them).
- RLS per-user isolation on plg_* tables: least-privilege webmail_plugin role
(migration 0014), plugin_session(user_id) sets LOCAL ROLE + app.current_user_id,
rls.enable_rls() (ENABLE+FORCE RLS + USING/WITH CHECK policy). Mail untouched.
- Per-plugin Alembic runner (shared env, dedicated version table) + install/uninstall
--drop-data CLI; skeleton fixture proves install -> run -> uninstall --drop-data
leaves zero residue (§12 M3 acceptance).
Phase 3 — plugin jobs (§11.2):
- JobSpec registered as arq function plg.{plugin}.{func} + cron (5-field parser);
singleton=True guarded by a per-(plugin,user) Redis lock; wired onto the shared
interactive worker.
Gates: ruff, mypy --strict (94 files + tests), full suite 326 passed. RLS isolation,
migration round-trip, and singleton lock covered by tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rcj3TpydMTzTSmU7kunzf8
118 lines
3.9 KiB
Python
118 lines
3.9 KiB
Python
"""Plugin discovery + core wiring (SPEC §11)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import httpx
|
|
import pytest
|
|
from fastapi import APIRouter, FastAPI
|
|
from httpx import ASGITransport
|
|
|
|
from app.events import get_event_bus
|
|
from app.plugins import loader
|
|
from app.plugins.sdk import Page, PageNav, Plugin, Slot, UiManifest
|
|
|
|
|
|
class _FakeEP:
|
|
def __init__(self, name: str, obj: object) -> None:
|
|
self.name = name
|
|
self._obj = obj
|
|
|
|
def load(self) -> object:
|
|
return self._obj
|
|
|
|
|
|
def test_discover_reads_entry_points(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
plugin = Plugin(name="filters", version="1.2")
|
|
monkeypatch.setattr(loader, "entry_points", lambda group: [_FakeEP("filters", plugin)])
|
|
loader.set_plugins_for_test(None) # force rediscovery through the fake
|
|
found = loader.discover_plugins()
|
|
assert [(p.name, p.version) for p in found] == [("filters", "1.2")]
|
|
|
|
|
|
def test_discover_accepts_module_exposing_plugin(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
class _Mod:
|
|
plugin = Plugin(name="labels", version="0.1")
|
|
|
|
monkeypatch.setattr(loader, "entry_points", lambda group: [_FakeEP("labels", _Mod())])
|
|
loader.set_plugins_for_test(None)
|
|
assert [p.name for p in loader.discover_plugins()] == ["labels"]
|
|
|
|
|
|
def test_discover_skips_duplicate_names(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
eps = [
|
|
_FakeEP("a", Plugin(name="dup", version="1")),
|
|
_FakeEP("b", Plugin(name="dup", version="2")),
|
|
]
|
|
monkeypatch.setattr(loader, "entry_points", lambda group: eps)
|
|
loader.set_plugins_for_test(None)
|
|
found = loader.discover_plugins()
|
|
assert [p.version for p in found] == ["1"] # first wins, duplicate dropped
|
|
|
|
|
|
async def test_register_hooks_forwards_events() -> None:
|
|
seen: list[dict[str, object]] = []
|
|
|
|
async def on_new(payload: dict[str, object]) -> None:
|
|
seen.append(payload)
|
|
|
|
loader.set_plugins_for_test(
|
|
[Plugin(name="watcher", version="1", hooks={"message.new": on_new})]
|
|
)
|
|
bus = get_event_bus()
|
|
loader.register_hooks(bus)
|
|
loader.register_hooks(bus) # idempotent: no double subscription
|
|
|
|
await bus.publish("message.new", {"user_id": "u", "message_id": "m"})
|
|
assert seen == [{"user_id": "u", "message_id": "m"}]
|
|
|
|
|
|
async def test_mount_routers_prefixes_by_name() -> None:
|
|
r = APIRouter()
|
|
|
|
@r.get("/ping")
|
|
async def _ping() -> dict[str, str]:
|
|
return {"ok": "1"}
|
|
|
|
loader.set_plugins_for_test([Plugin(name="demo", version="1", router=r)])
|
|
app = FastAPI()
|
|
loader.mount_routers(app)
|
|
transport = ASGITransport(app=app)
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
|
|
resp = await client.get("/api/plugins/demo/ping")
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {"ok": "1"}
|
|
|
|
|
|
def test_build_ui_manifest_aggregates() -> None:
|
|
manifest = UiManifest(
|
|
slots=[Slot(name="message.actions", bundle="/a.js")],
|
|
pages=[
|
|
Page(
|
|
route="/notes",
|
|
nav=PageNav(icon="note", label="Notes", order=5),
|
|
bundle="/p.js",
|
|
)
|
|
],
|
|
capabilities={"recipients.suggest": "/api/plugins/contacts/suggest"},
|
|
)
|
|
loader.set_plugins_for_test([Plugin(name="notes", version="2.0", ui_manifest=manifest)])
|
|
out = loader.build_ui_manifest()
|
|
assert out == {
|
|
"plugins": [
|
|
{
|
|
"name": "notes",
|
|
"version": "2.0",
|
|
"builtin": False,
|
|
"slots": [{"name": "message.actions", "bundle": "/a.js"}],
|
|
"pages": [
|
|
{
|
|
"route": "/notes",
|
|
"bundle": "/p.js",
|
|
"nav": {"icon": "note", "label": "Notes", "order": 5},
|
|
}
|
|
],
|
|
"capabilities": {"recipients.suggest": "/api/plugins/contacts/suggest"},
|
|
}
|
|
]
|
|
}
|