From 2aebadb774c06a86d1bf520f5e0f6bcec66a27c2 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Mon, 15 Jun 2026 22:56:42 +0200 Subject: [PATCH] Enhance chat memory tests with helper function for sources and verify exclusion of internal memory contexts. --- backend/tests/test_chat_memory.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/backend/tests/test_chat_memory.py b/backend/tests/test_chat_memory.py index c044f40..84f2690 100644 --- a/backend/tests/test_chat_memory.py +++ b/backend/tests/test_chat_memory.py @@ -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"], "")