"""Release gates reject corrupt targets and source references before packaging.""" from __future__ import annotations import shutil import pyarrow.parquet as pq import pytest from kitti_c2l import release from kitti_c2l.generate import generate_sequence_rows from kitti_c2l.sequence_mapping import SequenceInfo from kitti_c2l.windows import generate_sequence_windows from kitti_c2l.writer import rows_to_table, windows_to_table @pytest.fixture def small_release_table(fake_kitti_root, tmp_path, monkeypatch): info = SequenceInfo("04", "2011_09_30", "0016", 0, 3, 4, "train") monkeypatch.setattr(release, "load_sequence_mapping", lambda: {"04": info}) rows = list(generate_sequence_rows(info, fake_kitti_root)) path = tmp_path / "train.parquet" pq.write_table(rows_to_table(rows), path) return path, rows, info, fake_kitti_root def test_release_table_checks_source_calibration(small_release_table): path, rows, _, raw_root = small_release_table result = release.validate_table(path, kitti_calib_root=raw_root) assert result["rows"] == len(rows) == 20 assert result["sha256"] == release.sha256(path) @pytest.mark.parametrize( "field,value,reason", [ ("sample_seed", 1, "seed"), ("raw_frame_index", 99, "raw mapping"), ("sample_id", "bad_id", "invalid ID"), ("delta_cam_tx_cm", 999.0, "seeded perturbation"), ("miscal_t_cam_from_velo_cm", [999.0, 0.0, 0.0], "translation/units"), ("gt_R_cam_from_velo", [float("nan")] * 9, "non-finite"), ("miscal_R_cam_from_velo", [-1, 0, 0, 0, 1, 0, 0, 0, 1], "reflected"), ("stage", None, "null values"), ], ) def test_release_rejects_corrupt_rows(small_release_table, field, value, reason): path, rows, _, _ = small_release_table rows[0] = rows[0]._replace(**{field: value}) pq.write_table(rows_to_table(rows), path) with pytest.raises(release.ReleaseValidationError, match=reason): release.validate_table(path) @pytest.mark.parametrize("duplicate", [False, True]) def test_release_rejects_missing_or_duplicate_stages(small_release_table, duplicate): path, rows, _, _ = small_release_table rows = rows + [rows[0]] if duplicate else rows[:-1] pq.write_table(rows_to_table(rows), path) with pytest.raises(release.ReleaseValidationError, match="duplicate|coverage"): release.validate_table(path) def test_release_checks_short_final_window(small_release_table): path, _, info, raw_root = small_release_table path = path.with_name("windows_train.parquet") rows = list(generate_sequence_windows(info, raw_root)) pq.write_table(windows_to_table(rows), path) assert release.validate_table(path)["rows"] == 5 rows[0] = rows[0]._replace(window_length=30) pq.write_table(windows_to_table(rows), path) with pytest.raises(release.ReleaseValidationError, match="window coverage"): release.validate_table(path) def test_release_rejects_materialized_sensor_assets(tmp_path): data = tmp_path / "data" data.mkdir() for name in (*release.DATA_FILES, "frames_train-00000-of-00001.parquet"): (data / name).touch() with pytest.raises(release.ReleaseValidationError, match="sensor assets are excluded"): release.validate_release(tmp_path) def test_staging_uses_allowlist_and_refuses_overwrite(tmp_path, monkeypatch): import scripts.prepare_release as preparation root = tmp_path / "source" root.mkdir() for relative in preparation.RELEASE_FILES: destination = root / relative destination.parent.mkdir(parents=True, exist_ok=True) destination.write_bytes(b"fixture") (root / "private-notes.txt").write_text("not for release") monkeypatch.setattr(preparation, "validate_release", lambda *args, **kwargs: {}) output = tmp_path / "public" archive = preparation.prepare(root, output) assert archive.exists() assert {str(p.relative_to(output)) for p in output.rglob("*") if p.is_file()} == set( preparation.RELEASE_FILES ) | {"SHA256SUMS"} with pytest.raises(FileExistsError): preparation.prepare(root, output) # Identical inputs produce an identical archive despite a different source path/mtime. other_root = tmp_path / "other-source" shutil.copytree(root, other_root) other_output = tmp_path / "again" / "public" other_archive = preparation.prepare(other_root, other_output) assert release.sha256(archive) == release.sha256(other_archive) @pytest.fixture def recorded_hub_release(tmp_path): import json from pathlib import Path source = Path(__file__).resolve().parents[1] / "release/v1" destination = tmp_path / "release/v1" destination.mkdir(parents=True) for name in [ "hub_manifest.json", "manifest.json", "HF_DATASET_CARD.md", "hub_payload_verification.json", ]: shutil.copyfile(source / name, destination / name) return tmp_path, json.loads((destination / "hub_manifest.json").read_text()) def test_recorded_full_hub_inventory(recorded_hub_release): root, _ = recorded_hub_release result = release.validate_hub_manifest(root) assert len(result["shards"]) == 50 assert result["total_frames"] == 22400 @pytest.mark.parametrize( "problem", ["missing_shard", "duplicate_shard", "wrong_frame_count", "changed_metadata", "empty_shard"], ) def test_full_hub_inventory_rejects_invalid_snapshot(recorded_hub_release, problem): import json root, report = recorded_hub_release if problem == "missing_shard": report["shards"].pop() elif problem == "duplicate_shard": report["shards"][-1] = report["shards"][0] elif problem == "wrong_frame_count": report["shards"][0]["rows"] += 1 elif problem == "changed_metadata": report["metadata_files"][0]["sha256"] = "0" * 64 else: report["shards"][0]["bytes"] = 0 (root / "release/v1/hub_manifest.json").write_text(json.dumps(report)) with pytest.raises(release.ReleaseValidationError): release.validate_hub_manifest(root) def test_full_card_cannot_drop_existing_frames_configuration(recorded_hub_release): import yaml root, _ = recorded_hub_release path = root / "release/v1/HF_DATASET_CARD.md" _, header, body = path.read_text().split("---", 2) metadata = yaml.safe_load(header) metadata["configs"] = [ config for config in metadata["configs"] if config["config_name"] != "frames" ] path.write_text("---\n" + yaml.safe_dump(metadata) + "---" + body) with pytest.raises(release.ReleaseValidationError, match="all three configs"): release.validate_hub_manifest(root) def test_full_update_keeps_existing_payloads_and_uses_production_card(tmp_path, monkeypatch): import json import scripts.prepare_release as preparation root = tmp_path / "source" root.mkdir() for relative in preparation.RELEASE_FILES: target = root / relative target.parent.mkdir(parents=True, exist_ok=True) target.write_text("fixture") card = root / "release/v1/HF_DATASET_CARD.md" card.write_text("production card retaining frames") monkeypatch.setattr(preparation, "validate_release", lambda *args, **kwargs: {}) monkeypatch.setattr( preparation, "validate_hub_manifest", lambda *args: {"repo_id": "owner/dataset", "revision": "a" * 40}, ) output = tmp_path / "full-update" preparation.prepare(root, output, profile="full-update") assert (output / "README.md").read_text() == card.read_text() profile = json.loads((output / "release-profile.json").read_text()) assert profile["requires_existing_frame_shards"] is True assert profile["delete_remote_files"] is False assert not list((output / "data").glob("frames_*.parquet"))