"""Regenerate docs/diagrams/gemm_sweep.json by running the GEMM sweep.

Heavy: drives matmul-composite across all shapes x variants through the
simulator (24 runs; the 512 shape alone is 2048 tiles). Marked ``slow`` so it
is excluded from the default ``pytest`` run (addopts: -m "not slow") and runs
on demand:

    pytest -m slow tests/gemm/test_gemm_sweep.py

Delegates to scripts/gemm_sweep.py (the single source of the sweep logic) via
subprocess so there is no duplicated sim-driving code.
"""
from __future__ import annotations

import subprocess
import sys
from pathlib import Path

import pytest

from tests.gemm._gemm_plot_helpers import GEMM_SWEEP_JSON, ROOT


@pytest.mark.slow
def test_gemm_sweep_regenerates_json():
    script = ROOT / "scripts" / "gemm_sweep.py"
    assert script.exists(), f"missing {script}"
    proc = subprocess.run(
        [sys.executable, str(script)],
        cwd=str(ROOT), capture_output=True, text=True,
    )
    assert proc.returncode == 0, (
        f"gemm_sweep.py failed (rc={proc.returncode})\n"
        f"stdout:\n{proc.stdout[-2000:]}\nstderr:\n{proc.stderr[-2000:]}"
    )
    assert Path(GEMM_SWEEP_JSON).exists()
