Files
webmail-client/backend/tests/conftest.py
T
lukasandClaude Opus 4.8 d46ad13b7b M3 plugin platform: frontend host, reference plugins, UI kit + docs
Completes M3 (SPEC §11 + §12 acceptance) on top of the backend platform (4e077f1).

Frontend host (§11.3):
- src/plugins/: manifest loader, runtime ESM bundle loader (framework-agnostic
  mount contract), PluginSlot, PluginPage (app.page at /p/*), plugin context
  (scoped api/events/i18n), capabilities hook. Wired slots into ThreadView
  (message.actions/message.banner), MailLayout sidebar (sidebar.section + page nav)
  and Settings→Accounts (settings.page); ws.ts fans out core + plugin.* events.
- plugin-ui.css: theme-aware .wm-* classes so bundles don't re-derive inline styles.

SDK additions:
- CurrentUserId + require_csrf (plugin-router auth), messages.envelope/header,
  actions.set_flags/trash, sdk.enqueue.

Reference plugins (plugins/, installed via webmail.plugins entry points):
- hello: skeleton page-plugin (app.page + plg_hello_note w/ RLS + cron job) —
  install→run→uninstall --drop-data leaves zero residue.
- labels: message.actions + sidebar.section slots, KV only, zero core diffs.
- filters: message.new hook -> match (subject/from/to) -> Mark read / Pin / Delete
  (delete = move thread to Trash via the core path).

docs/plugins.md: full authoring guide + a complete "Unsubscribe button" worked
example (the §12 "writable from docs alone" bar).

Also: conftest resets the arq pool between tests (loop-bound global, like engine/redis).

Gates: backend ruff + mypy --strict (107) + full suite 337; frontend tsc + eslint
+ vitest (11) + vite build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rcj3TpydMTzTSmU7kunzf8
2026-07-27 08:30:24 +02:00

70 lines
2.5 KiB
Python

"""Shared test fixtures and environment defaults.
Ensures the SPEC §14 env vars exist even when tests run outside the conda
activate hook (e.g. CI), so ``app.config`` imports cleanly.
"""
from __future__ import annotations
import base64
import os
os.environ.setdefault("APP_DOMAIN", "test.webmail.local")
os.environ.setdefault(
"DATABASE_URL", "postgresql+asyncpg://webmail:webmail_dev@127.0.0.1:5433/webmail_dev"
)
# Force a dedicated Redis DB for tests (index 15) so the suite is isolated from the
# dev app's Redis (DB 0) and can be flushed between tests without touching real data.
os.environ["REDIS_URL"] = "redis://127.0.0.1:6380/15"
os.environ.setdefault("MASTER_KEY", base64.b64encode(b"\x00" * 32).decode())
os.environ.setdefault("BLOB_ROOT", "./var/blobs-test")
from collections.abc import AsyncIterator # noqa: E402
import pytest_asyncio # noqa: E402
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine # noqa: E402
from sqlalchemy.pool import NullPool # noqa: E402
from app.config import get_settings # noqa: E402
@pytest_asyncio.fixture(autouse=True)
async def _reset_global_engine() -> AsyncIterator[None]:
"""Dispose process-wide async resources after each test.
Each test runs on its own event loop; disposing here ensures the next test
(and any code using the global engine/redis, e.g. the search backend) rebinds
to its own loop rather than reusing a closed one.
"""
from app.redis_client import get_redis
# Flush the dedicated test Redis DB so rate-limit / session / token state does
# not leak between tests (each test starts from a clean slate).
await get_redis().flushdb()
yield
from app.db import dispose_engine
from app.queue import close_arq_pool
from app.redis_client import close_redis
await dispose_engine()
await close_redis()
# The arq enqueue pool is a loop-bound global like the engine/redis; reset it so
# a test that enqueues doesn't reuse a pool from a previous (closed) event loop.
await close_arq_pool()
@pytest_asyncio.fixture
async def db_session() -> AsyncIterator[AsyncSession]:
"""Rollback-per-test session (nothing persists to the dev database)."""
engine = create_async_engine(get_settings().database_url, poolclass=NullPool)
conn = await engine.connect()
trans = await conn.begin()
session = AsyncSession(bind=conn, expire_on_commit=False)
try:
yield session
finally:
await session.close()
await trans.rollback()
await conn.close()
await engine.dispose()