"""Regression coverage for the production-bytecode reproducibility command."""

from __future__ import annotations

import importlib.util
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch


ROOT = Path(__file__).resolve().parents[1]
MODULE_PATH = ROOT / "ci" / "check_v3_reproducible_build.py"
SPEC = importlib.util.spec_from_file_location("check_v3_reproducible_build", MODULE_PATH)
assert SPEC and SPEC.loader
gate = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(gate)


class ReproducibleBuildCommandTest(unittest.TestCase):
    def test_build_compiles_the_full_production_scope_but_excludes_non_deployable_test_and_script_artifacts(self) -> None:
        captured: list[str] = []

        with tempfile.TemporaryDirectory() as directory, patch.object(gate.shutil, "which", return_value="/usr/bin/forge"), patch.object(
            gate.subprocess, "run", side_effect=lambda command, **_: captured.extend(command)
        ), patch.object(gate, "_artifact_manifest", return_value={"artifactCount": 1, "artifacts": {}}):
            gate._build(Path(directory))

        self.assertIn("--force", captured)
        self.assertEqual(captured.count("--skip"), 2)
        self.assertIn("./test/**", captured)
        self.assertIn("./script/**", captured)
        self.assertNotIn("--match-path", captured)


if __name__ == "__main__":
    unittest.main()
