Initial commit: webmail client (M0/M1)

Three-pane webmail client.
- Backend: FastAPI + async SQLAlchemy + arq worker (Postgres/Redis), IMAP/SMTP
  sync, threading, search, drafts/outbox with undo, attachments, 2FA.
- Frontend: React 18 + Vite + TypeScript + TanStack Query + Tailwind; three-pane
  UI with rich-text composer, light/dark themes, resizable panels.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 23:20:34 +02:00
co-authored by Claude Opus 4.8
commit a5ec0d21cb
174 changed files with 22239 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
"""Query grammar parser (SPEC §8.2). Covers every operator, negation, quoting,
OR, and garbage input (the parser must never raise)."""
from __future__ import annotations
import uuid
import pytest
from app.search.queryparser import KNOWN_KEYS, free_text, parse, to_search_query
def _flat(query: str) -> list[dict[str, object]]:
return [clause.as_dict() for group in parse(query) for clause in group]
def test_plain_free_text() -> None:
groups = parse("hello world")
assert len(groups) == 2
assert all(len(g) == 1 for g in groups)
assert free_text(groups) == "hello world"
def test_quoted_phrase_is_one_term() -> None:
groups = parse('"hello world" foo')
assert groups[0][0].value == "hello world"
assert groups[0][0].phrase is True
assert groups[1][0].value == "foo"
@pytest.mark.parametrize("key", sorted(KNOWN_KEYS))
def test_every_operator_parses_as_filter(key: str) -> None:
groups = parse(f"{key}:value")
clause = groups[0][0]
assert clause.type == "filter"
assert clause.key == key
assert clause.value == "value"
def test_filter_with_quoted_value() -> None:
clause = parse('from:"John Doe"')[0][0]
assert clause.type == "filter"
assert clause.key == "from"
assert clause.value == "John Doe"
def test_negation_of_term_and_filter() -> None:
clauses = _flat("-spam -from:boss@x.com")
assert clauses[0] == {
"type": "text",
"key": None,
"value": "spam",
"negate": True,
"phrase": False,
}
assert clauses[1]["type"] == "filter"
assert clauses[1]["negate"] is True
assert clauses[1]["key"] == "from"
def test_or_groups_adjacent_clauses() -> None:
groups = parse("cat OR dog fish")
# (cat OR dog) AND fish
assert len(groups) == 2
assert [c.value for c in groups[0]] == ["cat", "dog"]
assert [c.value for c in groups[1]] == ["fish"]
def test_or_mixing_text_and_filter() -> None:
groups = parse("is:unread OR is:starred")
assert len(groups) == 1
assert [c.value for c in groups[0]] == ["unread", "starred"]
def test_unknown_key_is_free_text() -> None:
clause = parse("bogus:value")[0][0]
assert clause.type == "text"
assert clause.value == "bogus:value"
def test_case_insensitive_keys_lowercased() -> None:
clause = parse("FROM:alice")[0][0]
assert clause.type == "filter"
assert clause.key == "from"
@pytest.mark.parametrize(
"garbage",
['unbalanced "quote', "'", '"', "-", ":", "from:", " ", "", "OR OR OR", "a OR"],
)
def test_garbage_never_raises(garbage: str) -> None:
# Must not raise; returns some (possibly empty) parse.
result = parse(garbage)
assert isinstance(result, list)
def test_to_search_query_sets_user_id_and_text() -> None:
uid = uuid.uuid4()
sq = to_search_query("invoice from:alice", uid)
assert sq.user_id == uid
assert sq.text == "invoice"
assert "groups" in sq.filters
def test_dash_only_token_is_text() -> None:
# A lone "-" must not be treated as negation of an empty term.
clause = parse("-")[0][0]
assert clause.value == "-"
assert clause.negate is False
+178
View File
@@ -0,0 +1,178 @@
"""Operator search through the API (SPEC §8.2, §9.1), on the Postgres backend."""
from __future__ import annotations
import uuid
from collections.abc import AsyncIterator
from dataclasses import dataclass
from datetime import UTC, datetime
from email.message import EmailMessage
import httpx
import pytest_asyncio
from httpx import ASGITransport
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.pool import NullPool
from app.config import get_settings
from app.identity.passwords import hash_password
from app.identity.sessions import create_session
from app.indexer.indexer import persist_message
from app.main import create_app
from app.models import Account, Mailbox, User
from app.providers.base import RemoteMessage
from app.search.pg import PgSearchBackend
@dataclass
class Ctx:
client: httpx.AsyncClient
session: AsyncSession
user_id: uuid.UUID
account_id: uuid.UUID
mailbox_id: uuid.UUID
uid: int = 0
@pytest_asyncio.fixture
async def ctx() -> AsyncIterator[Ctx]:
engine = create_async_engine(get_settings().database_url, poolclass=NullPool)
sm = async_sessionmaker(engine, expire_on_commit=False)
session = sm()
user = User(handle=f"sq{uuid.uuid4().hex[:8]}@test.local", password_hash=hash_password("pw"))
session.add(user)
await session.flush()
account = Account(
user_id=user.id,
email="me@example.com",
imap_config={},
smtp_config={},
secret_ct=b"",
discovery_src="manual",
)
session.add(account)
await session.flush()
mailbox = Mailbox(account_id=account.id, user_id=user.id, path="INBOX", special_use="inbox")
session.add(mailbox)
await session.commit()
token = await create_session(user.id, "pytest")
client = httpx.AsyncClient(
transport=ASGITransport(app=create_app()),
base_url="http://testserver",
cookies={"session": token},
)
try:
yield Ctx(
client=client,
session=session,
user_id=user.id,
account_id=account.id,
mailbox_id=mailbox.id,
)
finally:
await client.aclose()
await session.execute(delete(User).where(User.id == user.id))
await session.commit()
await session.close()
await engine.dispose()
async def _ingest(
ctx: Ctx,
*,
frm: str,
subject: str,
body: str,
flags: frozenset[str] = frozenset(),
attach: bool = False,
) -> None:
ctx.uid += 1
m = EmailMessage()
m["Subject"] = subject
m["From"] = frm
m["To"] = "me@example.com"
m["Message-ID"] = f"<{uuid.uuid4().hex}@x.com>"
m["Date"] = "Mon, 09 Mar 2026 08:36:37 +0100"
m.set_content(body)
if attach:
m.add_attachment(b"data", maintype="application", subtype="pdf", filename="report.pdf")
result = await persist_message(
ctx.session,
user_id=ctx.user_id,
account_id=ctx.account_id,
mailbox_id=ctx.mailbox_id,
mailbox_special_use="inbox",
remote=RemoteMessage(
uid=ctx.uid, raw=m.as_bytes(), flags=flags, internal_date=datetime.now(UTC)
),
)
await ctx.session.commit()
assert result is not None
await PgSearchBackend().index(result.search_doc)
async def _search(ctx: Ctx, q: str) -> int:
resp = await ctx.client.get("/api/search", params={"q": q})
assert resp.status_code == 200, resp.text
return len(resp.json()["threads"])
@pytest_asyncio.fixture
async def seeded(ctx: Ctx) -> Ctx:
await _ingest(
ctx,
frm="Bob <bob@corp.com>",
subject="Quarterly revenue",
body="revenue numbers up",
flags=frozenset(),
) # unread
await _ingest(
ctx,
frm="Carol <carol@corp.com>",
subject="Lunch",
body="grab lunch",
flags=frozenset({"\\Seen"}),
) # read
await _ingest(
ctx,
frm="Dave <dave@corp.com>",
subject="Report attached",
body="see the pdf",
flags=frozenset({"\\Seen"}),
attach=True,
)
return ctx
async def test_free_text(seeded: Ctx) -> None:
assert await _search(seeded, "revenue") == 1
assert await _search(seeded, "lunch") == 1
async def test_from_operator(seeded: Ctx) -> None:
assert await _search(seeded, "from:bob") == 1
assert await _search(seeded, "from:corp.com") == 3 # all three share the domain
async def test_is_unread_operator(seeded: Ctx) -> None:
assert await _search(seeded, "is:unread") == 1
assert await _search(seeded, "is:read") == 2
async def test_has_attachment_operator(seeded: Ctx) -> None:
assert await _search(seeded, "has:attachment") == 1
async def test_subject_operator(seeded: Ctx) -> None:
assert await _search(seeded, "subject:revenue") == 1
async def test_negation(seeded: Ctx) -> None:
# everything unread that is NOT from bob -> none
assert await _search(seeded, "is:unread -from:bob") == 0
async def test_combined_operators(seeded: Ctx) -> None:
assert await _search(seeded, "from:dave has:attachment") == 1
assert await _search(seeded, "from:bob has:attachment") == 0