test_cmd_commit_tree.py
python
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3
docs: docstring sprint contract→find-symbol — idiomatic run…
Sonnet 4.6
patch
138 days ago
| 1 | """Comprehensive tests for ``muse commit-tree``. |
| 2 | |
| 3 | Coverage tiers |
| 4 | -------------- |
| 5 | - Unit: _FORMAT_CHOICES |
| 6 | - Integration: basic creation, --parent chain, merge commit, text format, |
| 7 | --agent-id/--model-id/--toolchain-id provenance, --branch override |
| 8 | - Security: >2 parents silently rejected, errors to stderr, no traceback |
| 9 | - Stress: 200 sequential commit-tree calls |
| 10 | """ |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import datetime |
| 14 | import json |
| 15 | import pathlib |
| 16 | |
| 17 | from muse.core.errors import ExitCode |
| 18 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 19 | from muse.core.store import CommitRecord, SnapshotRecord, read_commit, write_commit, write_snapshot |
| 20 | from muse.core._types import Manifest |
| 21 | from tests.cli_test_helper import CliRunner, InvokeResult |
| 22 | |
| 23 | runner = CliRunner() |
| 24 | |
| 25 | |
| 26 | # --------------------------------------------------------------------------- |
| 27 | # Helpers |
| 28 | # --------------------------------------------------------------------------- |
| 29 | |
| 30 | def _make_repo(tmp_path: pathlib.Path) -> pathlib.Path: |
| 31 | repo = tmp_path / "repo" |
| 32 | muse = repo / ".muse" |
| 33 | for sub in ("objects", "commits", "snapshots", "refs/heads"): |
| 34 | (muse / sub).mkdir(parents=True) |
| 35 | (muse / "HEAD").write_text("ref: refs/heads/main") |
| 36 | (muse / "repo.json").write_text(json.dumps({"repo_id": "test-repo", "domain": "code"})) |
| 37 | return repo |
| 38 | |
| 39 | |
| 40 | def _snap(repo: pathlib.Path) -> str: |
| 41 | manifest: Manifest = {} |
| 42 | sid = compute_snapshot_id(manifest) |
| 43 | write_snapshot(repo, SnapshotRecord( |
| 44 | snapshot_id=sid, |
| 45 | manifest=manifest, |
| 46 | created_at=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), |
| 47 | )) |
| 48 | return sid |
| 49 | |
| 50 | |
| 51 | def _commit( |
| 52 | repo: pathlib.Path, |
| 53 | snap_id: str, |
| 54 | parent: str | None = None, |
| 55 | message: str = "parent", |
| 56 | ) -> str: |
| 57 | committed_at = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 58 | parent_ids: list[str] = [parent] if parent else [] |
| 59 | commit_id = compute_commit_id(parent_ids, snap_id, message, committed_at.isoformat()) |
| 60 | write_commit(repo, CommitRecord( |
| 61 | commit_id=commit_id, |
| 62 | repo_id="test-repo", |
| 63 | branch="main", |
| 64 | snapshot_id=snap_id, |
| 65 | message=message, |
| 66 | committed_at=committed_at, |
| 67 | parent_commit_id=parent, |
| 68 | )) |
| 69 | return commit_id |
| 70 | |
| 71 | |
| 72 | def _ct(repo: pathlib.Path, *args: str) -> InvokeResult: |
| 73 | from muse.cli.app import main as cli |
| 74 | return runner.invoke( |
| 75 | cli, |
| 76 | ["commit-tree", *args], |
| 77 | env={"MUSE_REPO_ROOT": str(repo)}, |
| 78 | ) |
| 79 | |
| 80 | |
| 81 | # --------------------------------------------------------------------------- |
| 82 | # Unit |
| 83 | # --------------------------------------------------------------------------- |
| 84 | |
| 85 | |
| 86 | class TestUnit: |
| 87 | def test_format_choices(self) -> None: |
| 88 | from muse.cli.commands.commit_tree import _FORMAT_CHOICES |
| 89 | assert "json" in _FORMAT_CHOICES |
| 90 | assert "text" in _FORMAT_CHOICES |
| 91 | |
| 92 | |
| 93 | # --------------------------------------------------------------------------- |
| 94 | # Integration — basic creation |
| 95 | # --------------------------------------------------------------------------- |
| 96 | |
| 97 | |
| 98 | class TestBasicCreation: |
| 99 | def test_creates_commit(self, tmp_path: pathlib.Path) -> None: |
| 100 | repo = _make_repo(tmp_path) |
| 101 | sid = _snap(repo) |
| 102 | result = _ct(repo, "--snapshot", sid, "--message", "first commit") |
| 103 | assert result.exit_code == 0 |
| 104 | data = json.loads(result.output) |
| 105 | assert "commit_id" in data |
| 106 | assert data["commit_id"].startswith("sha256:") |
| 107 | assert len(data["commit_id"]) == 71 |
| 108 | |
| 109 | def test_commit_persisted_in_store(self, tmp_path: pathlib.Path) -> None: |
| 110 | repo = _make_repo(tmp_path) |
| 111 | sid = _snap(repo) |
| 112 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 113 | cid = data["commit_id"] |
| 114 | rec = read_commit(repo, cid) |
| 115 | assert rec is not None |
| 116 | assert rec.snapshot_id == sid |
| 117 | |
| 118 | def test_json_flag_shorthand(self, tmp_path: pathlib.Path) -> None: |
| 119 | repo = _make_repo(tmp_path) |
| 120 | sid = _snap(repo) |
| 121 | result = _ct(repo, "--json", "--snapshot", sid) |
| 122 | assert result.exit_code == 0 |
| 123 | assert "commit_id" in json.loads(result.output) |
| 124 | |
| 125 | def test_text_format_bare_commit_id(self, tmp_path: pathlib.Path) -> None: |
| 126 | repo = _make_repo(tmp_path) |
| 127 | sid = _snap(repo) |
| 128 | result = _ct(repo, "--format", "text", "--snapshot", sid) |
| 129 | assert result.exit_code == 0 |
| 130 | line = result.output.strip() |
| 131 | assert line.startswith("sha256:") |
| 132 | assert len(line) == 71 |
| 133 | |
| 134 | def test_message_stored(self, tmp_path: pathlib.Path) -> None: |
| 135 | repo = _make_repo(tmp_path) |
| 136 | sid = _snap(repo) |
| 137 | data = json.loads(_ct(repo, "--snapshot", sid, "--message", "my msg").output) |
| 138 | rec = read_commit(repo, data["commit_id"]) |
| 139 | assert rec is not None |
| 140 | assert rec.message == "my msg" |
| 141 | |
| 142 | def test_author_stored(self, tmp_path: pathlib.Path) -> None: |
| 143 | repo = _make_repo(tmp_path) |
| 144 | sid = _snap(repo) |
| 145 | data = json.loads(_ct(repo, "--snapshot", sid, "--author", "gabriel").output) |
| 146 | rec = read_commit(repo, data["commit_id"]) |
| 147 | assert rec is not None |
| 148 | assert rec.author == "gabriel" |
| 149 | |
| 150 | |
| 151 | # --------------------------------------------------------------------------- |
| 152 | # Integration — parent chain |
| 153 | # --------------------------------------------------------------------------- |
| 154 | |
| 155 | |
| 156 | class TestParentChain: |
| 157 | def test_single_parent_stored(self, tmp_path: pathlib.Path) -> None: |
| 158 | repo = _make_repo(tmp_path) |
| 159 | sid = _snap(repo) |
| 160 | p1_id = _commit(repo, sid) |
| 161 | data = json.loads(_ct(repo, "--snapshot", sid, "--parent", p1_id).output) |
| 162 | rec = read_commit(repo, data["commit_id"]) |
| 163 | assert rec is not None |
| 164 | assert rec.parent_commit_id == p1_id |
| 165 | assert rec.parent2_commit_id is None |
| 166 | |
| 167 | def test_merge_commit_two_parents(self, tmp_path: pathlib.Path) -> None: |
| 168 | repo = _make_repo(tmp_path) |
| 169 | sid = _snap(repo) |
| 170 | p1 = _commit(repo, sid, message="parent1") |
| 171 | p2 = _commit(repo, sid, message="parent2") |
| 172 | data = json.loads( |
| 173 | _ct(repo, "--snapshot", sid, "--parent", p1, "--parent", p2).output |
| 174 | ) |
| 175 | rec = read_commit(repo, data["commit_id"]) |
| 176 | assert rec is not None |
| 177 | assert rec.parent_commit_id == p1 |
| 178 | assert rec.parent2_commit_id == p2 |
| 179 | |
| 180 | def test_three_parents_rejected(self, tmp_path: pathlib.Path) -> None: |
| 181 | repo = _make_repo(tmp_path) |
| 182 | sid = _snap(repo) |
| 183 | p = _commit(repo, sid) |
| 184 | result = _ct( |
| 185 | repo, "--snapshot", sid, |
| 186 | "--parent", p, "--parent", p, "--parent", p, |
| 187 | ) |
| 188 | assert result.exit_code == ExitCode.USER_ERROR |
| 189 | |
| 190 | def test_missing_parent_errors(self, tmp_path: pathlib.Path) -> None: |
| 191 | repo = _make_repo(tmp_path) |
| 192 | sid = _snap(repo) |
| 193 | result = _ct(repo, "--snapshot", sid, "--parent", "dead" + "beef" * 15) |
| 194 | assert result.exit_code == ExitCode.USER_ERROR |
| 195 | |
| 196 | |
| 197 | # --------------------------------------------------------------------------- |
| 198 | # Integration — agent provenance flags |
| 199 | # --------------------------------------------------------------------------- |
| 200 | |
| 201 | |
| 202 | class TestAgentProvenance: |
| 203 | def test_agent_id_stored(self, tmp_path: pathlib.Path) -> None: |
| 204 | repo = _make_repo(tmp_path) |
| 205 | sid = _snap(repo) |
| 206 | data = json.loads( |
| 207 | _ct(repo, "--snapshot", sid, "--agent-id", "my-bot").output |
| 208 | ) |
| 209 | rec = read_commit(repo, data["commit_id"]) |
| 210 | assert rec is not None |
| 211 | assert rec.agent_id == "my-bot" |
| 212 | |
| 213 | def test_model_id_stored(self, tmp_path: pathlib.Path) -> None: |
| 214 | repo = _make_repo(tmp_path) |
| 215 | sid = _snap(repo) |
| 216 | data = json.loads( |
| 217 | _ct(repo, "--snapshot", sid, "--model-id", "claude-opus-4").output |
| 218 | ) |
| 219 | rec = read_commit(repo, data["commit_id"]) |
| 220 | assert rec is not None |
| 221 | assert rec.model_id == "claude-opus-4" |
| 222 | |
| 223 | def test_toolchain_id_stored(self, tmp_path: pathlib.Path) -> None: |
| 224 | repo = _make_repo(tmp_path) |
| 225 | sid = _snap(repo) |
| 226 | data = json.loads( |
| 227 | _ct(repo, "--snapshot", sid, "--toolchain-id", "cursor-agent-v2").output |
| 228 | ) |
| 229 | rec = read_commit(repo, data["commit_id"]) |
| 230 | assert rec is not None |
| 231 | assert rec.toolchain_id == "cursor-agent-v2" |
| 232 | |
| 233 | def test_full_provenance_round_trip(self, tmp_path: pathlib.Path) -> None: |
| 234 | repo = _make_repo(tmp_path) |
| 235 | sid = _snap(repo) |
| 236 | data = json.loads(_ct( |
| 237 | repo, |
| 238 | "--snapshot", sid, |
| 239 | "--agent-id", "audit-bot", |
| 240 | "--model-id", "claude-4", |
| 241 | "--toolchain-id", "muse-agent-v1", |
| 242 | ).output) |
| 243 | rec = read_commit(repo, data["commit_id"]) |
| 244 | assert rec is not None |
| 245 | assert rec.agent_id == "audit-bot" |
| 246 | assert rec.model_id == "claude-4" |
| 247 | assert rec.toolchain_id == "muse-agent-v1" |
| 248 | |
| 249 | def test_defaults_to_empty_strings(self, tmp_path: pathlib.Path) -> None: |
| 250 | repo = _make_repo(tmp_path) |
| 251 | sid = _snap(repo) |
| 252 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 253 | rec = read_commit(repo, data["commit_id"]) |
| 254 | assert rec is not None |
| 255 | assert rec.agent_id == "" |
| 256 | assert rec.model_id == "" |
| 257 | assert rec.toolchain_id == "" |
| 258 | |
| 259 | |
| 260 | # --------------------------------------------------------------------------- |
| 261 | # Integration — --branch override |
| 262 | # --------------------------------------------------------------------------- |
| 263 | |
| 264 | |
| 265 | class TestBranchOverride: |
| 266 | def test_branch_override_stored(self, tmp_path: pathlib.Path) -> None: |
| 267 | repo = _make_repo(tmp_path) |
| 268 | sid = _snap(repo) |
| 269 | data = json.loads(_ct(repo, "--snapshot", sid, "--branch", "feat/x").output) |
| 270 | rec = read_commit(repo, data["commit_id"]) |
| 271 | assert rec is not None |
| 272 | assert rec.branch == "feat/x" |
| 273 | |
| 274 | |
| 275 | # --------------------------------------------------------------------------- |
| 276 | # Error cases |
| 277 | # --------------------------------------------------------------------------- |
| 278 | |
| 279 | |
| 280 | class TestErrors: |
| 281 | def test_missing_snapshot_errors(self, tmp_path: pathlib.Path) -> None: |
| 282 | repo = _make_repo(tmp_path) |
| 283 | result = _ct(repo, "--snapshot", "dead" + "beef" * 15) |
| 284 | assert result.exit_code == ExitCode.USER_ERROR |
| 285 | |
| 286 | def test_invalid_snapshot_id_errors(self, tmp_path: pathlib.Path) -> None: |
| 287 | repo = _make_repo(tmp_path) |
| 288 | result = _ct(repo, "--snapshot", "not-hex") |
| 289 | assert result.exit_code == ExitCode.USER_ERROR |
| 290 | |
| 291 | def test_invalid_parent_id_errors(self, tmp_path: pathlib.Path) -> None: |
| 292 | repo = _make_repo(tmp_path) |
| 293 | sid = _snap(repo) |
| 294 | result = _ct(repo, "--snapshot", sid, "--parent", "bad-hex") |
| 295 | assert result.exit_code == ExitCode.USER_ERROR |
| 296 | |
| 297 | def test_unknown_format_errors(self, tmp_path: pathlib.Path) -> None: |
| 298 | repo = _make_repo(tmp_path) |
| 299 | sid = _snap(repo) |
| 300 | result = _ct(repo, "--snapshot", sid, "--format", "msgpack") |
| 301 | assert result.exit_code == ExitCode.USER_ERROR |
| 302 | |
| 303 | |
| 304 | # --------------------------------------------------------------------------- |
| 305 | # Security |
| 306 | # --------------------------------------------------------------------------- |
| 307 | |
| 308 | |
| 309 | class TestSecurity: |
| 310 | def test_no_traceback_on_bad_snapshot(self, tmp_path: pathlib.Path) -> None: |
| 311 | repo = _make_repo(tmp_path) |
| 312 | result = _ct(repo, "--snapshot", "bad") |
| 313 | assert "Traceback" not in result.output |
| 314 | |
| 315 | def test_no_traceback_on_too_many_parents(self, tmp_path: pathlib.Path) -> None: |
| 316 | repo = _make_repo(tmp_path) |
| 317 | sid = _snap(repo) |
| 318 | p = _commit(repo, sid) |
| 319 | result = _ct(repo, "--snapshot", sid, "--parent", p, "--parent", p, "--parent", p) |
| 320 | assert "Traceback" not in result.output |
| 321 | |
| 322 | |
| 323 | # --------------------------------------------------------------------------- |
| 324 | # Stress |
| 325 | # --------------------------------------------------------------------------- |
| 326 | |
| 327 | |
| 328 | class TestStress: |
| 329 | def test_200_sequential_commits(self, tmp_path: pathlib.Path) -> None: |
| 330 | repo = _make_repo(tmp_path) |
| 331 | sid = _snap(repo) |
| 332 | for i in range(200): |
| 333 | result = _ct(repo, "--snapshot", sid, "--message", f"commit {i}") |
| 334 | assert result.exit_code == 0, f"failed at iteration {i}" |
| 335 | data = json.loads(result.output) |
| 336 | assert data["commit_id"].startswith("sha256:") |
| 337 | assert len(data["commit_id"]) == 71 |
| 338 | |
| 339 | |
| 340 | # --------------------------------------------------------------------------- |
| 341 | # Supercharge — full JSON schema |
| 342 | # --------------------------------------------------------------------------- |
| 343 | |
| 344 | _FULL_KEYS = frozenset({ |
| 345 | "commit_id", |
| 346 | "snapshot_id", |
| 347 | "branch", |
| 348 | "message", |
| 349 | "committed_at", |
| 350 | "author", |
| 351 | "agent_id", |
| 352 | "model_id", |
| 353 | "toolchain_id", |
| 354 | "parent_commit_id", |
| 355 | "parent2_commit_id", |
| 356 | "duration_ms", |
| 357 | "exit_code", |
| 358 | }) |
| 359 | |
| 360 | |
| 361 | class TestJsonSchemaComplete: |
| 362 | """JSON output must carry the full commit record so agents need no follow-up read.""" |
| 363 | |
| 364 | def test_all_keys_present_on_success(self, tmp_path: pathlib.Path) -> None: |
| 365 | repo = _make_repo(tmp_path) |
| 366 | sid = _snap(repo) |
| 367 | data = json.loads(_ct(repo, "--snapshot", sid, "--message", "m").output) |
| 368 | assert _FULL_KEYS <= set(data.keys()), ( |
| 369 | f"Missing keys: {_FULL_KEYS - set(data.keys())}" |
| 370 | ) |
| 371 | |
| 372 | def test_snapshot_id_echoed(self, tmp_path: pathlib.Path) -> None: |
| 373 | repo = _make_repo(tmp_path) |
| 374 | sid = _snap(repo) |
| 375 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 376 | assert data["snapshot_id"] == sid |
| 377 | |
| 378 | def test_branch_echoed(self, tmp_path: pathlib.Path) -> None: |
| 379 | repo = _make_repo(tmp_path) |
| 380 | sid = _snap(repo) |
| 381 | data = json.loads(_ct(repo, "--snapshot", sid, "--branch", "feat/x").output) |
| 382 | assert data["branch"] == "feat/x" |
| 383 | |
| 384 | def test_message_echoed(self, tmp_path: pathlib.Path) -> None: |
| 385 | repo = _make_repo(tmp_path) |
| 386 | sid = _snap(repo) |
| 387 | data = json.loads(_ct(repo, "--snapshot", sid, "--message", "hello world").output) |
| 388 | assert data["message"] == "hello world" |
| 389 | |
| 390 | def test_author_echoed(self, tmp_path: pathlib.Path) -> None: |
| 391 | repo = _make_repo(tmp_path) |
| 392 | sid = _snap(repo) |
| 393 | data = json.loads(_ct(repo, "--snapshot", sid, "--author", "gabriel").output) |
| 394 | assert data["author"] == "gabriel" |
| 395 | |
| 396 | def test_agent_id_echoed(self, tmp_path: pathlib.Path) -> None: |
| 397 | repo = _make_repo(tmp_path) |
| 398 | sid = _snap(repo) |
| 399 | data = json.loads(_ct(repo, "--snapshot", sid, "--agent-id", "bot-x").output) |
| 400 | assert data["agent_id"] == "bot-x" |
| 401 | |
| 402 | def test_model_id_echoed(self, tmp_path: pathlib.Path) -> None: |
| 403 | repo = _make_repo(tmp_path) |
| 404 | sid = _snap(repo) |
| 405 | data = json.loads(_ct(repo, "--snapshot", sid, "--model-id", "claude-opus-4").output) |
| 406 | assert data["model_id"] == "claude-opus-4" |
| 407 | |
| 408 | def test_toolchain_id_echoed(self, tmp_path: pathlib.Path) -> None: |
| 409 | repo = _make_repo(tmp_path) |
| 410 | sid = _snap(repo) |
| 411 | data = json.loads(_ct(repo, "--snapshot", sid, "--toolchain-id", "v2").output) |
| 412 | assert data["toolchain_id"] == "v2" |
| 413 | |
| 414 | def test_parent_commit_id_null_when_no_parent(self, tmp_path: pathlib.Path) -> None: |
| 415 | repo = _make_repo(tmp_path) |
| 416 | sid = _snap(repo) |
| 417 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 418 | assert data["parent_commit_id"] is None |
| 419 | |
| 420 | def test_parent_commit_id_present(self, tmp_path: pathlib.Path) -> None: |
| 421 | repo = _make_repo(tmp_path) |
| 422 | sid = _snap(repo) |
| 423 | p1 = _commit(repo, sid) |
| 424 | data = json.loads(_ct(repo, "--snapshot", sid, "--parent", p1).output) |
| 425 | assert data["parent_commit_id"] == p1 |
| 426 | |
| 427 | def test_parent2_commit_id_null_when_no_second_parent(self, tmp_path: pathlib.Path) -> None: |
| 428 | repo = _make_repo(tmp_path) |
| 429 | sid = _snap(repo) |
| 430 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 431 | assert data["parent2_commit_id"] is None |
| 432 | |
| 433 | def test_parent2_commit_id_present_for_merge(self, tmp_path: pathlib.Path) -> None: |
| 434 | repo = _make_repo(tmp_path) |
| 435 | sid = _snap(repo) |
| 436 | p1 = _commit(repo, sid, message="p1") |
| 437 | p2 = _commit(repo, sid, message="p2") |
| 438 | data = json.loads(_ct(repo, "--snapshot", sid, "--parent", p1, "--parent", p2).output) |
| 439 | assert data["parent2_commit_id"] == p2 |
| 440 | |
| 441 | def test_committed_at_is_iso_string(self, tmp_path: pathlib.Path) -> None: |
| 442 | repo = _make_repo(tmp_path) |
| 443 | sid = _snap(repo) |
| 444 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 445 | ts = data["committed_at"] |
| 446 | assert isinstance(ts, str) |
| 447 | # Must parse as ISO datetime |
| 448 | datetime.datetime.fromisoformat(ts) |
| 449 | |
| 450 | def test_exit_code_zero(self, tmp_path: pathlib.Path) -> None: |
| 451 | repo = _make_repo(tmp_path) |
| 452 | sid = _snap(repo) |
| 453 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 454 | assert data["exit_code"] == 0 |
| 455 | |
| 456 | |
| 457 | # --------------------------------------------------------------------------- |
| 458 | # Supercharge — duration_ms |
| 459 | # --------------------------------------------------------------------------- |
| 460 | |
| 461 | |
| 462 | class TestElapsed: |
| 463 | def test_elapsed_present(self, tmp_path: pathlib.Path) -> None: |
| 464 | repo = _make_repo(tmp_path) |
| 465 | sid = _snap(repo) |
| 466 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 467 | assert "duration_ms" in data |
| 468 | |
| 469 | def test_elapsed_is_float(self, tmp_path: pathlib.Path) -> None: |
| 470 | repo = _make_repo(tmp_path) |
| 471 | sid = _snap(repo) |
| 472 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 473 | assert isinstance(data["duration_ms"], float) |
| 474 | |
| 475 | def test_elapsed_non_negative(self, tmp_path: pathlib.Path) -> None: |
| 476 | repo = _make_repo(tmp_path) |
| 477 | sid = _snap(repo) |
| 478 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 479 | assert data["duration_ms"] >= 0.0 |
| 480 | |
| 481 | |
| 482 | # --------------------------------------------------------------------------- |
| 483 | # Supercharge — exit_code |
| 484 | # --------------------------------------------------------------------------- |
| 485 | |
| 486 | |
| 487 | class TestExitCode: |
| 488 | def test_exit_code_zero_on_success(self, tmp_path: pathlib.Path) -> None: |
| 489 | repo = _make_repo(tmp_path) |
| 490 | sid = _snap(repo) |
| 491 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 492 | assert data["exit_code"] == 0 |
| 493 | |
| 494 | def test_process_exit_zero_on_success(self, tmp_path: pathlib.Path) -> None: |
| 495 | repo = _make_repo(tmp_path) |
| 496 | sid = _snap(repo) |
| 497 | result = _ct(repo, "--snapshot", sid) |
| 498 | assert result.exit_code == 0 |
| 499 | |
| 500 | |
| 501 | # --------------------------------------------------------------------------- |
| 502 | # Supercharge — data integrity |
| 503 | # --------------------------------------------------------------------------- |
| 504 | |
| 505 | |
| 506 | class TestDataIntegrity: |
| 507 | def test_commit_id_roundtrips_via_store(self, tmp_path: pathlib.Path) -> None: |
| 508 | """commit_id in JSON matches what was actually written to the store.""" |
| 509 | repo = _make_repo(tmp_path) |
| 510 | sid = _snap(repo) |
| 511 | data = json.loads(_ct(repo, "--snapshot", sid, "--message", "integrity check").output) |
| 512 | rec = read_commit(repo, data["commit_id"]) |
| 513 | assert rec is not None |
| 514 | assert rec.commit_id == data["commit_id"] |
| 515 | assert rec.snapshot_id == data["snapshot_id"] |
| 516 | assert rec.message == data["message"] |
| 517 | |
| 518 | def test_snapshot_id_matches_store(self, tmp_path: pathlib.Path) -> None: |
| 519 | repo = _make_repo(tmp_path) |
| 520 | sid = _snap(repo) |
| 521 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 522 | rec = read_commit(repo, data["commit_id"]) |
| 523 | assert rec is not None |
| 524 | assert rec.snapshot_id == sid |
| 525 | |
| 526 | def test_parent_id_matches_store(self, tmp_path: pathlib.Path) -> None: |
| 527 | repo = _make_repo(tmp_path) |
| 528 | sid = _snap(repo) |
| 529 | p1 = _commit(repo, sid) |
| 530 | data = json.loads(_ct(repo, "--snapshot", sid, "--parent", p1).output) |
| 531 | rec = read_commit(repo, data["commit_id"]) |
| 532 | assert rec is not None |
| 533 | assert rec.parent_commit_id == data["parent_commit_id"] == p1 |
| 534 | |
| 535 | def test_provenance_matches_store(self, tmp_path: pathlib.Path) -> None: |
| 536 | repo = _make_repo(tmp_path) |
| 537 | sid = _snap(repo) |
| 538 | data = json.loads(_ct( |
| 539 | repo, "--snapshot", sid, |
| 540 | "--agent-id", "integrity-bot", |
| 541 | "--model-id", "claude-sonnet-4-6", |
| 542 | "--toolchain-id", "test-chain", |
| 543 | ).output) |
| 544 | rec = read_commit(repo, data["commit_id"]) |
| 545 | assert rec is not None |
| 546 | assert rec.agent_id == data["agent_id"] == "integrity-bot" |
| 547 | assert rec.model_id == data["model_id"] == "claude-sonnet-4-6" |
| 548 | assert rec.toolchain_id == data["toolchain_id"] == "test-chain" |
| 549 | |
| 550 | |
| 551 | # --------------------------------------------------------------------------- |
| 552 | # Supercharge — security (ANSI injection) |
| 553 | # --------------------------------------------------------------------------- |
| 554 | |
| 555 | |
| 556 | class TestSecurityAnsi: |
| 557 | def test_ansi_in_message_does_not_corrupt_json(self, tmp_path: pathlib.Path) -> None: |
| 558 | repo = _make_repo(tmp_path) |
| 559 | sid = _snap(repo) |
| 560 | evil_message = "feat: \x1b[31mred\x1b[0m alert" |
| 561 | result = _ct(repo, "--snapshot", sid, "--message", evil_message) |
| 562 | assert result.exit_code == 0 |
| 563 | data = json.loads(result.output) |
| 564 | assert data["message"] == evil_message |
| 565 | |
| 566 | def test_ansi_in_author_does_not_corrupt_json(self, tmp_path: pathlib.Path) -> None: |
| 567 | repo = _make_repo(tmp_path) |
| 568 | sid = _snap(repo) |
| 569 | evil_author = "\x1b[32mgabriel\x1b[0m" |
| 570 | result = _ct(repo, "--snapshot", sid, "--author", evil_author) |
| 571 | assert result.exit_code == 0 |
| 572 | data = json.loads(result.output) |
| 573 | assert data["author"] == evil_author |
| 574 | |
| 575 | def test_long_message_handled(self, tmp_path: pathlib.Path) -> None: |
| 576 | repo = _make_repo(tmp_path) |
| 577 | sid = _snap(repo) |
| 578 | long_msg = "x" * 10_000 |
| 579 | data = json.loads(_ct(repo, "--snapshot", sid, "--message", long_msg).output) |
| 580 | rec = read_commit(repo, data["commit_id"]) |
| 581 | assert rec is not None |
| 582 | assert rec.message == long_msg |
| 583 | |
| 584 | |
| 585 | # --------------------------------------------------------------------------- |
| 586 | # Supercharge — performance |
| 587 | # --------------------------------------------------------------------------- |
| 588 | |
| 589 | |
| 590 | class TestPerformance: |
| 591 | def test_single_commit_under_500ms(self, tmp_path: pathlib.Path) -> None: |
| 592 | import time |
| 593 | repo = _make_repo(tmp_path) |
| 594 | sid = _snap(repo) |
| 595 | t0 = time.monotonic() |
| 596 | result = _ct(repo, "--snapshot", sid) |
| 597 | elapsed = time.monotonic() - t0 |
| 598 | assert result.exit_code == 0 |
| 599 | assert elapsed < 0.5, f"commit-tree took {elapsed:.3f}s — too slow" |
| 600 | |
| 601 | def test_duration_ms_reasonable(self, tmp_path: pathlib.Path) -> None: |
| 602 | repo = _make_repo(tmp_path) |
| 603 | sid = _snap(repo) |
| 604 | data = json.loads(_ct(repo, "--snapshot", sid).output) |
| 605 | assert data["duration_ms"] < 5.0, ( |
| 606 | f"reported elapsed {data['duration_ms']}s — implausibly slow" |
| 607 | ) |
File History
2 commits
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3
docs: docstring sprint contract→find-symbol — idiomatic run…
Sonnet 4.6
patch
138 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9
fix(cursorignore): remove git-ism (.git/worktrees)
Human
minor
⚠
141 days ago