hermes-agent/tests/gateway/test_delivery_ledger_single_connection.py
teknium1 76e08ebea5 test: restored tests exercise their claim instead of passing vacuously
Independent review of #120220 found restored tests that could pass without
reaching the code they name. Each fix below was A/B'd against a sabotaged
production line (red) and the real one (green).

- test_clarify_progress_leak: the fake agent slept 2x0.35 s as its only sync.
  It now waits (<= 5 s) for the progress task's first bubble; the queue is
  FIFO, so a leaked clarify bubble is what lands first. Reverting the clarify
  skip in run_turn_runner goes red on the leaked JSON.
- test_desktop_lifecycle_windows_live holder-scan case: the kanban row was
  rebuilt after the serve kill, re-reading the dead pid; NoSuchProcess was
  swallowed into an empty scan and returned False without reaching the
  classifier. Rows are now snapshotted while both processes live; a
  classifier that accepts everything goes red (the old version stayed green).
- *_windows_live (control socket, desktop lifecycle, plan reconciliation,
  shim fail-closed): fixed time.sleep settles become bounded waits on the
  real condition (argv visible to psutil, pipe gone, handle released) and
  child readline() calls get a 60 s bound.
- test_gateway_proc_fallback BSD ps case: drop the is_windows stub and mark
  it linux_only like its neighbour; a revert to `ps -A eww` goes red.
- test_delivery_ledger_single_connection counts only connections to the
  ledger's own database, not every sqlite3.connect in the process; pruning on
  a second connection goes red.
- test_codex_models: the hardcoded gpt-5.6 list becomes a relationship to
  DEFAULT_CODEX_MODELS and the forward-compat table; adding a -pro slug to
  either goes red.
- group-chat-view inline-code: the renderer stub wrapped the code in .aui-md,
  which the generic inline-code rule themes on its own, so the room's
  data-slot rule was never tested. Without the wrapper, renaming the room's
  data-slot goes red.
2026-09-23 10:34:54 -07:00

37 lines
1.5 KiB
Python

"""Recording a reply opens exactly one SQLite connection.
``record_obligation`` runs on every outbound final response. Its retention prune must run on the
recording connection, inside the recording transaction — the way the cron ledgers prune — not on a
second connection opened after the first one closed.
"""
from __future__ import annotations
import sqlite3
from pathlib import Path
from gateway import delivery_ledger as dl
def test_recording_a_reply_does_not_open_a_second_connection(tmp_path, monkeypatch):
db = tmp_path / "state.db"
monkeypatch.setattr(dl, "_db_path", lambda: db)
real_connect = sqlite3.connect
opened: list[str] = []
def counting_connect(*args, **kwargs):
target = args[0] if args else kwargs.get("database")
# Only the ledger's own database counts: unrelated sqlite users in the
# process (other modules, background threads) must not move the tally.
if Path(str(target)).resolve() == db.resolve():
opened.append(str(target))
return real_connect(*args, **kwargs)
monkeypatch.setattr(sqlite3, "connect", counting_connect)
dl.record_obligation(obligation_id="ob-warm", session_key="s", platform="p", chat_id="c",
thread_id=None, content="warm-up (schema)")
opened.clear()
dl.record_obligation(obligation_id="ob-1", session_key="s", platform="p", chat_id="c",
thread_id=None, content="hello")
assert len(opened) == 1, opened