galaxis-agent/tests/test_message_store.py

63 lines
1.8 KiB
Python
Raw Normal View History

"""Tests for MessageStore."""
import os
import tempfile
import pytest
from agent.message_store import MessageStore
@pytest.fixture
async def store():
"""Create a temporary MessageStore for testing."""
fd, db_path = tempfile.mkstemp(suffix=".db")
os.close(fd)
s = MessageStore(db_path=db_path)
await s.initialize()
yield s
await s.close()
os.unlink(db_path)
@pytest.mark.asyncio
async def test_push_and_get_messages(store):
"""메시지를 push하고 get한다."""
await store.push_message("thread-1", {"role": "human", "content": "추가 요청"})
messages = await store.get_messages("thread-1")
assert len(messages) == 1
assert messages[0]["content"] == "추가 요청"
@pytest.mark.asyncio
async def test_consume_messages(store):
"""메시지를 소비하면 삭제된다."""
await store.push_message("thread-1", {"role": "human", "content": "msg1"})
await store.push_message("thread-1", {"role": "human", "content": "msg2"})
messages = await store.consume_messages("thread-1")
assert len(messages) == 2
remaining = await store.get_messages("thread-1")
assert len(remaining) == 0
@pytest.mark.asyncio
async def test_no_messages_returns_empty(store):
"""메시지가 없으면 빈 리스트를 반환한다."""
messages = await store.get_messages("thread-999")
assert messages == []
@pytest.mark.asyncio
async def test_messages_isolated_by_thread(store):
"""스레드별로 메시지가 격리된다."""
await store.push_message("thread-1", {"content": "for thread 1"})
await store.push_message("thread-2", {"content": "for thread 2"})
msgs1 = await store.get_messages("thread-1")
msgs2 = await store.get_messages("thread-2")
assert len(msgs1) == 1
assert len(msgs2) == 1
assert msgs1[0]["content"] == "for thread 1"