thermograph/frontend/tests/unit/test_pages.py

69 lines
2.9 KiB
Python
Raw Normal View History

"""SPA-shell pages (/calendar, /day, /score, /compare, /legend, /alerts) and
static-asset serving -- moved here from backend/tests/web/test_api.py
(repo-split Stage 7a: frontend now owns these, not backend). These routes are static
shells/assets that don't call the backend, so the hermetic `client` fixture serves them."""
B = "/thermograph" # matches THERMOGRAPH_BASE set in tests/conftest.py
def test_calendar_serves_with_origin_filled_in(client):
r = client.get(f"{B}/calendar")
assert r.status_code == 200
assert "text/html" in r.headers["content-type"]
assert "__ORIGIN__" not in r.text
assert client.head(f"{B}/calendar").status_code == 200
def test_other_spa_shells_serve(client):
for path in ("/day", "/score", "/compare", "/legend", "/alerts"):
r = client.get(f"{B}{path}")
assert r.status_code == 200, path
assert "__ORIGIN__" not in r.text, path
def test_static_asset_serves(client):
r = client.get(f"{B}/app.js")
assert r.status_code == 200
assert "javascript" in r.headers["content-type"]
def test_old_static_index_is_gone(client):
"""index.html must not linger behind the static mount as indexable
duplicate content now that / is server-rendered (moved here from
backend/tests/web/test_homepage.py, repo-split Stage 7a -- frontend's own
StaticFiles mount is what actually answers this now)."""
assert client.get(f"{B}/index.html").status_code == 404
# --- product-event instrumentation flag ---------------------------------------
# track.js sends nothing unless it finds data-tg-events on <html>. The whole
# rollout story rests on that stamp being absent by default, so assert both
# halves: off unless THERMOGRAPH_EVENTS is set, and applied to BOTH HTML paths
# (the Jinja-rendered content pages and the static SPA shells).
def test_event_flag_is_off_by_default_on_every_html_path(client):
for path in ("/", "/calendar", "/day", "/score", "/compare", "/alerts"):
r = client.get(f"{B}{path}")
assert r.status_code == 200, path
assert "data-tg-events" not in r.text, path
def test_event_flag_stamps_both_html_paths_when_enabled(monkeypatch):
"""Rebuilt in-process rather than reusing `client`: the flag is read at
import and the SPA shells memoize their prepped template."""
import importlib
import content as content_mod
from fastapi.testclient import TestClient
monkeypatch.setenv("THERMOGRAPH_EVENTS", "1")
content_mod = importlib.reload(content_mod)
assert content_mod.EVENTS_ENABLED
import app as app_mod
app_mod = importlib.reload(app_mod)
flagged = TestClient(app_mod.app)
for path in ("/calendar", "/compare"):
assert 'data-tg-events="1"' in flagged.get(f"{B}{path}").text, path
# Leave the modules as the rest of the suite expects to find them.
monkeypatch.delenv("THERMOGRAPH_EVENTS")
importlib.reload(content_mod)
importlib.reload(app_mod)