49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""Unit tests for conversation history and topic isolation."""
|
|
|
|
from google.genai import types
|
|
from homeagent.agent.conversation import ConversationManager
|
|
|
|
|
|
def test_topic_isolation_and_persistence(tmp_path):
|
|
mgr = ConversationManager(data_dir=tmp_path, max_turns=5)
|
|
|
|
chat_id = 100
|
|
topic_1 = 10
|
|
topic_2 = 20
|
|
|
|
# Add turn to topic 1
|
|
content_t1 = types.Content(
|
|
role="user",
|
|
parts=[types.Part.from_text(text="Question in Topic 1")],
|
|
)
|
|
mgr.append_content(chat_id, topic_1, content_t1)
|
|
|
|
# Add turn to topic 2
|
|
content_t2 = types.Content(
|
|
role="user",
|
|
parts=[types.Part.from_text(text="Question in Topic 2")],
|
|
)
|
|
mgr.append_content(chat_id, topic_2, content_t2)
|
|
|
|
# Verify topic 1 only has topic 1 history
|
|
h1 = mgr.get_history(chat_id, topic_1)
|
|
assert len(h1) == 1
|
|
assert h1[0].parts[0].text == "Question in Topic 1"
|
|
|
|
# Verify topic 2 only has topic 2 history
|
|
h2 = mgr.get_history(chat_id, topic_2)
|
|
assert len(h2) == 1
|
|
assert h2[0].parts[0].text == "Question in Topic 2"
|
|
|
|
# Create new instance (simulating restart) to test disk persistence
|
|
mgr_restarted = ConversationManager(data_dir=tmp_path, max_turns=5)
|
|
loaded_h1 = mgr_restarted.get_history(chat_id, topic_1)
|
|
assert len(loaded_h1) == 1
|
|
assert loaded_h1[0].parts[0].text == "Question in Topic 1"
|
|
|
|
# Clear topic 1
|
|
mgr_restarted.clear(chat_id, topic_1)
|
|
assert len(mgr_restarted.get_history(chat_id, topic_1)) == 0
|
|
# Topic 2 remains intact
|
|
assert len(mgr_restarted.get_history(chat_id, topic_2)) == 1
|