Enhance chat memory tests with helper function for sources and verify exclusion of internal memory contexts.

This commit is contained in:
2026-06-15 22:56:42 +02:00
parent b5f90331e7
commit 2aebadb774

View File

@@ -1,5 +1,6 @@
from pathlib import Path
from datetime import datetime, timedelta
import json
import tempfile
import unittest
@@ -42,6 +43,21 @@ class ChatMemoryTests(unittest.TestCase):
db.flush()
return session
def _turn_with_sources(self, db, session_id, title, user_content, assistant_content, sources):
session = ChatSession(session_id=session_id, name=title)
db.add(session)
db.flush()
db.add(ChatMessage(session_pk=session.id, role="user", content=user_content))
db.flush()
db.add(ChatMessage(
session_pk=session.id,
role="assistant",
content=assistant_content,
sources_json=json.dumps(sources),
))
db.flush()
return session
def test_retrieves_completed_past_turns(self):
db = self.Session()
try:
@@ -147,3 +163,23 @@ class ChatMemoryTests(unittest.TestCase):
self.assertIn("song", context["context_block"].casefold())
self.assertIn("lied", context["context_block"].casefold())
def test_memory_only_answers_are_not_reindexed(self):
db = self.Session()
try:
self._turn_with_sources(
db,
"poison",
"Backfire Basics",
"erzähl mir über backfire basics",
"Backfire Basics is a hallucinated project description.",
[{"type": "chat_memory", "source_session_id": "older", "source_message_id": "m1"}],
)
db.commit()
sync_all_chat_memory(db)
context = build_chat_memory_context(db, "backfire basics")
finally:
db.close()
self.assertEqual(context["context_block"], "")