Files
hermes-webui/tests/test_sidebar_session_partition.py
T
74d7732778 stage #4597 (santastabber): reduce session-list work for archived histories (perf) (#4665)
Converged after deep bounce cycles: Finding A (production regression — skipped the
state.db source correction before the CLI filter, dropping WebUI sessions with stale-CLI
sidecar flags) FIXED via _apply_sidebar_state_db_overrides(result) before the routes.py
filter on both index + full-scan paths; Finding B (flaky lineage test) FIXED via hermetic
_persisted_session_ids_snapshot fixture. 16 own tests deterministic. Code byte-faithful
from PR head e1a3ecf22eca.

Co-authored-by: nesquena-hermes <agent@nesquena-hermes>
Co-authored-by: santastabber <santastabber@users.noreply.github.com>
2026-06-21 22:57:28 -07:00

75 lines
3.7 KiB
Python

"""Regression coverage for single-pass sidebar session partitioning."""
from __future__ import annotations
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SESSIONS_JS = (ROOT / "static" / "sessions.js").read_text(encoding="utf-8")
def _function_block(name: str) -> str:
start = SESSIONS_JS.index(f"function {name}(")
brace = SESSIONS_JS.index("{", start)
depth = 0
for idx in range(brace, len(SESSIONS_JS)):
char = SESSIONS_JS[idx]
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return SESSIONS_JS[start : idx + 1]
raise AssertionError(f"unbalanced braces in {name}")
def _partition_block() -> str:
return _function_block("_partitionSidebarSessionRows")
def test_render_uses_single_pass_partition_helper():
render_body = _function_block("renderSessionListFromCache")
assert "_partitionSidebarSessionRows(allMatched, activeSidForSidebar)" in render_body
assert "_renderSidebarRowsFromRawSessions(sessionsRaw, referenceRaw)" in render_body
assert "? sessions.length" in render_body
assert ": _renderSidebarRowsFromRawSessions(webuiSessionsRaw, webuiReferenceRaw).length;" in render_body
assert ": _renderSidebarRowsFromRawSessions(cliSessionsRaw, cliReferenceRaw).length;" in render_body
assert "const count=filter==='cli'?renderedCliSessionCount:renderedWebuiSessionCount;" in render_body
assert "const count=filter==='cli'?cliSessionCount:webuiSessionCount;" not in render_body
assert "withMessages.filter(" not in render_body
def test_partition_helper_applies_message_source_project_and_archive_gates():
block = _partition_block()
assert "function _sidebarRowHasVisibleMessages(s, activeSidForSidebar)" in SESSIONS_JS
assert "_sidebarRowHasVisibleMessages(s, activeSidForSidebar)" in block
assert "if(_sessionSourceFilter==='cli' && !window._showCliSessions && cliSessionCount===0)" in block
assert "const showCliOnly=_sessionSourceFilter==='cli';" in block
assert "if(!_showArchived&&s.archived) continue;" in block
assert "if(s.archived){" in block
assert "const serverArchivedCount=showCliOnly?_archivedCliCount:_archivedWebuiCount;" in block
assert "archivedCount: Math.max(showCliOnly ? cliArchivedCount : webuiArchivedCount, Number(serverArchivedCount||0))," in block
assert "return {" in block
assert "profileFiltered: showCliOnly ? cliProfileFiltered : webuiProfileFiltered," in block
assert "sessionsRaw: showCliOnly ? cliSessionsRaw : webuiSessionsRaw," in block
def test_partition_helper_keeps_raw_source_counts_while_render_owns_visible_counts():
render_body = _function_block("renderSessionListFromCache")
assert "webuiSessionCount," not in _partition_block()
assert "cliSessionCount," in _partition_block()
assert "webuiReferenceRaw," in _partition_block()
assert "cliReferenceRaw," in _partition_block()
assert "webuiSessionsRaw," in _partition_block()
assert "cliSessionsRaw," in _partition_block()
assert "const renderedWebuiSessionCount=" in render_body
assert "const renderedCliSessionCount=" in render_body
assert "_renderSidebarRowsFromRawSessions(webuiSessionsRaw, webuiReferenceRaw).length" in render_body
assert "_renderSidebarRowsFromRawSessions(cliSessionsRaw, cliReferenceRaw).length" in render_body
assert "function _countRenderedSidebarRowsFromRawSessions" not in SESSIONS_JS
assert "function _renderSidebarRowsFromRawSessions(sessionsRaw, referenceSessionsRaw){" in SESSIONS_JS
assert "_attachChildSessionsToSidebarRows(_collapseSessionLineageForSidebar(sessionsRaw), sessionsRaw, referenceRows)" in SESSIONS_JS