"""REC-245: the orchestrate merge-gate regex must classify only real merges.

`\\bgit\\s+merge\\b` (the old pattern) false-matched `git merge-base` (a word
boundary sits before the hyphen), so read-only plumbing commands were blocked
as if they were merges during an active orchestrate run.
"""

import importlib.util
from pathlib import Path

import pytest

# Load orch_pre_bash.py directly (it's a hook script, not an importable package module).
_HOOK = Path(__file__).resolve().parents[1] / "orchestrate_hooks" / "orch_pre_bash.py"
_spec = importlib.util.spec_from_file_location("orch_pre_bash", _HOOK)
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod)
_MERGE_RE = _mod._MERGE_RE


@pytest.mark.parametrize(
    "cmd,is_merge",
    [
        # read-only plumbing — must NOT be classified as a merge
        ("git merge-base --is-ancestor origin/main HEAD", False),
        ("git merge-tree a b", False),
        ("git merge-file x y z", False),
        ("git merge-index foo", False),
        ("git merge --abort", False),
        # plain pushes — must NOT be classified as a merge
        ("git push origin main", False),
        ("git push origin HEAD:main", False),
        # real merges — MUST be classified
        ("git merge feature", True),
        ("git merge --no-ff topic", True),
        ("  git   merge   topic", True),
        ("gh pr merge 167 --squash --delete-branch", True),
    ],
)
def test_merge_re_classification(cmd, is_merge):
    assert bool(_MERGE_RE.search(cmd)) is is_merge
