galaxis-agent/tests/test_task_history.py
머니페니 db6e9b4a41 feat: add TaskHistory for completed task audit logging
Implements SQLite-based task history tracking with metrics (cost, duration, tokens).
- TaskHistory class with record() and get_recent() methods
- Tracks task_id, thread_id, issue_number, repo_name, source, status
- Records duration_seconds, tokens_input, tokens_output, cost_usd, error_message
- 4 passing tests covering completed/failed recording, ordering, empty state
2026-03-20 18:40:47 +09:00

70 lines
2.3 KiB
Python

import pytest
import os
import tempfile
from agent.task_history import TaskHistory
@pytest.fixture
async def history():
fd, db_path = tempfile.mkstemp(suffix=".db")
os.close(fd)
h = TaskHistory(db_path=db_path)
await h.initialize()
yield h
await h.close()
os.unlink(db_path)
@pytest.mark.asyncio
async def test_record_completed(history):
await history.record(
task_id="task-1", thread_id="thread-1", issue_number=42,
repo_name="galaxis-po", source="gitea", status="completed",
created_at="2026-03-20T10:00:00Z", completed_at="2026-03-20T10:05:00Z",
duration_seconds=300.0, tokens_input=5000, tokens_output=2000, cost_usd=0.045,
)
records = await history.get_recent(limit=10)
assert len(records) == 1
assert records[0]["task_id"] == "task-1"
assert records[0]["status"] == "completed"
@pytest.mark.asyncio
async def test_record_failed(history):
await history.record(
task_id="task-2", thread_id="thread-2", issue_number=10,
repo_name="galaxis-po", source="discord", status="failed",
created_at="2026-03-20T11:00:00Z", completed_at="2026-03-20T11:01:00Z",
duration_seconds=60.0, tokens_input=1000, tokens_output=500, cost_usd=0.01,
error_message="Agent crashed",
)
records = await history.get_recent(limit=10)
assert len(records) == 1
assert records[0]["error_message"] == "Agent crashed"
@pytest.mark.asyncio
async def test_get_recent_ordered(history):
await history.record(
task_id="task-1", thread_id="t1", issue_number=1, repo_name="r",
source="gitea", status="completed", created_at="2026-03-20T10:00:00Z",
completed_at="2026-03-20T10:05:00Z", duration_seconds=300,
tokens_input=100, tokens_output=50, cost_usd=0.001,
)
await history.record(
task_id="task-2", thread_id="t2", issue_number=2, repo_name="r",
source="gitea", status="completed", created_at="2026-03-20T11:00:00Z",
completed_at="2026-03-20T11:05:00Z", duration_seconds=300,
tokens_input=200, tokens_output=100, cost_usd=0.002,
)
records = await history.get_recent(limit=10)
assert len(records) == 2
assert records[0]["task_id"] == "task-2"
@pytest.mark.asyncio
async def test_empty_history(history):
records = await history.get_recent(limit=10)
assert records == []