Change-detectors, tautologies, source-reading tests, redundant duplicates, mock-echo tests and dead/unrunnable tests. Per-test rationale in the lane ledger (category + reason for every removal).
110 lines
4.3 KiB
Python
110 lines
4.3 KiB
Python
"""Tests for issue #860 — SQLite session transcript deduplication.
|
|
|
|
Verifies that:
|
|
1. _flush_messages_to_session_db uses _last_flushed_db_idx to avoid re-writing
|
|
2. Multiple _persist_session calls don't duplicate messages
|
|
3. append_to_transcript(skip_db=True) skips SQLite but writes JSONL
|
|
4. The gateway doesn't double-write messages the agent already persisted
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: _flush_messages_to_session_db only writes new messages
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestFlushDeduplication:
|
|
"""Verify _flush_messages_to_session_db tracks what it already wrote."""
|
|
|
|
def _make_agent(self, session_db):
|
|
"""Create a minimal AIAgent with a real session DB."""
|
|
with patch.dict(os.environ, {"OPENROUTER_API_KEY": "test-key"}):
|
|
from run_agent import AIAgent
|
|
agent = AIAgent(
|
|
api_key="test-key",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
model="test/model",
|
|
quiet_mode=True,
|
|
session_db=session_db,
|
|
session_id="test-session-860",
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
)
|
|
# Simulate lazy session creation (normally done by run_conversation)
|
|
agent._ensure_db_session()
|
|
return agent
|
|
|
|
def test_flush_writes_only_new_messages(self):
|
|
"""First flush writes all new messages, second flush writes none."""
|
|
from hermes_state import SessionDB
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
db_path = Path(tmpdir) / "test.db"
|
|
db = SessionDB(db_path=db_path)
|
|
try:
|
|
agent = self._make_agent(db)
|
|
|
|
conversation_history = [
|
|
{"role": "user", "content": "old message"},
|
|
]
|
|
messages = list(conversation_history) + [
|
|
{"role": "user", "content": "new question"},
|
|
{"role": "assistant", "content": "new answer"},
|
|
]
|
|
|
|
# First flush — should write 2 new messages
|
|
agent._flush_messages_to_session_db(messages, conversation_history)
|
|
|
|
rows = db.get_messages(agent.session_id)
|
|
assert len(rows) == 2, f"Expected 2 messages, got {len(rows)}"
|
|
|
|
# Second flush with SAME messages — should write 0 new messages
|
|
agent._flush_messages_to_session_db(messages, conversation_history)
|
|
|
|
rows = db.get_messages(agent.session_id)
|
|
assert len(rows) == 2, f"Expected still 2 messages after second flush, got {len(rows)}"
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: append_to_transcript skip_db parameter
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestAppendToTranscriptSkipDb:
|
|
"""Verify skip_db=True skips the SQLite write."""
|
|
|
|
def test_skip_db_prevents_sqlite_write(self, tmp_path):
|
|
"""With skip_db=True and a real DB, message does NOT appear in SQLite."""
|
|
from gateway.config import GatewayConfig
|
|
from gateway.session import SessionStore
|
|
from hermes_state import SessionDB
|
|
|
|
db_path = tmp_path / "test_skip.db"
|
|
db = SessionDB(db_path=db_path)
|
|
|
|
config = GatewayConfig()
|
|
with patch("gateway.session.SessionStore._ensure_loaded"):
|
|
store = SessionStore(sessions_dir=tmp_path, config=config)
|
|
store._db = db
|
|
store._loaded = True
|
|
|
|
session_id = "test-skip-db-real"
|
|
db.create_session(session_id=session_id, source="test")
|
|
|
|
msg = {"role": "assistant", "content": "hello world"}
|
|
store.append_to_transcript(session_id, msg, skip_db=True)
|
|
|
|
# SQLite should NOT have the message
|
|
rows = db.get_messages(session_id)
|
|
assert len(rows) == 0, f"Expected 0 DB rows with skip_db=True, got {len(rows)}"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: _last_flushed_db_idx initialization
|
|
# ---------------------------------------------------------------------------
|