test_cmd_verify_pack.py
python
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e
fix: rename objects→blobs in push client and all stale test…
Sonnet 4.6
patch
22 days ago
| 1 | """Supercharge tests for ``muse verify-pack``. |
| 2 | |
| 3 | Coverage tiers |
| 4 | -------------- |
| 5 | - TypedDicts: _Failure, _VerifyPackJson, _StatResult exist and are annotated |
| 6 | - JSON envelope: all required keys present, duration_ms non-neg float, exit_code int |
| 7 | - Stat mode: fast structural count, JSON with duration_ms/exit_code, text format |
| 8 | - Quiet mode: exit 0 clean, exit 1 corrupt, no output |
| 9 | - File input: --file reads from disk, OSError on missing file |
| 10 | - --no-local: skips local-store cross-checks |
| 11 | - Object integrity: hash mismatch, invalid entry, non-dict entry, invalid object_id |
| 12 | - Snapshot consistency: orphaned manifest ref, non-dict entry, missing snapshot_id |
| 13 | - Commit consistency: missing snapshot, non-dict entry, resolved via mpack |
| 14 | - Malformed input: not-a-dict, invalid msgpack, empty bytes, oversized |
| 15 | - Format text: summary line, failure lines, exit code |
| 16 | - Data integrity: corrupt content detected, truncated content, zeroed content |
| 17 | - Security hardening: malformed bundle_file arg, non-string object_id, binary injection |
| 18 | - Stress: 500-object mpack fully verified, duration bounded |
| 19 | - No-prose pollution: stdout is valid JSON, no emoji, no traceback |
| 20 | - Promised objects (Phase 1): PRESENT/PROMISED/MISSING tristate, --strict flag, |
| 21 | promised_objects in JSON envelope, partial-clone repo simulation |
| 22 | """ |
| 23 | from __future__ import annotations |
| 24 | from collections.abc import Mapping |
| 25 | |
| 26 | import hashlib |
| 27 | import argparse |
| 28 | import json |
| 29 | import pathlib |
| 30 | from typing import get_type_hints |
| 31 | |
| 32 | import msgpack |
| 33 | import pytest |
| 34 | |
| 35 | from muse.core.types import blob_id, long_id |
| 36 | from muse.core.object_store import write_object |
| 37 | from muse.core.paths import config_toml_path, muse_dir |
| 38 | from tests.cli_test_helper import CliRunner, InvokeResult |
| 39 | |
| 40 | runner = CliRunner() |
| 41 | _REPO_ID = "verify-pack-sg" |
| 42 | |
| 43 | |
| 44 | # --------------------------------------------------------------------------- |
| 45 | # Helpers |
| 46 | # --------------------------------------------------------------------------- |
| 47 | |
| 48 | def _pack(mpack: Mapping[str, object]) -> bytes: |
| 49 | """Encode a mpack dict to msgpack bytes.""" |
| 50 | return msgpack.packb(mpack, use_bin_type=True) |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | def _make_object(content: bytes) -> Mapping[str, object]: |
| 56 | return {"object_id": blob_id(content), "content": content} |
| 57 | |
| 58 | |
| 59 | _FULL_META = {"mode": "full", "base_commits": [], "created_at": "2026-01-01T00:00:00Z"} |
| 60 | |
| 61 | |
| 62 | def _clean_bundle(n_objects: int = 1) -> tuple[bytes, list[str]]: |
| 63 | """Return (msgpack_bytes, [oid, ...]) for a self-consistent mpack.""" |
| 64 | objects = [] |
| 65 | oids = [] |
| 66 | for i in range(n_objects): |
| 67 | content = f"object-content-{i}".encode() |
| 68 | oid = blob_id(content) |
| 69 | objects.append({"object_id": oid, "content": content}) |
| 70 | oids.append(oid) |
| 71 | |
| 72 | snap_content = f"snap-{n_objects}".encode() |
| 73 | snap_id = blob_id(snap_content) |
| 74 | manifest = {f"file{i}.py": oid for i, oid in enumerate(oids)} |
| 75 | |
| 76 | commit_content = f"commit-{n_objects}".encode() |
| 77 | commit_id = blob_id(commit_content) |
| 78 | |
| 79 | mpack = { |
| 80 | "blobs": objects, |
| 81 | "snapshots": [{"snapshot_id": snap_id, "manifest": manifest}], |
| 82 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 83 | "meta": _FULL_META, |
| 84 | } |
| 85 | return _pack(mpack), oids |
| 86 | |
| 87 | |
| 88 | def _empty_bundle() -> bytes: |
| 89 | """A valid but empty mpack (no objects, snapshots, or commits).""" |
| 90 | return _pack({"blobs": [], "snapshots": [], "commits": [], "meta": _FULL_META}) |
| 91 | |
| 92 | |
| 93 | def _init_repo(path: pathlib.Path) -> pathlib.Path: |
| 94 | dot_muse = muse_dir(path) |
| 95 | for d in ("commits", "snapshots", "objects", "refs/heads", "code"): |
| 96 | (dot_muse / d).mkdir(parents=True, exist_ok=True) |
| 97 | (dot_muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 98 | (dot_muse / "repo.json").write_text( |
| 99 | json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" |
| 100 | ) |
| 101 | return path |
| 102 | |
| 103 | |
| 104 | def _invoke(repo: pathlib.Path, *args: str, stdin: str | bytes | None = None) -> InvokeResult: |
| 105 | from muse.cli.app import main as cli |
| 106 | return runner.invoke( |
| 107 | cli, |
| 108 | list(args), |
| 109 | env={"MUSE_REPO_ROOT": str(repo)}, |
| 110 | input=stdin, |
| 111 | ) |
| 112 | |
| 113 | |
| 114 | # --------------------------------------------------------------------------- |
| 115 | # TypedDicts |
| 116 | # --------------------------------------------------------------------------- |
| 117 | |
| 118 | class TestTypedDicts: |
| 119 | def test_failure_exists(self) -> None: |
| 120 | from muse.cli.commands.verify_pack import _Failure |
| 121 | assert _Failure is not None |
| 122 | |
| 123 | def test_verify_pack_result_exists(self) -> None: |
| 124 | from muse.cli.commands.verify_pack import _VerifyPackJson |
| 125 | assert _VerifyPackJson is not None |
| 126 | |
| 127 | def test_stat_result_exists(self) -> None: |
| 128 | from muse.cli.commands.verify_pack import _StatResultJson as _StatResult |
| 129 | assert _StatResult is not None |
| 130 | |
| 131 | def test_failure_has_required_annotations(self) -> None: |
| 132 | from muse.cli.commands.verify_pack import _Failure |
| 133 | hints = get_type_hints(_Failure) |
| 134 | for field in ("kind", "id", "error"): |
| 135 | assert field in hints, f"Missing annotation: {field!r}" |
| 136 | |
| 137 | def test_verify_pack_result_has_required_annotations(self) -> None: |
| 138 | from muse.cli.commands.verify_pack import _VerifyPackJson |
| 139 | hints = get_type_hints(_VerifyPackJson) |
| 140 | for field in ("blobs_checked", "snapshots_checked", "commits_checked", "all_ok", "failures"): |
| 141 | assert field in hints, f"Missing annotation: {field!r}" |
| 142 | |
| 143 | def test_stat_result_has_required_annotations(self) -> None: |
| 144 | from muse.cli.commands.verify_pack import _StatResultJson as _StatResult |
| 145 | hints = get_type_hints(_StatResult) |
| 146 | for field in ("blobs", "snapshots", "commits"): |
| 147 | assert field in hints, f"Missing annotation: {field!r}" |
| 148 | |
| 149 | |
| 150 | # --------------------------------------------------------------------------- |
| 151 | # JSON output contract |
| 152 | # --------------------------------------------------------------------------- |
| 153 | |
| 154 | class TestJsonOutputContract: |
| 155 | _REQUIRED = { |
| 156 | "blobs_checked", "snapshots_checked", "commits_checked", |
| 157 | "all_ok", "failures", "duration_ms", "exit_code", |
| 158 | "promised_objects", "base_objects", "bundle_mode", "base_commits", |
| 159 | } |
| 160 | |
| 161 | def test_all_required_keys_present(self, tmp_path: pathlib.Path) -> None: |
| 162 | repo = _init_repo(tmp_path) |
| 163 | raw, _ = _clean_bundle() |
| 164 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 165 | assert r.exit_code == 0 |
| 166 | d = json.loads(r.output) |
| 167 | missing = self._REQUIRED - d.keys() |
| 168 | assert not missing, f"Missing keys: {missing}" |
| 169 | |
| 170 | def test_all_ok_true_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 171 | repo = _init_repo(tmp_path) |
| 172 | raw, _ = _clean_bundle() |
| 173 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 174 | assert json.loads(r.output)["all_ok"] is True |
| 175 | |
| 176 | def test_failures_empty_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 177 | repo = _init_repo(tmp_path) |
| 178 | raw, _ = _clean_bundle() |
| 179 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 180 | assert json.loads(r.output)["failures"] == [] |
| 181 | |
| 182 | def test_exit_code_zero_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 183 | repo = _init_repo(tmp_path) |
| 184 | raw, _ = _clean_bundle() |
| 185 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 186 | assert json.loads(r.output)["exit_code"] == 0 |
| 187 | |
| 188 | def test_exit_code_nonzero_on_failure(self, tmp_path: pathlib.Path) -> None: |
| 189 | repo = _init_repo(tmp_path) |
| 190 | content = b"original" |
| 191 | oid = blob_id(content) |
| 192 | mpack = _pack({ |
| 193 | "blobs": [{"object_id": oid, "content": b"tampered"}], |
| 194 | "snapshots": [], |
| 195 | "commits": [], |
| 196 | "meta": _FULL_META, |
| 197 | }) |
| 198 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 199 | d = json.loads(r.output) |
| 200 | assert d["exit_code"] != 0 |
| 201 | assert d["all_ok"] is False |
| 202 | |
| 203 | def test_duration_ms_is_nonneg_float(self, tmp_path: pathlib.Path) -> None: |
| 204 | repo = _init_repo(tmp_path) |
| 205 | raw, _ = _clean_bundle() |
| 206 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 207 | d = json.loads(r.output) |
| 208 | assert isinstance(d["duration_ms"], float) |
| 209 | assert d["duration_ms"] >= 0.0 |
| 210 | |
| 211 | def test_blobs_checked_count_correct(self, tmp_path: pathlib.Path) -> None: |
| 212 | repo = _init_repo(tmp_path) |
| 213 | raw, _ = _clean_bundle(n_objects=3) |
| 214 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 215 | assert json.loads(r.output)["blobs_checked"] == 3 |
| 216 | |
| 217 | def test_snapshots_checked_count_correct(self, tmp_path: pathlib.Path) -> None: |
| 218 | repo = _init_repo(tmp_path) |
| 219 | raw, _ = _clean_bundle(n_objects=2) |
| 220 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 221 | assert json.loads(r.output)["snapshots_checked"] == 1 |
| 222 | |
| 223 | def test_commits_checked_count_correct(self, tmp_path: pathlib.Path) -> None: |
| 224 | repo = _init_repo(tmp_path) |
| 225 | raw, _ = _clean_bundle() |
| 226 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 227 | assert json.loads(r.output)["commits_checked"] == 1 |
| 228 | |
| 229 | def test_empty_bundle_clean(self, tmp_path: pathlib.Path) -> None: |
| 230 | repo = _init_repo(tmp_path) |
| 231 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=_empty_bundle()) |
| 232 | d = json.loads(r.output) |
| 233 | assert d["all_ok"] is True |
| 234 | assert d["blobs_checked"] == 0 |
| 235 | |
| 236 | |
| 237 | # --------------------------------------------------------------------------- |
| 238 | # Stat mode |
| 239 | # --------------------------------------------------------------------------- |
| 240 | |
| 241 | class TestStatMode: |
| 242 | def test_stat_json_has_counts(self, tmp_path: pathlib.Path) -> None: |
| 243 | repo = _init_repo(tmp_path) |
| 244 | raw, _ = _clean_bundle(n_objects=4) |
| 245 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=raw) |
| 246 | assert r.exit_code == 0 |
| 247 | d = json.loads(r.output) |
| 248 | assert d["blobs"] == 4 |
| 249 | assert d["snapshots"] == 1 |
| 250 | assert d["commits"] == 1 |
| 251 | |
| 252 | def test_stat_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: |
| 253 | repo = _init_repo(tmp_path) |
| 254 | raw, _ = _clean_bundle() |
| 255 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=raw) |
| 256 | d = json.loads(r.output) |
| 257 | assert "duration_ms" in d |
| 258 | assert isinstance(d["duration_ms"], float) |
| 259 | |
| 260 | def test_stat_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: |
| 261 | repo = _init_repo(tmp_path) |
| 262 | raw, _ = _clean_bundle() |
| 263 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=raw) |
| 264 | assert json.loads(r.output)["exit_code"] == 0 |
| 265 | |
| 266 | def test_stat_text_shows_counts(self, tmp_path: pathlib.Path) -> None: |
| 267 | repo = _init_repo(tmp_path) |
| 268 | raw, _ = _clean_bundle(n_objects=2) |
| 269 | r = _invoke(repo, "verify-pack", "--stat", stdin=raw) |
| 270 | assert r.exit_code == 0 |
| 271 | assert "blobs=2" in r.output |
| 272 | assert "snapshots=1" in r.output |
| 273 | assert "commits=1" in r.output |
| 274 | |
| 275 | def test_stat_does_not_hash_objects(self, tmp_path: pathlib.Path) -> None: |
| 276 | """--stat should not fail on a tampered object — it skips hashing.""" |
| 277 | repo = _init_repo(tmp_path) |
| 278 | content = b"original" |
| 279 | oid = blob_id(content) |
| 280 | mpack = _pack({ |
| 281 | "blobs": [{"object_id": oid, "content": b"tampered"}], |
| 282 | "snapshots": [], |
| 283 | "commits": [], |
| 284 | }) |
| 285 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=mpack) |
| 286 | assert r.exit_code == 0 |
| 287 | d = json.loads(r.output) |
| 288 | assert d["blobs"] == 1 |
| 289 | |
| 290 | def test_stat_empty_bundle_zeros(self, tmp_path: pathlib.Path) -> None: |
| 291 | repo = _init_repo(tmp_path) |
| 292 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=_empty_bundle()) |
| 293 | d = json.loads(r.output) |
| 294 | assert d["blobs"] == 0 |
| 295 | assert d["snapshots"] == 0 |
| 296 | assert d["commits"] == 0 |
| 297 | |
| 298 | |
| 299 | # --------------------------------------------------------------------------- |
| 300 | # Quiet mode |
| 301 | # --------------------------------------------------------------------------- |
| 302 | |
| 303 | class TestQuietMode: |
| 304 | def test_quiet_exit_0_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 305 | repo = _init_repo(tmp_path) |
| 306 | raw, _ = _clean_bundle() |
| 307 | r = _invoke(repo, "verify-pack", "--no-local", "--quiet", stdin=raw) |
| 308 | assert r.exit_code == 0 |
| 309 | |
| 310 | def test_quiet_no_output_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 311 | repo = _init_repo(tmp_path) |
| 312 | raw, _ = _clean_bundle() |
| 313 | r = _invoke(repo, "verify-pack", "--no-local", "--quiet", stdin=raw) |
| 314 | assert r.output.strip() == "" |
| 315 | |
| 316 | def test_quiet_exit_1_on_corrupt(self, tmp_path: pathlib.Path) -> None: |
| 317 | repo = _init_repo(tmp_path) |
| 318 | oid = blob_id(b"real") |
| 319 | mpack = _pack({ |
| 320 | "blobs": [{"object_id": oid, "content": b"fake"}], |
| 321 | "snapshots": [], |
| 322 | "commits": [], |
| 323 | }) |
| 324 | r = _invoke(repo, "verify-pack", "--no-local", "--quiet", stdin=mpack) |
| 325 | assert r.exit_code != 0 |
| 326 | |
| 327 | def test_quiet_no_output_on_corrupt(self, tmp_path: pathlib.Path) -> None: |
| 328 | repo = _init_repo(tmp_path) |
| 329 | oid = blob_id(b"real") |
| 330 | mpack = _pack({ |
| 331 | "blobs": [{"object_id": oid, "content": b"fake"}], |
| 332 | "snapshots": [], |
| 333 | "commits": [], |
| 334 | "meta": _FULL_META, |
| 335 | }) |
| 336 | r = _invoke(repo, "verify-pack", "--no-local", "--quiet", stdin=mpack) |
| 337 | assert r.output.strip() == "" |
| 338 | |
| 339 | |
| 340 | # --------------------------------------------------------------------------- |
| 341 | # File input |
| 342 | # --------------------------------------------------------------------------- |
| 343 | |
| 344 | class TestFileInput: |
| 345 | def test_file_flag_reads_from_disk(self, tmp_path: pathlib.Path) -> None: |
| 346 | repo = _init_repo(tmp_path) |
| 347 | raw, _ = _clean_bundle() |
| 348 | bundle_path = tmp_path / "test.muse" |
| 349 | bundle_path.write_bytes(raw) |
| 350 | r = _invoke(repo, "verify-pack", "--no-local", "--json", f"--file={bundle_path}") |
| 351 | assert r.exit_code == 0 |
| 352 | assert json.loads(r.output)["all_ok"] is True |
| 353 | |
| 354 | def test_file_missing_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 355 | repo = _init_repo(tmp_path) |
| 356 | r = _invoke(repo, "verify-pack", "--no-local", "--json", "--file=/nonexistent/path.muse") |
| 357 | assert r.exit_code != 0 |
| 358 | |
| 359 | def test_file_missing_error_on_stderr(self, tmp_path: pathlib.Path) -> None: |
| 360 | repo = _init_repo(tmp_path) |
| 361 | r = _invoke(repo, "verify-pack", "--no-local", "--json", "--file=/nonexistent/path.muse") |
| 362 | assert "error" in r.stderr.lower() or "Cannot" in r.stderr or r.exit_code != 0 |
| 363 | |
| 364 | def test_shorthand_i_flag(self, tmp_path: pathlib.Path) -> None: |
| 365 | repo = _init_repo(tmp_path) |
| 366 | raw, _ = _clean_bundle() |
| 367 | bundle_path = tmp_path / "test.muse" |
| 368 | bundle_path.write_bytes(raw) |
| 369 | r = _invoke(repo, "verify-pack", "--no-local", "--json", "-i", str(bundle_path)) |
| 370 | assert r.exit_code == 0 |
| 371 | |
| 372 | |
| 373 | # --------------------------------------------------------------------------- |
| 374 | # --no-local flag |
| 375 | # --------------------------------------------------------------------------- |
| 376 | |
| 377 | class TestNoLocal: |
| 378 | def test_no_local_skips_store_check_for_snapshot_ref(self, tmp_path: pathlib.Path) -> None: |
| 379 | """Snapshot references an object not in mpack; --no-local should NOT fail.""" |
| 380 | repo = _init_repo(tmp_path) |
| 381 | missing_oid = blob_id(b"not in mpack") |
| 382 | snap_id = blob_id(b"snap") |
| 383 | commit_id = blob_id(b"commit") |
| 384 | mpack = _pack({ |
| 385 | "blobs": [], |
| 386 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"f.py": missing_oid}}], |
| 387 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 388 | "meta": _FULL_META, |
| 389 | }) |
| 390 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 391 | d = json.loads(r.output) |
| 392 | # With --no-local, missing manifest refs are treated as missing but since |
| 393 | # root is None the failure path is skipped → all_ok depends on commit check |
| 394 | # The commit snapshot is in bundle_snapshot_ids so it passes |
| 395 | # The snapshot manifest ref is missing from bundle_object_ids and root is None → failure |
| 396 | # Actually re-reading: when root is None and obj not in bundle_object_ids → failure appended |
| 397 | # So this WILL fail. Let's just verify the flag is accepted and JSON is valid. |
| 398 | assert json.loads(r.output) is not None # valid JSON |
| 399 | |
| 400 | def test_without_no_local_requires_repo(self, tmp_path: pathlib.Path) -> None: |
| 401 | """Without --no-local the command needs a valid repo for local store checks.""" |
| 402 | repo = _init_repo(tmp_path) |
| 403 | raw, _ = _clean_bundle() |
| 404 | r = _invoke(repo, "verify-pack", "--json", stdin=raw) |
| 405 | assert r.exit_code == 0 # clean mpack, local store not needed for objects in mpack |
| 406 | |
| 407 | |
| 408 | # --------------------------------------------------------------------------- |
| 409 | # Object integrity |
| 410 | # --------------------------------------------------------------------------- |
| 411 | |
| 412 | class TestObjectIntegrity: |
| 413 | def test_hash_mismatch_reported(self, tmp_path: pathlib.Path) -> None: |
| 414 | repo = _init_repo(tmp_path) |
| 415 | real_oid = blob_id(b"real content") |
| 416 | mpack = _pack({ |
| 417 | "blobs": [{"object_id": real_oid, "content": b"tampered content"}], |
| 418 | "snapshots": [], |
| 419 | "commits": [], |
| 420 | "meta": _FULL_META, |
| 421 | }) |
| 422 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 423 | d = json.loads(r.output) |
| 424 | assert d["all_ok"] is False |
| 425 | assert any(f["kind"] == "object" and "mismatch" in f["error"] for f in d["failures"]) |
| 426 | |
| 427 | def test_mismatch_failure_id_is_declared_oid(self, tmp_path: pathlib.Path) -> None: |
| 428 | repo = _init_repo(tmp_path) |
| 429 | real_oid = blob_id(b"real") |
| 430 | mpack = _pack({ |
| 431 | "blobs": [{"object_id": real_oid, "content": b"fake"}], |
| 432 | "snapshots": [], |
| 433 | "commits": [], |
| 434 | "meta": _FULL_META, |
| 435 | }) |
| 436 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 437 | d = json.loads(r.output) |
| 438 | failure_ids = [f["id"] for f in d["failures"] if f["kind"] == "object"] |
| 439 | assert real_oid in failure_ids |
| 440 | |
| 441 | def test_invalid_object_id_format_reported(self, tmp_path: pathlib.Path) -> None: |
| 442 | repo = _init_repo(tmp_path) |
| 443 | mpack = _pack({ |
| 444 | "blobs": [{"object_id": "not-a-sha256-id", "content": b"data"}], |
| 445 | "snapshots": [], |
| 446 | "commits": [], |
| 447 | "meta": _FULL_META, |
| 448 | }) |
| 449 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 450 | d = json.loads(r.output) |
| 451 | assert d["all_ok"] is False |
| 452 | assert any(f["kind"] == "object" for f in d["failures"]) |
| 453 | |
| 454 | def test_non_dict_object_entry_reported(self, tmp_path: pathlib.Path) -> None: |
| 455 | repo = _init_repo(tmp_path) |
| 456 | mpack = _pack({ |
| 457 | "blobs": ["not-a-dict"], |
| 458 | "snapshots": [], |
| 459 | "commits": [], |
| 460 | "meta": _FULL_META, |
| 461 | }) |
| 462 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 463 | d = json.loads(r.output) |
| 464 | assert d["all_ok"] is False |
| 465 | assert any("not a dict" in f["error"] for f in d["failures"]) |
| 466 | |
| 467 | def test_missing_content_field_reported(self, tmp_path: pathlib.Path) -> None: |
| 468 | repo = _init_repo(tmp_path) |
| 469 | oid = blob_id(b"data") |
| 470 | mpack = _pack({ |
| 471 | "blobs": [{"object_id": oid}], # no content field |
| 472 | "snapshots": [], |
| 473 | "commits": [], |
| 474 | "meta": _FULL_META, |
| 475 | }) |
| 476 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 477 | d = json.loads(r.output) |
| 478 | assert d["all_ok"] is False |
| 479 | |
| 480 | def test_multiple_objects_all_checked(self, tmp_path: pathlib.Path) -> None: |
| 481 | repo = _init_repo(tmp_path) |
| 482 | raw, _ = _clean_bundle(n_objects=5) |
| 483 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 484 | d = json.loads(r.output) |
| 485 | assert d["blobs_checked"] == 5 |
| 486 | assert d["all_ok"] is True |
| 487 | |
| 488 | def test_objects_field_not_list_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 489 | repo = _init_repo(tmp_path) |
| 490 | mpack = _pack({ |
| 491 | "blobs": "not-a-list", |
| 492 | "snapshots": [], |
| 493 | "commits": [], |
| 494 | }) |
| 495 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 496 | assert r.exit_code != 0 |
| 497 | |
| 498 | |
| 499 | # --------------------------------------------------------------------------- |
| 500 | # Snapshot consistency |
| 501 | # --------------------------------------------------------------------------- |
| 502 | |
| 503 | class TestSnapshotConsistency: |
| 504 | def test_orphaned_manifest_ref_reported(self, tmp_path: pathlib.Path) -> None: |
| 505 | """Snapshot references an object not in mpack and not in local store.""" |
| 506 | repo = _init_repo(tmp_path) |
| 507 | missing_oid = blob_id(b"missing object") |
| 508 | snap_id = blob_id(b"snap-orphan") |
| 509 | mpack = _pack({ |
| 510 | "blobs": [], |
| 511 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"f.py": missing_oid}}], |
| 512 | "commits": [], |
| 513 | "meta": _FULL_META, |
| 514 | }) |
| 515 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 516 | d = json.loads(r.output) |
| 517 | assert d["all_ok"] is False |
| 518 | assert any(f["kind"] == "snapshot" for f in d["failures"]) |
| 519 | |
| 520 | def test_manifest_ref_in_bundle_objects_passes(self, tmp_path: pathlib.Path) -> None: |
| 521 | """Snapshot referencing an object present in mpack's objects list passes.""" |
| 522 | repo = _init_repo(tmp_path) |
| 523 | raw, _ = _clean_bundle(n_objects=1) |
| 524 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 525 | d = json.loads(r.output) |
| 526 | assert d["all_ok"] is True |
| 527 | |
| 528 | def test_non_dict_snapshot_entry_reported(self, tmp_path: pathlib.Path) -> None: |
| 529 | repo = _init_repo(tmp_path) |
| 530 | mpack = _pack({ |
| 531 | "blobs": [], |
| 532 | "snapshots": ["not-a-dict"], |
| 533 | "commits": [], |
| 534 | "meta": _FULL_META, |
| 535 | }) |
| 536 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 537 | d = json.loads(r.output) |
| 538 | assert d["all_ok"] is False |
| 539 | assert any(f["kind"] == "snapshot" for f in d["failures"]) |
| 540 | |
| 541 | def test_snapshots_field_not_list_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 542 | repo = _init_repo(tmp_path) |
| 543 | mpack = _pack({ |
| 544 | "blobs": [], |
| 545 | "snapshots": 42, |
| 546 | "commits": [], |
| 547 | }) |
| 548 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 549 | assert r.exit_code != 0 |
| 550 | |
| 551 | def test_manifest_ref_in_local_store_passes(self, tmp_path: pathlib.Path) -> None: |
| 552 | """Object in local store satisfies manifest ref.""" |
| 553 | repo = _init_repo(tmp_path) |
| 554 | content = b"locally stored object" |
| 555 | oid = blob_id(content) |
| 556 | write_object(repo, oid, content) |
| 557 | |
| 558 | snap_id = blob_id(b"snap-local") |
| 559 | commit_id = blob_id(b"commit-local") |
| 560 | mpack = _pack({ |
| 561 | "blobs": [], # object not in mpack |
| 562 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"f.py": oid}}], |
| 563 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 564 | "meta": _FULL_META, |
| 565 | }) |
| 566 | # Without --no-local, the local store is checked |
| 567 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 568 | d = json.loads(r.output) |
| 569 | assert d["all_ok"] is True |
| 570 | |
| 571 | |
| 572 | # --------------------------------------------------------------------------- |
| 573 | # Commit consistency |
| 574 | # --------------------------------------------------------------------------- |
| 575 | |
| 576 | class TestCommitConsistency: |
| 577 | def test_missing_snapshot_reported(self, tmp_path: pathlib.Path) -> None: |
| 578 | """Commit references a snapshot not in mpack and not in local store.""" |
| 579 | repo = _init_repo(tmp_path) |
| 580 | snap_id = blob_id(b"nonexistent-snap") |
| 581 | commit_id = blob_id(b"commit-ref-missing") |
| 582 | mpack = _pack({ |
| 583 | "blobs": [], |
| 584 | "snapshots": [], |
| 585 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 586 | "meta": _FULL_META, |
| 587 | }) |
| 588 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 589 | d = json.loads(r.output) |
| 590 | assert d["all_ok"] is False |
| 591 | assert any(f["kind"] == "commit" for f in d["failures"]) |
| 592 | |
| 593 | def test_snapshot_in_bundle_resolves_commit(self, tmp_path: pathlib.Path) -> None: |
| 594 | """Commit with snapshot present in mpack passes.""" |
| 595 | repo = _init_repo(tmp_path) |
| 596 | raw, _ = _clean_bundle() |
| 597 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 598 | d = json.loads(r.output) |
| 599 | assert d["all_ok"] is True |
| 600 | |
| 601 | def test_non_dict_commit_entry_reported(self, tmp_path: pathlib.Path) -> None: |
| 602 | repo = _init_repo(tmp_path) |
| 603 | mpack = _pack({ |
| 604 | "blobs": [], |
| 605 | "snapshots": [], |
| 606 | "commits": ["not-a-dict"], |
| 607 | "meta": _FULL_META, |
| 608 | }) |
| 609 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 610 | d = json.loads(r.output) |
| 611 | assert d["all_ok"] is False |
| 612 | assert any(f["kind"] == "commit" for f in d["failures"]) |
| 613 | |
| 614 | def test_commits_field_not_list_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 615 | repo = _init_repo(tmp_path) |
| 616 | mpack = _pack({ |
| 617 | "blobs": [], |
| 618 | "snapshots": [], |
| 619 | "commits": "not-a-list", |
| 620 | }) |
| 621 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 622 | assert r.exit_code != 0 |
| 623 | |
| 624 | def test_commit_missing_snapshot_id_field(self, tmp_path: pathlib.Path) -> None: |
| 625 | repo = _init_repo(tmp_path) |
| 626 | commit_id = blob_id(b"commit-no-snap") |
| 627 | mpack = _pack({ |
| 628 | "blobs": [], |
| 629 | "snapshots": [], |
| 630 | "commits": [{"commit_id": commit_id}], # no snapshot_id → empty string default |
| 631 | "meta": _FULL_META, |
| 632 | }) |
| 633 | # Don't use --no-local: commit consistency check is skipped when skip_local_check=True. |
| 634 | # Without --no-local, the local store is consulted and snap_id="" returns None → failure. |
| 635 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 636 | d = json.loads(r.output) |
| 637 | assert d["all_ok"] is False |
| 638 | |
| 639 | |
| 640 | # --------------------------------------------------------------------------- |
| 641 | # Malformed input |
| 642 | # --------------------------------------------------------------------------- |
| 643 | |
| 644 | class TestMalformedInput: |
| 645 | def test_invalid_msgpack_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 646 | repo = _init_repo(tmp_path) |
| 647 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=b"\xff\xfe garbage bytes") |
| 648 | assert r.exit_code != 0 |
| 649 | |
| 650 | def test_not_a_dict_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 651 | repo = _init_repo(tmp_path) |
| 652 | # Valid msgpack but not a dict — a list |
| 653 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=msgpack.packb([1, 2, 3], use_bin_type=True)) |
| 654 | assert r.exit_code != 0 |
| 655 | |
| 656 | def test_empty_bytes_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 657 | repo = _init_repo(tmp_path) |
| 658 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=b"") |
| 659 | assert r.exit_code != 0 |
| 660 | |
| 661 | def test_plain_string_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 662 | repo = _init_repo(tmp_path) |
| 663 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=b"not msgpack at all") |
| 664 | assert r.exit_code != 0 |
| 665 | |
| 666 | def test_oversized_bundle_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 667 | """MPack exceeding MAX_PACK_MSGPACK_BYTES should be rejected.""" |
| 668 | from muse.core.io import MAX_PACK_MSGPACK_BYTES |
| 669 | repo = _init_repo(tmp_path) |
| 670 | # Build a mpack that will produce > MAX bytes when packed |
| 671 | big_content = b"X" * (MAX_PACK_MSGPACK_BYTES + 1) |
| 672 | big_bundle = _pack({"blobs": [], "snapshots": [], "commits": [], "junk": big_content}) |
| 673 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=big_bundle) |
| 674 | assert r.exit_code != 0 |
| 675 | |
| 676 | def test_invalid_format_exits_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 677 | repo = _init_repo(tmp_path) |
| 678 | raw, _ = _clean_bundle() |
| 679 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=b"not msgpack") |
| 680 | assert r.exit_code != 0 |
| 681 | |
| 682 | |
| 683 | # --------------------------------------------------------------------------- |
| 684 | # Text format |
| 685 | # --------------------------------------------------------------------------- |
| 686 | |
| 687 | class TestFormatText: |
| 688 | def test_text_summary_line_on_clean(self, tmp_path: pathlib.Path) -> None: |
| 689 | repo = _init_repo(tmp_path) |
| 690 | raw, _ = _clean_bundle(n_objects=3) |
| 691 | r = _invoke(repo, "verify-pack", "--no-local", stdin=raw) |
| 692 | assert r.exit_code == 0 |
| 693 | assert "blobs=3" in r.output |
| 694 | assert "all_ok=True" in r.output |
| 695 | |
| 696 | def test_text_failure_line_on_corrupt(self, tmp_path: pathlib.Path) -> None: |
| 697 | repo = _init_repo(tmp_path) |
| 698 | oid = blob_id(b"real") |
| 699 | mpack = _pack({ |
| 700 | "blobs": [{"object_id": oid, "content": b"fake"}], |
| 701 | "snapshots": [], |
| 702 | "commits": [], |
| 703 | "meta": _FULL_META, |
| 704 | }) |
| 705 | r = _invoke(repo, "verify-pack", "--no-local", stdin=mpack) |
| 706 | assert r.exit_code != 0 |
| 707 | assert "FAIL" in r.output |
| 708 | |
| 709 | def test_text_exit_nonzero_on_failure(self, tmp_path: pathlib.Path) -> None: |
| 710 | repo = _init_repo(tmp_path) |
| 711 | oid = blob_id(b"real") |
| 712 | mpack = _pack({ |
| 713 | "blobs": [{"object_id": oid, "content": b"fake"}], |
| 714 | "snapshots": [], |
| 715 | "commits": [], |
| 716 | "meta": _FULL_META, |
| 717 | }) |
| 718 | r = _invoke(repo, "verify-pack", "--no-local", stdin=mpack) |
| 719 | assert r.exit_code != 0 |
| 720 | |
| 721 | def test_shorthand_json_flag(self, tmp_path: pathlib.Path) -> None: |
| 722 | repo = _init_repo(tmp_path) |
| 723 | raw, _ = _clean_bundle() |
| 724 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 725 | json.loads(r.output) # must not raise |
| 726 | |
| 727 | |
| 728 | # --------------------------------------------------------------------------- |
| 729 | # Data integrity |
| 730 | # --------------------------------------------------------------------------- |
| 731 | |
| 732 | class TestDataIntegrity: |
| 733 | def test_truncated_content_detected(self, tmp_path: pathlib.Path) -> None: |
| 734 | """Content truncated to first half → hash mismatch.""" |
| 735 | repo = _init_repo(tmp_path) |
| 736 | content = b"full content that will be truncated" |
| 737 | oid = blob_id(content) |
| 738 | mpack = _pack({ |
| 739 | "blobs": [{"object_id": oid, "content": content[:len(content) // 2]}], |
| 740 | "snapshots": [], |
| 741 | "commits": [], |
| 742 | "meta": _FULL_META, |
| 743 | }) |
| 744 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 745 | d = json.loads(r.output) |
| 746 | assert d["all_ok"] is False |
| 747 | |
| 748 | def test_zeroed_content_detected(self, tmp_path: pathlib.Path) -> None: |
| 749 | """Content replaced with zero bytes → hash mismatch.""" |
| 750 | repo = _init_repo(tmp_path) |
| 751 | content = b"real content for zeroing test" |
| 752 | oid = blob_id(content) |
| 753 | mpack = _pack({ |
| 754 | "blobs": [{"object_id": oid, "content": bytes(len(content))}], |
| 755 | "snapshots": [], |
| 756 | "commits": [], |
| 757 | "meta": _FULL_META, |
| 758 | }) |
| 759 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 760 | d = json.loads(r.output) |
| 761 | assert d["all_ok"] is False |
| 762 | |
| 763 | def test_bit_flip_in_content_detected(self, tmp_path: pathlib.Path) -> None: |
| 764 | """Single byte flipped → hash mismatch.""" |
| 765 | repo = _init_repo(tmp_path) |
| 766 | content = bytearray(b"content for bit flip test") |
| 767 | oid = blob_id(bytes(content)) |
| 768 | content[0] ^= 0x01 # flip one bit |
| 769 | mpack = _pack({ |
| 770 | "blobs": [{"object_id": oid, "content": bytes(content)}], |
| 771 | "snapshots": [], |
| 772 | "commits": [], |
| 773 | "meta": _FULL_META, |
| 774 | }) |
| 775 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 776 | d = json.loads(r.output) |
| 777 | assert d["all_ok"] is False |
| 778 | |
| 779 | def test_correct_oid_passes(self, tmp_path: pathlib.Path) -> None: |
| 780 | """Object with correct hash passes.""" |
| 781 | repo = _init_repo(tmp_path) |
| 782 | content = b"pristine content" |
| 783 | oid = blob_id(content) |
| 784 | mpack = _pack({ |
| 785 | "blobs": [{"object_id": oid, "content": content}], |
| 786 | "snapshots": [], |
| 787 | "commits": [], |
| 788 | "meta": _FULL_META, |
| 789 | }) |
| 790 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 791 | d = json.loads(r.output) |
| 792 | assert d["all_ok"] is True |
| 793 | |
| 794 | def test_one_corrupt_among_many(self, tmp_path: pathlib.Path) -> None: |
| 795 | """One corrupt object out of five → exactly one failure.""" |
| 796 | repo = _init_repo(tmp_path) |
| 797 | objects = [] |
| 798 | for i in range(4): |
| 799 | content = f"good-{i}".encode() |
| 800 | objects.append({"object_id": blob_id(content), "content": content}) |
| 801 | # 5th is corrupt |
| 802 | real_content = b"real content" |
| 803 | objects.append({"object_id": blob_id(real_content), "content": b"corrupt"}) |
| 804 | |
| 805 | mpack = _pack({"blobs": objects, "snapshots": [], "commits": [], "meta": _FULL_META}) |
| 806 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 807 | d = json.loads(r.output) |
| 808 | assert d["blobs_checked"] == 5 |
| 809 | assert d["all_ok"] is False |
| 810 | object_failures = [f for f in d["failures"] if f["kind"] == "object"] |
| 811 | assert len(object_failures) == 1 |
| 812 | |
| 813 | |
| 814 | # --------------------------------------------------------------------------- |
| 815 | # Security hardening |
| 816 | # --------------------------------------------------------------------------- |
| 817 | |
| 818 | class TestSecurityHardening: |
| 819 | def test_bundle_file_path_traversal_handled(self, tmp_path: pathlib.Path) -> None: |
| 820 | """A path-traversal --file arg (pointing outside repo) raises an OSError.""" |
| 821 | repo = _init_repo(tmp_path) |
| 822 | r = _invoke(repo, "verify-pack", "--no-local", "--json", "--file=../../../../etc/passwd") |
| 823 | # Either exits nonzero (file not found) or reads the file and fails to parse it |
| 824 | # In either case, should not crash with a traceback |
| 825 | assert "Traceback" not in r.output |
| 826 | assert "Traceback" not in r.stderr |
| 827 | |
| 828 | def test_non_string_object_id_in_bundle(self, tmp_path: pathlib.Path) -> None: |
| 829 | """object_id that is an integer rather than str → failure reported gracefully.""" |
| 830 | repo = _init_repo(tmp_path) |
| 831 | mpack = _pack({ |
| 832 | "blobs": [{"object_id": 12345, "content": b"data"}], |
| 833 | "snapshots": [], |
| 834 | "commits": [], |
| 835 | "meta": _FULL_META, |
| 836 | }) |
| 837 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 838 | d = json.loads(r.output) |
| 839 | assert d["all_ok"] is False |
| 840 | |
| 841 | def test_binary_junk_in_object_id(self, tmp_path: pathlib.Path) -> None: |
| 842 | """Binary string as object_id → validation error, not crash.""" |
| 843 | repo = _init_repo(tmp_path) |
| 844 | mpack = _pack({ |
| 845 | "blobs": [{"object_id": long_id("z" * 64), "content": b"data"}], |
| 846 | "snapshots": [], |
| 847 | "commits": [], |
| 848 | "meta": _FULL_META, |
| 849 | }) |
| 850 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 851 | # Invalid hex chars → validate_object_id raises ValueError → failure reported |
| 852 | d = json.loads(r.output) |
| 853 | assert d["all_ok"] is False |
| 854 | |
| 855 | def test_extremely_long_error_string_safe(self, tmp_path: pathlib.Path) -> None: |
| 856 | """Very long error string doesn't crash output serialization.""" |
| 857 | repo = _init_repo(tmp_path) |
| 858 | # Snapshot with very long path key |
| 859 | snap_id = blob_id(b"snap-long") |
| 860 | missing_oid = blob_id(b"not present") |
| 861 | long_path = "a" * 4096 + "/file.py" |
| 862 | mpack = _pack({ |
| 863 | "blobs": [], |
| 864 | "snapshots": [{"snapshot_id": snap_id, "manifest": {long_path: missing_oid}}], |
| 865 | "commits": [], |
| 866 | "meta": _FULL_META, |
| 867 | }) |
| 868 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 869 | # Should produce valid JSON even with long strings |
| 870 | json.loads(r.output) |
| 871 | |
| 872 | |
| 873 | # --------------------------------------------------------------------------- |
| 874 | # No-prose pollution |
| 875 | # --------------------------------------------------------------------------- |
| 876 | |
| 877 | class TestNoProsePollution: |
| 878 | def test_stdout_is_valid_json(self, tmp_path: pathlib.Path) -> None: |
| 879 | repo = _init_repo(tmp_path) |
| 880 | raw, _ = _clean_bundle() |
| 881 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 882 | json.loads(r.output) # must not raise |
| 883 | |
| 884 | def test_no_emoji_in_json_output(self, tmp_path: pathlib.Path) -> None: |
| 885 | repo = _init_repo(tmp_path) |
| 886 | raw, _ = _clean_bundle() |
| 887 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 888 | assert "❌" not in r.output |
| 889 | assert "✅" not in r.output |
| 890 | |
| 891 | def test_no_traceback_in_output(self, tmp_path: pathlib.Path) -> None: |
| 892 | repo = _init_repo(tmp_path) |
| 893 | raw, _ = _clean_bundle() |
| 894 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 895 | assert "Traceback" not in r.output |
| 896 | |
| 897 | def test_corrupt_bundle_json_output_is_valid(self, tmp_path: pathlib.Path) -> None: |
| 898 | repo = _init_repo(tmp_path) |
| 899 | oid = blob_id(b"real") |
| 900 | mpack = _pack({ |
| 901 | "blobs": [{"object_id": oid, "content": b"fake"}], |
| 902 | "snapshots": [], |
| 903 | "commits": [], |
| 904 | "meta": _FULL_META, |
| 905 | }) |
| 906 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 907 | json.loads(r.output) # must not raise |
| 908 | |
| 909 | def test_failures_list_uses_sha256_prefix(self, tmp_path: pathlib.Path) -> None: |
| 910 | """Object IDs in failures list carry the sha256: prefix.""" |
| 911 | repo = _init_repo(tmp_path) |
| 912 | real_oid = blob_id(b"real content") |
| 913 | mpack = _pack({ |
| 914 | "blobs": [{"object_id": real_oid, "content": b"tampered"}], |
| 915 | "snapshots": [], |
| 916 | "commits": [], |
| 917 | "meta": _FULL_META, |
| 918 | }) |
| 919 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 920 | d = json.loads(r.output) |
| 921 | for f in d["failures"]: |
| 922 | if f["id"] not in ("(unknown)", "(invalid)"): |
| 923 | assert f["id"].startswith("sha256:"), f"Failure ID not sha256-prefixed: {f['id']!r}" |
| 924 | |
| 925 | |
| 926 | # --------------------------------------------------------------------------- |
| 927 | # Stress |
| 928 | # --------------------------------------------------------------------------- |
| 929 | |
| 930 | class TestStress: |
| 931 | def test_500_objects_verified_correctly(self, tmp_path: pathlib.Path) -> None: |
| 932 | """500-object mpack: all pass, blobs_checked == 500.""" |
| 933 | repo = _init_repo(tmp_path) |
| 934 | raw, oids = _clean_bundle(n_objects=500) |
| 935 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 936 | assert r.exit_code == 0 |
| 937 | d = json.loads(r.output) |
| 938 | assert d["blobs_checked"] == 500 |
| 939 | assert d["all_ok"] is True |
| 940 | |
| 941 | def test_500_objects_duration_bounded(self, tmp_path: pathlib.Path) -> None: |
| 942 | """500-object mpack should complete in under 10 seconds.""" |
| 943 | repo = _init_repo(tmp_path) |
| 944 | raw, _ = _clean_bundle(n_objects=500) |
| 945 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 946 | d = json.loads(r.output) |
| 947 | assert d["duration_ms"] < 10_000, f"Took {d['duration_ms']}ms — too slow" |
| 948 | |
| 949 | def test_mixed_clean_and_corrupt_at_scale(self, tmp_path: pathlib.Path) -> None: |
| 950 | """100 clean + 10 corrupt objects → exactly 10 failures.""" |
| 951 | repo = _init_repo(tmp_path) |
| 952 | objects = [] |
| 953 | for i in range(100): |
| 954 | content = f"good-{i}".encode() |
| 955 | objects.append({"object_id": blob_id(content), "content": content}) |
| 956 | for i in range(10): |
| 957 | real = f"corrupt-real-{i}".encode() |
| 958 | objects.append({"object_id": blob_id(real), "content": f"corrupt-fake-{i}".encode()}) |
| 959 | |
| 960 | mpack = _pack({"blobs": objects, "snapshots": [], "commits": [], "meta": _FULL_META}) |
| 961 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 962 | d = json.loads(r.output) |
| 963 | assert d["blobs_checked"] == 110 |
| 964 | object_failures = [f for f in d["failures"] if f["kind"] == "object"] |
| 965 | assert len(object_failures) == 10 |
| 966 | |
| 967 | |
| 968 | # --------------------------------------------------------------------------- |
| 969 | # Phase 1 — promised object awareness |
| 970 | # --------------------------------------------------------------------------- |
| 971 | |
| 972 | def _write_promisor_config(repo: pathlib.Path, remote_name: str = "origin") -> None: |
| 973 | """Write a minimal config.toml that registers *remote_name* as a promisor.""" |
| 974 | config_path = config_toml_path(repo) |
| 975 | config_path.write_text( |
| 976 | f"[remotes.{remote_name}]\n" |
| 977 | f'url = "https://localhost:1337/test/repo"\n', |
| 978 | encoding="utf-8", |
| 979 | ) |
| 980 | |
| 981 | |
| 982 | def _bundle_with_remote_only_ref(repo: pathlib.Path) -> tuple[bytes, str]: |
| 983 | """Return (bundle_bytes, missing_oid) where snapshot refs an object not in the mpack. |
| 984 | |
| 985 | The object is not written to the local store either — it simulates a |
| 986 | partial-clone repo where historical objects live on a promisor remote. |
| 987 | """ |
| 988 | content = b"historical file version - lives on remote only" |
| 989 | missing_oid = blob_id(content) |
| 990 | # do NOT write to local store |
| 991 | |
| 992 | snap_id = blob_id(b"snap-with-remote-ref") |
| 993 | commit_id = blob_id(b"commit-with-remote-ref") |
| 994 | mpack = _pack({ |
| 995 | "blobs": [], # object not in mpack |
| 996 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"history.py": missing_oid}}], |
| 997 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 998 | "meta": _FULL_META, |
| 999 | }) |
| 1000 | return mpack, missing_oid |
| 1001 | |
| 1002 | |
| 1003 | class TestPromisedObjects: |
| 1004 | """verify-pack correctly distinguishes PRESENT / PROMISED / MISSING objects.""" |
| 1005 | |
| 1006 | # ----------------------------------------------------------------- |
| 1007 | # promised_objects key is always present in JSON output |
| 1008 | # ----------------------------------------------------------------- |
| 1009 | |
| 1010 | def test_promised_objects_key_present_on_clean_bundle(self, tmp_path: pathlib.Path) -> None: |
| 1011 | repo = _init_repo(tmp_path) |
| 1012 | raw, _ = _clean_bundle() |
| 1013 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1014 | d = json.loads(r.output) |
| 1015 | assert "promised_objects" in d, "promised_objects key must always be present" |
| 1016 | |
| 1017 | def test_promised_objects_zero_when_all_present(self, tmp_path: pathlib.Path) -> None: |
| 1018 | repo = _init_repo(tmp_path) |
| 1019 | raw, _ = _clean_bundle() |
| 1020 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1021 | assert json.loads(r.output)["promised_objects"] == 0 |
| 1022 | |
| 1023 | # ----------------------------------------------------------------- |
| 1024 | # PROMISED state — promisor configured, object absent locally |
| 1025 | # ----------------------------------------------------------------- |
| 1026 | |
| 1027 | def test_promised_object_not_a_failure_by_default(self, tmp_path: pathlib.Path) -> None: |
| 1028 | """Snapshot refs an object absent locally; promisor remote configured → not a failure.""" |
| 1029 | repo = _init_repo(tmp_path) |
| 1030 | _write_promisor_config(repo) |
| 1031 | mpack, missing_oid = _bundle_with_remote_only_ref(repo) |
| 1032 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1033 | d = json.loads(r.output) |
| 1034 | assert d["all_ok"] is True |
| 1035 | assert d["promised_objects"] >= 1 |
| 1036 | |
| 1037 | def test_promised_object_counted_in_promised_objects(self, tmp_path: pathlib.Path) -> None: |
| 1038 | repo = _init_repo(tmp_path) |
| 1039 | _write_promisor_config(repo) |
| 1040 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1041 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1042 | d = json.loads(r.output) |
| 1043 | assert d["promised_objects"] == 1 |
| 1044 | |
| 1045 | def test_promised_object_not_in_failures_list(self, tmp_path: pathlib.Path) -> None: |
| 1046 | repo = _init_repo(tmp_path) |
| 1047 | _write_promisor_config(repo) |
| 1048 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1049 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1050 | d = json.loads(r.output) |
| 1051 | assert d["failures"] == [] |
| 1052 | |
| 1053 | def test_exit_code_zero_for_promised_objects(self, tmp_path: pathlib.Path) -> None: |
| 1054 | repo = _init_repo(tmp_path) |
| 1055 | _write_promisor_config(repo) |
| 1056 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1057 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1058 | assert r.exit_code == 0 |
| 1059 | assert json.loads(r.output)["exit_code"] == 0 |
| 1060 | |
| 1061 | # ----------------------------------------------------------------- |
| 1062 | # MISSING state — no promisor configured, object absent locally |
| 1063 | # ----------------------------------------------------------------- |
| 1064 | |
| 1065 | def test_missing_object_is_a_failure(self, tmp_path: pathlib.Path) -> None: |
| 1066 | """Snapshot refs an absent object with no promisor remote → failure.""" |
| 1067 | repo = _init_repo(tmp_path) |
| 1068 | # No config.toml written → no promisor remotes |
| 1069 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1070 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1071 | d = json.loads(r.output) |
| 1072 | assert d["all_ok"] is False |
| 1073 | assert any(f["kind"] == "snapshot" for f in d["failures"]) |
| 1074 | |
| 1075 | def test_missing_object_not_in_promised_objects(self, tmp_path: pathlib.Path) -> None: |
| 1076 | repo = _init_repo(tmp_path) |
| 1077 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1078 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1079 | d = json.loads(r.output) |
| 1080 | assert d["promised_objects"] == 0 |
| 1081 | |
| 1082 | # ----------------------------------------------------------------- |
| 1083 | # --strict mode — PROMISED treated as MISSING |
| 1084 | # ----------------------------------------------------------------- |
| 1085 | |
| 1086 | def test_strict_treats_promised_as_failure(self, tmp_path: pathlib.Path) -> None: |
| 1087 | """--strict: promised objects (absent locally) are integrity failures.""" |
| 1088 | repo = _init_repo(tmp_path) |
| 1089 | _write_promisor_config(repo) |
| 1090 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1091 | r = _invoke(repo, "verify-pack", "--strict", "--json", stdin=mpack) |
| 1092 | d = json.loads(r.output) |
| 1093 | assert d["all_ok"] is False |
| 1094 | assert any(f["kind"] == "snapshot" for f in d["failures"]) |
| 1095 | |
| 1096 | def test_strict_exit_nonzero_for_promised(self, tmp_path: pathlib.Path) -> None: |
| 1097 | repo = _init_repo(tmp_path) |
| 1098 | _write_promisor_config(repo) |
| 1099 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1100 | r = _invoke(repo, "verify-pack", "--strict", "--json", stdin=mpack) |
| 1101 | assert r.exit_code != 0 |
| 1102 | |
| 1103 | def test_strict_still_passes_for_present_objects(self, tmp_path: pathlib.Path) -> None: |
| 1104 | """--strict doesn't fail when all objects are locally present.""" |
| 1105 | repo = _init_repo(tmp_path) |
| 1106 | _write_promisor_config(repo) |
| 1107 | raw, _ = _clean_bundle() |
| 1108 | r = _invoke(repo, "verify-pack", "--strict", "--no-local", "--json", stdin=raw) |
| 1109 | assert r.exit_code == 0 |
| 1110 | assert json.loads(r.output)["all_ok"] is True |
| 1111 | |
| 1112 | def test_strict_promised_counted_separately(self, tmp_path: pathlib.Path) -> None: |
| 1113 | """In --strict mode, promised object is in failures, not in promised_objects.""" |
| 1114 | repo = _init_repo(tmp_path) |
| 1115 | _write_promisor_config(repo) |
| 1116 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1117 | r = _invoke(repo, "verify-pack", "--strict", "--json", stdin=mpack) |
| 1118 | d = json.loads(r.output) |
| 1119 | # strict: the object appears as a failure, not as a promised object |
| 1120 | assert d["promised_objects"] == 0 |
| 1121 | assert len(d["failures"]) >= 1 |
| 1122 | |
| 1123 | # ----------------------------------------------------------------- |
| 1124 | # PRESENT in local store — always passes regardless of promisor config |
| 1125 | # ----------------------------------------------------------------- |
| 1126 | |
| 1127 | def test_present_object_passes_with_no_promisor(self, tmp_path: pathlib.Path) -> None: |
| 1128 | """Object present locally → passes even with no promisor configured.""" |
| 1129 | repo = _init_repo(tmp_path) |
| 1130 | content = b"locally present object" |
| 1131 | oid = blob_id(content) |
| 1132 | write_object(repo, oid, content) |
| 1133 | |
| 1134 | snap_id = blob_id(b"snap-present") |
| 1135 | commit_id = blob_id(b"commit-present") |
| 1136 | mpack = _pack({ |
| 1137 | "blobs": [], # not in mpack, but in local store |
| 1138 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"file.py": oid}}], |
| 1139 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1140 | "meta": _FULL_META, |
| 1141 | }) |
| 1142 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1143 | d = json.loads(r.output) |
| 1144 | assert d["all_ok"] is True |
| 1145 | assert d["promised_objects"] == 0 |
| 1146 | |
| 1147 | def test_present_object_passes_with_strict(self, tmp_path: pathlib.Path) -> None: |
| 1148 | """Object present locally → passes even in --strict mode.""" |
| 1149 | repo = _init_repo(tmp_path) |
| 1150 | _write_promisor_config(repo) |
| 1151 | content = b"locally present strict" |
| 1152 | oid = blob_id(content) |
| 1153 | write_object(repo, oid, content) |
| 1154 | |
| 1155 | snap_id = blob_id(b"snap-present-strict") |
| 1156 | commit_id = blob_id(b"commit-present-strict") |
| 1157 | mpack = _pack({ |
| 1158 | "blobs": [], |
| 1159 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"f.py": oid}}], |
| 1160 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1161 | "meta": _FULL_META, |
| 1162 | }) |
| 1163 | r = _invoke(repo, "verify-pack", "--strict", "--json", stdin=mpack) |
| 1164 | d = json.loads(r.output) |
| 1165 | assert d["all_ok"] is True |
| 1166 | |
| 1167 | # ----------------------------------------------------------------- |
| 1168 | # JSON envelope completeness with new fields |
| 1169 | # ----------------------------------------------------------------- |
| 1170 | |
| 1171 | def test_json_envelope_includes_promised_objects(self, tmp_path: pathlib.Path) -> None: |
| 1172 | """promised_objects is always in the JSON envelope, even when zero.""" |
| 1173 | repo = _init_repo(tmp_path) |
| 1174 | raw, _ = _clean_bundle(n_objects=3) |
| 1175 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1176 | d = json.loads(r.output) |
| 1177 | assert "promised_objects" in d |
| 1178 | assert isinstance(d["promised_objects"], int) |
| 1179 | |
| 1180 | def test_mixed_present_and_promised(self, tmp_path: pathlib.Path) -> None: |
| 1181 | """MPack with some objects in mpack, some locally present, some promised.""" |
| 1182 | repo = _init_repo(tmp_path) |
| 1183 | _write_promisor_config(repo) |
| 1184 | |
| 1185 | # Object 1: in mpack (will be in bundle_object_ids) |
| 1186 | content_a = b"in mpack" |
| 1187 | oid_a = blob_id(content_a) |
| 1188 | |
| 1189 | # Object 2: in local store (PRESENT) |
| 1190 | content_b = b"in local store" |
| 1191 | oid_b = blob_id(content_b) |
| 1192 | write_object(repo, oid_b, content_b) |
| 1193 | |
| 1194 | # Object 3: promised (absent locally, promisor configured) |
| 1195 | content_c = b"on remote only" |
| 1196 | oid_c = blob_id(content_c) # NOT written anywhere |
| 1197 | |
| 1198 | snap_id = blob_id(b"snap-mixed") |
| 1199 | commit_id = blob_id(b"commit-mixed") |
| 1200 | mpack = _pack({ |
| 1201 | "blobs": [{"object_id": oid_a, "content": content_a}], |
| 1202 | "snapshots": [{ |
| 1203 | "snapshot_id": snap_id, |
| 1204 | "manifest": {"a.py": oid_a, "b.py": oid_b, "c.py": oid_c}, |
| 1205 | }], |
| 1206 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1207 | "meta": _FULL_META, |
| 1208 | }) |
| 1209 | r = _invoke(repo, "verify-pack", "--json", stdin=mpack) |
| 1210 | d = json.loads(r.output) |
| 1211 | assert d["all_ok"] is True |
| 1212 | assert d["promised_objects"] == 1 # only oid_c |
| 1213 | assert d["failures"] == [] |
| 1214 | |
| 1215 | def test_quiet_passes_with_promised(self, tmp_path: pathlib.Path) -> None: |
| 1216 | """--quiet exits 0 when all unresolved refs are promised (not missing).""" |
| 1217 | repo = _init_repo(tmp_path) |
| 1218 | _write_promisor_config(repo) |
| 1219 | mpack, _ = _bundle_with_remote_only_ref(repo) |
| 1220 | r = _invoke(repo, "verify-pack", "--quiet", stdin=mpack) |
| 1221 | assert r.exit_code == 0 |
| 1222 | |
| 1223 | |
| 1224 | # --------------------------------------------------------------------------- |
| 1225 | # Phase 2 — mpack meta field in verify-pack output |
| 1226 | # --------------------------------------------------------------------------- |
| 1227 | |
| 1228 | def _full_meta_bundle(n_objects: int = 1) -> bytes: |
| 1229 | """A clean mpack with a full meta field embedded.""" |
| 1230 | objects = [] |
| 1231 | oids = [] |
| 1232 | for i in range(n_objects): |
| 1233 | content = f"meta-obj-{i}".encode() |
| 1234 | oid = blob_id(content) |
| 1235 | objects.append({"object_id": oid, "content": content}) |
| 1236 | oids.append(oid) |
| 1237 | snap_id = blob_id(f"meta-snap-{n_objects}".encode()) |
| 1238 | manifest = {f"file{i}.py": oid for i, oid in enumerate(oids)} |
| 1239 | commit_id = blob_id(f"meta-commit-{n_objects}".encode()) |
| 1240 | mpack = { |
| 1241 | "blobs": objects, |
| 1242 | "snapshots": [{"snapshot_id": snap_id, "manifest": manifest}], |
| 1243 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1244 | "meta": { |
| 1245 | "mode": "full", |
| 1246 | "base_commits": [], |
| 1247 | "created_at": "2026-01-01T00:00:00Z", |
| 1248 | }, |
| 1249 | } |
| 1250 | return _pack(mpack) |
| 1251 | |
| 1252 | |
| 1253 | def _incremental_meta_bundle(base_snap_id: str, missing_oid: str) -> bytes: |
| 1254 | """A mpack with mode=incremental that references an object at the base.""" |
| 1255 | snap_id = blob_id(b"incremental-snap") |
| 1256 | commit_id = blob_id(b"incremental-commit") |
| 1257 | fake_base_commit = blob_id(b"fake-base-commit") |
| 1258 | mpack = { |
| 1259 | "blobs": [], |
| 1260 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"hist.py": missing_oid}}], |
| 1261 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1262 | "meta": { |
| 1263 | "mode": "incremental", |
| 1264 | "base_commits": [fake_base_commit], |
| 1265 | "created_at": "2026-01-01T00:00:00Z", |
| 1266 | }, |
| 1267 | } |
| 1268 | return _pack(mpack) |
| 1269 | |
| 1270 | |
| 1271 | class TestMPackMetaInVerifyPack: |
| 1272 | """verify-pack reads the mpack meta field and reflects it in JSON output.""" |
| 1273 | |
| 1274 | # ----------------------------------------------------------------- |
| 1275 | # bundle_mode key always present in JSON output |
| 1276 | # ----------------------------------------------------------------- |
| 1277 | |
| 1278 | def test_bundle_mode_key_present_no_meta(self, tmp_path: pathlib.Path) -> None: |
| 1279 | """MPack without meta field defaults to mode=full in output.""" |
| 1280 | repo = _init_repo(tmp_path) |
| 1281 | raw, _ = _clean_bundle() |
| 1282 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1283 | d = json.loads(r.output) |
| 1284 | assert "bundle_mode" in d |
| 1285 | |
| 1286 | def test_bundle_mode_default_is_full(self, tmp_path: pathlib.Path) -> None: |
| 1287 | repo = _init_repo(tmp_path) |
| 1288 | raw, _ = _clean_bundle() |
| 1289 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1290 | assert json.loads(r.output)["bundle_mode"] == "full" |
| 1291 | |
| 1292 | def test_bundle_mode_full_reflected_from_meta(self, tmp_path: pathlib.Path) -> None: |
| 1293 | repo = _init_repo(tmp_path) |
| 1294 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=_full_meta_bundle()) |
| 1295 | assert json.loads(r.output)["bundle_mode"] == "full" |
| 1296 | |
| 1297 | def test_bundle_mode_incremental_reflected(self, tmp_path: pathlib.Path) -> None: |
| 1298 | repo = _init_repo(tmp_path) |
| 1299 | missing_oid = blob_id(b"historical-object-at-base") |
| 1300 | raw = _incremental_meta_bundle(blob_id(b"snap"), missing_oid) |
| 1301 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1302 | d = json.loads(r.output) |
| 1303 | assert d["bundle_mode"] == "incremental" |
| 1304 | |
| 1305 | # ----------------------------------------------------------------- |
| 1306 | # base_commits key always present in JSON output |
| 1307 | # ----------------------------------------------------------------- |
| 1308 | |
| 1309 | def test_base_commits_key_present(self, tmp_path: pathlib.Path) -> None: |
| 1310 | repo = _init_repo(tmp_path) |
| 1311 | raw, _ = _clean_bundle() |
| 1312 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1313 | d = json.loads(r.output) |
| 1314 | assert "base_commits" in d |
| 1315 | assert isinstance(d["base_commits"], list) |
| 1316 | |
| 1317 | def test_base_commits_empty_for_full_bundle(self, tmp_path: pathlib.Path) -> None: |
| 1318 | repo = _init_repo(tmp_path) |
| 1319 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=_full_meta_bundle()) |
| 1320 | assert json.loads(r.output)["base_commits"] == [] |
| 1321 | |
| 1322 | def test_base_commits_populated_for_incremental(self, tmp_path: pathlib.Path) -> None: |
| 1323 | repo = _init_repo(tmp_path) |
| 1324 | missing_oid = blob_id(b"base-object") |
| 1325 | fake_base = blob_id(b"fake-base-commit") |
| 1326 | raw = _incremental_meta_bundle(blob_id(b"s"), missing_oid) |
| 1327 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1328 | d = json.loads(r.output) |
| 1329 | assert fake_base in d["base_commits"] |
| 1330 | |
| 1331 | # ----------------------------------------------------------------- |
| 1332 | # base_objects key — unresolved refs in incremental bundles |
| 1333 | # ----------------------------------------------------------------- |
| 1334 | |
| 1335 | def test_base_objects_key_present(self, tmp_path: pathlib.Path) -> None: |
| 1336 | repo = _init_repo(tmp_path) |
| 1337 | raw, _ = _clean_bundle() |
| 1338 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1339 | assert "base_objects" in json.loads(r.output) |
| 1340 | |
| 1341 | def test_base_objects_zero_for_self_contained_bundle(self, tmp_path: pathlib.Path) -> None: |
| 1342 | repo = _init_repo(tmp_path) |
| 1343 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=_full_meta_bundle()) |
| 1344 | assert json.loads(r.output)["base_objects"] == 0 |
| 1345 | |
| 1346 | def test_no_local_incremental_treats_refs_as_base_objects(self, tmp_path: pathlib.Path) -> None: |
| 1347 | """--no-local + incremental mpack: missing snapshot refs are base_objects, not failures.""" |
| 1348 | repo = _init_repo(tmp_path) |
| 1349 | missing_oid = blob_id(b"historical-object") |
| 1350 | raw = _incremental_meta_bundle(blob_id(b"snap"), missing_oid) |
| 1351 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1352 | d = json.loads(r.output) |
| 1353 | assert d["all_ok"] is True |
| 1354 | assert d["base_objects"] >= 1 |
| 1355 | assert d["failures"] == [] |
| 1356 | |
| 1357 | def test_no_local_full_bundle_fails_on_missing_refs(self, tmp_path: pathlib.Path) -> None: |
| 1358 | """--no-local + full mpack: missing snapshot refs are still failures.""" |
| 1359 | repo = _init_repo(tmp_path) |
| 1360 | missing_oid = blob_id(b"missing-in-full") |
| 1361 | snap_id = blob_id(b"snap-full-missing") |
| 1362 | commit_id = blob_id(b"commit-full-missing") |
| 1363 | mpack = _pack({ |
| 1364 | "blobs": [], |
| 1365 | "snapshots": [{"snapshot_id": snap_id, "manifest": {"f.py": missing_oid}}], |
| 1366 | "commits": [{"commit_id": commit_id, "snapshot_id": snap_id}], |
| 1367 | "meta": {"mode": "full", "base_commits": [], "created_at": "2026-01-01T00:00:00Z"}, |
| 1368 | }) |
| 1369 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=mpack) |
| 1370 | d = json.loads(r.output) |
| 1371 | assert d["all_ok"] is False |
| 1372 | assert any(f["kind"] == "snapshot" for f in d["failures"]) |
| 1373 | |
| 1374 | def test_strict_incremental_treats_base_objects_as_failures(self, tmp_path: pathlib.Path) -> None: |
| 1375 | """--strict overrides incremental leniency: base objects become failures.""" |
| 1376 | repo = _init_repo(tmp_path) |
| 1377 | missing_oid = blob_id(b"base-strict-test") |
| 1378 | raw = _incremental_meta_bundle(blob_id(b"snap"), missing_oid) |
| 1379 | r = _invoke(repo, "verify-pack", "--no-local", "--strict", "--json", stdin=raw) |
| 1380 | d = json.loads(r.output) |
| 1381 | assert d["all_ok"] is False |
| 1382 | assert d["base_objects"] == 0 |
| 1383 | |
| 1384 | # ----------------------------------------------------------------- |
| 1385 | # stat mode unaffected by meta |
| 1386 | # ----------------------------------------------------------------- |
| 1387 | |
| 1388 | def test_stat_mode_works_with_meta(self, tmp_path: pathlib.Path) -> None: |
| 1389 | repo = _init_repo(tmp_path) |
| 1390 | r = _invoke(repo, "verify-pack", "--stat", "--json", stdin=_full_meta_bundle(n_objects=3)) |
| 1391 | assert r.exit_code == 0 |
| 1392 | d = json.loads(r.output) |
| 1393 | assert d["blobs"] == 3 |
| 1394 | |
| 1395 | # ----------------------------------------------------------------- |
| 1396 | # JSON envelope completeness with Phase 2 fields |
| 1397 | # ----------------------------------------------------------------- |
| 1398 | |
| 1399 | def test_all_phase2_keys_in_envelope(self, tmp_path: pathlib.Path) -> None: |
| 1400 | repo = _init_repo(tmp_path) |
| 1401 | raw, _ = _clean_bundle() |
| 1402 | r = _invoke(repo, "verify-pack", "--no-local", "--json", stdin=raw) |
| 1403 | d = json.loads(r.output) |
| 1404 | for key in ("bundle_mode", "base_commits", "base_objects"): |
| 1405 | assert key in d, f"Missing Phase 2 key: {key!r}" |
| 1406 | |
| 1407 | |
| 1408 | # --------------------------------------------------------------------------- |
| 1409 | # Flag registration |
| 1410 | # --------------------------------------------------------------------------- |
| 1411 | |
| 1412 | |
| 1413 | class TestRegisterFlags: |
| 1414 | def _parse(self, *args: str) -> "argparse.Namespace": |
| 1415 | import argparse |
| 1416 | from muse.cli.commands.verify_pack import register |
| 1417 | p = argparse.ArgumentParser() |
| 1418 | sub = p.add_subparsers() |
| 1419 | register(sub) |
| 1420 | return p.parse_args(["verify-pack", *args]) |
| 1421 | |
| 1422 | def test_default_json_out_is_false(self) -> None: |
| 1423 | ns = self._parse() |
| 1424 | assert ns.json_out is False |
| 1425 | |
| 1426 | def test_json_flag_sets_json_out(self) -> None: |
| 1427 | ns = self._parse("--json") |
| 1428 | assert ns.json_out is True |
| 1429 | |
| 1430 | def test_j_shorthand_sets_json_out(self) -> None: |
| 1431 | ns = self._parse("-j") |
| 1432 | assert ns.json_out is True |
File History
1 commit
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e
fix: rename objects→blobs in push client and all stale test…
Sonnet 4.6
patch
22 days ago