"""Smoke tests for source-distribution packaging."""

from __future__ import annotations

import shutil
import subprocess
import tarfile
from pathlib import Path


def test_sdist_includes_agent_assets(tmp_path: Path) -> None:
    repo_root = Path(__file__).resolve().parents[1]
    uv = shutil.which("uv")
    if uv is None:
        raise AssertionError("uv executable is required to build the source distribution smoke test")

    subprocess.run(
        [uv, "build", "--sdist", "--out-dir", str(tmp_path)],
        check=True,
        cwd=repo_root,
        capture_output=True,
        text=True,
    )

    artifacts = sorted(tmp_path.glob("*.tar.gz"))
    assert artifacts, "uv build --sdist did not produce a source distribution"

    with tarfile.open(artifacts[0], "r:gz") as archive:
        members = {member.name for member in archive.getmembers()}

    expected_suffixes = {
        "AGENTS.md",
        "CAPABILITIES.md",
        "CHANGELOG.md",
        "agent-snippets/downstream-AGENTS.md",
        "examples/http_fetch_with_retries.py",
        "examples/poll_remote_job.py",
        "examples/bounded_async_runtime.py",
        "examples/download_and_mirror_asset.py",
        "examples/azure_function_and_job_store.py",
        "skills/infra-core-http-polling/SKILL.md",
        "skills/infra-core-http-polling/agents/openai.yaml",
        "skills/infra-core-azure-storage/SKILL.md",
        "skills/infra-core-azure-storage/agents/openai.yaml",
        "skills/infra-core-azure-functions-jobs/SKILL.md",
        "skills/infra-core-azure-functions-jobs/agents/openai.yaml",
    }

    for suffix in expected_suffixes:
        assert any(member.endswith(suffix) for member in members), f"Missing {suffix} from source distribution"
