from __future__ import annotations import base64 import pytest import requests from parser_client import ( ParserClient, ParserConfig, ParserServiceError, api_key_name_for_environment, build_payload, selected_environment, ) class FakeResponse: def __init__( self, status: int, body: dict, headers: dict | None = None, ) -> None: self.status_code = status self._body = body self.headers = headers or {} self.ok = 200 <= status < 300 def json(self) -> dict: return self._body class FakeSession: def __init__(self, responses: list[FakeResponse | Exception]) -> None: self.responses = list(responses) self.calls: list[dict] = [] def post(self, url: str, **kwargs): self.calls.append({"url": url, **kwargs}) response = self.responses.pop(0) if isinstance(response, Exception): raise response return response def config() -> ParserConfig: return ParserConfig( api_url="https://stg.api.cohere.test/v2/parse", api_key="secret-key", model="parse-v5.0", timeout=12, max_retries=3, ) def clear_cohere_environment(monkeypatch: pytest.MonkeyPatch) -> None: for name in ( "COHERE_ENV", "COHERE_API_KEY", "COHERE_STG_API_KEY", "COHERE_API_URL", ): monkeypatch.delenv(name, raising=False) def test_environment_defaults_to_staging(monkeypatch: pytest.MonkeyPatch) -> None: clear_cohere_environment(monkeypatch) monkeypatch.setenv("COHERE_STG_API_KEY", "staging-key") parsed = ParserConfig.from_environment() assert selected_environment() == "staging" assert api_key_name_for_environment() == "COHERE_STG_API_KEY" assert parsed.api_key == "staging-key" assert parsed.api_url == "https://stg.api.cohere.ai/v2/parse" def test_production_environment_uses_production_key_and_url( monkeypatch: pytest.MonkeyPatch, ) -> None: clear_cohere_environment(monkeypatch) monkeypatch.setenv("COHERE_ENV", "production") monkeypatch.setenv("COHERE_API_KEY", "production-key") monkeypatch.setenv("COHERE_STG_API_KEY", "staging-key") parsed = ParserConfig.from_environment() assert api_key_name_for_environment() == "COHERE_API_KEY" assert parsed.api_key == "production-key" assert parsed.api_url == "https://api.cohere.com/v2/parse" def test_api_url_can_override_the_selected_environment( monkeypatch: pytest.MonkeyPatch, ) -> None: clear_cohere_environment(monkeypatch) monkeypatch.setenv("COHERE_STG_API_KEY", "staging-key") monkeypatch.setenv("COHERE_API_URL", "https://gateway.example.test/v2/parse") parsed = ParserConfig.from_environment() assert parsed.api_url == "https://gateway.example.test/v2/parse" def test_environment_rejects_invalid_values(monkeypatch: pytest.MonkeyPatch) -> None: clear_cohere_environment(monkeypatch) monkeypatch.setenv("COHERE_ENV", "preview") with pytest.raises(ParserServiceError, match="staging.*production"): ParserConfig.from_environment() def test_environment_requires_its_matching_key( monkeypatch: pytest.MonkeyPatch, ) -> None: clear_cohere_environment(monkeypatch) monkeypatch.setenv("COHERE_ENV", "production") monkeypatch.setenv("COHERE_STG_API_KEY", "staging-key") with pytest.raises(ParserServiceError, match="configured for production"): ParserConfig.from_environment() def test_build_payload_uses_image_only_contract() -> None: payload = build_payload("parser-model", b"png-bytes") assert payload == { "model": "parser-model", "document": { "type": "image_url", "image_url": payload["document"]["image_url"], }, "output_format": "blocks", } data_url = payload["document"]["image_url"] assert data_url.startswith("data:image/png;base64,") assert base64.b64decode(data_url.split(",", 1)[1]) == b"png-bytes" def parse_response(content: str = "# Invoice") -> dict: return { "id": "parse-response-123", "pages": [ { "index": 0, "type": "blocks", "blocks": [ {"type": "text", "text": {"content": content}}, { "type": "table", "table": { "type": "html", "html": "
| Total |