Files
LLeMbas/tests/conftest.py
T
Jaroslav Beneš 2f09d8363d Something can happen because time passed, and land somewhere worth reading
Nothing in LLeMbas ever happened on its own. Every reply was downstream of
somebody pressing Send, and the one exception -- jobs.wake, waking a chat when a
background job finishes -- was downstream of a command they had run. PLAN.md
never listed scheduling as unbuilt because services/chat.py:618 had recorded it
as a decision: "a scheduler is a whole new concern for a single-worker
application". This is that concern, taken on deliberately, plus the two places
its output goes.

Reports first, because it is useful with no scheduling at all. A report is not a
Chat with one Message in it: it has no turns and no reply, it is read top to
bottom, and it must be writable with no chat behind it -- being the fallback for
a run whose own chat has gone. As a Chat it would need a sidebar row per daily
report, a title that regenerates itself, a composer to suppress and a bubble with
a rewind button around something that is not a turn. The section's character is
enforced by absence: nothing under reports/ includes the composer or renders
chat/_message.html, so there is no sse-connect anywhere and nothing on those
pages *can* start a generation. The test reads that off the OpenAPI schema, not
by walking app.routes -- this FastAPI keeps an included router wrapped rather
than flattening it, so the walk finds nothing and the assertion passes for the
wrong reason.

rule.py is pure, total, and was finished before anything called it. No session,
no wall clock, nothing that raises: validate clamps what it recognises, drops
what it does not, and answers {} for prose -- at which point the caller shows the
manual form. It had to be that way because the compile step's output is model
output that becomes a *timer*, which is the sharpest case of hard rule 6 here.
The invariant, pinned: anything validate accepts has a computable next
occurrence. A schedule that can never fire looks exactly like a working one on
every screen it appears on.

Wall-clock and elapsed time are kept apart because they mean different things.
at.times are wall-clock in the owner's zone, so 15:00 stays 15:00 across a
daylight-saving change -- that is what "every Monday at 3PM" means. every is
elapsed real time, so six hours stays six hours across a 23- or 25-hour day --
that is what a timer means. Conflating them gets one of the two wrong twice a
year. A time inside the spring-forward gap fires at the first minute that exists;
left to zoneinfo's own resolution it lands an hour away wearing a wall-clock time
that did not happen, and a daily 02:30 report vanishing once a year on a machine
nobody watches is the failure this file is arranged around.

The ticker claims and commits *before* it fires. The other order is a hot loop: a
firing that raises is retried every tick for ever against whatever it was that
failed, and the only symptom is load. Its blanket except is copied from the
terminal reaper for a sharper reason -- a ticker that dies on one bad row stops
every schedule on the instance and says nothing at all. No request fails, no
reply errors, no dot appears. The reports simply stop.

Three rules that look like bugs from outside: a firing arriving while the chat is
still answering queues rather than starting a second reply, and past max_queued
is skipped with the reason on the row; Run now does not advance next_fire_at, or
testing a schedule silently consumes the run it was testing; resuming recomputes
from now, or a schedule paused for a month fires the instant it comes back, once
per occurrence it missed. Catching up lives in the sweep and not in a startup
hook, because a suspended host and a long stall reproduce "its time passed while
nothing was running" with no restart to hang one on.

services/wake.py is the lock discipline extracted rather than copied. A finished
job and a due schedule are the same problem, and both depend on there being no
await between the running_for check and the writes; two lock dictionaries for one
invariant is how one of them drifts. jobs.wake is now a caller that supplies
wording, and _completion_text stayed exactly where it was because tool.background
quotes its opening sentence.

A scheduled run has no reader, so ask_user is withdrawn from resolve_tools rather
than merely discouraged in core.unattended -- a rule living only in a system
message is one a page the model just read can argue with, and a parked question
holds the reply for the whole approval_timeout with nobody to answer it. For the
same reason a task chat may not be an agent chat in v1: Manual, Edit and Plan all
stop to ask on RISK_EXECUTE, so the only two outcomes would be unattended
execution and a reply that stalls. That deserves its own pass.

Messages is bounded in the request and unbounded on disk. Only the latest chunk
is sent; everything else stays exactly where it was written. Nothing is folded
into text and nothing is deleted -- the visible conversation is identical either
way, so destroying the older rows would buy only disk, against being irreversible
and losing every attachment and tool call in the range, and it would contradict
the rule compaction already holds. should_compact refuses this kind for the
matching reason: two mechanisms narrowing one transcript is how a summary ends up
summarising a summary. The history route is the mirror of thread_tail and keeps
its four properties; the fifth is its own, that prepending moves the scroll
position, so app.js records scrollHeight before the swap and adds the difference
back after.

An empty Chat.kind meant "both sides of the switch" and had been read as "no
filter" since there were only two of them. The sidebar passes "" precisely when
agent chats are switched off -- so the moment a third kind existed, every task
chat and every Messages conversation appeared in somebody's ordinary chat list,
on exactly the instances whose owners would never think to look. KINDS stays the
two-sided fork, because set_sidebar_kind validates against it and a third entry
there makes the tree filterable to a side with no button to leave it; ALL_KINDS
is what a row may be. Both narrowings are pinned, because they are two
implementations of one rule and only one of them is SQL.

Per-user timezone had to exist for any of this: harness.py:179 was telling every
reader the *server's* idea of the date, which is survivable while the answer is
prose and stops being survivable the moment somebody says "every Monday at 3" and
something has to work out when that is.

Three things were caught by a test being wrong rather than by the code being
wrong. The task-chat "no composer" assertions were passing against a page
rendering its no-models-configured branch. A permission test asserted the same
thing twice because the administrator bypasses every permission. And every
Messages test passed with default_model never called, because none of them
configured a model -- so the pair it returns was being assigned straight to
model_id, and SQLite refuses a tuple in a String column. The fixtures now say why
they exist.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 21:31:36 +02:00

284 lines
9.4 KiB
Python

"""Test fixtures.
Every test runs against a throwaway SQLite file in a tmp_path, never the real
data directory. The environment has to be set before lembas.config is imported,
because Settings is a cached singleton read at import time.
"""
from __future__ import annotations
import os
import tempfile
from collections.abc import Iterator
from pathlib import Path
import pytest
# Must happen before any lembas import.
_TMP = tempfile.mkdtemp(prefix="lembas-tests-")
os.environ.update(
{
"LEMBAS_SECRET_KEY": "test-secret-key-not-for-real-use",
"LEMBAS_DATA_DIR": _TMP,
"LEMBAS_ALLOW_SIGNUP": "true",
"LEMBAS_LOG_LEVEL": "warning",
}
)
from fastapi.testclient import TestClient # noqa: E402
from sqlalchemy.orm import Session # noqa: E402
from lembas.config import settings # noqa: E402
from lembas.db.base import Base # noqa: E402
from lembas.db.session import get_engine, get_session_factory, reset_engine # noqa: E402
@pytest.fixture(autouse=True)
def fresh_database(tmp_path: Path) -> Iterator[None]:
"""Point the engine at a per-test database and build the schema.
reset_engine() is essential: the engine is a module-level singleton, so
without it every test after the first would share the first one's file.
"""
settings.data_dir = tmp_path
reset_engine()
settings.ensure_dirs()
import lembas.db.models # noqa: F401 (registers the tables)
# sync_schema rather than create_all: it is what startup runs, and it also
# builds the full-text indexes, which are not SQLAlchemy models and so are
# invisible to create_all. Tests were otherwise running against a schema
# production does not have.
from lembas.db.migrations import sync_schema
Base.metadata.create_all(bind=get_engine())
sync_schema(get_engine())
yield
reset_engine()
@pytest.fixture(autouse=True)
def fresh_generation_registry() -> Iterator[None]:
"""Empty the in-flight reply registry between tests.
`_RUNNING` and `_TASKS` are module-level dicts, so a test that starts a
reply and does not wait for it leaves an entry behind for the rest of the
session -- holding a Generation, and a Task belonging to an event loop that
has since closed. `_prune()` will not clear it either: it only drops
generations that have finished, and it runs on every `ensure()`.
Cheap, and it keeps a test that posts a message from meeting the leftovers
of one that asked a question.
"""
from lembas.services import generation as generation_service
generation_service._RUNNING.clear()
generation_service._TASKS.clear()
yield
generation_service._RUNNING.clear()
generation_service._TASKS.clear()
@pytest.fixture(autouse=True)
def fresh_terminal_registry() -> Iterator[None]:
"""Empty the open-shell registry between tests, for the same reason.
A leaked entry holds an asyncssh connection belonging to an event loop that
has since closed, and the reaper task is module-level too -- one left
running would wake up inside the next test's loop.
"""
from lembas.services.agent import terminal as terminal_service
def _clear() -> None:
reaper = terminal_service._REAPER
if reaper is not None:
reaper.cancel()
terminal_service._REAPER = None
terminal_service._SESSIONS.clear()
_clear()
yield
_clear()
@pytest.fixture(autouse=True)
def fresh_schedule_ticker() -> Iterator[None]:
"""Stop the schedule ticker and forget any firings, for the same reason.
The ticker is a module-level task like the terminal reaper, and a firing is
a task holding a chat id. One left running would wake up inside the next
test's event loop, against the next test's database, and fire something
nobody in that test has ever heard of.
The wake locks go too: they are keyed on chat id, and `make_chat` recycles
ids freely across a session.
"""
from lembas.services import wake as wake_service
from lembas.services.schedule import ticker as ticker_service
def _clear() -> None:
running = ticker_service._TICKER
if running is not None:
running.cancel()
ticker_service._TICKER = None
for task in list(ticker_service._FIRING):
task.cancel()
ticker_service._FIRING.clear()
wake_service._LOCKS.clear()
_clear()
yield
_clear()
@pytest.fixture(autouse=True)
def fresh_project_index() -> Iterator[None]:
"""Empty the directory-listing cache between tests, for the third time.
Keyed on (profile, directory) and both are recycled freely by fixtures, so
without this a test asserting "the listing said X" can be answered by the
previous test's walk of an entirely different tmp_path.
"""
from lembas.services.agent import index as index_service
from lembas.services.agent import instructions as instructions_service
index_service.clear()
instructions_service.clear()
yield
index_service.clear()
instructions_service.clear()
@pytest.fixture
def db() -> Iterator[Session]:
session = get_session_factory()()
try:
yield session
finally:
session.close()
@pytest.fixture
def client() -> Iterator[TestClient]:
from lembas.main import app
# raise_server_exceptions=False so error-handler behaviour is exercised
# rather than the exception propagating into the test.
with TestClient(app, raise_server_exceptions=False) as test_client:
yield test_client
@pytest.fixture
def registered(client: TestClient) -> dict[str, str]:
"""Register the first account. It becomes the administrator."""
credentials = {
"name": "Frodo",
"email": "frodo@shire.test",
"password": "speak-friend-and-enter",
}
response = client.post("/auth/register", data=credentials, follow_redirects=False)
assert response.status_code == 303, response.text
return credentials
@pytest.fixture
def make_chat(db: Session):
"""Create a chat row directly, as scaffolding for other tests.
Chats are normally created by POST /api/chats/start along with their first
exchange -- there is deliberately no endpoint that makes an empty one. Most
tests want a chat to act on, not that flow, so they get one straight from
the database rather than having to subtract an opening turn from every
assertion. The flow itself is covered in test_chat.py.
"""
from sqlalchemy import select
from lembas.db.models import Chat, Model, User
def _create(email: str | None = None, model_id: str | None = None) -> str:
user = (
db.scalar(select(User).where(User.email == email))
if email
else db.scalars(select(User).order_by(User.created_at)).first()
)
model = (
db.scalar(select(Model).where(Model.model_id == model_id))
if model_id
else db.scalars(select(Model).order_by(Model.position)).first()
)
chat = Chat(
user_id=user.id,
model_id=model.model_id if model else "",
connection_id=model.connection_id if model else None,
)
db.add(chat)
db.commit()
return chat.id
return _create
@pytest.fixture
def mock_http():
"""Answer every outgoing httpx request with a handler of the test's choosing.
The services build their own AsyncClient because each needs its own timeout,
so there is no client to inject; patching the class is what reaches them.
Returns a callable that installs a handler and is undone on teardown.
"""
import httpx
original = httpx.AsyncClient
def install(handler):
class Patched(original):
def __init__(self, **kwargs):
super().__init__(transport=httpx.MockTransport(handler), **kwargs)
httpx.AsyncClient = Patched
yield install
httpx.AsyncClient = original
def control_named(html: str, name: str) -> dict[str, str]:
"""The attributes of the one element carrying `name="…"`.
Exists so a test can ask "does the control that carries the name also carry
the verb?". Two selects in the composer once delegated their `hx-patch` to
an empty sibling form through the `form=` attribute, which scopes values but
routes no events -- htmx binds a trigger to the annotated element, and
`change` reaches ancestors, never siblings. Both controls were decorative
for a whole release, and the tests passed the entire time because they
asserted the markup that was there rather than the property that mattered.
"""
from html.parser import HTMLParser
found: list[dict[str, str]] = []
class Finder(HTMLParser):
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
got = {key: (value or "") for key, value in attrs}
if got.get("name") == name:
found.append(got)
Finder().feed(html)
assert len(found) == 1, f"expected one element named {name!r}, found {len(found)}"
return found[0]
@pytest.fixture
def user_id(db: Session, registered: dict[str, str]) -> str:
"""The registered user's id.
Chats have a real foreign key to users and SQLite enforces it (the
connect-time PRAGMA in db/session.py turns that on), so tests that build a
Chat directly need a user that actually exists.
"""
from sqlalchemy import select
from lembas.db.models import User
return db.scalar(select(User).where(User.email == registered["email"])).id