# SPDX-License-Identifier: AGPL-3.0-or-later
"""The phase5-run-entry-fixes-5 review's committed probes (J-class).

F36's RecursionEscape face (a deeply-nested YAML document escapes
``load_run_spec`` raw as ``RecursionError``, falsifying ``RunSpecError``'s
"one named failure, never a run-time surprise" docstring) and F37's
``UnicodeDecodeError`` pin (the file-decode face of the Loader-decode refusal
shape, unpinned by the absorbed probe which drives the ``ParserError`` face
only).
"""

import pytest

from orchestrator import run_entry

pytestmark = pytest.mark.slow


def test_review_f36_deeply_nested_spec_refused_as_a_named_run_spec_error(tmp_path):
    """F36: a deeply-nested YAML document raises RecursionError inside
    yaml.safe_load, which escapes load_run_spec raw — not the documented named
    RunSpecError.  Red at FINAL; the next step's loader widening (or the
    sentence's re-scoping) absorbs this green."""
    bad = tmp_path / "DEEP.yaml"
    bad.write_bytes(b"run_id: " + b"[" * 2000 + b"]" * 2000 + b"\n")
    try:
        run_entry.load_run_spec(str(bad))
    except run_entry.RunSpecError:
        return  # the documented named refusal shape — after the widening
    except Exception as error:  # noqa: BLE001
        pytest.fail(
            f"F36: a deeply-nested spec file is refused as "
            f"{type(error).__name__}, not the documented named `RunSpecError`: "
            f"{error!r}"
        )
    pytest.fail("F36: the loader accepted a deeply-nested spec file")


def test_review_f37_invalid_utf8_spec_refused_as_a_named_run_spec_error(tmp_path):
    """F37: the file-decode face of the Loader-decode refusal shape — a spec
    file whose bytes are not valid UTF-8 is refused as a named RunSpecError,
    not a raw UnicodeDecodeError.  Green at FINAL; the pin exists because the
    absorbed probe drives the ParserError face only and the drop-
    UnicodeDecodeError mutant leaves the committed suite green without it."""
    bad = tmp_path / "BAD_UTF8.yaml"
    bad.write_bytes(b"run_id: \xff\xfe\n")
    try:
        run_entry.load_run_spec(str(bad))
    except run_entry.RunSpecError:
        return  # the documented named refusal shape
    except Exception as error:  # noqa: BLE001
        pytest.fail(
            f"F37: an invalid-UTF-8 spec file is refused as "
            f"{type(error).__name__}, not the documented named `RunSpecError`: "
            f"{error!r}"
        )
    pytest.fail("F37: the loader accepted an invalid-UTF-8 spec file")
