from __future__ import annotations

from pathlib import Path

from skills._shared.python.skill_testing import assert_skill_contract


def _skill_root() -> Path:
    return Path(__file__).resolve().parents[1]


def _skill_text() -> str:
    return (_skill_root() / "SKILL.md").read_text(encoding="utf-8")


def test_workflows_issue_deliver_tracking_issue_contract() -> None:
    assert_skill_contract(_skill_root())


def test_deliver_tracking_issue_declares_scriptless_orchestration() -> None:
    text = _skill_text()

    assert "None. This is a scriptless orchestration workflow" in text
    assert "execute-from-tracking-issue/scripts/tracking-issue-lifecycle.sh" in text
    assert "tracking-issue-closeout/scripts/tracking-issue-closeout.sh" in text
    assert "deliver-github-pr/scripts/deliver-github-pr.sh" in text
    assert "close-github-pr/scripts/close-github-pr.sh" in text


def test_deliver_tracking_issue_forces_review_for_small_prs() -> None:
    text = _skill_text()

    assert "shared delivery specialist review gate" in text
    assert "scope --base <pr-base-ref> --testing --maintainability" in text
    assert "This forced minimum applies even when `diff_lines < 50`" in text
    assert "must not skip only because the diff is small" in text
    assert "record the lenses" in text


def test_deliver_tracking_issue_records_provider_review_outcome_comment() -> None:
    text = _skill_text()

    assert "delivery review outcome comment URL" in text
    assert "skills/workflows/_shared/references/DELIVERY_REVIEW_OUTCOME_COMMENT.md" in text
    assert "every PR has a provider-side delivery review outcome comment URL recorded" in text
    assert "without duplicating" in text


def test_deliver_tracking_issue_keeps_composed_skill_boundaries() -> None:
    text = _skill_text()

    assert "`code-review-specialists` remains read-only." in text
    assert "`tracking-issue-closeout` remains the final closeout lane only." in text
    assert "use `Refs #<issue>` rather than `Closes #<issue>`" in text
    assert "use `dispatch-plan-delivery` or `dispatch-issue-closeout` instead" in text


def test_deliver_tracking_issue_requires_true_completion() -> None:
    text = _skill_text()

    assert "latest issue-hosted execution state is `complete`" in text
    assert "every task row is `done` or explicitly `deferred`" in text
    assert "every PR has a specialist review result" in text
    assert "Do not report success while the next action is still to run checks" in text


def test_deliver_tracking_issue_documents_repair_loop() -> None:
    text = _skill_text()

    assert "Repair concrete findings on the same PR branch" in text
    assert "rerun focused validation, required checks, and the affected specialist lenses" in text
    assert "Repeat review and repair until there are no concrete unresolved findings" in text


def test_deliver_tracking_issue_classifies_runtime_findings() -> None:
    text = _skill_text()

    assert "## Runtime Findings And Follow-Up Policy" in text
    assert "`stop-for-confirmation`" in text
    assert "`fix-now`" in text
    assert "`accepted-residual`" in text
    assert "`follow-up-linked`" in text
    assert "`deferred-task`" in text
    assert "`no-action`" in text


def test_deliver_tracking_issue_stops_for_material_design_drift() -> None:
    text = _skill_text()

    assert "The planned file, API, command, schema, migration, deployment surface, or runtime boundary" in text
    assert "Do not silently rewrite the plan, widen scope, defer tasks, or substitute a different design" in text
    assert "Leave the GitHub issue open." in text
    assert "known facts and evidence" in text
    assert "the exact source/plan mismatch" in text
    assert "the specific confirmation needed from the user" in text
    assert "Resume delivery only after the user or project policy confirms the amended scope or stop decision." in text


def test_deliver_tracking_issue_records_non_blocking_followups() -> None:
    text = _skill_text()

    assert "Keep moving only after recording the finding in the next issue-hosted session or validation comment." in text
    assert "follow-up link, deferred task row, or explicit `no-action` decision" in text
    assert "Accepted residual risks must be low enough that the issue can honestly close as completed." in text
    assert "any actionable follow-up is linked or deferred" in text
