test_cmd_count_objects.py
python
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago
| 1 | """Tests for ``muse count-objects`` — object store diagnostics. |
| 2 | |
| 3 | Coverage tiers: |
| 4 | - Unit: _count_loose_objects, _collect_reachable_ids helpers |
| 5 | - Integration: empty store, single object, multi-shard, verbose breakdown, |
| 6 | --unreachable counts GC candidates, JSON schema, text format, |
| 7 | objects match expected after N commits |
| 8 | - End-to-end: full CLI via CliRunner |
| 9 | - Security: read-only — no mutations; no content reads (stat only) |
| 10 | - Stress: store with many objects; --unreachable on multi-commit repo |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | from collections.abc import Mapping |
| 15 | |
| 16 | import datetime |
| 17 | import json |
| 18 | import os |
| 19 | import pathlib |
| 20 | |
| 21 | import pytest |
| 22 | |
| 23 | from tests.cli_test_helper import CliRunner |
| 24 | from muse.core.object_store import write_object |
| 25 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 26 | from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot |
| 27 | from muse.core._types import Manifest, blob_id |
| 28 | |
| 29 | runner = CliRunner() |
| 30 | |
| 31 | _REPO_ID = "count-objects-test" |
| 32 | _counter = 0 |
| 33 | |
| 34 | |
| 35 | # --------------------------------------------------------------------------- |
| 36 | # Helpers |
| 37 | # --------------------------------------------------------------------------- |
| 38 | |
| 39 | |
| 40 | def _sha(data: bytes) -> str: |
| 41 | return blob_id(data) |
| 42 | |
| 43 | |
| 44 | def _init_repo(path: pathlib.Path) -> pathlib.Path: |
| 45 | muse = path / ".muse" |
| 46 | for d in ("commits", "snapshots", "objects", "refs/heads", "code"): |
| 47 | (muse / d).mkdir(parents=True, exist_ok=True) |
| 48 | (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 49 | (muse / "repo.json").write_text( |
| 50 | json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" |
| 51 | ) |
| 52 | return path |
| 53 | |
| 54 | |
| 55 | def _env(repo: pathlib.Path) -> Mapping[str, str]: |
| 56 | return {"MUSE_REPO_ROOT": str(repo)} |
| 57 | |
| 58 | |
| 59 | def _commit_files( |
| 60 | root: pathlib.Path, |
| 61 | files: Mapping[str, bytes], |
| 62 | branch: str = "main", |
| 63 | ) -> str: |
| 64 | global _counter |
| 65 | _counter += 1 |
| 66 | manifest: Manifest = {} |
| 67 | for rel_path, content in files.items(): |
| 68 | obj_id = _sha(content) |
| 69 | write_object(root, obj_id, content) |
| 70 | manifest[rel_path] = obj_id |
| 71 | abs_path = root / rel_path |
| 72 | abs_path.parent.mkdir(parents=True, exist_ok=True) |
| 73 | abs_path.write_bytes(content) |
| 74 | snap_id = compute_snapshot_id(manifest) |
| 75 | write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) |
| 76 | committed_at = datetime.datetime.now(datetime.timezone.utc) |
| 77 | # Read the current tip to use as parent (proper chain for reachability BFS). |
| 78 | ref_path = root / ".muse" / "refs" / "heads" / branch |
| 79 | parent_id = ref_path.read_text(encoding="utf-8").strip() if ref_path.exists() else None |
| 80 | parents = [parent_id] if parent_id else [] |
| 81 | commit_id = compute_commit_id( |
| 82 | repo_id=_REPO_ID, |
| 83 | parent_ids=parents, |
| 84 | snapshot_id=snap_id, |
| 85 | message=f"commit {_counter}", |
| 86 | committed_at_iso=committed_at.isoformat(), |
| 87 | ) |
| 88 | write_commit( |
| 89 | root, |
| 90 | CommitRecord( |
| 91 | commit_id=commit_id, |
| 92 | repo_id=_REPO_ID, |
| 93 | created_on_branch=branch, |
| 94 | snapshot_id=snap_id, |
| 95 | message=f"commit {_counter}", |
| 96 | committed_at=committed_at, |
| 97 | parent_commit_id=parent_id, |
| 98 | ), |
| 99 | ) |
| 100 | ref_path.write_text(commit_id, encoding="utf-8") |
| 101 | return commit_id |
| 102 | |
| 103 | |
| 104 | def _invoke(repo: pathlib.Path, *args: str): |
| 105 | from muse.cli.app import main as cli |
| 106 | return runner.invoke(cli, ["count-objects", *args], env=_env(repo)) |
| 107 | |
| 108 | |
| 109 | # --------------------------------------------------------------------------- |
| 110 | # Unit — _count_loose_objects |
| 111 | # --------------------------------------------------------------------------- |
| 112 | |
| 113 | |
| 114 | def test_count_loose_objects_empty_store(tmp_path: pathlib.Path) -> None: |
| 115 | from muse.cli.commands.count_objects import _count_loose_objects |
| 116 | root = _init_repo(tmp_path) |
| 117 | count, size = _count_loose_objects(root) |
| 118 | assert count == 0 |
| 119 | assert size == 0 |
| 120 | |
| 121 | |
| 122 | def test_count_loose_objects_single_object(tmp_path: pathlib.Path) -> None: |
| 123 | from muse.cli.commands.count_objects import _count_loose_objects |
| 124 | root = _init_repo(tmp_path) |
| 125 | content = b"hello world" |
| 126 | obj_id = _sha(content) |
| 127 | write_object(root, obj_id, content) |
| 128 | count, size = _count_loose_objects(root) |
| 129 | assert count == 1 |
| 130 | assert size > 0 |
| 131 | |
| 132 | |
| 133 | def test_count_loose_objects_multiple_shards(tmp_path: pathlib.Path) -> None: |
| 134 | from muse.cli.commands.count_objects import _count_loose_objects |
| 135 | root = _init_repo(tmp_path) |
| 136 | # Write 5 distinct objects (may land in different shards) |
| 137 | for i in range(5): |
| 138 | content = f"object {i}".encode() |
| 139 | write_object(root, _sha(content), content) |
| 140 | count, _ = _count_loose_objects(root) |
| 141 | assert count == 5 |
| 142 | |
| 143 | |
| 144 | # --------------------------------------------------------------------------- |
| 145 | # Unit — _collect_reachable_ids |
| 146 | # --------------------------------------------------------------------------- |
| 147 | |
| 148 | |
| 149 | def test_collect_reachable_ids_empty_repo(tmp_path: pathlib.Path) -> None: |
| 150 | from muse.cli.commands.count_objects import _collect_reachable_ids |
| 151 | root = _init_repo(tmp_path) |
| 152 | ids = _collect_reachable_ids(root) |
| 153 | assert isinstance(ids, set) |
| 154 | assert len(ids) == 0 |
| 155 | |
| 156 | |
| 157 | def test_collect_reachable_ids_after_commit(tmp_path: pathlib.Path) -> None: |
| 158 | from muse.cli.commands.count_objects import _collect_reachable_ids |
| 159 | root = _init_repo(tmp_path) |
| 160 | _commit_files(root, {"a.py": b"# a\n"}) |
| 161 | ids = _collect_reachable_ids(root) |
| 162 | # At minimum: the blob object for a.py |
| 163 | assert len(ids) >= 1 |
| 164 | |
| 165 | |
| 166 | def test_collect_reachable_ids_includes_all_blobs(tmp_path: pathlib.Path) -> None: |
| 167 | from muse.cli.commands.count_objects import _collect_reachable_ids |
| 168 | root = _init_repo(tmp_path) |
| 169 | files = {"a.py": b"# a\n", "b.py": b"# b\n", "c.py": b"# c\n"} |
| 170 | _commit_files(root, files) |
| 171 | ids = _collect_reachable_ids(root) |
| 172 | # All three blob IDs must be reachable |
| 173 | for content in files.values(): |
| 174 | assert _sha(content) in ids |
| 175 | |
| 176 | |
| 177 | # --------------------------------------------------------------------------- |
| 178 | # Integration — JSON output schema |
| 179 | # --------------------------------------------------------------------------- |
| 180 | |
| 181 | |
| 182 | def test_count_objects_json_schema_keys(tmp_path: pathlib.Path) -> None: |
| 183 | root = _init_repo(tmp_path) |
| 184 | _commit_files(root, {"a.py": b"# a\n"}) |
| 185 | result = _invoke(root, "--json") |
| 186 | assert result.exit_code == 0 |
| 187 | data = json.loads(result.stdout) |
| 188 | for key in ("loose_objects", "loose_size_kb", "total_objects", "total_size_kb", |
| 189 | "object_store_path", "duration_ms", "exit_code"): |
| 190 | assert key in data, f"Missing key: {key}" |
| 191 | |
| 192 | |
| 193 | def test_count_objects_json_count_matches_written(tmp_path: pathlib.Path) -> None: |
| 194 | root = _init_repo(tmp_path) |
| 195 | # Write 3 unique blobs directly (no commit overhead) |
| 196 | for i in range(3): |
| 197 | content = f"direct blob {i}".encode() |
| 198 | write_object(root, _sha(content), content) |
| 199 | result = _invoke(root, "--json") |
| 200 | data = json.loads(result.stdout) |
| 201 | assert data["loose_objects"] >= 3 |
| 202 | |
| 203 | |
| 204 | def test_count_objects_json_empty_store(tmp_path: pathlib.Path) -> None: |
| 205 | root = _init_repo(tmp_path) |
| 206 | result = _invoke(root, "--json") |
| 207 | assert result.exit_code == 0 |
| 208 | data = json.loads(result.stdout) |
| 209 | assert data["loose_objects"] == 0 |
| 210 | assert data["total_objects"] == 0 |
| 211 | |
| 212 | |
| 213 | def test_count_objects_json_size_nonzero_after_write(tmp_path: pathlib.Path) -> None: |
| 214 | root = _init_repo(tmp_path) |
| 215 | write_object(root, _sha(b"x" * 1000), b"x" * 1000) |
| 216 | result = _invoke(root, "--json") |
| 217 | data = json.loads(result.stdout) |
| 218 | assert data["loose_size_kb"] > 0 or data["total_size_kb"] > 0 |
| 219 | |
| 220 | |
| 221 | def test_count_objects_json_object_store_path_present(tmp_path: pathlib.Path) -> None: |
| 222 | root = _init_repo(tmp_path) |
| 223 | result = _invoke(root, "--json") |
| 224 | data = json.loads(result.stdout) |
| 225 | assert "objects" in data["object_store_path"] |
| 226 | |
| 227 | |
| 228 | # --------------------------------------------------------------------------- |
| 229 | # Integration — text output format |
| 230 | # --------------------------------------------------------------------------- |
| 231 | |
| 232 | |
| 233 | def test_count_objects_text_output_nonempty(tmp_path: pathlib.Path) -> None: |
| 234 | root = _init_repo(tmp_path) |
| 235 | _commit_files(root, {"a.py": b"# a\n"}) |
| 236 | result = _invoke(root) |
| 237 | assert result.exit_code == 0 |
| 238 | assert result.stdout.strip() |
| 239 | |
| 240 | |
| 241 | def test_count_objects_text_mentions_count(tmp_path: pathlib.Path) -> None: |
| 242 | root = _init_repo(tmp_path) |
| 243 | for i in range(5): |
| 244 | write_object(root, _sha(f"obj{i}".encode()), f"obj{i}".encode()) |
| 245 | result = _invoke(root) |
| 246 | # The count should appear somewhere in the output |
| 247 | assert any(char.isdigit() for char in result.stdout) |
| 248 | |
| 249 | |
| 250 | # --------------------------------------------------------------------------- |
| 251 | # Integration — --verbose shard breakdown |
| 252 | # --------------------------------------------------------------------------- |
| 253 | |
| 254 | |
| 255 | def test_count_objects_verbose_json_has_shards(tmp_path: pathlib.Path) -> None: |
| 256 | root = _init_repo(tmp_path) |
| 257 | for i in range(4): |
| 258 | content = f"shard content {i}".encode() |
| 259 | write_object(root, _sha(content), content) |
| 260 | result = _invoke(root, "--verbose", "--json") |
| 261 | assert result.exit_code == 0 |
| 262 | data = json.loads(result.stdout) |
| 263 | assert "shards" in data |
| 264 | assert isinstance(data["shards"], list) |
| 265 | |
| 266 | |
| 267 | def test_count_objects_verbose_shards_sum_to_total(tmp_path: pathlib.Path) -> None: |
| 268 | root = _init_repo(tmp_path) |
| 269 | for i in range(6): |
| 270 | content = f"v content {i}".encode() |
| 271 | write_object(root, _sha(content), content) |
| 272 | result = _invoke(root, "--verbose", "--json") |
| 273 | data = json.loads(result.stdout) |
| 274 | shard_total = sum(s["count"] for s in data["shards"]) |
| 275 | assert shard_total == data["loose_objects"] |
| 276 | |
| 277 | |
| 278 | # --------------------------------------------------------------------------- |
| 279 | # Integration — --unreachable |
| 280 | # --------------------------------------------------------------------------- |
| 281 | |
| 282 | |
| 283 | def test_count_objects_unreachable_zero_after_clean_commit(tmp_path: pathlib.Path) -> None: |
| 284 | """After a commit where all blobs are referenced, unreachable should be 0.""" |
| 285 | root = _init_repo(tmp_path) |
| 286 | _commit_files(root, {"a.py": b"# a\n", "b.py": b"# b\n"}) |
| 287 | result = _invoke(root, "--unreachable", "--json") |
| 288 | assert result.exit_code == 0 |
| 289 | data = json.loads(result.stdout) |
| 290 | assert "unreachable_objects" in data |
| 291 | assert data["unreachable_objects"] == 0 |
| 292 | |
| 293 | |
| 294 | def test_count_objects_unreachable_detects_orphan_blobs(tmp_path: pathlib.Path) -> None: |
| 295 | """Blobs written but not referenced by any commit are unreachable.""" |
| 296 | root = _init_repo(tmp_path) |
| 297 | _commit_files(root, {"a.py": b"# a\n"}) |
| 298 | # Write an extra blob that is NOT referenced by any commit |
| 299 | orphan = b"i am an orphan blob" |
| 300 | write_object(root, _sha(orphan), orphan) |
| 301 | result = _invoke(root, "--unreachable", "--json") |
| 302 | data = json.loads(result.stdout) |
| 303 | assert data["unreachable_objects"] >= 1 |
| 304 | |
| 305 | |
| 306 | def test_count_objects_unreachable_empty_repo(tmp_path: pathlib.Path) -> None: |
| 307 | root = _init_repo(tmp_path) |
| 308 | result = _invoke(root, "--unreachable", "--json") |
| 309 | assert result.exit_code == 0 |
| 310 | data = json.loads(result.stdout) |
| 311 | assert data["unreachable_objects"] == 0 |
| 312 | |
| 313 | |
| 314 | # --------------------------------------------------------------------------- |
| 315 | # Security — read-only, no mutations |
| 316 | # --------------------------------------------------------------------------- |
| 317 | |
| 318 | |
| 319 | def test_count_objects_does_not_modify_store(tmp_path: pathlib.Path) -> None: |
| 320 | """count-objects must not write, delete, or move any object files.""" |
| 321 | root = _init_repo(tmp_path) |
| 322 | _commit_files(root, {"a.py": b"# a\n"}) |
| 323 | objects_dir = root / ".muse" / "objects" |
| 324 | # Collect (path, mtime) before |
| 325 | before = { |
| 326 | str(p): p.stat().st_mtime |
| 327 | for p in objects_dir.rglob("*") |
| 328 | if p.is_file() |
| 329 | } |
| 330 | _invoke(root, "--json") |
| 331 | _invoke(root, "--unreachable", "--json") |
| 332 | # Collect after |
| 333 | after = { |
| 334 | str(p): p.stat().st_mtime |
| 335 | for p in objects_dir.rglob("*") |
| 336 | if p.is_file() |
| 337 | } |
| 338 | assert before == after, "count-objects modified the object store" |
| 339 | |
| 340 | |
| 341 | # --------------------------------------------------------------------------- |
| 342 | # Stress |
| 343 | # --------------------------------------------------------------------------- |
| 344 | |
| 345 | |
| 346 | def test_count_objects_large_store(tmp_path: pathlib.Path) -> None: |
| 347 | """Store with 200 objects — count should be accurate.""" |
| 348 | root = _init_repo(tmp_path) |
| 349 | for i in range(200): |
| 350 | content = f"stress object {i:04d}".encode() |
| 351 | write_object(root, _sha(content), content) |
| 352 | result = _invoke(root, "--json") |
| 353 | assert result.exit_code == 0 |
| 354 | data = json.loads(result.stdout) |
| 355 | assert data["loose_objects"] == 200 |
| 356 | |
| 357 | |
| 358 | def test_count_objects_unreachable_large_repo(tmp_path: pathlib.Path) -> None: |
| 359 | """10 commits with 10 files each — all referenced, unreachable = 0.""" |
| 360 | root = _init_repo(tmp_path) |
| 361 | for i in range(10): |
| 362 | files = {f"pkg/file_{i}_{j}.py": f"# {i},{j}\n".encode() for j in range(10)} |
| 363 | _commit_files(root, files) |
| 364 | result = _invoke(root, "--unreachable", "--json") |
| 365 | assert result.exit_code == 0 |
| 366 | data = json.loads(result.stdout) |
| 367 | assert data["unreachable_objects"] == 0 |
| 368 | |
| 369 | |
| 370 | # --------------------------------------------------------------------------- |
| 371 | # TestJsonSchemaComplete |
| 372 | # --------------------------------------------------------------------------- |
| 373 | |
| 374 | |
| 375 | _REQUIRED_KEYS = frozenset({ |
| 376 | "loose_objects", |
| 377 | "loose_size_kb", |
| 378 | "total_objects", |
| 379 | "total_size_kb", |
| 380 | "object_store_path", |
| 381 | "duration_ms", |
| 382 | "exit_code", |
| 383 | }) |
| 384 | |
| 385 | _REQUIRED_KEYS_UNREACHABLE = _REQUIRED_KEYS | {"unreachable_objects"} |
| 386 | _REQUIRED_KEYS_VERBOSE = _REQUIRED_KEYS | {"shards"} |
| 387 | |
| 388 | |
| 389 | class TestJsonSchemaComplete: |
| 390 | """Every required key must appear in every JSON output path.""" |
| 391 | |
| 392 | def test_base_keys_present(self, tmp_path: pathlib.Path) -> None: |
| 393 | root = _init_repo(tmp_path) |
| 394 | result = _invoke(root, "--json") |
| 395 | assert result.exit_code == 0 |
| 396 | data = json.loads(result.stdout) |
| 397 | missing = _REQUIRED_KEYS - data.keys() |
| 398 | assert not missing, f"Missing keys: {missing}" |
| 399 | |
| 400 | def test_unreachable_keys_present(self, tmp_path: pathlib.Path) -> None: |
| 401 | root = _init_repo(tmp_path) |
| 402 | result = _invoke(root, "--unreachable", "--json") |
| 403 | assert result.exit_code == 0 |
| 404 | data = json.loads(result.stdout) |
| 405 | missing = _REQUIRED_KEYS_UNREACHABLE - data.keys() |
| 406 | assert not missing, f"Missing keys: {missing}" |
| 407 | |
| 408 | def test_verbose_keys_present(self, tmp_path: pathlib.Path) -> None: |
| 409 | root = _init_repo(tmp_path) |
| 410 | result = _invoke(root, "--verbose", "--json") |
| 411 | assert result.exit_code == 0 |
| 412 | data = json.loads(result.stdout) |
| 413 | missing = _REQUIRED_KEYS_VERBOSE - data.keys() |
| 414 | assert not missing, f"Missing keys: {missing}" |
| 415 | |
| 416 | def test_all_flags_keys_present(self, tmp_path: pathlib.Path) -> None: |
| 417 | root = _init_repo(tmp_path) |
| 418 | result = _invoke(root, "--unreachable", "--verbose", "--json") |
| 419 | assert result.exit_code == 0 |
| 420 | data = json.loads(result.stdout) |
| 421 | missing = (_REQUIRED_KEYS_UNREACHABLE | _REQUIRED_KEYS_VERBOSE) - data.keys() |
| 422 | assert not missing, f"Missing keys: {missing}" |
| 423 | |
| 424 | def test_exit_code_field_is_zero_on_success(self, tmp_path: pathlib.Path) -> None: |
| 425 | root = _init_repo(tmp_path) |
| 426 | result = _invoke(root, "--json") |
| 427 | assert result.exit_code == 0 |
| 428 | assert json.loads(result.stdout)["exit_code"] == 0 |
| 429 | |
| 430 | def test_exit_code_is_integer(self, tmp_path: pathlib.Path) -> None: |
| 431 | root = _init_repo(tmp_path) |
| 432 | result = _invoke(root, "--json") |
| 433 | assert isinstance(json.loads(result.stdout)["exit_code"], int) |
| 434 | |
| 435 | def test_json_is_compact(self, tmp_path: pathlib.Path) -> None: |
| 436 | root = _init_repo(tmp_path) |
| 437 | result = _invoke(root, "--json") |
| 438 | lines = [ln for ln in result.stdout.splitlines() if ln.strip()] |
| 439 | assert len(lines) == 1, "JSON output must be a single line" |
| 440 | |
| 441 | def test_exit_code_in_json_matches_process_exit(self, tmp_path: pathlib.Path) -> None: |
| 442 | root = _init_repo(tmp_path) |
| 443 | result = _invoke(root, "--json") |
| 444 | assert json.loads(result.stdout)["exit_code"] == result.exit_code |
| 445 | |
| 446 | |
| 447 | # --------------------------------------------------------------------------- |
| 448 | # TestElapsedSeconds |
| 449 | # --------------------------------------------------------------------------- |
| 450 | |
| 451 | |
| 452 | class TestElapsedSeconds: |
| 453 | """``duration_ms`` must be a non-negative float in every JSON path.""" |
| 454 | |
| 455 | def _assert_elapsed(self, data: Mapping[str, object]) -> None: # type: ignore[type-arg] |
| 456 | assert "duration_ms" in data |
| 457 | assert isinstance(data["duration_ms"], float) |
| 458 | assert data["duration_ms"] >= 0.0 |
| 459 | |
| 460 | def test_elapsed_base(self, tmp_path: pathlib.Path) -> None: |
| 461 | root = _init_repo(tmp_path) |
| 462 | result = _invoke(root, "--json") |
| 463 | self._assert_elapsed(json.loads(result.stdout)) |
| 464 | |
| 465 | def test_elapsed_with_unreachable(self, tmp_path: pathlib.Path) -> None: |
| 466 | root = _init_repo(tmp_path) |
| 467 | _commit_files(root, {"a.py": b"# a\n"}) |
| 468 | result = _invoke(root, "--unreachable", "--json") |
| 469 | self._assert_elapsed(json.loads(result.stdout)) |
| 470 | |
| 471 | def test_elapsed_with_verbose(self, tmp_path: pathlib.Path) -> None: |
| 472 | root = _init_repo(tmp_path) |
| 473 | _commit_files(root, {"a.py": b"# a\n"}) |
| 474 | result = _invoke(root, "--verbose", "--json") |
| 475 | self._assert_elapsed(json.loads(result.stdout)) |
| 476 | |
| 477 | def test_elapsed_all_flags(self, tmp_path: pathlib.Path) -> None: |
| 478 | root = _init_repo(tmp_path) |
| 479 | _commit_files(root, {"a.py": b"# a\n"}) |
| 480 | result = _invoke(root, "--unreachable", "--verbose", "--json") |
| 481 | self._assert_elapsed(json.loads(result.stdout)) |
| 482 | |
| 483 | def test_elapsed_is_float_not_int(self, tmp_path: pathlib.Path) -> None: |
| 484 | root = _init_repo(tmp_path) |
| 485 | result = _invoke(root, "--json") |
| 486 | data = json.loads(result.stdout) |
| 487 | assert isinstance(data["duration_ms"], float) |
| 488 | |
| 489 | def test_elapsed_reasonable_upper_bound(self, tmp_path: pathlib.Path) -> None: |
| 490 | root = _init_repo(tmp_path) |
| 491 | result = _invoke(root, "--json") |
| 492 | assert json.loads(result.stdout)["duration_ms"] < 5.0 |
| 493 | |
| 494 | def test_elapsed_six_decimal_places(self, tmp_path: pathlib.Path) -> None: |
| 495 | root = _init_repo(tmp_path) |
| 496 | result = _invoke(root, "--json") |
| 497 | elapsed = json.loads(result.stdout)["duration_ms"] |
| 498 | assert round(elapsed, 6) == elapsed |
| 499 | |
| 500 | |
| 501 | # --------------------------------------------------------------------------- |
| 502 | # TestExitCode |
| 503 | # --------------------------------------------------------------------------- |
| 504 | |
| 505 | |
| 506 | class TestExitCode: |
| 507 | """``exit_code`` in JSON must mirror the process exit code.""" |
| 508 | |
| 509 | def test_exit_code_zero_empty_store(self, tmp_path: pathlib.Path) -> None: |
| 510 | root = _init_repo(tmp_path) |
| 511 | result = _invoke(root, "--json") |
| 512 | assert result.exit_code == 0 |
| 513 | assert json.loads(result.stdout)["exit_code"] == 0 |
| 514 | |
| 515 | def test_exit_code_zero_with_objects(self, tmp_path: pathlib.Path) -> None: |
| 516 | root = _init_repo(tmp_path) |
| 517 | _commit_files(root, {"a.py": b"# a\n"}) |
| 518 | result = _invoke(root, "--json") |
| 519 | assert result.exit_code == 0 |
| 520 | assert json.loads(result.stdout)["exit_code"] == 0 |
| 521 | |
| 522 | def test_exit_code_zero_unreachable_flag(self, tmp_path: pathlib.Path) -> None: |
| 523 | root = _init_repo(tmp_path) |
| 524 | _commit_files(root, {"a.py": b"# a\n"}) |
| 525 | result = _invoke(root, "--unreachable", "--json") |
| 526 | assert result.exit_code == 0 |
| 527 | assert json.loads(result.stdout)["exit_code"] == 0 |
| 528 | |
| 529 | def test_exit_code_zero_verbose_flag(self, tmp_path: pathlib.Path) -> None: |
| 530 | root = _init_repo(tmp_path) |
| 531 | result = _invoke(root, "--verbose", "--json") |
| 532 | assert result.exit_code == 0 |
| 533 | assert json.loads(result.stdout)["exit_code"] == 0 |
| 534 | |
| 535 | def test_exit_code_matches_process_exit(self, tmp_path: pathlib.Path) -> None: |
| 536 | root = _init_repo(tmp_path) |
| 537 | result = _invoke(root, "--json") |
| 538 | assert json.loads(result.stdout)["exit_code"] == result.exit_code |
| 539 | |
| 540 | |
| 541 | # --------------------------------------------------------------------------- |
| 542 | # Data integrity — unreachable detection correctness with sha256: prefix |
| 543 | # --------------------------------------------------------------------------- |
| 544 | |
| 545 | |
| 546 | class TestUnreachableDetection: |
| 547 | """Verify unreachable detection correctly handles sha256:-prefixed IDs.""" |
| 548 | |
| 549 | def test_all_committed_blobs_reachable(self, tmp_path: pathlib.Path) -> None: |
| 550 | root = _init_repo(tmp_path) |
| 551 | _commit_files(root, {"x.py": b"x = 1\n", "y.py": b"y = 2\n"}) |
| 552 | result = _invoke(root, "--unreachable", "--json") |
| 553 | assert result.exit_code == 0 |
| 554 | assert json.loads(result.stdout)["unreachable_objects"] == 0 |
| 555 | |
| 556 | def test_orphan_blob_detected(self, tmp_path: pathlib.Path) -> None: |
| 557 | root = _init_repo(tmp_path) |
| 558 | _commit_files(root, {"a.py": b"# committed\n"}) |
| 559 | write_object(root, _sha(b"orphan"), b"orphan") |
| 560 | result = _invoke(root, "--unreachable", "--json") |
| 561 | assert json.loads(result.stdout)["unreachable_objects"] >= 1 |
| 562 | |
| 563 | def test_multiple_orphans_all_counted(self, tmp_path: pathlib.Path) -> None: |
| 564 | root = _init_repo(tmp_path) |
| 565 | _commit_files(root, {"a.py": b"# committed\n"}) |
| 566 | for i in range(5): |
| 567 | content = f"orphan {i}".encode() |
| 568 | write_object(root, _sha(content), content) |
| 569 | result = _invoke(root, "--unreachable", "--json") |
| 570 | assert json.loads(result.stdout)["unreachable_objects"] >= 5 |
| 571 | |
| 572 | def test_reachable_set_uses_sha256_prefix(self, tmp_path: pathlib.Path) -> None: |
| 573 | """_collect_reachable_ids must return sha256:-prefixed IDs.""" |
| 574 | from muse.cli.commands.count_objects import _collect_reachable_ids |
| 575 | root = _init_repo(tmp_path) |
| 576 | content = b"# test\n" |
| 577 | _commit_files(root, {"a.py": content}) |
| 578 | ids = _collect_reachable_ids(root) |
| 579 | assert len(ids) > 0 |
| 580 | for obj_id in ids: |
| 581 | assert obj_id.startswith("sha256:"), f"ID missing sha256: prefix: {obj_id!r}" |
| 582 | |
| 583 | |
| 584 | class TestRegisterFlags: |
| 585 | def test_default_json_out_is_false(self): |
| 586 | import argparse |
| 587 | from muse.cli.commands.count_objects import register |
| 588 | p = argparse.ArgumentParser() |
| 589 | subs = p.add_subparsers() |
| 590 | register(subs) |
| 591 | args = p.parse_args(["count-objects"]) |
| 592 | assert args.json_out is False |
| 593 | |
| 594 | def test_json_flag_sets_json_out(self): |
| 595 | import argparse |
| 596 | from muse.cli.commands.count_objects import register |
| 597 | p = argparse.ArgumentParser() |
| 598 | subs = p.add_subparsers() |
| 599 | register(subs) |
| 600 | args = p.parse_args(["count-objects", "--json"]) |
| 601 | assert args.json_out is True |
| 602 | |
| 603 | def test_j_shorthand_sets_json_out(self): |
| 604 | import argparse |
| 605 | from muse.cli.commands.count_objects import register |
| 606 | p = argparse.ArgumentParser() |
| 607 | subs = p.add_subparsers() |
| 608 | register(subs) |
| 609 | args = p.parse_args(["count-objects", "-j"]) |
| 610 | assert args.json_out is True |
File History
3 commits
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago
sha256:7f9e2ef5286aedad9c1e6011b4c46ca27f39dbdad6e3409357e36b26e46b3b7c
docs: docstring sprint for-each-ref→hotspots — idiomatic ru…
Sonnet 4.6
patch
138 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9
fix(cursorignore): remove git-ism (.git/worktrees)
Human
minor
⚠
141 days ago