|
| 1 | +"""Regression coverage for session-switch busy-state race and live-turn restore. |
| 2 | +
|
| 3 | +Switching from a streaming session to an idle one must clear S.busy before the |
| 4 | +async _ensureMessagesLoaded gap. Otherwise _isSessionLocallyStreaming() treats |
| 5 | +the newly opened session as locally streaming while messages are still loading. |
| 6 | +
|
| 7 | +Switching back to a streaming session must restore the snapshotted live turn |
| 8 | +instead of rebuilding thinking/worklog chrome from scratch. |
| 9 | +""" |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +REPO = Path(__file__).resolve().parents[1] |
| 14 | +SESSIONS_SRC = (REPO / "static" / "sessions.js").read_text(encoding="utf-8") |
| 15 | +UI_SRC = (REPO / "static" / "ui.js").read_text(encoding="utf-8") |
| 16 | + |
| 17 | + |
| 18 | +def _function_body(src: str, signature: str) -> str: |
| 19 | + start = src.find(signature) |
| 20 | + assert start != -1, f"missing {signature}" |
| 21 | + brace = src.find("{", start) |
| 22 | + assert brace != -1, f"missing opening brace for {signature}" |
| 23 | + depth = 0 |
| 24 | + for i in range(brace, len(src)): |
| 25 | + ch = src[i] |
| 26 | + if ch == "{": |
| 27 | + depth += 1 |
| 28 | + elif ch == "}": |
| 29 | + depth -= 1 |
| 30 | + if depth == 0: |
| 31 | + return src[brace + 1 : i] |
| 32 | + raise AssertionError(f"could not extract function body for {signature}") |
| 33 | + |
| 34 | + |
| 35 | +def test_loadSession_clears_busy_before_async_message_load_when_server_idle(): |
| 36 | + body = _function_body(SESSIONS_SRC, "async function loadSession(") |
| 37 | + |
| 38 | + idle_reset = body.find("if(!activeStreamId){") |
| 39 | + assert idle_reset != -1, "loadSession must gate idle cleanup on missing active_stream_id" |
| 40 | + idle_block = body[idle_reset : idle_reset + 500] |
| 41 | + assert "S.busy=false" in idle_block, "idle switch must clear S.busy immediately" |
| 42 | + assert "S.activeStreamId=null" in idle_block, "idle switch must clear S.activeStreamId immediately" |
| 43 | + |
| 44 | + ensure_load = body.find("await _ensureMessagesLoaded(sid)") |
| 45 | + assert ensure_load != -1, "loadSession must still lazy-load messages for idle sessions" |
| 46 | + assert idle_reset < ensure_load, ( |
| 47 | + "S.busy must be cleared before _ensureMessagesLoaded so session-list polling " |
| 48 | + "during the async gap does not mark the new session as locally streaming" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_loadSession_snapshots_live_turn_before_wiping_message_pane(): |
| 53 | + body = _function_body(SESSIONS_SRC, "async function loadSession(") |
| 54 | + |
| 55 | + snap_pos = body.find("snapshotLiveTurnHtmlForSession(currentSid)") |
| 56 | + # Anchor on the actual loading-placeholder marker (unique), not the |
| 57 | + # whitespace-sensitive innerHTML literal which also matches the |
| 58 | + # "Session not available" error handler. (Maintainer review.) |
| 59 | + wipe_pos = body.find("Loading conversation...") |
| 60 | + assert snap_pos != -1, "loadSession must snapshot the outgoing live turn before switching" |
| 61 | + assert wipe_pos != -1, "loadSession must still show the loading placeholder on switch" |
| 62 | + assert snap_pos < wipe_pos, "snapshot must run before msgInner is replaced with the loading placeholder" |
| 63 | + |
| 64 | + |
| 65 | +def test_loadSession_restores_live_turn_on_active_stream_return_path(): |
| 66 | + body = _function_body(SESSIONS_SRC, "async function loadSession(") |
| 67 | + |
| 68 | + # The restore that actually fires on switch-back is the Phase 2a path: after |
| 69 | + # loadInflightState() rehydrates INFLIGHT for an active stream, the streaming |
| 70 | + # branch calls restoreLiveTurnHtmlForSession(sid). (The old Phase-2b idle-branch |
| 71 | + # call was unreachable — INFLIGHT is always seeded by then — so assert the live |
| 72 | + # Phase 2a path. Maintainer review.) |
| 73 | + phase2a = body.find("Phase 2a") |
| 74 | + assert phase2a != -1, "loadSession must keep the Phase 2a streaming-restore branch" |
| 75 | + inflight_load = body.find("loadInflightState(sid", phase2a) |
| 76 | + assert inflight_load != -1, "Phase 2a must rehydrate INFLIGHT from persisted state for an active stream" |
| 77 | + restore = body.find("restoreLiveTurnHtmlForSession(sid)", inflight_load) |
| 78 | + assert restore != -1, ( |
| 79 | + "the active-stream return path must restore the snapshotted live-turn HTML " |
| 80 | + "after rehydrating INFLIGHT (Phase 2a), instead of rebuilding the worklog shell" |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def test_activity_timer_reads_pending_started_at(): |
| 85 | + body = _function_body(UI_SRC, "function _activityElapsedStartedAt(") |
| 86 | + assert "pending_started_at" in body |
| 87 | + assert "data-turn-started-at" in body or "turnStartedAt" in body |
0 commit comments