35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from conversation_manager import ConversationManager
|
|
|
|
|
|
def test_conversation_manager_persists_history(tmp_path: Path) -> None:
|
|
manager = ConversationManager(storage_dir=tmp_path, conversation_id="test")
|
|
manager.append_message("user", "Hello there!")
|
|
manager.append_message("assistant", "General Kenobi.")
|
|
|
|
conversation_file = tmp_path / "test.json"
|
|
assert conversation_file.exists()
|
|
|
|
data = json.loads(conversation_file.read_text(encoding="utf-8"))
|
|
assert len(data["messages"]) == 2
|
|
assert data["messages"][0]["content"] == "Hello there!"
|
|
|
|
reloaded = ConversationManager(storage_dir=tmp_path, conversation_id="test")
|
|
assert [msg["content"] for msg in reloaded.messages] == [
|
|
"Hello there!",
|
|
"General Kenobi.",
|
|
]
|
|
|
|
|
|
def test_conversation_manager_rejects_invalid_role(tmp_path: Path) -> None:
|
|
manager = ConversationManager(storage_dir=tmp_path, conversation_id="invalid")
|
|
|
|
with pytest.raises(ValueError):
|
|
manager.append_message("narrator", "This should fail.")
|