from __future__ import annotations

import httpx
import pytest

from terminaluse.core.parse_error import ParsingError
from terminaluse.raw_client import RawTerminalUseBase


class _StubHttpClient:
    def request(self, *args, **kwargs) -> httpx.Response:  # type: ignore[no-untyped-def]
        return httpx.Response(
            status_code=200,
            headers={"content-type": "application/json", "x-test-header": "present"},
            json={"unexpected": "payload"},
        )


class _StubClientWrapper:
    def __init__(self) -> None:
        self.httpx_client = _StubHttpClient()


def test_raw_client_raises_parsing_error_for_malformed_success_response() -> None:
    client = RawTerminalUseBase(client_wrapper=_StubClientWrapper())  # type: ignore[arg-type]

    with pytest.raises(ParsingError) as exc_info:
        client.search(q="demo")

    error = exc_info.value
    assert error.status_code == 200
    assert error.body == {"unexpected": "payload"}
    assert error.headers is not None
    assert error.headers["x-test-header"] == "present"
    assert error.cause is not None
