"""Render LLeMbas pages in a real browser, at a real size.
Run it:
python scripts/shoot.py OUTDIR [/chat,/settings] # measure + capture
python scripts/shoot.py OUTDIR --manifest-screenshots # the two the
# manifest wants
Needs a `chromium` on PATH and the development dependencies installed. It is a
development instrument, like the Node DOM stub the JavaScript is driven under
and like `fetch_vendor.py` -- it is not imported by the application and nothing
in `src/` knows it exists.
Not a test runner: an instrument. It renders a page through TestClient, rewrites
every asset URL to a file:// path, and refuses to continue if even one is left
pointing at `testserver` -- because the last harness that did this silently
measured an unstyled document and reported all five tab panels visible at once.
A dramatic finding that was entirely an artefact of a rewrite matching nothing.
"""
from __future__ import annotations
import json
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
SRC = REPO / "src"
sys.path.insert(0, str(SRC))
STATIC = SRC / "lembas/web/static"
CHROMIUM = shutil.which("chromium") or shutil.which("chromium-browser")
# Routes that are served by the app rather than mounted, so the rewrite has to
# fetch them rather than point at a file that does not exist.
ROUTE_ASSETS = {"/branding.css": "branding.css", "/sw.js": "sw.js"}
MEASURE = """
"""
def build_client():
import lembas.config as config_mod
tmp = Path(tempfile.mkdtemp(prefix="lembas-shoot-"))
config_mod.settings.data_dir = tmp
config_mod.settings.secret_key = "x" * 43
from fastapi.testclient import TestClient
from lembas.db.session import init_db, session_scope
from lembas.main import create_app
init_db()
app = create_app()
client = TestClient(app)
client.post(
"/auth/register",
data={"name": "Frodo", "email": "f@example.com", "password": "mellonmellon"},
follow_redirects=False,
)
from lembas.db.models import Connection, Model
with session_scope() as db:
connection = Connection(
name="local", base_url="http://127.0.0.1:1", api_key_encrypted=""
)
db.add(connection)
db.flush()
for name in ("gemma4-moe", "qwen3-coder"):
db.add(Model(connection_id=connection.id, model_id=name, display_name=name))
return client
def rewrite(html: str, client, assets: Path) -> str:
"""Point every asset at a file on disk, and prove none was missed."""
for route, name in ROUTE_ASSETS.items():
response = client.get(route)
if response.status_code == 200:
(assets / name).write_text(response.text)
html = re.sub(
r'(?:http://testserver)?/static/([^"\'?\s>]+)(\?[^"\'\s>]*)?',
lambda m: f"file://{STATIC}/{m.group(1)}",
html,
)
html = re.sub(
r'(?:http://testserver)?/branding\.css(\?[^"\'\s>]*)?',
f"file://{assets}/branding.css",
html,
)
# Fail loudly, and only about things that decide how the page LOOKS: every
# `src`, and `href` on a . An `href` on an anchor is a destination,
# not an asset -- flagging those makes the guard cry wolf on every page and
# a guard nobody believes is worse than none.
leftovers = re.findall(r']*\bhref="([^"]+)"', html)
leftovers += re.findall(r'\bsrc="([^"]+)"', html)
blocking = [
url
for url in leftovers
if url.startswith(("/", "http://testserver"))
and not url.startswith(("/branding/", "/manifest", "/sw.js"))
]
if blocking:
raise SystemExit(
"UNREWRITTEN ASSET URLS -- this would measure an unstyled document: "
f"{sorted(set(blocking))[:8]}"
)
# The one-time notifications offer is a modal over the very page we came
# to measure, and it is gated on a localStorage key. Set it in the head, so
# it runs before the deferred script that reads it.
quiet = (
""
)
return html.replace("", quiet + MEASURE + "", 1)
def shoot(client, path: str, width: int, height: int, theme: str, outdir: Path) -> dict:
"""One page, at one size, in one theme.
The page is rendered inside an