"""llm.complete: `max_tokens` and `timeout` are threaded into the backend call. The OpenAI client is faked so nothing hits a network. We assert the generation cap reaches the create() call and the fast-fail timeout reaches the client (with max_retries=0 so summary.py owns the retry policy, not the SDK). """ from __future__ import annotations import types import pytest from lyra import llm @pytest.fixture def fake_openai(monkeypatch): recorded: dict = {} class FakeCompletions: def create(self, **kwargs): recorded["create"] = kwargs msg = types.SimpleNamespace(content="ok") return types.SimpleNamespace(choices=[types.SimpleNamespace(message=msg)]) class FakeClient: def __init__(self, **kwargs): recorded["client"] = kwargs self.chat = types.SimpleNamespace(completions=FakeCompletions()) monkeypatch.setattr(llm, "OpenAI", FakeClient) monkeypatch.setattr(llm, "load", lambda: types.SimpleNamespace( mi50_base_url="http://mi50/v1", mi50_model="local-gpu", cloud_model="gpt-4o-mini", openai_api_key="sk-test", local_model="l", )) return recorded def test_mi50_threads_max_tokens_and_timeout(fake_openai): out = llm.complete([{"role": "user", "content": "hi"}], backend="mi50", max_tokens=768, timeout=150) assert out == "ok" assert fake_openai["create"]["max_tokens"] == 768 assert fake_openai["client"]["timeout"] == 150 assert fake_openai["client"]["max_retries"] == 0 def test_cloud_threads_max_tokens_and_timeout(fake_openai): llm.complete([{"role": "user", "content": "hi"}], backend="cloud", max_tokens=768, timeout=150) assert fake_openai["create"]["max_tokens"] == 768 assert fake_openai["client"]["timeout"] == 150 assert fake_openai["client"]["max_retries"] == 0 def test_default_bounds_calls_even_without_explicit_timeout(fake_openai): # No cap / timeout passed -> still bounded: 300s default + no SDK retries, so # no call can silently inherit the SDK's 600s x2 (~30 min). No length cap # unless asked, though. llm.complete([{"role": "user", "content": "hi"}], backend="mi50") assert "max_tokens" not in fake_openai["create"] assert fake_openai["client"]["timeout"] == 300 assert fake_openai["client"]["max_retries"] == 0