Files
brain-of-reese/tests/unit/test_frontend_hidden_tab.py
T
ducoterra cb8767512a
Build and Push Containers / build-and-push-app (push) Successful in 1m39s
Build and Push Containers / build-and-push-db (push) Successful in 15s
phase: 159_edit_and_resend_message
All completion criteria verified. Final report:

**Phase 159 — Edit the last question and re-ask it: VERIFIED COMPLETE** (all 4 tasks already in `complete/`; this pass verified end-to-end)

**Verified (implementation landed + tests green):**
- `app.js`: `EDIT_ICON`, `lastUserWrap`, `appendEditButton`/`markLastEditable` (single-target discipline, in-flight guard), `openEditor` (labelled textarea, `maxlength=4000`, Escape/Ctrl+⌘+Enter, empty guard), save path: replace → splice tail → DOM removal → `lastBrainWrap=null` → `saveConversation`+`persistConversation` → `runTurn(text,{reask:true})` — matches README exactly
- `styles.css`: `.edit-btn`/`.edit-form`/`.edit-error`/`.is-editing` + mobile 44px; unit pins 40, E2E 7, integration `test_chats_api.py` extended

**Test/lint results (all green):**
- `uv run pytest` — 3,661 tests, exit 0
- `uv run pytest --cov=app --cov-report=term-missing` — TOTAL **99%** (>90%)
- `uv run ruff check . && uv run pyright` — all checks passed; 0 errors
- `uv run pytest tests/e2e/test_edit_and_resend.py -v --no-cov` (DB up) — **7/7 passed**
- Regressions in isolation: `test_retry_answer.py` 4/4, `test_failed_turn_retry.py` 4/4, `test_chat_persistence.py` 4/4

**Completion criteria:** all 7 ✓ — Edit on last bubble + prefill + Escape/Cancel restore (E2E #3); replace+truncate+re-ask with no duplicated question (E2E #1); refresh survival (E2E #4); failed/“question too long” turn editable + trim re-asks (E2E #5); earlier bubbles un-editable + byte-identical Retry/Stop/persistence (E2E #2 + regressions + unit pins); suite/coverage/lint ✓; E2E + regressions isolated ✓. Commit + phase-dir move: harness's job (no git ops by me, per protocol).

**Documentation:** README.md — full review; phase 159 section cross-checked against `app.js` source (cap, keys, truncate, image survival) — consistent, no changes needed; all 30 `.env.example` vars documented. AGENTS.md — full review; all rules/quick-ref commands valid — verified consistent, no changes needed.

**Notable:** none — no defects found in prior work; no live-infrastructure changes required.

**Next pending phase:** `160_tool_budget_telemetry`
2026-10-05 19:07:33 -04:00

235 lines
11 KiB
Python

"""Unit: a hidden tab never stops a turn (phase 73, task 02).
The JS behavior itself is E2E-covered (tests/e2e/test_hidden_tab_stream.py,
task 03); here we pin the app.js mechanisms the story depends on — the
pagehide-partial ↔ settle correlation (C1 hardening, unconditional) and the
visibility-aware pre-token guard (task 01, C2 confirmed) — so a silent
regression in app.js is caught without a browser (house pattern:
tests/unit/test_frontend_feedback.py).
"""
from __future__ import annotations
import re
from pathlib import Path
FRONTEND = Path(__file__).resolve().parents[2] / "frontend"
APP_JS = FRONTEND / "assets" / "app.js"
def _js() -> str:
return APP_JS.read_text(encoding="utf-8")
# ---------- C1: the pagehide partial is replaced, never duplicated ----------
def test_leave_partial_index_is_a_module_turn_local() -> None:
"""`leavePartialIndex` is a module-scope turn local (like
persistedOnLeave): declared -1, reset at the top of runTurn, and
written ONLY by the pagehide handler (the one `= conversation.length
- 1` assignment) — plus the phase-159 (task 02) truncation reset
(the SANCTIONED third write: the edit's Save clears it to -1 WHEN
IT POINTED PAST editIdx — a partial saved from the dropped tail
must not be re-persisted into the new shape; the settle's identity
guard would otherwise fall back to an append; the next runTurn
also resets it turn-locally regardless). No other code may move
the correlation."""
js = _js()
assert "let leavePartialIndex = -1" in js, "module-scope declaration"
# The -1 writes: the declaration, the runTurn reset, and the
# phase-159 truncation reset (the sanctioned third).
assert js.count("leavePartialIndex = -1;") == 3, (
"only the declaration, the runTurn reset, and the phase-159 "
"truncation reset write -1"
)
# The phase-159 (task 02) truncation reset is GATED: it fires only
# when the pointer pointed past the edited record (a partial on or
# before the edited record is untouched).
assert "if (leavePartialIndex > editIdx) leavePartialIndex = -1;" in js
# The one and only index write is the pagehide handler's.
assert js.count("leavePartialIndex = conversation.length - 1") == 1, (
"only the pagehide handler records the partial's index"
)
# The reset sits at the top of runTurn, with the other turn locals
# (the acc / thinkingAcc / persistedOnLeave group, just before the
# fresh AbortController).
turn = js.find("async function runTurn")
abort_idx = js.find("turnAbort = new AbortController()", turn)
assert -1 < turn < abort_idx, "runTurn must exist"
turn_top = js[turn:abort_idx]
assert "leavePartialIndex = -1;" in turn_top, "reset per turn at the top"
assert turn_top.index("persistedOnLeave = false;") < turn_top.index(
"leavePartialIndex = -1;"
), "reset alongside the other turn locals (phase-20 group)"
def test_pagehide_records_the_partial_index_after_the_push() -> None:
"""The pagehide handler keeps its phase-20 guards (idempotency,
in-flight only, nothing brain-side yet) and, AFTER the partial is
pushed through rememberBrainTurn, records the pushed record's index
so the turn's settle can find it."""
js = _js()
fn = js.find('window.addEventListener("pagehide"')
assert fn != -1, "the pagehide listener must exist"
body = js[fn: js.find("\n});", fn)]
assert "if (persistedOnLeave) return;" in body, "phase-20 idempotency guard"
assert "if (!acc) return;" in body, "nothing brain-side yet → no save"
i_guard = body.index("persistedOnLeave = true;")
i_push = body.index("rememberBrainTurn(acc, {")
i_index = body.index("leavePartialIndex = conversation.length - 1")
assert i_guard < i_push < i_index, (
"flag → push the partial → record its index (the record it just pushed)"
)
def test_remember_brain_turn_replaces_in_place_with_identity_guard() -> None:
"""rememberBrainTurn's optional in-place mode: `replaceIndex >= 0`
REPLACES the record at that index instead of appending — and only
when the index STILL points at a brain record (the identity guard:
a New-Chat click or restore between pagehide and settle falls back
to the append). Either way the record is written once, and the
save points (localStorage + the headless auto-save) run once on the
written record."""
js = _js()
fn = js.find("function rememberBrainTurn")
assert fn != -1, "rememberBrainTurn must exist"
body = js[fn: js.find("\n}\n", fn)]
assert "function rememberBrainTurn(rawText, meta, replaceIndex = -1)" in body, (
"the optional in-place mode defaults to the plain append"
)
assert 'const rec = { who: "brain", text: rawText || "…", ...meta }' in body, (
"one record object, written once"
)
assert (
'if (replaceIndex >= 0 && conversation[replaceIndex]?.who === "brain")' in body
), "identity guard: the index must still point at a brain record"
assert "conversation[replaceIndex] = rec" in body, "the in-place replace"
assert "conversation.push(rec)" in body, "the append (no partial / guard miss)"
i_replace = body.index("conversation[replaceIndex] = rec")
i_push = body.index("conversation.push(rec)")
assert i_replace < i_push, "replace branch precedes the append fallback"
i_save = body.index("saveConversation()")
i_persist = body.index("persistConversation()")
assert i_push < i_save < i_persist, (
"both save points run ONCE, after the single write — the auto-save "
"refreshes the row exactly once for the replaced record"
)
def test_every_settle_writes_through_the_correlation() -> None:
"""The three brain-record write sites in runTurn's settle paths —
the `done` save point, the zero-frame empty-answer fallback, and the
stop-path partial — all pass `leavePartialIndex`, so the replace is
the only write after a pagehide partial (the invariant). The
pagehide handler itself pushes with the plain two-arg form (it IS
the partial)."""
js = _js()
# done save point.
done = js.find('ev.type === "done"')
done_branch = js[done: js.find('ev.type === "error"', done)]
assert "leavePartialIndex" in done_branch, "the done settle is correlated"
# zero-frame empty-answer fallback.
fallback = js.find("!aborted && !wrap")
fallback_block = js[fallback: js.find(") catch (err) {", fallback)]
assert "leavePartialIndex" in fallback_block, "the fallback settle is correlated"
# stop-path partial (the `stopped` marker persist).
catch = js.find("} catch (err) {")
stop_idx = js.find('stoppedByUser || err?.name === "AbortError"', catch)
stop_branch = js[stop_idx: js.find("} else {", stop_idx)]
assert "leavePartialIndex" in stop_branch, "the stop settle is correlated"
# The pagehide handler pushes the partial through rememberBrainTurn
# (phase 158, D4: + the optional segments — non-empty only).
fn = js.find('window.addEventListener("pagehide"')
body = js[fn: js.find("\n});", fn)]
assert "rememberBrainTurn(acc, {" in body
assert "thinking: thinkingAcc || undefined" in body
assert (
"segments: pagehideSegments.length ? pagehideSegments : undefined"
in body
), "phase 158: the partial's timeline persists (non-empty only)"
assert "leavePartialIndex" not in body.replace(
"leavePartialIndex = conversation.length - 1", ""
), "the pagehide handler only records the index — it never settles"
def test_real_navigation_behavior_is_unchanged() -> None:
"""The leave-save invariant: a REAL navigation never runs a settle
(the page unloads), so the pagehide partial stays persisted exactly
as phase 20 left it — the handler's guards and the rememberBrainTurn
ride-through (localStorage + auto-save) are untouched."""
js = _js()
fn = js.find('window.addEventListener("pagehide"')
body = js[fn: js.find("\n});", fn)]
for guard in (
"if (persistedOnLeave) return;",
"if (uiState !== UI_STATE.thinking && uiState !== UI_STATE.streaming)",
"if (!acc) return;",
):
assert guard in body, f"phase-20 guard intact: {guard}"
# The phase-48 teardown contract (real close / navigation / Stop still
# aborts the fetch) is untouched.
assert "turnAbort?.abort()" in js, "the abort owner still aborts the fetch"
assert re.search(r"export\s+const\s+TURN_TIMEOUT_MS\s*=\s*300_?000\s*;", js), (
"the guard constant is still 300s (raised from the phase-17/48 value)"
)
# ---------- C2: hidden time does not count toward the pre-token guard ----------
def test_guard_arms_with_a_rearmable_callback() -> None:
"""armTurnTimeout remembers the guard's callback (turnTimeoutCb) so
the visibility listener can re-arm it; clearTurnTimeout clears BOTH
the timer and the callback — armed ⇔ callback set."""
js = _js()
fn = js.find("function armTurnTimeout")
assert fn != -1, "armTurnTimeout must exist"
body = js[fn: js.find("\n}\n", fn)]
assert "turnTimeoutCb = onTimeout" in body, "the callback is remembered"
assert body.index("turnTimeoutCb = onTimeout") < body.index(
"turnTimeout = setTimeout(onTimeout, TURN_TIMEOUT_MS)"
), "the callback is stored with the arm"
cfn = js.find("function clearTurnTimeout")
cbody = js[cfn: js.find("\n}\n", cfn)]
assert "clearTimeout(turnTimeout)" in cbody
assert "turnTimeout = 0" in cbody
assert "turnTimeoutCb = null" in cbody, "clearing the timer drops the callback"
def test_visibility_rearm_counts_only_visible_pre_token_time() -> None:
"""On visibilitychange, when the tab returns to VISIBLE with the
guard still armed (the pre-token window — the timer id is non-zero
and the callback set), it re-arms with a FRESH TURN_TIMEOUT_MS via
armTurnTimeout (the same arm path — one timer, one owner). Hidden
transitions never re-arm. Exactly one such listener exists."""
js = _js()
assert js.count('document.addEventListener("visibilitychange"') == 1, (
"one visibility listener (the guard re-arm)"
)
fn = js.find('document.addEventListener("visibilitychange"')
body = js[fn: js.find("});", fn)]
assert 'document.visibilityState === "visible"' in body, (
"only a return to visible re-arms — hidden transitions do not extend it"
)
assert "turnTimeout && turnTimeoutCb" in body, (
"only an ARMED guard (pre-token window) is re-armed — the timer clears "
"on the first thinking/delta/retry frame and every terminal transition"
)
assert "armTurnTimeout(turnTimeoutCb)" in body, (
"the re-arm goes through the same arm path (a fresh TURN_TIMEOUT_MS)"
)
assert "TURN_TIMEOUT_MS" in body or "armTurnTimeout" in body, (
"the fresh deadline is the owner-locked TURN_TIMEOUT_MS"
)
def test_header_documents_the_phase_73_contract() -> None:
"""House convention: the file-header doc inventories each phase's
contract — phase 73 documents the hidden-tab rule (only close /
navigation / Stop aborts) and both hardenings."""
js = _js()
header = js[: js.find("import {")]
assert "phase 73" in header.lower(), "the header must carry the phase-73 section"
assert "leavePartialIndex" in header, "the correlation local is documented"
assert "visibilitychange" in header, "the visibility re-arm is documented"