Files
Timmy-time-dashboard/tests/timmy/test_paperclip.py
Alexander Whitestone c45f78171b WIP: Gemini Code progress on #978
Automated salvage commit — agent session ended (exit 124).
Work in progress, may need continuation.
2026-03-23 14:10:08 -04:00

107 lines
3.6 KiB
Python

"""Tests for the Paperclip integration."""
from __future__ import annotations
import asyncio
from unittest.mock import AsyncMock, patch
import pytest
from timmy.paperclip import (
PaperclipClient,
PaperclipPoller,
PaperclipTask,
ResearchOrchestrator,
)
@pytest.fixture
def mock_settings(monkeypatch):
"""Mock the settings for the Paperclip integration."""
monkeypatch.setattr("timmy.paperclip.settings.paperclip_enabled", True)
monkeypatch.setattr("timmy.paperclip.settings.paperclip_url", "http://localhost:3100")
monkeypatch.setattr("timmy.paperclip.settings.paperclip_api_key", "test-key")
monkeypatch.setattr("timmy.paperclip.settings.paperclip_agent_id", "test-agent")
monkeypatch.setattr("timmy.paperclip.settings.paperclip_company_id", "test-company")
monkeypatch.setattr("timmy.paperclip.settings.paperclip_poll_interval", 1)
monkeypatch.setattr("timmy.paperclip.settings.gitea_url", "http://localhost:3000")
monkeypatch.setattr("timmy.paperclip.settings.gitea_token", "test-token")
monkeypatch.setattr("timmy.paperclip.settings.gitea_repo", "test/repo")
@pytest.mark.asyncio
async def test_paperclip_client_get_tasks(mock_settings, httpx_mock):
"""Test that the Paperclip client can get tasks."""
httpx_mock.add_response(
url="http://localhost:3100/api/tasks?agent_id=test-agent&company_id=test-company&status=queued",
json=[
{
"id": "1",
"kind": "research",
"context": {"issue_number": 123},
}
],
)
client = PaperclipClient()
tasks = await client.get_tasks()
assert len(tasks) == 1
assert tasks[0].id == "1"
assert tasks[0].kind == "research"
assert tasks[0].context == {"issue_number": 123}
@pytest.mark.asyncio
async def test_paperclip_client_update_task_status(mock_settings, httpx_mock):
"""Test that the Paperclip client can update a task's status."""
httpx_mock.add_response(
url="http://localhost:3100/api/tasks/1",
method="PATCH",
)
client = PaperclipClient()
await client.update_task_status("1", "running")
@pytest.mark.asyncio
async def test_research_orchestrator_run(mock_settings, httpx_mock):
"""Test that the ResearchOrchestrator can run a research task."""
httpx_mock.add_response(
url="http://localhost:3000/api/v1/repos/test/repo/issues/123",
json={"title": "Test Issue", "body": "This is a test issue."},
)
httpx_mock.add_response(
url="http://localhost:3000/api/v1/repos/test/repo/issues/123/comments",
method="POST",
)
with patch("timmy.paperclip.triage_research_report", new_callable=AsyncMock) as mock_triage:
mock_triage.return_value = []
orchestrator = ResearchOrchestrator()
result = await orchestrator.run({"issue_number": 123})
assert result == "Research complete for issue #123"
@pytest.mark.asyncio
async def test_paperclip_poller_poll(mock_settings):
"""Test that the Paperclip poller can poll for tasks."""
with patch("timmy.paperclip.PaperclipClient.get_tasks", new_callable=AsyncMock) as mock_get_tasks:
mock_get_tasks.return_value = [
PaperclipTask(
id="1",
kind="research",
context={"issue_number": 123},
)
]
with patch("timmy.paperclip.PaperclipPoller.run_research_task", new_callable=AsyncMock):
poller = PaperclipPoller()
poller.poll_interval = 0.1
task = asyncio.create_task(poller.poll())
await asyncio.sleep(0.2)
task.cancel()
mock_get_tasks.assert_called()