"""Test PR merge gate logic in finish-task.sh (task-2280)."""
import subprocess


def test_pr_gate_skips_when_no_project_path():
    """PR gate should skip when PROJECT_PATH is empty."""
    result = subprocess.run(
        ["bash", "-c", """
        PROJECT_PATH=""
        TASK_ID="test-dummy"
        TEAM_SHORT="dev4"
        if [ -n "$PROJECT_PATH" ] && [ -d "$PROJECT_PATH/.git" ]; then
            echo "WOULD_RUN"
        else
            echo "SKIPPED"
        fi
        """],
        capture_output=True, text=True
    )
    assert result.stdout.strip() == "SKIPPED"


def test_pr_gate_skips_when_no_git_dir():
    """PR gate should skip when .git directory does not exist."""
    result = subprocess.run(
        ["bash", "-c", """
        PROJECT_PATH="/tmp/nonexistent-dir-xyz"
        TASK_ID="test-dummy"
        TEAM_SHORT="dev4"
        if [ -n "$PROJECT_PATH" ] && [ -d "$PROJECT_PATH/.git" ]; then
            echo "WOULD_RUN"
        else
            echo "SKIPPED"
        fi
        """],
        capture_output=True, text=True
    )
    assert result.stdout.strip() == "SKIPPED"


def test_pr_gate_block_exists_in_script():
    """Verify PR-GATE block exists in finish-task.sh."""
    with open("/home/jay/workspace/scripts/finish-task.sh") as f:
        content = f.read()
    assert "[PR-GATE] BLOCKED" in content
    assert "gh pr list --state open" in content
