"""Live (not mocked) crash/fail detection (PAPER-95).

The supervisor's headline claim is crash isolation: a test that segfaults
must be recorded `crashed` and the run must continue. The unit visibility
test mocks the JSONL; this drives the REAL supervisor on a temp tree with a
genuinely crashing / failing test and asserts the recorded outcome. Faster
than the full `--self-check` (no 120s hang scenario), so it runs per-PR.
"""

from __future__ import annotations

import sys
from pathlib import Path

import pytest

from mgf.test_supervisor.self_check import _run_canned

REPO_ROOT = Path(__file__).resolve().parents[2]
PYTEST_CMD = [sys.executable, "-m", "pytest"]


@pytest.mark.regression
def test_live_crash_recorded_as_crashed() -> None:
    """A test that fakes a SIGSEGV (os._exit(139)) is recorded `crashed`."""
    ok = _run_canned(
        REPO_ROOT,
        PYTEST_CMD,
        "test_canned_crash.py",
        "import os\ndef test_crash():\n    os._exit(139)\n",
        "crashed",
        None,
        verbose=False,
    )
    assert ok


@pytest.mark.regression
def test_live_failure_recorded_as_failed() -> None:
    """A plain assertion failure is recorded `failed` with the message."""
    ok = _run_canned(
        REPO_ROOT,
        PYTEST_CMD,
        "test_canned_fail.py",
        'def test_fail():\n    assert 2 + 2 == 5, "deliberate self-test failure"\n',
        "failed",
        "deliberate self-test failure",
        verbose=False,
    )
    assert ok
