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

The whole-face forcing's own inventory stays incomplete: the safe
constructor's typed-scalar tag conversions leak plain Python exceptions out of
``yaml.safe_load`` for malformed typed tags (``!!int``/``!!float`` ->
``ValueError``, ``!!timestamp`` -> ``ValueError`` and ``AttributeError``,
``!!bool`` -> ``KeyError``), and none of the delivered tuple's classes catch
them — the raw class escapes ``load_run_spec`` and the documented CLI head
dies on its traceback instead of the named refusal.  Red at FINAL; a later
loader widening (or the sentence's re-scoping) absorbs these green with no
test edit, like F27's, F28's, F34's, F36's, and F37's probes before them.
"""

import pytest

from orchestrator import run_entry

pytestmark = pytest.mark.slow


def test_review_f38_int_float_typed_scalar_refused_as_a_named_run_spec_error(tmp_path):
    """F38: a malformed typed scalar (``!!int``/``!!float``) raises a raw
    ValueError out of yaml.safe_load, which the delivered tuple does not
    catch — not the documented named RunSpecError."""
    for text, label in (
        ("run_id: !!int 'garbage'\n", "int"),
        ("run_id: !!float 'garbage'\n", "float"),
    ):
        try:
            run_entry.load_run_spec(str(_write(tmp_path, label, text)))
        except run_entry.RunSpecError:
            continue
        except Exception as error:  # noqa: BLE001
            pytest.fail(
                f"F38: an ill-typed {label} scalar is refused as "
                f"{type(error).__name__}, not the documented named `RunSpecError`: "
                f"{error!r}"
            )
        pytest.fail(f"F38: the loader accepted an ill-typed {label} scalar")


def test_review_f39_timestamp_typed_scalar_refused_as_a_named_run_spec_error(tmp_path):
    """F39: a malformed ``!!timestamp`` scalar raises a raw AttributeError or
    ValueError out of yaml.safe_load — not the documented named refusal."""
    for text, label in (
        ("run_id: !!timestamp 'nope'\n", "nope"),
        ("run_id: !!timestamp '2020-13-45T99:99:99Z'\n", "out-of-range"),
    ):
        try:
            run_entry.load_run_spec(str(_write(tmp_path, label, text)))
        except run_entry.RunSpecError:
            continue
        except Exception as error:  # noqa: BLE001
            pytest.fail(
                f"F39: an ill-typed timestamp scalar ({label}) is refused as "
                f"{type(error).__name__}, not the documented named `RunSpecError`: "
                f"{error!r}"
            )
        pytest.fail(f"F39: the loader accepted an ill-typed timestamp scalar ({label})")


def test_review_f40_boolean_typed_scalar_refused_as_a_named_run_spec_error(tmp_path):
    """F40: a malformed ``!!bool`` scalar raises a raw KeyError out of
    yaml.safe_load — not the documented named refusal."""
    try:
        run_entry.load_run_spec(str(_write(tmp_path, "bool", "run_id: !!bool 'garbage'\n")))
    except run_entry.RunSpecError:
        return  # the documented named refusal shape — after the widening
    except Exception as error:  # noqa: BLE001
        pytest.fail(
            f"F40: an ill-typed bool scalar is refused as "
            f"{type(error).__name__}, not the documented named `RunSpecError`: "
            f"{error!r}"
        )
    pytest.fail("F40: the loader accepted an ill-typed bool scalar")


def _write(tmp_path, name, text):
    path = tmp_path / f"{name}.yaml"
    path.write_text(text)
    return path
