Private
Public Access
0
0
Files
manual_slop/tests/test_tier4_patch_generation.py
T
ed 51c054ece8 refactor(ai_client): remove top-level SDK imports; use _require_warmed
Phase 3 T3.2 + T3.3 of startup_speedup_20260606 track.

The 5 heavy SDKs (anthropic, google.genai, openai, google.genai.types,
requests) are no longer imported at module level. Each function that
needs them now calls _require_warmed(name) to get the module from
sys.modules (populated by AppController's warmup on _io_pool).

This is the load-bearing wall of the Main Thread Purity Invariant:
heavy modules are never in the main thread's import chain.

run_discussion_compression now uses _require_warmed for both
google.genai.types (gemini branch) and requests (deepseek branch).

Tests/test_tier4_patch_generation.py adapted: the 2 tests that
mocked 'src.ai_client.types' (no longer a module-level attr)
now mock 'src.ai_client._require_warmed' (the new public mechanism).

T3.1 tests now pass (9/9). T3.3 breakage fixed.
All 25 ai_client + tier4 tests pass.
2026-06-06 16:09:16 -04:00

69 lines
3.0 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from src import ai_client
from src import mma_prompts
def test_tier4_patch_prompt_exists() -> None:
"""Test that TIER4_PATCH_PROMPT is defined in mma_prompts.py."""
assert hasattr(mma_prompts, "TIER4_PATCH_PROMPT")
prompt = mma_prompts.TIER4_PATCH_PROMPT
assert "unified diff" in prompt.lower() or "diff -u" in prompt.lower()
assert "---" in prompt
assert "+++" in prompt
assert "@@" in prompt
def test_tier4_patch_prompt_format_instructions() -> None:
"""Test that the patch prompt includes format instructions for unified diff."""
prompt = mma_prompts.TIER4_PATCH_PROMPT
assert "--- a/" in prompt or "---" in prompt
assert "+++ b/" in prompt or "+++" in prompt
def test_run_tier4_patch_generation_exists() -> None:
"""Test that run_tier4_patch_generation function exists in ai_client."""
assert hasattr(ai_client, "run_tier4_patch_generation")
assert callable(ai_client.run_tier4_patch_generation)
def test_run_tier4_patch_generation_empty_error() -> None:
"""Test that run_tier4_patch_generation returns empty string on empty error."""
with patch("src.ai_client._ensure_gemini_client"), \
patch("src.ai_client._gemini_client") as mock_client:
mock_resp = MagicMock()
mock_resp.text = ""
mock_client.models.generate_content.return_value = mock_resp
result = ai_client.run_tier4_patch_generation("", "file context")
assert result == ""
def test_run_tier4_patch_generation_calls_ai() -> None:
"""Test that run_tier4_patch_generation calls the AI with the correct prompt."""
mock_types = MagicMock()
mock_types.GenerateContentConfig = MagicMock()
with patch("src.ai_client._ensure_gemini_client"), \
patch("src.ai_client._gemini_client", create=True) as mock_client, \
patch("src.ai_client._require_warmed", return_value=mock_types):
mock_resp = MagicMock()
mock_resp.text = "--- a/test.py\n+++ b/test.py\n@@ -1 +1 @@\n-old\n+new"
mock_client.models.generate_content.return_value = mock_resp
error = "TypeError: unsupported operand"
file_context = "def foo():\n pass"
result = ai_client.run_tier4_patch_generation(error, file_context)
mock_client.models.generate_content.assert_called()
def test_run_tier4_patch_generation_returns_diff() -> None:
"""Test that run_tier4_patch_generation returns diff text."""
mock_types = MagicMock()
mock_types.GenerateContentConfig = MagicMock()
with patch("src.ai_client._ensure_gemini_client"), \
patch("src.ai_client._gemini_client", create=True) as mock_client, \
patch("src.ai_client._require_warmed", return_value=mock_types):
expected_diff = "--- a/src/test.py\n+++ b/src/test.py\n@@ -10,5 +10,6 @@\n def test_func():\n- old_value = 1\n+ old_value = 1\n+ new_value = 2"
mock_resp = MagicMock()
mock_resp.text = expected_diff
mock_client.models.generate_content.return_value = mock_resp
result = ai_client.run_tier4_patch_generation("error", "context")
assert "---" in result
assert "+++" in result
assert "@@" in result