from __future__ import annotations

from pathlib import Path

from skills._shared.python.skill_testing import assert_skill_contract


def test_workflows_issue_dispatch_subagent_pr_contract() -> None:
    skill_root = Path(__file__).resolve().parents[1]
    assert_skill_contract(skill_root)


def test_dispatch_subagent_pr_skill_mentions_worktree_isolation() -> None:
    skill_md = Path(__file__).resolve().parents[1] / "SKILL.md"
    text = skill_md.read_text(encoding="utf-8")
    assert "worktree" in text.lower()
    assert "Subagent" in text
    assert "Task Decomposition" in text
    assert "$AGENT_HOME/out/dispatch-plan-delivery" in text
    assert "SUBAGENT_INIT_SNAPSHOT_PATH" not in text
    assert "DISPATCH_RECORD_PATH" in text
    assert "workflow_role" in text
    assert "implementation" in text


def test_dispatch_subagent_pr_skill_requires_native_git_gh_commands() -> None:
    skill_md = Path(__file__).resolve().parents[1] / "SKILL.md"
    text = skill_md.read_text(encoding="utf-8")
    assert "git worktree" in text
    assert "gh pr create" in text
    assert "gh pr comment" in text
    assert "create-dispatch-lane-pr/scripts/create-dispatch-lane-pr.sh" in text
    assert "BASE is required from lane assignment" in text
    assert "do not assume `main`" in text


def test_dispatch_subagent_pr_skill_uses_shared_task_lane_policy() -> None:
    skill_md = Path(__file__).resolve().parents[1] / "SKILL.md"
    text = skill_md.read_text(encoding="utf-8")
    assert "_shared/references/TASK_LANE_CONTINUITY.md" in text


def test_dispatch_subagent_pr_skill_excludes_deleted_wrapper_script() -> None:
    skill_md = Path(__file__).resolve().parents[1] / "SKILL.md"
    text = skill_md.read_text(encoding="utf-8")
    assert ("manage_dispatch_subagent_pr" + ".sh") not in text


def test_dispatch_subagent_prompt_template_excludes_single_pr_mode() -> None:
    prompt_template = (
        Path(__file__).resolve().parents[1] / "references" / "SUBAGENT_TASK_PROMPT_TEMPLATE.md"
    ).read_text(encoding="utf-8")

    assert "pr-shared" in prompt_template
    assert "pr-isolated" in prompt_template
    assert "single-pr" not in prompt_template


def test_dispatch_subagent_pr_requires_test_first_evidence_gate() -> None:
    skill_root = Path(__file__).resolve().parents[1]
    text = (skill_root / "SKILL.md").read_text(encoding="utf-8")
    prompt_template = (skill_root / "references" / "SUBAGENT_TASK_PROMPT_TEMPLATE.md").read_text(
        encoding="utf-8"
    )
    pr_template = (skill_root / "references" / "PR_BODY_TEMPLATE.md").read_text(encoding="utf-8")

    assert "## Test-First Evidence Gate" in text
    assert "failing-test evidence or an explicit waiver" in text
    assert "before editing production behavior" in text
    assert "Change classification" in prompt_template
    assert "Failing test before fix" in prompt_template
    assert "Final validation" in pr_template
    assert "Waiver reason" in pr_template
