Skip to content

The agent that grows with you

test(gateway): trim flush-resolver tests to positive + fence invariants

kshitijk4poor43fdc0fef551
diff --git a/tests/gateway/test_session_recovery.py b/tests/gateway/test_session_recovery.py
index 1a1db905470..455c703c700 100644
--- a/tests/gateway/test_session_recovery.py
+++ b/tests/gateway/test_session_recovery.py
@@ -1,5 +1,5 @@
"""Tests for SessionRecoveryMixin.resolve_session_id_for_key — flush-recovery
-session_key → session_id resolution (profile- and WhatsApp-alias aware)."""
+session_key → session_id resolution (profile aware)."""
from unittest.mock import patch
@@ -28,39 +28,11 @@ def _store(tmp_path, db):
return store
-def _no_aliases(identifier):
- return {identifier}
-
-
-def test_resolve_exact_key_returns_row_id(tmp_path):
- db = _FakeGatewayDB({"agent:main:telegram:dm:99": {"id": "tel-row"}})
- store = _store(tmp_path, db)
- assert store.resolve_session_id_for_key("agent:main:telegram:dm:99") == ("tel-row", db)
- assert db.queries == ["agent:main:telegram:dm:99"]
-
-
-def test_resolve_whatsapp_phone_key_to_lid_row(tmp_path, monkeypatch):
- lid = "1234567890123456789"
- key = f"agent:test-bot:whatsapp:dm:{lid}"
- db = _FakeGatewayDB({key: {"id": "lid-row"}})
- store = _store(tmp_path, db)
- monkeypatch.setattr(
- "gateway.whatsapp_identity.expand_whatsapp_aliases",
- lambda ident: {"15551234567", lid} if ident == "15551234567" else {ident},
- )
- resolved = store.resolve_session_id_for_key(
- "agent:test-bot:whatsapp:dm:15551234567"
- )
- assert resolved == ("lid-row", db)
- assert key in db.queries
-
-
-def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path, monkeypatch):
+def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path):
"""A profile-namespaced key must never resolve into the root store's
``agent:main`` row: the exact-key finder only matches the namespaced key."""
db = _FakeGatewayDB({"agent:main:whatsapp:dm:15551234567": {"id": "main-row"}})
store = _store(tmp_path, db)
- monkeypatch.setattr("gateway.whatsapp_identity.expand_whatsapp_aliases", _no_aliases)
assert (
store.resolve_session_id_for_key("agent:test-bot:whatsapp:dm:15551234567")
is None
@@ -69,11 +41,11 @@ def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path, monkey
assert not any(q.startswith("agent:main") for q in db.queries)
-def test_resolve_returns_none_when_no_matching_row(tmp_path, monkeypatch):
- db = _FakeGatewayDB({})
+def test_resolve_db_fallback_rejects_row_started_after_flush(tmp_path):
+ """A row created after the flush timestamp cannot be the message's origin; only a row that
+ already existed at flush time is adopted, and it comes back with its owning db."""
+ key = "agent:main:telegram:dm:42"
+ db = _FakeGatewayDB({key: {"id": "late-row", "started_at": 1700000100.0}})
store = _store(tmp_path, db)
- monkeypatch.setattr("gateway.whatsapp_identity.expand_whatsapp_aliases", _no_aliases)
- assert (
- store.resolve_session_id_for_key("agent:test-bot:whatsapp:dm:15551234567")
- is None
- )
+ assert store.resolve_session_id_for_key(key, not_after=1700000000) is None
+ assert store.resolve_session_id_for_key(key, not_after=1700000100) == ("late-row", db)
diff --git a/tests/gateway/test_shutdown_flush.py b/tests/gateway/test_shutdown_flush.py
index 53a2a7a65ca..136a316912f 100644
--- a/tests/gateway/test_shutdown_flush.py
+++ b/tests/gateway/test_shutdown_flush.py
@@ -127,49 +127,6 @@ def test_recover_payload_without_session_id_uses_resolver_and_deletes_file(
assert not flush_file.exists()
-def test_recover_payload_without_session_id_resolver_none_preserves_file(
- tmp_path, monkeypatch
-):
- """When the resolver cannot map the key, the file is preserved (never delete an
- unrecoverable message)."""
- flush_dir = _make_flush_dir(tmp_path)
- monkeypatch.setattr("gateway.shutdown_flush._get_flush_dir", lambda: flush_dir)
- payload = {
- "session_key": "agent:main:whatsapp:dm:15551234567",
- "data": {"text": "lost message"},
- }
- flush_file = flush_dir / "no_sid.json"
- flush_file.write_text(json.dumps(payload), encoding="utf-8")
-
- mock_db = MagicMock()
- count = recover_pending_to_db(mock_db, session_resolver=lambda key, not_after=None: None)
-
- assert count == 0
- mock_db.append_message.assert_not_called()
- assert flush_file.exists()
-
-
-def test_recover_payload_without_session_id_no_resolver_preserves_file(
- tmp_path, monkeypatch
-):
- """Existing behaviour: without a resolver, a session_id-less payload is preserved."""
- flush_dir = _make_flush_dir(tmp_path)
- monkeypatch.setattr("gateway.shutdown_flush._get_flush_dir", lambda: flush_dir)
- payload = {
- "session_key": "agent:main:whatsapp:dm:15551234567",
- "data": {"text": "lost message"},
- }
- flush_file = flush_dir / "no_sid.json"
- flush_file.write_text(json.dumps(payload), encoding="utf-8")
-
- mock_db = MagicMock()
- count = recover_pending_to_db(mock_db)
-
- assert count == 0
- mock_db.append_message.assert_not_called()
- assert flush_file.exists()
-
-
def test_recover_closes_owned_db_when_unexpected_exception_escapes(
tmp_path, monkeypatch
):
@@ -359,37 +316,3 @@ def test_flushed_overflow_is_replayed_by_recover_pending_to_db(tmp_path, monkeyp
def test_flush_overflow_noop_on_empty():
assert flush_overflow_to_file({}) == 0
assert flush_overflow_to_file({"k": []}) == 0
-
-
-def test_recover_resolves_session_key_via_resolver(tmp_path, monkeypatch):
- """Real flush files carry only `text` (MessageEvent has no session_id
- attribute). Without a resolver they were skipped forever; with one,
- the message must be recovered and the file cleaned up."""
- flush_dir = _make_flush_dir(tmp_path)
- monkeypatch.setattr(
- "gateway.shutdown_flush._get_flush_dir", lambda: flush_dir
- )
- ts = int(time.time())
- payload = {
- "session_key": "agent:main:telegram:dm:42",
- "ts": ts,
- "data": {"text": "are you there?"}, # what _serialise_value actually produces
- }
- flush_file = flush_dir / "pending-test.json"
- flush_file.write_text(json.dumps(payload), encoding="utf-8")
-
- mock_db = MagicMock()
- count = recover_pending_to_db(
- mock_db,
- session_resolver=lambda key, not_after=None: (
- ("20260731_abc123", None) if key == "agent:main:telegram:dm:42" else None),
- )
-
- assert count == 1
- mock_db.append_message.assert_called_once_with(
- session_id="20260731_abc123",
- role="user",
- content="are you there?",
- timestamp=ts,
- )
- assert not flush_file.exists()