Files
meshcore-hub/tests/test_web/test_caching.py
Louis King a5fabf7d46 chore(web): remove lit-html fallback & legacy code — Phase 4
The React frontend is complete (Phases 1-3); the lit-html fallback is dead
(its vendor globals were removed in Phase 3). Delete it and the scaffolding:

- Delete the entire src/meshcore_hub/web/static/js/spa/ lit-html tree,
  LitBridge.tsx, and legacy.d.ts.
- Remove the @legacy alias from vite.config.ts and tsconfig.json.
- Remove lit-html and qrcodejs from package.json (both unused now).
- Remove the lit-html fallback {% else %} branch from spa.html — the Vite
  build is now required to serve the UI (no fallback bundle).

Tests (fallback no longer exists):
- test_home/advertisements/nodes/messages.py: assert the React mount point
  (id="app") instead of a bundled-or-fallback script tag.
- test_caching.py: JS-cache tests are header-only (static JS is bundled into
  dist/, absent in test env; the middleware sets headers on 404 too); the
  dist-bundle HTML test drops its fallback branch.

Docs:
- AGENTS.md: new Frontend (React) section (host-run npm/vite/tsc toolchain,
  react-chartjs-2/react-leaflet/react-qr-code, CSS load order); clarified the
  compose-stack rule to exempt frontend tooling.
- REACT_MIGRATION.md: Phase 4 complete, final file structure, decisions.

Verified: tsc --noEmit clean, npm run build, full pytest (1463 passed,
22 skipped), pre-commit (passed).
2026-07-21 19:13:01 +01:00

238 lines
9.8 KiB
Python

"""Tests for HTTP caching middleware and version parameters."""
from bs4 import BeautifulSoup
from meshcore_hub import __version__
class TestCacheControlHeaders:
"""Test Cache-Control headers are correctly set for different resource types."""
def test_static_css_with_version(self, client):
"""Static CSS with version parameter should have long-term cache."""
response = client.get(f"/static/css/app.css?v={__version__}")
assert response.status_code == 200
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "public, max-age=31536000, immutable"
)
def test_static_js_with_version(self, client):
"""Static JS with version parameter should have long-term cache.
Only the header is asserted (not status): JS source is bundled into
static/dist/ and absent from host checkouts, and the middleware sets
headers on 404 responses too.
"""
response = client.get(f"/static/js/app.js?v={__version__}")
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "public, max-age=31536000, immutable"
)
def test_static_module_with_version(self, client):
"""Bundled ES modules in static/dist/ use content-hashed immutable cache.
Only the header is asserted (not status): static/dist/ is a build
artifact absent from host checkouts, and the middleware sets headers on
404 responses too.
"""
response = client.get("/static/dist/assets/app.js")
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "public, max-age=31536000, immutable"
)
def test_static_vendor_font(self, client):
"""Vendored fonts should have long-term immutable cache.
Only the header is asserted (not status): static/vendor/ is a build
artifact absent from host checkouts, and the middleware sets headers
on 404 responses too.
"""
response = client.get(
"/static/vendor/fonts/ibm-plex-sans-latin-wght-normal.woff2"
)
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "public, max-age=31536000, immutable"
)
def test_static_css_without_version(self, client):
"""Static CSS without version should have short fallback cache."""
response = client.get("/static/css/app.css")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"
def test_static_js_without_version(self, client):
"""Static JS without version should have short fallback cache.
Only the header is asserted (not status): see test_static_js_with_version.
"""
response = client.get("/static/js/app.js")
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"
def test_spa_shell_html(self, client):
"""SPA shell HTML should not be cached."""
response = client.get("/")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "no-cache, public"
def test_spa_route_html(self, client):
"""Client-side route should not be cached."""
response = client.get("/dashboard")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "no-cache, public"
def test_map_data_endpoint(self, client, mock_http_client):
"""Map data endpoint should have short cache (5 minutes)."""
# Mock the API response for map data
mock_http_client.set_response(
"GET",
"/api/v1/nodes/map",
200,
{"nodes": []},
)
response = client.get("/map/data")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=300"
def test_health_endpoint(self, client):
"""Health endpoint should never be cached."""
response = client.get("/health")
assert response.status_code == 200
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "no-cache, no-store, must-revalidate"
)
def test_healthz_endpoint(self, client):
"""Healthz endpoint should never be cached."""
response = client.get("/healthz")
assert response.status_code == 200
assert "cache-control" in response.headers
assert (
response.headers["cache-control"] == "no-cache, no-store, must-revalidate"
)
def test_robots_txt(self, client):
"""Robots.txt should have moderate cache (1 hour)."""
response = client.get("/robots.txt")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"
def test_sitemap_xml(self, client):
"""Sitemap.xml should have moderate cache (1 hour)."""
response = client.get("/sitemap.xml")
assert response.status_code == 200
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"
def test_api_proxy_no_cache_header_added(self, client, mock_http_client):
"""API proxy should not add cache headers (lets backend control caching)."""
# The mock client doesn't add cache-control headers by default
# Middleware should not add any either for /api/* paths
response = client.get("/api/v1/nodes")
assert response.status_code == 200
# Cache-control should either not be present, or be from the backend
# Since our mock doesn't add it, middleware shouldn't add it either
# (In production, backend would set its own cache-control)
class TestVersionParameterInHTML:
"""Test that version parameters are correctly added to static file references."""
def test_css_link_has_version(self, client):
"""CSS link should include version parameter."""
response = client.get("/")
assert response.status_code == 200
soup = BeautifulSoup(response.text, "html.parser")
css_link = soup.find(
"link", {"href": lambda x: x and "/static/css/app.css" in x}
)
assert css_link is not None
assert f"?v={__version__}" in css_link["href"]
def test_app_js_has_version(self, client):
"""SPA bundle script should be served content-hashed from static/dist/.
The bundle only exists after a frontend build; without one there is no
script tag, so the dist/ origin is only asserted when present.
"""
response = client.get("/")
assert response.status_code == 200
soup = BeautifulSoup(response.text, "html.parser")
bundled_script = soup.find(
"script",
{"src": lambda x: x and "/static/dist/" in x and x.endswith(".js")},
)
if bundled_script:
assert "/static/dist/" in bundled_script["src"]
def test_cdn_resources_unchanged(self, client):
"""CDN resources should not have version parameters."""
response = client.get("/")
assert response.status_code == 200
soup = BeautifulSoup(response.text, "html.parser")
# Check external CDN resources don't have our version param
cdn_scripts = soup.find_all("script", {"src": lambda x: x and "cdn" in x})
for script in cdn_scripts:
assert f"?v={__version__}" not in script["src"]
cdn_links = soup.find_all("link", {"href": lambda x: x and "cdn" in x})
for link in cdn_links:
assert f"?v={__version__}" not in link["href"]
class TestMediaFileCaching:
"""Test caching behavior for custom media files."""
def test_media_file_with_version(self, client, tmp_path):
"""Media files with version parameter should have long-term cache."""
# Note: This test assumes media files are served via StaticFiles
# In practice, you may need to create a test media file
response = client.get(f"/media/test.png?v={__version__}")
# May be 404 if no test media exists, but header should still be set
if response.status_code == 200:
assert "cache-control" in response.headers
assert (
response.headers["cache-control"]
== "public, max-age=31536000, immutable"
)
def test_media_file_without_version(self, client):
"""Media files without version should have short cache."""
response = client.get("/media/test.png")
# May be 404 if no test media exists, but header should still be set
if response.status_code == 200:
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"
class TestCustomPageCaching:
"""Test caching behavior for custom markdown pages."""
def test_custom_page_cache(self, client):
"""Custom pages should have moderate cache (1 hour)."""
# Custom pages are served by the web app (not API proxy)
# They use the PageLoader which reads from CONTENT_HOME
# For this test, we'll check that a 404 still gets cache headers
# (In a real deployment with content files, this would return 200)
response = client.get("/spa/pages/test")
# May be 404 if no test page exists, but cache header should still be set
assert "cache-control" in response.headers
assert response.headers["cache-control"] == "public, max-age=3600"