|
| 1 | +# AutoBot - AI-Powered Automation Platform |
| 2 | +# Copyright (c) 2025 mrveiss |
| 3 | +# Author: mrveiss |
| 4 | +"""Contract tests for ``tests.fixtures.mocks.make_llm_response`` (#7134). |
| 5 | +
|
| 6 | +Pins the canonical LLMResponse-stub factory's behavior so the 7+ ad-hoc |
| 7 | +test patterns that exist today can be migrated onto it with confidence. |
| 8 | +
|
| 9 | +Why a fixture-test exists at all: the factory's whole value comes from |
| 10 | +returning the **real** ``LLMResponse`` from ``llm_interface_pkg.models`` |
| 11 | +when importable. If a future field rename or type change happens on |
| 12 | +that dataclass, this test fails first — the migration target is broken, |
| 13 | +not silently producing wrong-shape mocks. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from tests.fixtures import make_llm_response |
| 21 | + |
| 22 | + |
| 23 | +def test_returns_real_llmresponse_when_importable() -> None: |
| 24 | + """The factory's primary value: returns the actual LLMResponse class |
| 25 | + so the field contract is pinned. If imports work in this environment, |
| 26 | + the result must be an instance of the real LLMResponse. |
| 27 | + """ |
| 28 | + try: |
| 29 | + from llm_interface_pkg.models import LLMResponse |
| 30 | + except ImportError: |
| 31 | + pytest.skip("llm_interface_pkg.models not importable — fallback path is fine") |
| 32 | + |
| 33 | + response = make_llm_response(content="hello") |
| 34 | + assert isinstance(response, LLMResponse) |
| 35 | + |
| 36 | + |
| 37 | +def test_default_content_is_empty_string() -> None: |
| 38 | + """Bare call returns a healthy, no-content response.""" |
| 39 | + response = make_llm_response() |
| 40 | + assert response.content == "" |
| 41 | + assert response.error is None |
| 42 | + |
| 43 | + |
| 44 | +def test_passes_through_all_documented_kwargs() -> None: |
| 45 | + response = make_llm_response( |
| 46 | + content="hello", |
| 47 | + error="rate limit", |
| 48 | + model="gpt-4", |
| 49 | + provider="openai", |
| 50 | + ) |
| 51 | + assert response.content == "hello" |
| 52 | + assert response.error == "rate limit" |
| 53 | + assert response.model == "gpt-4" |
| 54 | + assert response.provider == "openai" |
| 55 | + |
| 56 | + |
| 57 | +def test_keyword_only_args() -> None: |
| 58 | + """All args are keyword-only — positional would invite the same |
| 59 | + field-shape drift the function exists to prevent. |
| 60 | + """ |
| 61 | + with pytest.raises(TypeError): |
| 62 | + make_llm_response("hello") # type: ignore[misc] |
| 63 | + |
| 64 | + |
| 65 | +def test_error_field_supports_none_default() -> None: |
| 66 | + """The healthy default — ``error=None`` — must not be coerced to ``""``; |
| 67 | + callers like ``WorkflowDocumenter`` check ``if not response.error`` and |
| 68 | + an empty-string error would be indistinguishable from None for falsy |
| 69 | + checks but DIFFERENT from None for ``response.error is None`` checks. |
| 70 | + """ |
| 71 | + response = make_llm_response(content="ok") |
| 72 | + assert response.error is None |
| 73 | + # Falsy check works either way: |
| 74 | + assert not response.error |
| 75 | + |
| 76 | + |
| 77 | +def test_back_compat_underscore_alias_still_works() -> None: |
| 78 | + """``_build_mock_response(content)`` is the legacy entry point used |
| 79 | + inside ``MockLLMService.chat`` — keeping it functional avoids breaking |
| 80 | + the existing demo `__main__` blocks. |
| 81 | + """ |
| 82 | + from tests.fixtures.mocks import _build_mock_response |
| 83 | + |
| 84 | + response = _build_mock_response("hello") |
| 85 | + assert response.content == "hello" |
| 86 | + # Legacy alias still uses the documented "mock" defaults |
| 87 | + assert response.model == "mock" |
| 88 | + assert response.provider == "mock" |
| 89 | + |
| 90 | + |
| 91 | +def test_factory_is_re_exported_from_fixtures_package() -> None: |
| 92 | + """``from tests.fixtures import make_llm_response`` is the documented |
| 93 | + import path — pinning it prevents an accidental rename / removal. |
| 94 | + """ |
| 95 | + import tests.fixtures as fixtures |
| 96 | + |
| 97 | + assert "make_llm_response" in fixtures.__all__ |
| 98 | + assert callable(fixtures.make_llm_response) |
0 commit comments