import hashlib import json import runpy import sys import tarfile from pathlib import Path def test_model_bundle_embeds_manifest_without_mutating_source(tmp_path: Path, monkeypatch): source = tmp_path / "source" image_dir = source / "visual" / "image" text_dir = source / "visual" / "text" image_dir.mkdir(parents=True) text_dir.mkdir(parents=True) model = image_dir / "model.bin" model.write_bytes(b"image model") (text_dir / "config.json").write_text("{}", encoding="utf-8") source_manifest = source / "manifest.json" source_manifest.write_text("source sentinel", encoding="utf-8") output = tmp_path / "models.tar.gz" script = Path(__file__).parents[1] / "scripts" / "build-model-bundle.py" namespace = runpy.run_path(str(script)) monkeypatch.setattr(sys, "argv", [str(script), str(source), str(output), "--version", "test-v1"]) namespace["main"]() assert source_manifest.read_text(encoding="utf-8") == "source sentinel" with tarfile.open(output, "r:gz") as archive: assert archive.getnames().count("manifest.json") == 1 manifest_file = archive.extractfile("manifest.json") assert manifest_file is not None manifest = json.load(manifest_file) assert manifest["version"] == "test-v1" assert "manifest.json" not in manifest["files"] assert manifest["files"]["visual/image/model.bin"] == hashlib.sha256(model.read_bytes()).hexdigest()