55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
"""Probe the installed Hermes lookup code with synthetic rows and mocked writes.
|
|
|
|
Run from the Hermes source root with its Python environment (see docs/DETAILS.md).
|
|
This script never connects to the gateway or opens a real session database.
|
|
"""
|
|
|
|
import inspect
|
|
import unittest
|
|
from contextlib import nullcontext
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock, patch
|
|
|
|
from gateway.session_local_migration import adopt_legacy_session
|
|
from gateway.session_local_title import resolve_titled_session
|
|
from tui_gateway.methods_session import _session_list_by_title
|
|
|
|
|
|
class LookupContract(unittest.TestCase):
|
|
def test_canonical_title_can_reach_legacy_adoption(self):
|
|
row = {"id": "fixture-old-chat"}
|
|
db = Mock()
|
|
db.get_session.side_effect = lambda key: row if key == row["id"] else None
|
|
db.resolve_session_by_title.return_value = row["id"]
|
|
db.get_compression_tip.return_value = None
|
|
conn = Mock()
|
|
conn.execute.return_value.fetchone.return_value = None
|
|
db._read_ctx.return_value = nullcontext(conn)
|
|
authority, actor = SimpleNamespace(db=db), object()
|
|
with (
|
|
patch("gateway.session_route_recovery.handed_off_ref", return_value=None),
|
|
patch("gateway.session_local_migration.adopt_legacy_session", return_value="adopted") as adopt,
|
|
):
|
|
self.assertEqual(resolve_titled_session(authority, actor, "Bot Chat"), "adopted")
|
|
adopt.assert_called_once_with(authority, actor, row)
|
|
|
|
def test_installed_adoption_contains_durable_commit(self):
|
|
# A source assertion, not an execution of the commit path.
|
|
self.assertIn("commit_local_session(authority.db", inspect.getsource(adopt_legacy_session))
|
|
|
|
def test_legacy_title_can_reach_archive_recovery(self):
|
|
db = Mock()
|
|
db.get_session_by_title.return_value = {"id": "fixture-archived", "archived": True}
|
|
db.get_session.return_value = None
|
|
with (
|
|
patch("tui_gateway.methods_session._unarchive_recoverable", return_value=True) as recover,
|
|
# The handler normally gets its response envelope helper at gateway bootstrap.
|
|
patch("tui_gateway.methods_session._ok", side_effect=lambda _rid, value: value, create=True),
|
|
):
|
|
self.assertEqual(_session_list_by_title(1, db, "Bot Chat"), {"sessions": []})
|
|
recover.assert_called_once_with(db, "fixture-archived")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|