Root cause (owner repro, verified in a real browser 2026-09-06): the five navbar views (Chat, RAG, Sources, Tuning, History) were separate HTML documents, so a navbar click was a REAL cross-document navigation — the chat page unloaded, the in-flight SSE fetch was aborted, and the phase-48 teardown (app/api/chat.py `finally`, "chat: turn cancelled") stopped the model. Observed: send question -> click RAG mid-stream -> click Chat -> the answer never finished: no `query_log` row, and on return a dangling question with no brain record (the pre-token pagehide partial persist skips because `acc` is empty). Phase-48 LOCKED-DECISION REFINEMENT (owner-confirmed 2026-09-06, flagged per AGENTS.md rule 3, not silently deviated): "real navigation cancels the fetch" now means LEAVING THE APP — tab close, external/other-document navigation, the Stop button. In-app navbar switches are client-side view switches and no longer cancel. Fix — Option A (SPA shell), chosen over B (Service Worker owns the stream) and C (server-side turn registry + resume): - frontend/index.html is the shell: ONE `<main id="main">` holds the five `<section class="view">` blocks; hidden views carry BOTH `hidden` and `inert` (WCAG — no focus/keyboard traversal). The shared header, the single `doc-modal-*` skeleton, and the `#app-version` footer each exist exactly once; the per-view copies from the four folded pages are dropped. - New frontend/assets/router.js (vanilla module — no framework, no bundler, No-CDN rule intact): lazy-imports a view module on FIRST show only (mount-once, hide-forever — the chat view's in-flight SSE reader persists across switches; that persistence IS the fix); intercepts same-shell navbar links with preventDefault + history.pushState (never a document load); handles popstate; single writer of `.nav-link` active state (is-active + aria-current), document.title, and the per-view meta description (values carried over from the old pages' heads, brand-resolved at write time). - Each folded page's JS becomes `export async function mount(root)` — root-scoped queries; `initSharedHeader()` dropped (the header boots once in the shell via the chat module; the admin flag comes from the same cached `fetchIsAdmin()` promise — zero extra requests). - app/main.py: a small list-driven route factory serves the shell for /tuning.html, /sources.html, /git-sources.html, /history.html — registered AFTER the API routers and BEFORE the static catch-all (routes-first). The phase-33 caching middleware applies no-cache + `?v=` rewriting unchanged; app/core/caching.py needed NO change (the view paths did not change — pinned by the integration tests). - The four old view .html files are DELETED (one source of truth); deep links to the old URLs keep working (the router picks the view from the pathname); `/?chat=<id>` is unaffected; the Containerfile bundles router.js (inlining the lazy view modules) and drops the folded page files. - app/schemas.py: HistoryTurn.text cap 4000 -> 32000 — the shell keeps long saved answers in the chat, and the old cap (stricter than the 24_000-char total history budget) 422-rejected any second turn in such a chat (found by the phase-42 E2E suite on the shell). Boundaries: login.html, shared.html, doc-edit.html, document.html REMAIN separate documents (flow pages, not navbar tabs); a mid-stream navigation to doc-edit/document.html still cancels per phase 48 (follow-up candidate, out of scope). The SSE API is unchanged. Real departures still cancel the turn — phase 48 intact (pinned by tests/e2e/test_stop_generation.py, unchanged, and by the new suite's real-departure control). Tests: - Phase-20 suite REWRITTEN to the new semantics (tests/e2e/test_sources_midstream_bug.py): a navbar switch no longer cancels — the stream survives the switch and the FULL answer settles; the pagehide partial persist REMAINS for real departures (the partial's exact shape — first streamed chunk prefix, no done metadata — is still pinned there). - NEW story suite tests/e2e/test_nav_switch_keeps_stream.py (mock LLM): the owner repro (send -> RAG mid-stream -> Chat: window sentinel survives = same document, FULL answer, exactly one brain turn in bor.chat.v1, exactly one settled query_log row, auto-saved row matches) + the same mid-stream switch against the other three views + the real-departure-still-cancels control + the no-switch baseline. - tests/unit/test_frontend_router.py: source-level pins of the router invariants (click interceptor targets ONLY same-shell view paths, pushState-only switches, mount-once guard, hidden+inert pair, single-writer active state/title); shell-route integration tests (each folded path serves the shell with no-cache + `?v=` body; a non-view path still 404s); the file-reading unit pins re-pointed at the shell (the four view files are gone — the shell is the source of truth). Verification (this commit): full suite green — 1565 unit+integration tests, app/ coverage 99% (>90% floor); ruff + pyright clean; the phase's E2E suites green in isolation (house protocol, AGENTS.md rule 9). Owner repro verified in a real browser against the real LLM (dev server :8010, headful Chromium): "tell me about everquest" -> RAG mid-stream -> Chat — the answer completed with one brain bubble and no error banner, `query_log` gained exactly one settled row (deflected=True: the dev KB holds no EverQuest docs — the settle, not the topic, is the proof), zero "chat: turn cancelled" lines for that turn; the control (real navigation to /shared.html mid-stream) still cancelled (no settled row, the cancel line logged, the partial persisted on return). Screenshots: .agents/screenshots/76_manual_*. Phase 76 (76_spa_nav_shell) complete — moved to .agents/phases/complete/.
379 lines
15 KiB
Python
379 lines
15 KiB
Python
"""Integration tests: HTTP API surface (no database required)."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from app.config import get_settings
|
|
|
|
|
|
def test_health_reports_ok(client) -> None:
|
|
r = client.get("/api/health")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["status"] == "ok"
|
|
assert body["db"] in {"up", "down"}
|
|
assert body["version"]
|
|
|
|
|
|
def test_config_returns_default_app_metadata(client) -> None:
|
|
"""GET /api/config is public (anonymous) and returns exactly six
|
|
keys — the phase-39 app metadata, the phase-59 docs flag (inert
|
|
false while BOR_DOCS_REPO is empty — the "Save as doc" gating),
|
|
and the phase-62 UI customization strings (composer placeholder,
|
|
footer line, theme file name)."""
|
|
r = client.get("/api/config")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert set(body) == {
|
|
"app_name", "version", "docs_repo_configured",
|
|
"input_placeholder", "footer_text", "theme",
|
|
}
|
|
assert body["app_name"] == "Brain of Reese"
|
|
assert body["version"] == get_settings().app_version
|
|
assert body["docs_repo_configured"] is False
|
|
# Phase 62: UNSET => the phase-61 neutral copy stands (the
|
|
# byte-identical contract); an empty theme = the built-in palette.
|
|
assert body["input_placeholder"] == "Ask me anything…"
|
|
assert body["footer_text"] == "Powered by self-hosted models"
|
|
assert body["theme"] == ""
|
|
|
|
|
|
def test_config_follows_overridden_app_name(client) -> None:
|
|
"""GET /api/config reflects a Settings override (e.g. BOR_APP_NAME)."""
|
|
from app.config import Settings
|
|
from app.main import app as fastapi_app
|
|
|
|
fastapi_app.dependency_overrides[get_settings] = lambda: Settings(
|
|
app_name="Brain of Testy"
|
|
)
|
|
try:
|
|
r = client.get("/api/config")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert set(body) == {
|
|
"app_name", "version", "docs_repo_configured",
|
|
"input_placeholder", "footer_text", "theme",
|
|
}
|
|
assert body["app_name"] == "Brain of Testy"
|
|
assert body["version"] == "0.1.0"
|
|
assert body["docs_repo_configured"] is False
|
|
finally:
|
|
fastapi_app.dependency_overrides.clear()
|
|
|
|
|
|
def test_config_serves_ui_customization_overrides(client) -> None:
|
|
"""Phase 62: the three UI customization keys mirror Settings
|
|
overrides (``BOR_INPUT_PLACEHOLDER`` / ``BOR_FOOTER_TEXT`` /
|
|
``BOR_THEME``) verbatim — the values the frontend brand layer
|
|
applies at boot, so this dict is the whole contract."""
|
|
from app.config import Settings
|
|
from app.main import app as fastapi_app
|
|
|
|
fastapi_app.dependency_overrides[get_settings] = lambda: Settings(
|
|
input_placeholder="Ask the vault…",
|
|
footer_text="Powered by my own models",
|
|
theme="indigo.css",
|
|
)
|
|
try:
|
|
r = client.get("/api/config")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert set(body) == {
|
|
"app_name", "version", "docs_repo_configured",
|
|
"input_placeholder", "footer_text", "theme",
|
|
}
|
|
assert body["input_placeholder"] == "Ask the vault…"
|
|
assert body["footer_text"] == "Powered by my own models"
|
|
assert body["theme"] == "indigo.css"
|
|
finally:
|
|
fastapi_app.dependency_overrides.clear()
|
|
|
|
|
|
def test_config_docs_flag_tracks_settings(client) -> None:
|
|
"""Phase 59 (task 05): ``docs_repo_configured`` mirrors
|
|
``settings.docs_configured`` — a real bool (never a truthy string)
|
|
that flips true the moment BOR_DOCS_REPO is non-empty: that flag is
|
|
the entire frontend gating of the "Save as doc" button."""
|
|
from app.config import Settings
|
|
from app.main import app as fastapi_app
|
|
|
|
fastapi_app.dependency_overrides[get_settings] = lambda: Settings(
|
|
app_name="Brain of Testy",
|
|
docs_repo="/srv/docs-repo",
|
|
)
|
|
try:
|
|
r = client.get("/api/config")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert isinstance(body["docs_repo_configured"], bool)
|
|
assert body["docs_repo_configured"] is True
|
|
finally:
|
|
fastapi_app.dependency_overrides.clear()
|
|
|
|
|
|
def test_suggestions_returns_list(client) -> None:
|
|
r = client.get("/api/suggestions")
|
|
assert r.status_code == 200
|
|
suggestions = r.json()["suggestions"]
|
|
assert isinstance(suggestions, list)
|
|
assert len(suggestions) >= 3
|
|
assert all(isinstance(s, str) and s.strip() for s in suggestions)
|
|
|
|
|
|
def test_suggestions_honors_bor_suggestions_env_override(monkeypatch) -> None:
|
|
"""GET /api/suggestions reflects the BOR_SUGGESTIONS JSON env override."""
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import create_app
|
|
|
|
override = [
|
|
"How do I back up with Borg?",
|
|
"How is my K3S cluster set up?",
|
|
"How do I deploy a service?",
|
|
"What proxy fronts reeseapps.com?",
|
|
]
|
|
get_settings.cache_clear()
|
|
try:
|
|
monkeypatch.setenv("BOR_SUGGESTIONS", json.dumps(override))
|
|
fresh_client = TestClient(create_app())
|
|
finally:
|
|
get_settings.cache_clear()
|
|
|
|
r = fresh_client.get("/api/suggestions")
|
|
assert r.status_code == 200
|
|
assert r.json() == {"suggestions": override}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("path", "marker"),
|
|
[
|
|
("/", "Brain of Reese"),
|
|
# Phase 76 (task 02): /sources.html + /git-sources.html are SHELL
|
|
# routes — the body is the shell (index.html) whose static
|
|
# <title> is "Brain of Reese" (the per-view title is set
|
|
# CLIENT-side by the router, invisible to httpx). The marker
|
|
# asserts the shell body (the view section is inside it) instead
|
|
# of the old page's title.
|
|
("/sources.html", 'id="view-rag"'), # phase 76: shell route (was "Knowledge base")
|
|
("/document.html", "Brain of Reese"), # phase 10: viewer page
|
|
("/login.html", "Sign in"), # phase 16: admin sign-in page
|
|
# Phase 76 (task 01): /tuning.html is a SHELL route — same
|
|
# shell-body marker pattern as the two task-02 paths above.
|
|
("/tuning.html", 'id="view-tuning"'), # phase 76: shell route (was "Global Tuning")
|
|
("/git-sources.html", 'id="view-git-sources"'), # phase 76: shell route (was "Git sources")
|
|
# Phase 76 (task 03): /history.html is a SHELL route too — the
|
|
# shell-body marker (the History view section is inside the
|
|
# shell; the old standalone page's title is client-side now).
|
|
("/history.html", 'id="view-history"'), # phase 76: shell route (was "Saved chats")
|
|
("/shared.html", "Shared conversation"), # phase 51: anonymous shared page
|
|
],
|
|
)
|
|
def test_html_pages_served_locally_no_cdn(client, path: str, marker: str) -> None:
|
|
"""No-CDN check (PLAN §7.3, re-verified on BOTH pages in phase 07 and
|
|
on the viewer page in phase 10): each page is served by FastAPI and
|
|
references only same-origin assets (no https:// script/link tags)."""
|
|
r = client.get(path)
|
|
assert r.status_code == 200
|
|
assert marker in r.text
|
|
assert 'src="https://' not in r.text
|
|
assert 'href="https://' not in r.text
|
|
|
|
|
|
# Phase 33 (cache busting): the five HTML pages revalidate (no-cache) with
|
|
# ?v=<token> asset refs; /assets/* is immutable for a year; /api/* is
|
|
# untouched. The token itself is unit-tested in tests/unit/test_caching.py.
|
|
|
|
|
|
def test_index_page_no_cache_with_versioned_asset_refs(client) -> None:
|
|
"""GET / — always revalidated, and the stylesheet reference carries
|
|
the process version token (non-empty, matching asset_version())."""
|
|
from app.core.caching import asset_version
|
|
|
|
token = asset_version()
|
|
assert token # non-empty in every supported environment
|
|
|
|
r = client.get("/")
|
|
assert r.status_code == 200
|
|
assert r.headers["cache-control"] == "no-cache"
|
|
assert f'href="/assets/styles.css?v={token}"' in r.text
|
|
# The unversioned reference is gone from the served body.
|
|
assert 'href="/assets/styles.css">' not in r.text
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path",
|
|
["/sources.html", "/document.html", "/login.html", "/tuning.html",
|
|
"/git-sources.html", "/history.html", # phase 50: + History (shell route, task 03)
|
|
"/shared.html"], # phase 51: + the anonymous shared page
|
|
)
|
|
def test_html_pages_no_cache_with_versioned_refs(client, path: str) -> None:
|
|
"""Each of the other four pages revalidates and carries at least one
|
|
versioned asset reference."""
|
|
from app.core.caching import asset_version
|
|
|
|
r = client.get(path)
|
|
assert r.status_code == 200
|
|
assert r.headers["cache-control"] == "no-cache"
|
|
assert f"?v={asset_version()}" in r.text
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("path", "view_id", "old_title"),
|
|
[
|
|
("/tuning.html", 'id="view-tuning"', "Global Tuning · Brain of Reese"),
|
|
("/sources.html", 'id="view-rag"', "Sources · Brain of Reese"),
|
|
("/git-sources.html", 'id="view-git-sources"', "Git sources · Brain of Reese"),
|
|
("/history.html", 'id="view-history"', "Saved chats · Brain of Reese"), # phase 76 task 03
|
|
],
|
|
)
|
|
def test_shell_routes_serve_the_shell_no_cache_versioned(
|
|
client, path: str, view_id: str, old_title: str
|
|
) -> None:
|
|
"""Phase 76 (task 01: /tuning.html; task 02: /sources.html +
|
|
/git-sources.html; task 03: /history.html — all four non-chat
|
|
navbar views): every shell route serves the shell
|
|
(``frontend/index.html``), so the phase-33 page contract applies to
|
|
it exactly as to a static page: 200, text/html, ``Cache-Control:
|
|
no-cache``, ``?v=<token>`` asset refs, no validators (the
|
|
middleware wraps the whole app and lists the path in ``HTML_PAGES``
|
|
— unchanged). The body IS the shell: the view section is inside it,
|
|
the old standalone page's title is gone. The static catch-all stays
|
|
intact: an unknown path still 404s. (The conditional-GET
|
|
revalidation contract for these paths is pinned by
|
|
``tests/integration/test_caching_revalidation.py``.)"""
|
|
from app.core.caching import asset_version
|
|
|
|
r = client.get(path)
|
|
assert r.status_code == 200
|
|
assert r.headers["content-type"].split(";", 1)[0] == "text/html"
|
|
assert r.headers["cache-control"] == "no-cache"
|
|
assert "etag" not in r.headers
|
|
assert "last-modified" not in r.headers
|
|
assert f"?v={asset_version()}" in r.text
|
|
# The body is the SHELL: the chat view AND the route's view section
|
|
# are inside it…
|
|
assert 'id="view-chat"' in r.text
|
|
assert view_id in r.text
|
|
# …with the shell's static title (the per-view title is the
|
|
# router's client-side job), and the old page's own <title> is gone.
|
|
assert "<title>Brain of Reese</title>" in r.text
|
|
assert f"{old_title}</title>" not in r.text
|
|
# The catch-all is intact: an unknown path still 404s.
|
|
assert client.get("/nonexistent.html").status_code == 404
|
|
|
|
|
|
def test_index_html_variant_no_cache_versioned(client) -> None:
|
|
"""/index.html is the same page as / — same caching treatment."""
|
|
from app.core.caching import asset_version
|
|
|
|
r = client.get("/index.html")
|
|
assert r.status_code == 200
|
|
assert r.headers["cache-control"] == "no-cache"
|
|
assert f"?v={asset_version()}" in r.text
|
|
|
|
|
|
def test_assets_served_immutable_for_a_year(client) -> None:
|
|
r = client.get("/assets/styles.css")
|
|
assert r.status_code == 200
|
|
cc = r.headers["cache-control"]
|
|
assert "public" in cc
|
|
assert "max-age=31536000" in cc
|
|
assert "immutable" in cc
|
|
# The asset body is untouched (header-only middleware).
|
|
assert client.get("/assets/app.js?v=whichever").status_code == 200
|
|
|
|
|
|
def test_api_health_gets_no_cache_control_injected(client) -> None:
|
|
"""Baseline (pre-middleware) behavior for /api/*: FastAPI's JSON
|
|
responses ship no Cache-Control header — the middleware must not
|
|
inject one."""
|
|
r = client.get("/api/health")
|
|
assert r.status_code == 200
|
|
assert "cache-control" not in r.headers
|
|
|
|
|
|
def test_styles_and_js_served(client) -> None:
|
|
assert client.get("/assets/styles.css").status_code == 200
|
|
assert client.get("/assets/app.js").status_code == 200
|
|
assert client.get("/assets/sources.js").status_code == 200
|
|
assert client.get("/assets/markdown.js").status_code == 200 # phase 10: shared renderer
|
|
assert client.get("/assets/document.js").status_code == 200 # phase 10: viewer page
|
|
assert client.get("/assets/login.js").status_code == 200 # phase 16: login page
|
|
assert client.get("/assets/document-modal.js").status_code == 200 # phase 26: modal module
|
|
assert client.get("/assets/tuning.js").status_code == 200 # phase 27: tuning page
|
|
assert client.get("/assets/git-sources.js").status_code == 200 # phase 35: git sources page
|
|
assert client.get("/assets/shared.js").status_code == 200 # phase 51: shared page module
|
|
|
|
|
|
# Emoji code points banned from UI chrome (phase 08): the pictograph
|
|
# blocks, VS-16/ZWJ, plus the exact glyphs the old light theme used
|
|
# (🧠 🧑 👋 📂 ⚠).
|
|
_EMOJI_GLYPHS = "\U0001F9E0\U0001F9D1\U0001F44B\U0001F4C2\u26A0"
|
|
|
|
|
|
def _find_emoji(text: str) -> list[str]:
|
|
hits: list[str] = []
|
|
for ch in text:
|
|
cp = ord(ch)
|
|
if (
|
|
0x1F300 <= cp <= 0x1FAFF
|
|
or 0x2600 <= cp <= 0x27BF
|
|
or 0x2B00 <= cp <= 0x2BFF
|
|
or cp in (0xFE0F, 0x200D)
|
|
or ch in _EMOJI_GLYPHS
|
|
):
|
|
hits.append(ch)
|
|
return hits
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path",
|
|
[
|
|
"/",
|
|
"/sources.html",
|
|
"/document.html",
|
|
"/login.html", # phase 16
|
|
"/tuning.html", # phase 27
|
|
"/git-sources.html", # phase 35
|
|
"/assets/app.js",
|
|
"/assets/sources.js",
|
|
"/assets/markdown.js",
|
|
"/assets/document.js",
|
|
"/assets/login.js", # phase 16
|
|
"/assets/document-modal.js", # phase 26: the document modal module
|
|
"/assets/git-sources.js", # phase 35: the git sources page module
|
|
"/shared.html", # phase 51: the anonymous shared page
|
|
"/assets/shared.js", # phase 51: the shared page module
|
|
"/assets/styles.css",
|
|
],
|
|
)
|
|
def test_ui_chrome_has_no_emoji(client, path: str) -> None:
|
|
"""Permanent regression guard (phase 08): the UI chrome — all pages,
|
|
the JS that renders it, and the stylesheet — is emoji-free.
|
|
|
|
Phase 37 revision (owner permission 2026-08-26, PLAN §4): the agent's
|
|
``.tool-call`` line carries the CONTENT marks — 🔎 (list) and 📄
|
|
(read) — the only emoji in the whole frontend, and only as the exact
|
|
tool-line template strings in app.js. Phase 68 revision: the search
|
|
tool line (the ``grep`` tool, phase 70) adds the third template
|
|
literal ("🔎 Searching for "). Phase 70 revision: the scoped ``ls``
|
|
tool line adds the fourth ("🔎 Listing documents in "). The guard strips
|
|
precisely those four literals; any other emoji, or those marks
|
|
anywhere else, still fails."""
|
|
r = client.get(path)
|
|
assert r.status_code == 200
|
|
text = r.text
|
|
if path in ("/assets/app.js", "/assets/shared.js"):
|
|
text = text.replace('"🔎 Listing documents"', "")
|
|
text = text.replace('"📄 Reading "', "")
|
|
text = text.replace('"🔎 Searching for "', "")
|
|
text = text.replace('"🔎 Listing documents in "', "")
|
|
assert _find_emoji(text) == [], f"emoji found in {path}: {_find_emoji(text)!r}"
|
|
|
|
|
|
def test_chat_requires_message(client) -> None:
|
|
r = client.post("/api/chat", json={"message": ""})
|
|
assert r.status_code == 422
|