# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Bypass tests for scripts/hooks/prose-only, the classifier that lets the commit and push hooks run
# the documentation check instead of the test suite. A false "prose only" skips the suite for a
# change that could break it, so every case here is a change set the classifier must send to the
# full gate.
#
# The script runs through `bash`, not directly: the Phase 3 oracle's read-only overlays lose the
# execute bit, and every other gate command in this repository is run through an interpreter for
# the same reason.

import subprocess
from pathlib import Path

import pytest

SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "hooks" / "prose-only"


def _classify(*paths: str, raw: str | None = None) -> int:
    stdin = raw if raw is not None else "".join(f"{path}\n" for path in paths)
    return subprocess.run(
        ("bash", str(SCRIPT)), input=stdin, capture_output=True, text=True
    ).returncode


@pytest.mark.parametrize(
    "paths",
    [
        ("docs/method.md",),
        ("docs/record.md", "docs/residuals.md"),
        ("docs/templates/report.txt", "docs/templates/review-brief.txt"),
        ("docs/research/some-new-report.md",),
        ("docs/research/nested/deeper/report.md",),
        ("docs/method.md", "docs/research/x.md", "docs/templates/y.txt"),
    ],
)
def test_prose_only_change_sets_skip_the_suite(paths):
    assert _classify(*paths) == 0


@pytest.mark.parametrize(
    "paths",
    [
        ("src/orchestrator/controller.py",),
        ("tests/test_reducer.py",),
        ("scripts/check-tests",),
        ("scripts/hooks/prose-only",),
        ("docs/tables/node-lifecycle.yaml",),
        ("docs/control-plane.md",),
        ("docs/glossary.md",),
        ("README.md",),
        ("AGENTS.md",),
        ("requirements-dev.txt",),
        ("pytest.ini",),
        (".github/workflows/tests.yml",),
    ],
)
def test_anything_a_test_or_gate_can_read_takes_the_full_gate(paths):
    assert _classify(*paths) == 1


def test_one_non_prose_path_among_prose_takes_the_full_gate():
    assert _classify("docs/method.md", "docs/research/x.md", "src/orchestrator/model.py") == 1


def test_the_non_prose_path_may_come_first_or_last():
    assert _classify("src/a.py", "docs/method.md") == 1
    assert _classify("docs/method.md", "src/a.py") == 1


@pytest.mark.parametrize("raw", ["", "\n", "\n\n"])
def test_an_empty_change_set_takes_the_full_gate(raw):
    """An empty commit or an unreadable diff must not look like a prose-only one."""
    assert _classify(raw=raw) == 1


@pytest.mark.parametrize(
    "path",
    [
        "docs/research/../../src/orchestrator/controller.py",
        "docs/templates/../../scripts/check-tests",
        "docs/research/..",
        "docs/research/a/../../../tests/conftest.py",
    ],
)
def test_a_path_that_climbs_out_of_its_prefix_takes_the_full_gate(path):
    assert _classify(path) == 1


@pytest.mark.parametrize(
    "path",
    [
        "docs/methodology.md",
        "docs/method.md.bak",
        "docs/method.md/extra.py",
        "docs/records.md",
        "docs/researchers/x.md",
        "docs/research",
        "docs/templates",
        "xdocs/research/x.md",
        "/docs/research/x.md",
    ],
)
def test_lookalike_paths_are_not_prose(path):
    assert _classify(path) == 1
