from __future__ import annotations

from pathlib import Path


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


def test_plan_workflow_readme_defines_source_plan_state_roles() -> None:
    text = (_repo_root() / "skills" / "workflows" / "plan" / "README.md").read_text(encoding="utf-8")
    normalized = " ".join(text.split())

    assert "## Artifact Roles" in text
    assert "Source doc: durable facts" in text
    assert "Plan: execution-control artifact" in text
    assert "Execution state: resume ledger" in text
    assert "target scope, execution window" in text
    assert "A source doc should explain why and what" in normalized
    assert "a plan should sequence how" in normalized


def test_plan_workflow_readme_defines_cleanup_selection_boundary() -> None:
    text = (_repo_root() / "skills" / "workflows" / "plan" / "README.md").read_text(encoding="utf-8")
    normalized = " ".join(text.split())

    assert "## Cleanup Selection" in text
    assert "Use `durable-artifact-cleanup` for named source docs" in text
    assert "Use `plan-tooling artifact-audit` when available" in text
    assert "`delete`, `keep`, `rehome`, and `manual-review`" in text
    assert "not as an auto-delete authority" in normalized
    assert "Use `docs-plan-cleanup` for broad `docs/plans/` hygiene" in text
    assert "It is the batch executor, not the policy audit" in normalized
    assert "run `durable-artifact-cleanup` first to classify the scope" in text


def test_plan_workflow_readme_prefers_plan_for_multistep_execution() -> None:
    text = (_repo_root() / "skills" / "workflows" / "plan" / "README.md").read_text(encoding="utf-8")
    normalized = " ".join(text.split())

    assert "default bridge from source docs to implementation" in text
    assert "execute-from-plan" in text
    assert "resume implementation from a plan by default" in text
    assert "execute-from-tracking-issue" in text
    assert "issue-hosted source/plan snapshots" in normalized
    assert "the default target scope is the whole plan" in normalized
    assert "Target scope and execution window are distinct" in text
    assert "requires an explicit user selector or a staged execution proposal" in normalized
    assert "Direct source-doc execution is only for bounded" in normalized


def test_plan_workflow_docs_reference_conditional_browser_qa_evidence() -> None:
    root = _repo_root()
    readme = (root / "skills" / "workflows" / "plan" / "README.md").read_text(encoding="utf-8")
    create_plan = (root / "skills" / "workflows" / "plan" / "create-plan" / "SKILL.md").read_text(encoding="utf-8")
    execute_from_plan = (
        root / "skills" / "workflows" / "plan" / "execute-from-plan" / "SKILL.md"
    ).read_text(encoding="utf-8")
    baseline = (
        root / "skills" / "workflows" / "plan" / "_shared" / "references" / "PLAN_AUTHORING_BASELINE.md"
    ).read_text(encoding="utf-8")

    for text in [readme, create_plan, execute_from_plan, baseline]:
        assert "`browser-qa`" in text
        assert "JavaScript" in text
        assert "deploy-preview inspection" in text

    assert "browser-session.json" in readme
    assert "browser-session verify" in readme
    assert "browser-session verify" in execute_from_plan
    assert "conditional, not a default requirement" in baseline
    assert "do not make browser QA mandatory for non-browser tasks" in readme
