#!/usr/bin/env python3
"""Run only tests owned by the rendering, RLV, and LSL milestone."""

from __future__ import annotations

import json
import os
import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
ENV = os.environ.copy()
ENV.setdefault("CARGO_BUILD_JOBS", "1")
ENV.setdefault("CARGO_INCREMENTAL", "0")
ENV.setdefault("CARGO_PROFILE_DEV_DEBUG", "0")
ENV.setdefault("CARGO_PROFILE_TEST_DEBUG", "0")
ENV.setdefault("RUST_TEST_THREADS", "1")
ENV.pop("RUN_LIVE_TESTS", None)


def run(command: list[str]) -> None:
    print("+", " ".join(command), flush=True)
    subprocess.run(command, cwd=ROOT, env=ENV, check=True)


run(
    [
        "cargo",
        "test",
        "-p",
        "libremetaverse-rendering-simple",
        "-p",
        "libremetaverse-rendering-mesh-foundry",
        "-p",
        "libremetaverse-rlv",
        "-p",
        "libremetaverse-lsl-tools",
        "--locked",
        "-j",
        "1",
    ]
)

catalog = json.loads((ROOT / "tests" / "upstream-tests.json").read_text())
rlv_targets = sorted(
    {
        Path(test["rust_file"]).stem
        for test in catalog["tests"]
        if test["source"].startswith("LibreMetaverse.Tests/RLV/")
    }
)
if len(rlv_targets) != 24:
    raise SystemExit(f"milestone 10 expected 24 RLV test targets, found {rlv_targets}")

compat = ["cargo", "test", "-p", "libremetaverse-compat-tests"]
for target in (
    "imaging_meshing_semantics",
    "rendering_shims",
    "extension_shims",
    "milestone_10_integration",
    *rlv_targets,
):
    compat.extend(("--test", target))
compat.extend(("--locked", "-j", "1"))
run(compat)

run(
    [
        "cargo",
        "check",
        "--manifest-path",
        "tests/api-compile/Cargo.toml",
        "--locked",
        "-j",
        "1",
    ]
)
