""" F93 — F6a-Fix: JSON-Parse-Fehler in _call_generate → success=False. Rainer-QG Round 1 (Befund F6a): json.JSONDecodeError/TypeError/ValueError wurden fälschlich als success=True gewertet. Semantisch korrekt ist success=False, damit ein kaputtes Modell/Endpoint den Eskalations-Streak triggert (Spec: Eskalation bei "Modell-/Endpoint-Problem"). Diese Tests verifizieren, dass der Parse-Fehler-Pfad in ``OllamaLLMClient._call_generate`` den adaptiven Timeout mit ``success=False`` speist (und nicht mehr ``success=True``). """ from __future__ import annotations import json import sys from pathlib import Path from unittest import mock import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[2])) from plugin.archivar import classifier as classifier_mod from plugin.archivar.classifier import OllamaLLMClient class _FakeResponse: """Context-Manager-Response, dessen read() einen gegebenen Body liefert.""" def __init__(self, body: bytes): self._body = body def __enter__(self): return self def __exit__(self, *exc): return False def read(self) -> bytes: return self._body def _make_client(): """OllamaLLMClient ohne echten Netzwerkzugriff (nur für _call_generate).""" return OllamaLLMClient(base_url="http://localhost:11434", model="qwen2.5:7b") class TestF6aParseErrorRecordsFailure: def test_json_decode_error_records_failure(self): """JSONDecodeError (Server liefert Müll) → success=False.""" client = _make_client() # urlopen liefert ungültiges JSON → json.loads wirft JSONDecodeError. with mock.patch.object( classifier_mod.urllib.request, "urlopen", return_value=_FakeResponse(b"not-json{{{"), ), mock.patch.object( classifier_mod.adaptive_timeout, "current_timeout", return_value=30.0 ), mock.patch.object( classifier_mod.adaptive_timeout, "record" ) as mock_record: result = client._call_generate("test prompt") assert result is None # record wurde mit success=False aufgerufen (F6a-Fix). mock_record.assert_called_once() _, kwargs = mock_record.call_args assert kwargs["success"] is False def test_type_error_records_failure(self): """TypeError (Verarbeitungsfehler) → success=False.""" client = _make_client() # json.loads wirft TypeError (z.B. wenn resp.read() kein str/bytes liefert). with mock.patch.object( classifier_mod.urllib.request, "urlopen", return_value=_FakeResponse(b"{}"), ), mock.patch.object( classifier_mod.adaptive_timeout, "current_timeout", return_value=30.0 ), mock.patch.object( classifier_mod.json, "loads", side_effect=TypeError("bad type") ), mock.patch.object( classifier_mod.adaptive_timeout, "record" ) as mock_record: result = client._call_generate("test prompt") assert result is None mock_record.assert_called_once() _, kwargs = mock_record.call_args assert kwargs["success"] is False def test_value_error_records_failure(self): """ValueError (z.B. falsches Format) → success=False.""" client = _make_client() # json.loads wirft ValueError bei ungültigem JSON-Format. with mock.patch.object( classifier_mod.urllib.request, "urlopen", return_value=_FakeResponse(b"{}"), ), mock.patch.object( classifier_mod.adaptive_timeout, "current_timeout", return_value=30.0 ), mock.patch.object( classifier_mod.json, "loads", side_effect=ValueError("bad value") ), mock.patch.object( classifier_mod.adaptive_timeout, "record" ) as mock_record: result = client._call_generate("test prompt") assert result is None mock_record.assert_called_once() _, kwargs = mock_record.call_args assert kwargs["success"] is False def test_valid_json_records_success(self): """Gegenprobe: gültiges JSON → success=True (kein Regression).""" client = _make_client() valid_body = json.dumps({"response": "direct_question"}).encode("utf-8") with mock.patch.object( classifier_mod.urllib.request, "urlopen", return_value=_FakeResponse(valid_body), ), mock.patch.object( classifier_mod.adaptive_timeout, "current_timeout", return_value=30.0 ), mock.patch.object( classifier_mod.adaptive_timeout, "record" ) as mock_record: result = client._call_generate("test prompt") assert result == "direct_question" mock_record.assert_called_once() _, kwargs = mock_record.call_args assert kwargs["success"] is True