Files
hermes-agent/tests/hermes_state/test_schema_read_probe.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

104 lines
4.1 KiB
Python

"""Contract tests for schema_read_probe_statements().
Read-only SessionDB opens skip _reconcile_columns() by design, so dashboard
read paths heal stale stores via a probe-then-writable-reopen dance in
``hermes_cli.web_server_sessions._open_session_db_at_path``. These tests pin the
probe's contract: it is DERIVED from SCHEMA_SQL (any column added there is
covered automatically — the previous hand-written probe went stale within
days) and it must fail at prepare time on a store missing any declared
column or table.
"""
import sqlite3
import pytest
from hermes_state_common import DEFERRED_INDEX_SQL, SCHEMA_SQL
from hermes_state_schema import SessionSchemaMixin, schema_read_probe_statements
def _fresh_schema_conn() -> sqlite3.Connection:
conn = sqlite3.connect(":memory:")
conn.executescript(SCHEMA_SQL)
conn.executescript(DEFERRED_INDEX_SQL)
return conn
class TestSchemaReadProbeStatements:
def test_probes_cover_every_declared_column(self):
"""Invariant: every column SCHEMA_SQL declares appears in a probe.
This is the anti-staleness contract — a column added to SCHEMA_SQL
must be probed without anyone remembering to update a list.
"""
expected = SessionSchemaMixin._parse_schema_columns(SCHEMA_SQL)
statements = schema_read_probe_statements()
by_table = {}
for statement in statements:
for table in expected:
if f'FROM "{table}"' in statement:
by_table[table] = statement
for table, cols in expected.items():
assert table in by_table, f"no probe statement for table {table}"
for col in cols:
assert f'"{table}"."{col}"' in by_table[table], (
f"column {table}.{col} declared in SCHEMA_SQL but not probed"
)
def test_probes_pass_on_fresh_schema(self):
conn = _fresh_schema_conn()
try:
for statement in schema_read_probe_statements():
conn.execute(statement).fetchone()
finally:
conn.close()
def test_probes_fail_on_missing_column(self):
"""The shipped regression: a store predating sessions.last_activity_at
(#72424) passed the old hand-written probe, then 500'd inside
list_sessions_rich on every sidebar poll until the user's first
message forced a writable open.
"""
conn = _fresh_schema_conn()
try:
# Indexes that depend on the column must be removed before SQLite
# can emulate a pre-column legacy store via DROP COLUMN.
conn.execute("DROP INDEX IF EXISTS idx_sessions_effective_activity")
conn.execute("ALTER TABLE sessions DROP COLUMN last_activity_at")
# The failure must come from the sessions probe naming the exact
# column — not incidentally from some other statement — so a
# probe-generation bug that misassigns columns to tables can't
# sneak through.
sessions_probe = next(
s
for s in schema_read_probe_statements()
if 'FROM "sessions"' in s
)
with pytest.raises(sqlite3.OperationalError) as excinfo:
conn.execute(sessions_probe)
assert "no such column: sessions.last_activity_at" in str(
excinfo.value
)
finally:
conn.close()
def test_probes_fail_on_missing_table(self):
conn = sqlite3.connect(":memory:")
try:
conn.executescript(SCHEMA_SQL)
conn.executescript("DROP TABLE gateway_routing")
gateway_probe = next(
s
for s in schema_read_probe_statements()
if 'FROM "gateway_routing"' in s
)
with pytest.raises(sqlite3.OperationalError) as excinfo:
conn.execute(gateway_probe)
assert "no such table" in str(excinfo.value).lower()
finally:
conn.close()
def test_probe_statements_are_cached(self):
assert schema_read_probe_statements() is schema_read_probe_statements()