gabriel / musehub public
test_musehub_ui_htmx_infra.py python
127 lines 5.2 KB
Raw
sha256:3c58668648c7323bb9f5c6881cfe6a3f14fc93fcb73b537d253732952a5bf8bf chore: bump version to 0.2.0rc12 Sonnet 4.6 patch 8 days ago
1 """Tests for HTMX infrastructure — static assets, base.html wiring, and helpers.
2
3 Verifies that:
4 - htmx.min.js and alpinejs.min.js are present in the static directory.
5 - base.html includes the correct <script> tags and hx-boost attribute.
6 - app.js contains the HTMX after-swap hook.
7 - The is_htmx() / is_htmx_boosted() helpers return the correct values.
8 - The static files are reachable via the /static/ HTTP endpoint.
9 """
10
11 from __future__ import annotations
12
13 from pathlib import Path
14
15 import pytest
16 from httpx import AsyncClient
17
18 STATIC_DIR = Path(__file__).parent.parent / "musehub" / "templates" / "musehub" / "static"
19 BASE_HTML = Path(__file__).parent.parent / "musehub" / "templates" / "musehub" / "base.html"
20 APP_JS = STATIC_DIR / "app.js"
21
22
23 # ── Static asset presence ────────────────────────────────────────────────────
24
25 def test_htmx_min_js_static_file_exists() -> None:
26 """htmx.min.js must be present in the MuseHub static directory."""
27 assert (STATIC_DIR / "htmx.min.js").exists(), "htmx.min.js not found in static dir"
28
29
30 def test_alpinejs_min_js_static_file_exists() -> None:
31 """alpinejs.min.js must be present in the MuseHub static directory."""
32 assert (STATIC_DIR / "alpinejs.min.js").exists(), "alpinejs.min.js not found in static dir"
33
34
35 # ── base.html wiring ─────────────────────────────────────────────────────────
36
37 def test_base_html_includes_htmx_script() -> None:
38 """base.html must reference htmx.min.js so HTMX is available on every page."""
39 content = BASE_HTML.read_text()
40 assert "htmx.min.js" in content, "htmx.min.js script tag missing from base.html"
41
42
43 def test_base_html_includes_alpinejs_script() -> None:
44 """base.html must reference alpinejs.min.js so Alpine.js is available on every page."""
45 content = BASE_HTML.read_text()
46 assert "alpinejs.min.js" in content, "alpinejs.min.js script tag missing from base.html"
47
48
49 def test_base_html_hx_boost_on_container() -> None:
50 """The main container div must carry hx-boost so navigation links get SPA-feel transitions."""
51 content = BASE_HTML.read_text()
52 assert 'hx-boost="true"' in content, 'hx-boost="true" missing from container div in base.html'
53
54
55 def test_base_html_htmx_loading_indicator() -> None:
56 """base.html must include the #htmx-loading progress bar element."""
57 content = BASE_HTML.read_text()
58 assert 'id="htmx-loading"' in content, "#htmx-loading element missing from base.html"
59
60
61 # ── app.js HTMX hooks ────────────────────────────────────────────────────────
62
63
64 def test_musehub_js_has_htmx_after_swap_hook() -> None:
65 """app.js must register an htmx:afterSwap listener to re-run initRepoNav after fragments swap."""
66 content = APP_JS.read_text()
67 assert "htmx:afterSwap" in content, "htmx:afterSwap listener missing from app.js"
68
69
70 # ── is_htmx() / is_htmx_boosted() helpers ───────────────────────────────────
71
72 def test_is_htmx_helper_true() -> None:
73 """is_htmx() must return True when the HX-Request: true header is present."""
74 from unittest.mock import MagicMock
75
76 from musehub.api.routes.musehub.htmx_helpers import is_htmx
77
78 request = MagicMock()
79 request.headers = {"HX-Request": "true"}
80 assert is_htmx(request) is True
81
82
83 def test_is_htmx_helper_false() -> None:
84 """is_htmx() must return False when the HX-Request header is absent."""
85 from unittest.mock import MagicMock
86
87 from musehub.api.routes.musehub.htmx_helpers import is_htmx
88
89 request = MagicMock()
90 request.headers = {}
91 assert is_htmx(request) is False
92
93
94 def test_is_htmx_boosted_helper_true() -> None:
95 """is_htmx_boosted() must return True when HX-Boosted: true is present."""
96 from unittest.mock import MagicMock
97
98 from musehub.api.routes.musehub.htmx_helpers import is_htmx_boosted
99
100 request = MagicMock()
101 request.headers = {"HX-Boosted": "true"}
102 assert is_htmx_boosted(request) is True
103
104
105 def test_is_htmx_boosted_helper_false() -> None:
106 """is_htmx_boosted() must return False when the HX-Boosted header is absent."""
107 from unittest.mock import MagicMock
108
109 from musehub.api.routes.musehub.htmx_helpers import is_htmx_boosted
110
111 request = MagicMock()
112 request.headers = {}
113 assert is_htmx_boosted(request) is False
114
115
116 # ── HTTP endpoint reachability ───────────────────────────────────────────────
117
118 async def test_htmx_min_js_served_over_http(client: AsyncClient) -> None:
119 """GET /static/htmx.min.js must return 200."""
120 resp = await client.get("/static/htmx.min.js")
121 assert resp.status_code == 200, f"Expected 200, got {resp.status_code}"
122
123
124 async def test_alpinejs_min_js_served_over_http(client: AsyncClient) -> None:
125 """GET /static/alpinejs.min.js must return 200."""
126 resp = await client.get("/static/alpinejs.min.js")
127 assert resp.status_code == 200, f"Expected 200, got {resp.status_code}"
File History 1 commit
sha256:3c58668648c7323bb9f5c6881cfe6a3f14fc93fcb73b537d253732952a5bf8bf chore: bump version to 0.2.0rc12 Sonnet 4.6 patch 8 days ago