auto-commit-service/tests/test_llm_client.py

74 lines
2.8 KiB
Python

"""Tests for LLM client module."""
import pytest
from unittest.mock import AsyncMock, patch
from auto_commit_service.git.diff_parser import DiffSummary
from auto_commit_service.llm.client import (
LlamaCommitClient,
LlamaServiceError,
LlamaServiceUnavailable,
)
class TestLlamaCommitClient:
"""Tests for LlamaCommitClient class."""
@pytest.fixture
def client(self) -> LlamaCommitClient:
"""Create a test client."""
return LlamaCommitClient(
base_url="http://localhost:8000",
model="fast",
timeout=5.0,
)
async def test_health_check_unavailable(self, client: LlamaCommitClient) -> None:
"""Test health check when service is unavailable."""
# No mock server, should fail
health = await client.health_check()
assert health["status"] == "error"
assert not health["fast_model_loaded"]
async def test_is_available_when_down(self, client: LlamaCommitClient) -> None:
"""Test is_available when service is down."""
available = await client.is_available()
assert not available
def test_clean_response_removes_quotes(self, client: LlamaCommitClient) -> None:
"""Test that clean_response removes surrounding quotes."""
result = client._clean_response('"✨ Add new feature"')
assert result == "✨ Add new feature"
def test_clean_response_removes_code_blocks(self, client: LlamaCommitClient) -> None:
"""Test that clean_response removes markdown code blocks."""
result = client._clean_response("```\n✨ Add new feature\n```")
assert result == "✨ Add new feature"
def test_clean_response_takes_first_line(self, client: LlamaCommitClient) -> None:
"""Test that clean_response takes only first line."""
result = client._clean_response("✨ Add feature\nThis is extra text")
assert result == "✨ Add feature"
def test_clean_response_adds_emoji_if_missing(self, client: LlamaCommitClient) -> None:
"""Test that clean_response adds emoji for known types."""
result = client._clean_response("Add new authentication")
assert result.startswith("")
def test_clean_response_preserves_valid_emoji(self, client: LlamaCommitClient) -> None:
"""Test that valid emoji prefixes are preserved."""
result = client._clean_response("🔧 Update dependencies")
assert result == "🔧 Update dependencies"
class TestClientContextManager:
"""Tests for async context manager."""
async def test_context_manager(self) -> None:
"""Test using client as async context manager."""
async with LlamaCommitClient() as client:
assert client._client is not None
# After exit, client should be closed
assert client._client is None or client._client.is_closed