# SPDX-License-Identifier: AGPL-3.0-or-later
"""R08: the hidden evaluator must not share an interpreter with candidate code.

A hidden check runs as a trusted collector that spawns the candidate driver in a
child process; the expected values live only controller-side. These tests pin the
observable consequences: an early exit, output suppression, or a wrong result
cannot be graded PASS, and the expected values are not present in the sandbox.
"""
from __future__ import annotations

import os
import shutil
import tempfile

import pytest

from orchestrator import experiment, oracle, sandbox


def _caps():
    found = sandbox.capabilities()
    if not found.profile_available:
        pytest.fail(f"NAMESPACE_V1 unavailable (fail-closed gate): {found.unavailable_reason()}")
    return found


def _solution_tree(workdir: str, *, parse_source: str | None = None) -> str:
    target = experiment.build_fixed_target(workdir)
    base = experiment._materialise(target.repository, target.commit, workdir, "base")
    tree = os.path.join(workdir, "candidate")
    shutil.copytree(base, tree)
    for relative, content in experiment._SOLUTION_FILES.items():
        with open(os.path.join(tree, relative), "w") as handle:
            handle.write(content)
    if parse_source is not None:
        with open(os.path.join(tree, "src/parser/parse.py"), "w") as handle:
            handle.write(parse_source)
    return tree


def _hidden_parse():
    return oracle.OracleCheck(
        "hidden-parse",
        ("AC-PARSE",),
        weight=1.0,
        expected_outcome=oracle.GRADE_PASS,
        timeout_seconds=experiment._HIDDEN_CHECK_TIMEOUT_SECONDS,
        mandatory=True,
    )


def test_known_good_solution_is_a_pass():
    """Anti-vacuity: the isolation harness still grades a correct candidate PASS."""
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-good-")
    try:
        tree = _solution_tree(workdir)
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_PASS
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_candidate_early_exit_is_not_a_pass():
    """A candidate that exits during import produces no observation and fails."""
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-exit-")
    try:
        tree = _solution_tree(workdir, parse_source="import os\nos._exit(0)\n")
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_FAIL
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_candidate_cannot_suppress_its_own_observation():
    """A candidate that silences the driver's output cannot earn a pass."""
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-suppress-")
    try:
        silenced = (
            "import builtins\n"
            "builtins.print = lambda *args, **kwargs: None\n"
            "def parse(text):\n    return text.split()\n"
        )
        tree = _solution_tree(workdir, parse_source=silenced)
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_FAIL
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_candidate_wrong_result_is_a_fail():
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-wrong-")
    try:
        tree = _solution_tree(workdir, parse_source="def parse(text):\n    return []\n")
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_FAIL
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_expected_values_are_not_present_in_the_sandbox_script():
    """The collector carries the inputs, never the expected values."""
    import json as _json

    for check_id, driver in experiment._HIDDEN_DRIVERS:
        script = experiment._collector_script(driver)
        assert _json.dumps(experiment._HIDDEN_EXPECTED[check_id]) not in script
        assert f"{check_id} ok" not in script


def test_collector_does_not_import_candidate_top_level_modules():
    """F1: a candidate-supplied top-level ``json.py`` must not be imported by
    the collector or the driver in place of the standard library."""
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-shadow-")
    try:
        tree = _solution_tree(workdir)
        with open(os.path.join(tree, "json.py"), "w") as handle:
            handle.write(
                "import os\n"
                "open('/tmp/.shadow_pids', 'a').write(str(os.getpid()) + ' ')\n"
                "def dumps(*args, **kwargs):\n    return 'PWNED'\n"
            )
        # If either process imported the shadow, the COLLECT payload would be
        # malformed and the check would fail; a PASS proves the stdlib was used.
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_PASS
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_candidate_output_volume_is_bounded():
    """F2: a candidate cannot flood the collector's memory; exceeding the cap is
    a failure, not a pass."""
    _caps()
    workdir = tempfile.mkdtemp(prefix="r08-flood-")
    try:
        noisy = (
            "import sys\n"
            "def parse(text):\n"
            "    sys.stdout.write('x' * (4 * 1024 * 1024))\n"
            "    return text.split()\n"
        )
        tree = _solution_tree(workdir, parse_source=noisy)
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_FAIL
        # Anti-vacuity: modest noise does not fail a correct candidate.
        modest = (
            "import sys\n"
            "def parse(text):\n"
            "    sys.stdout.write('noise\\n')\n"
            "    return text.split()\n"
        )
        tree = _solution_tree(os.path.join(workdir, "modest"), parse_source=modest)
        assert experiment.hidden_runner(_hidden_parse(), tree) == oracle.GRADE_PASS
    finally:
        shutil.rmtree(workdir, ignore_errors=True)


def test_hidden_suite_hash_binds_drivers_and_expected_values(monkeypatch):
    """F3: changing what a check executes or asserts changes the frozen hash."""
    workdir = tempfile.mkdtemp(prefix="r08-hash-")
    try:
        target = experiment.build_fixed_target(workdir)
        manifest = experiment.build_hidden_oracle(target)
        base = experiment.hidden_suite_manifest_hash(target, manifest)
        monkeypatch.setitem(
            experiment._HIDDEN_EXPECTED,
            "hidden-parse",
            {"a": ["PWNED"], "b": [], "c": []},
        )
        assert experiment.hidden_suite_manifest_hash(target, manifest) != base
        monkeypatch.undo()
        from orchestrator import hidden_oracle as _oracle_module

        tampered = list(_oracle_module._HIDDEN_DRIVERS)
        tampered[0] = (tampered[0][0], tampered[0][1] + "# tampered\n")
        monkeypatch.setattr(_oracle_module, "_HIDDEN_DRIVERS", tuple(tampered))
        assert experiment.hidden_suite_manifest_hash(target, manifest) != base
    finally:
        shutil.rmtree(workdir, ignore_errors=True)
