"""Is it a *package*? These five are about the artifact, not the algorithms.

`test_mypy_strict_clean` and `test_ruff_clean` are fast. The other three build a wheel and a
venv, so they carry the `slow` marker and are deselected by default:

    uv run pytest -q          # the five fast checks
    uv run pytest -q -m ''    # all eight — what "done" means
"""

from __future__ import annotations

import json
import os
import zipfile
from pathlib import Path

import pytest
from conftest import PROJECT, run, tool_cmd  # tests/ is on sys.path under pytest


def test_mypy_strict_clean(tmp_path: Path) -> None:
    cmd = tool_cmd("mypy")
    if cmd is None:
        pytest.skip("mypy is not installed here — `uv sync` installs it")
    result = run([*cmd, "--strict", "--no-error-summary",
                  "--cache-dir", str(tmp_path / "mypy"), "src"], cwd=PROJECT)
    assert result.returncode == 0, f"mypy --strict src is red:\n{result.stdout}{result.stderr}"


def test_ruff_clean(tmp_path: Path) -> None:
    cmd = tool_cmd("ruff")
    if cmd is None:
        pytest.skip("ruff is not installed here — `uv sync` installs it")
    result = run([*cmd, "check", "--no-cache", "."], cwd=PROJECT)
    assert result.returncode == 0, f"ruff check . is not clean:\n{result.stdout}{result.stderr}"


@pytest.mark.slow
def test_wheel_contains_py_typed(wheel: Path) -> None:
    names = zipfile.ZipFile(wheel).namelist()
    assert "llmutils/py.typed" in names, (
        "the wheel ships no py.typed, so every downstream `mypy` treats llmutils as untyped. "
        f"It contains: {sorted(names)}"
    )


@pytest.mark.slow
def test_wheel_builds_and_installs(installed_python: Path, tmp_path: Path) -> None:
    where = run([str(installed_python), "-c", "import llmutils; print(llmutils.__file__)"],
                cwd=tmp_path)
    assert where.returncode == 0, f"the installed wheel does not import:\n{where.stderr}"
    assert "site-packages" in where.stdout, f"that came from {where.stdout.strip()}, not the wheel"

    got = run([str(installed_python), "-c",
               "import json, llmutils; print(json.dumps(llmutils.chunk('a b c d e', 2, 1)))"],
              cwd=tmp_path)
    assert got.returncode == 0, f"{got.stdout}{got.stderr}"
    assert json.loads(got.stdout) == [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"]]


@pytest.mark.slow
def test_entry_point_prints_version(installed_python: Path, tmp_path: Path) -> None:
    bin_dir = installed_python.parent
    script = bin_dir / ("llmutils.exe" if os.name == "nt" else "llmutils")
    assert script.exists(), (
        "installing the wheel created no `llmutils` launcher — check [project.scripts]. "
        f"The venv has: {sorted(p.name for p in bin_dir.iterdir())}"
    )
    result = run([str(script), "--version"], cwd=tmp_path)
    assert result.returncode == 0, f"{result.stdout}{result.stderr}"
    assert result.stdout.strip() == "llmutils 0.1.0", (
        f"expected 'llmutils 0.1.0' from importlib.metadata, got {result.stdout.strip()!r}"
    )
