Files
LLeMbas/tests/test_connection_headers.py
T
HomerandClaude Opus 5 28390095a9 A phone, and how much of this could not be used on one
The sidebar was a 280px panel laid over the page below the phone breakpoint,
opened from first paint, with the only control that closed it underneath it --
and that control existed on /chat and on none of the seven other pages carrying
a sidebar, Settings included. It starts closed at that width now, slides, dims
the page behind it, and closes by tapping beside it, by Escape, or by its own
button, which is inside the drawer where it can be reached.

Everything a finger has to hit was 36px, or 28 for renaming a chat, every action
on a message and every panel's close button. Raising --control-h under a coarse
pointer is the only fix that reaches all forty of them, which is what that token
is for. The row and message actions were also hover-only, so on a phone they did
not exist at all.

Installing: the splash and the browser chrome follow the instance's theme rather
than always being Moria's near-black; there are screenshots, so the install
offer is a dialog rather than a one-line bar; a new release no longer takes over
a page somebody is reading; the notification badge is a silhouette rather than a
grey square; and a browser rotating its own subscription no longer ends
notifications for good.

Every request now says it is happening -- nothing did before, so anything slower
than a few milliseconds looked like a click that had not registered.

A chat can be archived. The column has been filtered on in four places since
folders arrived and written by nothing, which is what made it look built.

chat.css may contain media queries. The ban protected the composer toolbar from
being "fixed" with a breakpoint; that guarantee is asserted directly now, and
the old test would have passed a version of the file that wrapped the toolbar
without one.

scripts/shoot.py is the instrument all of this was found with: it renders a page
through TestClient into a real headless browser at a real size and refuses to
run if an asset URL was left pointing at testserver.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-25 13:39:35 +00:00

86 lines
2.9 KiB
Python

"""Extra headers on a connection: read on every request, written by no form.
`Connection.extra_headers_json` has been sent with every request to an endpoint
since it was added and there was nowhere to set it, so its one documented use --
OpenRouter reads `HTTP-Referer` and `X-Title` and attributes usage with them --
was unreachable. Nothing advertised it, so nothing was untrue; it was simply a
column that could only ever be empty.
"""
from __future__ import annotations
from fastapi.testclient import TestClient
from lembas.db.models import Connection
def _connection(client: TestClient, db):
client.post(
"/admin/connections",
data={"name": "OpenRouter", "base_url": "http://127.0.0.1:1", "api_key": ""},
follow_redirects=False,
)
from sqlalchemy import select
return db.scalars(select(Connection)).first().id
def _save(client: TestClient, connection_id: str, headers: str):
return client.post(
f"/admin/connections/{connection_id}",
data={
"name": "OpenRouter",
"base_url": "http://127.0.0.1:1",
"api_key": "",
"enabled": "on",
"unload_url": "",
"unload_method": "POST",
"extra_headers": headers,
},
follow_redirects=False,
)
def test_headers_are_stored_as_a_dict(client: TestClient, db, registered):
"""Asserted on the row, not on the form: a field that renders and is never
read looks exactly like one that works."""
cid = _connection(client, db)
_save(client, cid, "HTTP-Referer: https://example.org\nX-Title: LLeMbas")
db.expire_all()
stored = db.get(Connection, cid).extra_headers_json
assert stored == {
"HTTP-Referer": "https://example.org",
"X-Title": "LLeMbas",
}
def test_they_reach_the_endpoint(client: TestClient, db, registered):
"""The whole point. `openai_client` passes them to httpx verbatim."""
cid = _connection(client, db)
_save(client, cid, "X-Title: LLeMbas")
db.expire_all()
from lembas.services.llm.openai_client import Endpoint
endpoint = Endpoint.from_connection(db.get(Connection, cid))
assert endpoint.extra_headers["X-Title"] == "LLeMbas"
def test_clearing_the_box_clears_them(client: TestClient, db, registered):
cid = _connection(client, db)
_save(client, cid, "X-Title: LLeMbas")
_save(client, cid, "")
db.expire_all()
assert db.get(Connection, cid).extra_headers_json == {}
def test_a_name_cannot_smuggle_in_a_second_header(client: TestClient, db, registered):
"""One field must write one header. A colon or a newline in a *name* is how
one becomes two, and a header nobody can see the effect of is worse than one
that is visibly missing -- so a bad line is dropped, never repaired."""
cid = _connection(client, db)
_save(client, cid, "Bad Name: x\nX-Ok: y\n: nothing\nAlso-Bad\n")
db.expire_all()
assert db.get(Connection, cid).extra_headers_json == {"X-Ok": "y"}