test_musehub_ui_sessions_ssr.py
python
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2
chore: bump version to 0.2.0rc15 for musehub#113 fix release
Sonnet 4.6
patch
15 days ago
| 1 | """SSR tests for the MuseHub sessions list and session detail pages (issue #573). |
| 2 | |
| 3 | Verifies that both ``GET /{owner}/{repo_slug}/sessions`` and |
| 4 | ``GET /{owner}/{repo_slug}/sessions/{session_id}`` render session |
| 5 | data server-side rather than relying on client-side JavaScript fetches. |
| 6 | |
| 7 | Tests: |
| 8 | - test_sessions_list_renders_session_name_server_side |
| 9 | — Seed a session, GET page, assert session_id present in HTML without JS |
| 10 | - test_sessions_list_active_badge_present |
| 11 | — Active session → badge with "live" in HTML |
| 12 | - test_sessions_list_htmx_fragment_path |
| 13 | — GET with HX-Request: true → fragment only (no <html>) |
| 14 | - test_sessions_list_empty_state_when_no_sessions |
| 15 | — No sessions → empty state rendered server-side |
| 16 | - test_session_detail_renders_session_id |
| 17 | — GET detail page, assert session metadata in HTML |
| 18 | - test_session_detail_renders_participants |
| 19 | — Seed participant, assert user_id in HTML |
| 20 | - test_session_detail_unknown_id_404 |
| 21 | — Non-existent session_id → 404 |
| 22 | """ |
| 23 | from __future__ import annotations |
| 24 | |
| 25 | from musehub.db.musehub_identity_models import MusehubIdentity |
| 26 | |
| 27 | import secrets |
| 28 | from datetime import datetime, timezone |
| 29 | |
| 30 | import pytest |
| 31 | from httpx import AsyncClient |
| 32 | from sqlalchemy.ext.asyncio import AsyncSession |
| 33 | |
| 34 | from musehub.core.genesis import compute_identity_id, compute_repo_id, compute_session_id |
| 35 | from musehub.db.musehub_repo_models import MusehubRepo, MusehubSession |
| 36 | from musehub.types.json_types import StrDict |
| 37 | |
| 38 | # Owner must match the auth_headers identity ("testuser") — the sessions page is |
| 39 | # owner-only (claims.handle == owner). _USER_ID is already testuser's id, so this |
| 40 | # aligns the owner string + owner_user_id + the authenticated caller. |
| 41 | _OWNER = "testuser" |
| 42 | _SLUG = "symphony-no-9" |
| 43 | _USER_ID = compute_identity_id(b"testuser") |
| 44 | |
| 45 | |
| 46 | # --------------------------------------------------------------------------- |
| 47 | # Seed helpers |
| 48 | # --------------------------------------------------------------------------- |
| 49 | |
| 50 | |
| 51 | async def _make_repo(db: AsyncSession) -> str: |
| 52 | """Seed a repo and return its repo_id string.""" |
| 53 | created_at = datetime.now(tz=timezone.utc) |
| 54 | repo = MusehubRepo( |
| 55 | repo_id=compute_repo_id(_USER_ID, _SLUG, "code", created_at.isoformat()), |
| 56 | name=_SLUG, |
| 57 | owner=_OWNER, |
| 58 | slug=_SLUG, |
| 59 | visibility="public", |
| 60 | owner_user_id=_USER_ID, |
| 61 | created_at=created_at, |
| 62 | updated_at=created_at, |
| 63 | ) |
| 64 | db.add(repo) |
| 65 | await db.commit() |
| 66 | await db.refresh(repo) |
| 67 | return str(repo.repo_id) |
| 68 | |
| 69 | |
| 70 | async def _make_session( |
| 71 | db: AsyncSession, |
| 72 | repo_id: str, |
| 73 | *, |
| 74 | is_active: bool = False, |
| 75 | participants: list[str] | None = None, |
| 76 | intent: str = "Record the final movement", |
| 77 | location: str = "Studio A", |
| 78 | notes: str = "", |
| 79 | commits: list[str] | None = None, |
| 80 | ) -> MusehubSession: |
| 81 | """Seed a recording session and return the ORM row.""" |
| 82 | started_at = datetime.now(timezone.utc) |
| 83 | session_id = compute_session_id(repo_id, compute_identity_id(b"composer"), started_at.isoformat()) |
| 84 | ended_at = None if is_active else started_at |
| 85 | row = MusehubSession( |
| 86 | session_id=session_id, |
| 87 | repo_id=repo_id, |
| 88 | started_at=started_at, |
| 89 | ended_at=ended_at, |
| 90 | participants=participants or [], |
| 91 | intent=intent, |
| 92 | location=location, |
| 93 | notes=notes, |
| 94 | commits=commits or [], |
| 95 | is_active=is_active, |
| 96 | ) |
| 97 | db.add(row) |
| 98 | await db.commit() |
| 99 | await db.refresh(row) |
| 100 | return row |
| 101 | |
| 102 | |
| 103 | # --------------------------------------------------------------------------- |
| 104 | # Sessions list SSR tests |
| 105 | # --------------------------------------------------------------------------- |
| 106 | |
| 107 | |
| 108 | async def test_sessions_list_renders_session_name_server_side( |
| 109 | client: AsyncClient, |
| 110 | auth_headers: StrDict, |
| 111 | db_session: AsyncSession, |
| 112 | test_user: MusehubIdentity, |
| 113 | ) -> None: |
| 114 | """Session ID appears in the HTML response without a JS round-trip. |
| 115 | |
| 116 | The handler queries the DB during the request and inlines the session |
| 117 | identifier into the Jinja2 template so browsers receive a complete page |
| 118 | on first load. |
| 119 | """ |
| 120 | repo_id = await _make_repo(db_session) |
| 121 | row = await _make_session(db_session, repo_id, intent="Compose bridge section") |
| 122 | resp = await client.get( |
| 123 | f"/{_OWNER}/{_SLUG}/sessions", headers=auth_headers |
| 124 | ) |
| 125 | assert resp.status_code == 200 |
| 126 | body = resp.text |
| 127 | assert row.session_id[:8] in body |
| 128 | assert "session-row" in body |
| 129 | |
| 130 | |
| 131 | async def test_sessions_list_active_badge_present( |
| 132 | client: AsyncClient, |
| 133 | auth_headers: StrDict, |
| 134 | db_session: AsyncSession, |
| 135 | test_user: MusehubIdentity, |
| 136 | ) -> None: |
| 137 | """Active session renders a live badge in the server-rendered HTML.""" |
| 138 | repo_id = await _make_repo(db_session) |
| 139 | await _make_session(db_session, repo_id, is_active=True) |
| 140 | resp = await client.get( |
| 141 | f"/{_OWNER}/{_SLUG}/sessions", headers=auth_headers |
| 142 | ) |
| 143 | assert resp.status_code == 200 |
| 144 | body = resp.text |
| 145 | assert "live" in body.lower() or "Live" in body |
| 146 | |
| 147 | |
| 148 | async def test_sessions_list_htmx_fragment_path( |
| 149 | client: AsyncClient, |
| 150 | auth_headers: StrDict, |
| 151 | db_session: AsyncSession, |
| 152 | test_user: MusehubIdentity, |
| 153 | ) -> None: |
| 154 | """GET with HX-Request: true returns rows fragment, not the full page. |
| 155 | |
| 156 | When HTMX issues a partial swap request the response must NOT contain |
| 157 | the full page chrome and MUST contain the session row markup. |
| 158 | """ |
| 159 | repo_id = await _make_repo(db_session) |
| 160 | row = await _make_session(db_session, repo_id) |
| 161 | htmx_headers = {**auth_headers, "HX-Request": "true"} |
| 162 | resp = await client.get( |
| 163 | f"/{_OWNER}/{_SLUG}/sessions", headers=htmx_headers |
| 164 | ) |
| 165 | assert resp.status_code == 200 |
| 166 | body = resp.text |
| 167 | assert row.session_id[:8] in body |
| 168 | assert "<!DOCTYPE html>" not in body |
| 169 | assert "<html" not in body |
| 170 | |
| 171 | |
| 172 | async def test_sessions_list_empty_state_when_no_sessions( |
| 173 | client: AsyncClient, |
| 174 | auth_headers: StrDict, |
| 175 | db_session: AsyncSession, |
| 176 | test_user: MusehubIdentity, |
| 177 | ) -> None: |
| 178 | """Empty session list renders an empty-state component server-side (no JS fetch needed).""" |
| 179 | await _make_repo(db_session) |
| 180 | resp = await client.get( |
| 181 | f"/{_OWNER}/{_SLUG}/sessions", headers=auth_headers |
| 182 | ) |
| 183 | assert resp.status_code == 200 |
| 184 | body = resp.text |
| 185 | assert '<div class="session-row' not in body |
| 186 | assert "empty-state" in body or "No sessions yet" in body |
| 187 | |
| 188 | |
| 189 | # --------------------------------------------------------------------------- |
| 190 | # Session detail SSR tests |
| 191 | # --------------------------------------------------------------------------- |
| 192 | |
| 193 | |
| 194 | async def test_session_detail_renders_session_id( |
| 195 | client: AsyncClient, |
| 196 | auth_headers: StrDict, |
| 197 | db_session: AsyncSession, |
| 198 | test_user: MusehubIdentity, |
| 199 | ) -> None: |
| 200 | """Session detail page renders the session ID and metadata server-side.""" |
| 201 | repo_id = await _make_repo(db_session) |
| 202 | row = await _make_session( |
| 203 | db_session, repo_id, intent="Lay down the horn section", location="Studio B" |
| 204 | ) |
| 205 | resp = await client.get( |
| 206 | f"/{_OWNER}/{_SLUG}/sessions/{row.session_id}", |
| 207 | headers=auth_headers, |
| 208 | ) |
| 209 | assert resp.status_code == 200 |
| 210 | body = resp.text |
| 211 | assert row.session_id[:8] in body |
| 212 | assert "Studio B" in body |
| 213 | |
| 214 | |
| 215 | async def test_session_detail_renders_participants( |
| 216 | client: AsyncClient, |
| 217 | auth_headers: StrDict, |
| 218 | db_session: AsyncSession, |
| 219 | test_user: MusehubIdentity, |
| 220 | ) -> None: |
| 221 | """Participant user IDs appear in the session detail HTML response.""" |
| 222 | repo_id = await _make_repo(db_session) |
| 223 | row = await _make_session( |
| 224 | db_session, repo_id, participants=["alice", "bob"] |
| 225 | ) |
| 226 | resp = await client.get( |
| 227 | f"/{_OWNER}/{_SLUG}/sessions/{row.session_id}", |
| 228 | headers=auth_headers, |
| 229 | ) |
| 230 | assert resp.status_code == 200 |
| 231 | body = resp.text |
| 232 | assert "alice" in body |
| 233 | assert "bob" in body |
| 234 | |
| 235 | |
| 236 | async def test_session_detail_unknown_id_404( |
| 237 | client: AsyncClient, |
| 238 | auth_headers: StrDict, |
| 239 | db_session: AsyncSession, |
| 240 | test_user: MusehubIdentity, |
| 241 | ) -> None: |
| 242 | """Non-existent session_id returns HTTP 404.""" |
| 243 | await _make_repo(db_session) |
| 244 | fake_id = secrets.token_hex(16) |
| 245 | resp = await client.get( |
| 246 | f"/{_OWNER}/{_SLUG}/sessions/{fake_id}", |
| 247 | headers=auth_headers, |
| 248 | ) |
| 249 | assert resp.status_code == 404 |
File History
11 commits
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2
chore: bump version to 0.2.0rc15 for musehub#113 fix release
Sonnet 4.6
patch
15 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352
Merge branch 'task/version-tags-phase3-server' into dev
Human
17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53
merge: rescue snapshot-recovery hardening (c00aa21d) into d…
Opus 4.8
minor
⚠
30 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a
fix: remove false-positive proposal_comments index drop fro…
Sonnet 4.6
patch
34 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3
feat: render markdown mists as HTML with heading anchor links
Sonnet 4.6
patch
35 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
36 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee
Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump …
Human
36 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c
Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo…
Human
36 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd
Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop…
Human
36 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f
fix: use wire_bytes not mpack_bytes_raw in compute_object_b…
Sonnet 4.6
patch
48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583
rename: delta_add → delta_upsert across wire format, models…
Sonnet 4.6
patch
50 days ago