Private
Public Access
0
0
Files
manual_slop/tests/test_grok_provider.py
T
ed d7c6d67f69 feat(ai_client): wire v2 matrix fields into old vendor send functions
The matrix has v2 fields (reasoning, web_search, x_search)
populated for the old vendors (minimax-M2.5/M2.7, grok-*),
but the send functions didn't consult them. This commit
makes the code path actually USE the matrix:

  _send_minimax: gate reasoning_extractor on caps.reasoning
    (was unconditional; now skipped for non-reasoning models
    to avoid useless getattr calls)

  _send_grok: populate OpenAICompatibleRequest.extra_body with
    search_parameters when caps.web_search or caps.x_search is
    True. caps.web_search -> {mode: auto}; caps.x_search ->
    {sources: [{type: x}]} per the xAI Live Search spec

  OpenAICompatibleRequest: added extra_body field. Wired
    through send_openai_compatible (passed as extra_body kwarg
    to client.chat.completions.create).

Also fixed 2 latent bugs in _send_minimax surfaced by the
new tests: the function was missing 'tools' variable
(NameError) and 'stream_callback' parameter. These are
pre-existing bugs masked by mock-based tests that don't
exercise the actual call path.

Also cancelled t5_6/7/8 (the invented 'deferred tool-loop
conversion' work). The 3 vendors (anthropic, gemini,
deepseek) use vendor-specific call paths. Their inline
loops are NOT defects. The '3-5 days' / '1-2 weeks'
estimates were made up by the agent. The audit script's
DEFERRED_VENDORS exclusion is permanent.

Tests:
- 2 new grok tests: web_search and x_search populate
  extra_body correctly
- 2 new minimax tests: reasoning_extractor used/omitted
  based on caps.reasoning
- 122/122 vendor+tool+provider+import-isolation tests pass
  (no regressions; +4 new tests this commit)
- 3 audit scripts pass
2026-06-11 22:27:42 -04:00

57 lines
2.9 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from src import ai_client
@pytest.fixture(autouse=True)
def _reset_grok_state():
if hasattr(ai_client, '_grok_client'):
ai_client._grok_client = None
if hasattr(ai_client, '_grok_history'):
ai_client._grok_history = []
yield
def test_send_grok_uses_xai_endpoint(monkeypatch: pytest.MonkeyPatch) -> None:
ai_client.set_provider("grok", "grok-2")
mock_client = MagicMock()
mock_client.chat.completions.create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content="hi from grok", tool_calls=[]))],
usage=MagicMock(prompt_tokens=10, completion_tokens=5),
)
with patch("src.ai_client._ensure_grok_client", return_value=mock_client):
result = ai_client._send_grok("system", "user", ".", None, "", False, None, None, None)
assert result == "hi from grok"
assert mock_client.chat.completions.create.called
def test_grok_2_vision_supports_image() -> None:
from src.vendor_capabilities import get_capabilities
caps = get_capabilities("grok", "grok-2-vision")
assert caps.vision is True
def test_grok_web_search_adds_search_parameters_to_extra_body() -> None:
"""caps.web_search=True should populate search_parameters.mode=auto in extra_body."""
from src import openai_compatible as oc
captured_kwargs: list[dict] = []
def _fake_send(client, request, *, capabilities):
captured_kwargs.append({"extra_body": request.extra_body, "model": request.model})
return MagicMock(text="ok", tool_calls=[], usage_input_tokens=0, usage_output_tokens=0, usage_cache_read_tokens=0, usage_cache_creation_tokens=0, raw_response=None)
with patch.object(oc, "send_openai_compatible", side_effect=_fake_send), \
patch("src.ai_client._ensure_grok_client", return_value=MagicMock()), \
patch("src.ai_client._get_deepseek_tools", return_value=[]):
ai_client._send_grok("system", "user", ".", None, "", False, None, None, None)
assert len(captured_kwargs) == 1
eb = captured_kwargs[0]["extra_body"]
assert eb is not None
assert eb["search_parameters"]["mode"] == "auto"
def test_grok_x_search_adds_x_source_to_extra_body() -> None:
"""caps.x_search=True should add sources=[{type:x}] to search_parameters."""
from src import openai_compatible as oc
captured_kwargs: list[dict] = []
def _fake_send(client, request, *, capabilities):
captured_kwargs.append({"extra_body": request.extra_body})
return MagicMock(text="ok", tool_calls=[], usage_input_tokens=0, usage_output_tokens=0, usage_cache_read_tokens=0, usage_cache_creation_tokens=0, raw_response=None)
with patch.object(oc, "send_openai_compatible", side_effect=_fake_send), \
patch("src.ai_client._ensure_grok_client", return_value=MagicMock()), \
patch("src.ai_client._get_deepseek_tools", return_value=[]):
ai_client._send_grok("system", "user", ".", None, "", False, None, None, None)
assert captured_kwargs[0]["extra_body"]["search_parameters"]["sources"] == [{"type": "x"}]