PWA, one send/stop button, audio in and out, web search as a tool
Four pieces of work.
**Installable.** A manifest carrying the instance name, PWA icons rasterised
from the existing mark at design time, a service worker and a themed offline
page. The worker caches the shell only and bails out on /api/, /auth/, /admin/
and anything accepting text/event-stream -- passing a reply stream through a
worker turns it into one delivery at the end, or nothing. It is served from
GET /sw.js rather than the static mount because a worker's scope is the path it
came from.
**Send and Stop are one button.** They were two, and the hidden one was never
hidden: `.btn` is display: inline-flex, which outranks the browser's own
`[hidden] { display: none }`, so Stop sat permanently beside Send. app.css now
forces the attribute to win -- every control toggled with `hidden` depended on
that -- and the composer renders one button carrying both icons, with ui.js
flipping data-composer-action and the type with it.
**Audio.** Speech to text and text to speech against any OpenAI-shaped
/v1/audio/* endpoint: dictate into the composer, have a reply read out.
Instance settings in Admin, per-reader overrides in Settings, with the voice
list discovered from the server where it offers one. Recorded audio is capped
and never written to disk -- it is not an attachment, it has no owner, and
nothing would ever sweep it.
**Web search, as a tool.** This is the tool loop PLAN.md described as the real
work: one reply is now a bounded sequence of requests rather than one. The model
asks, the tool runs, the result goes back and it is asked again, up to three
rounds. Providers are DuckDuckGo (no setup), SearXNG and Firecrawl.
Two decisions worth stating. Tools are only offered to models flagged `tools`,
because an endpoint without support rejects the whole request rather than
ignoring the array -- the same reason images only reach models flagged
`vision`. And tool results are not replayed as context on the next turn, for the
same reasons reasoning is not: the answer already contains what the model made
of them, and replaying stale results into every later request wastes the window
and reliably sends a small model into a search loop. The sources stay visible in
the transcript instead.
Search results are untrusted third-party text and are treated as such: escaped,
and only http/https URLs rendered as links.
338 tests, ruff clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
"""Installing as an app, and the composer's single send/stop button."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from lembas.services import settings_store
|
||||
from lembas.web.templating import STATIC_DIR
|
||||
|
||||
|
||||
# --- Manifest ----------------------------------------------------------------
|
||||
def test_the_manifest_is_readable_when_signed_out(client: TestClient):
|
||||
"""A browser fetches the manifest outside any page's session."""
|
||||
response = client.get("/manifest.webmanifest")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("application/manifest+json")
|
||||
|
||||
|
||||
def test_the_manifest_carries_the_instance_name(client: TestClient, db, registered):
|
||||
client.post(
|
||||
"/admin/general", data={"instance_name": "Rivendell"}, follow_redirects=False
|
||||
)
|
||||
assert client.get("/manifest.webmanifest").json()["name"] == "Rivendell"
|
||||
|
||||
|
||||
def test_the_manifest_offers_a_maskable_icon(client: TestClient):
|
||||
"""Without one, Android crops the corners off the wafer."""
|
||||
icons = client.get("/manifest.webmanifest").json()["icons"]
|
||||
assert any(icon["purpose"] == "maskable" for icon in icons)
|
||||
assert any(icon["sizes"] == "512x512" and icon["purpose"] == "any" for icon in icons)
|
||||
|
||||
|
||||
def test_every_manifest_icon_exists(client: TestClient):
|
||||
for icon in client.get("/manifest.webmanifest").json()["icons"]:
|
||||
assert client.get(icon["src"]).status_code == 200, icon["src"]
|
||||
|
||||
|
||||
def test_the_manifest_starts_at_the_chat(client: TestClient):
|
||||
payload = client.get("/manifest.webmanifest").json()
|
||||
assert payload["start_url"] == "/chat"
|
||||
assert payload["scope"] == "/"
|
||||
assert payload["display"] == "standalone"
|
||||
|
||||
|
||||
# --- Service worker ----------------------------------------------------------
|
||||
def test_the_worker_is_served_from_the_root(client: TestClient):
|
||||
"""A worker under /static/js/ would have scope /static/js/ and control
|
||||
nothing."""
|
||||
response = client.get("/sw.js")
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("text/javascript")
|
||||
|
||||
|
||||
def test_the_worker_is_never_cached(client: TestClient):
|
||||
"""A stale worker keeps serving a stale cache."""
|
||||
assert "no-store" in client.get("/sw.js").headers["cache-control"]
|
||||
|
||||
|
||||
def test_the_worker_leaves_the_api_alone():
|
||||
"""The reply stream, the unread poll and attachment downloads all live
|
||||
under /api/. A cached response on any of them is at best stale."""
|
||||
source = (STATIC_DIR / "js" / "sw.js").read_text()
|
||||
assert '"/api/"' in source
|
||||
assert "text/event-stream" in source
|
||||
|
||||
|
||||
def test_every_precached_asset_exists(client: TestClient):
|
||||
"""addAll is all-or-nothing in most implementations, and a missing entry is
|
||||
invisible until someone opens the developer tools."""
|
||||
source = (STATIC_DIR / "js" / "sw.js").read_text()
|
||||
shell = source.split("var SHELL = [", 1)[1].split("];", 1)[0]
|
||||
paths = [line.strip().strip('",') for line in shell.splitlines() if '"' in line]
|
||||
|
||||
assert paths
|
||||
for path in paths:
|
||||
assert client.get(path).status_code == 200, path
|
||||
|
||||
|
||||
def test_the_offline_page_stands_on_its_own(client: TestClient):
|
||||
"""Cached at install time, so it must render with no user and no chats."""
|
||||
response = client.get("/offline")
|
||||
assert response.status_code == 200
|
||||
assert 'class="sidebar' not in response.text
|
||||
assert 'id="thread' not in response.text
|
||||
|
||||
|
||||
def test_the_page_links_the_manifest_and_the_apple_icon(client: TestClient, registered):
|
||||
page = client.get("/chat").text
|
||||
assert 'rel="manifest"' in page
|
||||
assert 'rel="apple-touch-icon"' in page
|
||||
assert 'name="theme-color"' in page
|
||||
|
||||
|
||||
# --- Send and Stop -----------------------------------------------------------
|
||||
def test_the_hidden_attribute_wins_over_component_styles():
|
||||
"""`.btn` is display: inline-flex, which beats the browser's own
|
||||
`[hidden] { display: none }`. Without this rule a button hidden from
|
||||
JavaScript stays on screen -- which is how Stop came to sit permanently
|
||||
beside Send."""
|
||||
css = (STATIC_DIR / "css" / "app.css").read_text()
|
||||
assert "[hidden]" in css
|
||||
assert "display: none !important" in css
|
||||
|
||||
|
||||
def test_the_composer_has_exactly_one_send_button(client: TestClient, db, registered):
|
||||
"""One button that becomes Stop, not two that take turns being hidden."""
|
||||
_add_a_model(db)
|
||||
page = client.get("/chat").text
|
||||
assert page.count("data-composer-action") == 1
|
||||
|
||||
|
||||
def test_the_send_button_carries_both_icons(client: TestClient, db, registered):
|
||||
"""Rendered together and chosen in CSS, so the swap costs no layout and
|
||||
cannot flash an empty button."""
|
||||
_add_a_model(db)
|
||||
page = client.get("/chat").text
|
||||
assert "composer__icon--send" in page
|
||||
assert "composer__icon--stop" in page
|
||||
|
||||
|
||||
def test_the_composer_starts_in_the_send_state(client: TestClient, db, registered):
|
||||
_add_a_model(db)
|
||||
assert 'data-composer-action="send"' in client.get("/chat").text
|
||||
|
||||
|
||||
def _add_a_model(db):
|
||||
from lembas.db.models import Connection, Model
|
||||
|
||||
connection = Connection(name="c", base_url="http://127.0.0.1:1", api_key_encrypted="")
|
||||
db.add(connection)
|
||||
db.commit()
|
||||
db.add(Model(connection_id=connection.id, model_id="m"))
|
||||
db.commit()
|
||||
|
||||
|
||||
# --- The icons themselves ----------------------------------------------------
|
||||
def test_the_generated_icons_are_committed():
|
||||
"""They come from scripts/build_artwork.py and are committed like the SVGs;
|
||||
the running application has no rasteriser."""
|
||||
for name in (
|
||||
"icon-192.png",
|
||||
"icon-512.png",
|
||||
"icon-maskable-512.png",
|
||||
"apple-touch-icon-180.png",
|
||||
):
|
||||
path = Path(STATIC_DIR) / "img" / name
|
||||
assert path.exists(), name
|
||||
assert path.read_bytes()[:8] == b"\x89PNG\r\n\x1a\n", name
|
||||
|
||||
|
||||
def test_the_mic_appears_only_when_dictation_is_configured(
|
||||
client: TestClient, db, registered
|
||||
):
|
||||
_add_a_model(db)
|
||||
assert "data-mic" not in client.get("/chat").text
|
||||
|
||||
settings_store.update(
|
||||
db,
|
||||
{"stt_enabled": True, "stt_base_url": "http://stt"},
|
||||
key=settings_store.AUDIO,
|
||||
)
|
||||
assert "data-mic" in client.get("/chat").text
|
||||
Reference in New Issue
Block a user