"""Supercharge tests for ``muse patch-id``. TDD — [RED] tests fail until the feature lands; [GREEN] tests replace the broken ``test_cmd_patch_id.py`` (which used raw hex object IDs without the required ``sha256:`` prefix). New features under test ----------------------- - ``duration_ms`` [RED] — wall-clock ms in JSON output - ``exit_code`` [RED] — always present in JSON output - ``files_changed`` [RED] — count of files in the diff in JSON output - ``stable`` [RED] — boolean reflecting --stable flag in JSON output Gap-fill / regression coverage [GREEN] ---------------------------------------- - _compute_patch_id unit tests (all using sha256: prefix) - JSON schema keys present - Text output format « » - Same diff → same patch-id (cherry-pick detection) - Different diff → different patch-id - --stable whitespace normalisation via CLI and unit - Initial commit (no parent) has deterministic patch-id - Branch name ref, explicit commit ID ref - Error paths: empty repo, bad ref - Security: ANSI, null byte, path traversal, very long ref, no tracebacks - Data integrity: patch_id changes when content changes - Multi-file commit patch-id covers all changed files - Binary file diffs included in patch-id - Performance: duration_ms < 2000ms for 20-file commit - Stress: 10 distinct commits → 10 distinct patch-ids """ from __future__ import annotations from collections.abc import Mapping import datetime import json import pathlib import pytest from muse.cli.commands.patch_id import _compute_patch_id from muse.core.object_store import write_object from muse.core.ids import hash_commit as compute_commit_id, hash_snapshot as compute_snapshot_id from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot from muse.core.types import Manifest, blob_id, split_id from muse.core.paths import heads_dir, muse_dir, ref_path from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() _TS = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) _REPO_ID = "patch-id-test" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _oid(content: bytes) -> str: """Return a sha256:-prefixed object ID for content.""" return blob_id(content) def _init_repo(path: pathlib.Path) -> pathlib.Path: muse = muse_dir(path) for d in ("commits", "snapshots", "objects", "refs/heads"): (muse / d).mkdir(parents=True, exist_ok=True) (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") (muse / "repo.json").write_text( json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" ) return path def _write_obj(repo: pathlib.Path, content: bytes) -> str: oid = _oid(content) write_object(repo, oid, content) return oid def _commit( repo: pathlib.Path, msg: str, manifest: dict[str, str], *, branch: str = "main", parent: str | None = None, ) -> str: sid = compute_snapshot_id(manifest) write_snapshot(repo, SnapshotRecord(snapshot_id=sid, manifest=manifest, created_at=_TS)) parent_ids = [parent] if parent else [] cid = compute_commit_id( parent_ids=parent_ids, snapshot_id=sid, message=msg, committed_at_iso=_TS.isoformat(), author="gabriel",) write_commit(repo, CommitRecord( commit_id=cid, branch=branch, snapshot_id=sid, message=msg, committed_at=_TS, author="gabriel", parent_commit_id=parent, parent2_commit_id=None, )) ref = ref_path(repo, branch) ref.parent.mkdir(parents=True, exist_ok=True) ref.write_text(cid) return cid def _pid(repo: pathlib.Path, *args: str) -> InvokeResult: return runner.invoke(None, ["patch-id", *args], env={"MUSE_REPO_ROOT": str(repo)}) def _json_out(r: InvokeResult) -> Mapping[str, object]: for line in r.output.splitlines(): line = line.strip() if line.startswith("{"): return json.loads(line) raise ValueError(f"No JSON in output:\n{r.output!r}") # --------------------------------------------------------------------------- # _compute_patch_id unit tests [GREEN — fix sha256: prefix] # --------------------------------------------------------------------------- class TestComputePatchId: def test_same_diff_same_id(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"x = 1\n") oid_b = _write_obj(repo, b"x = 2\n") base = {"f.py": oid_a} target = {"f.py": oid_b} assert _compute_patch_id(repo, base, target, stable=False) == \ _compute_patch_id(repo, base, target, stable=False) def test_result_is_64_hex_chars(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"a") oid_b = _write_obj(repo, b"b") pid = _compute_patch_id(repo, {"f.py": oid_a}, {"f.py": oid_b}, stable=False) assert pid.startswith("sha256:") and len(pid) == 71 assert all(c in "0123456789abcdef" for c in split_id(pid)[1]) def test_different_content_different_id(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"v1\n") oid_b = _write_obj(repo, b"v2\n") oid_c = _write_obj(repo, b"v3\n") id1 = _compute_patch_id(repo, {"f.py": oid_a}, {"f.py": oid_b}, stable=False) id2 = _compute_patch_id(repo, {"f.py": oid_a}, {"f.py": oid_c}, stable=False) assert id1 != id2 def test_no_op_commit_deterministic(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"unchanged\n") manifest = {"f.py": oid} pid1 = _compute_patch_id(repo, manifest, manifest, stable=False) pid2 = _compute_patch_id(repo, manifest, manifest, stable=False) assert pid1 == pid2 def test_stable_normalizes_trailing_whitespace(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_base = _write_obj(repo, b"x = 1\n") oid_clean = _write_obj(repo, b"x = 2\n") oid_ws = _write_obj(repo, b"x = 2 \n") base = {"f.py": oid_base} id_clean = _compute_patch_id(repo, base, {"f.py": oid_clean}, stable=True) id_ws = _compute_patch_id(repo, base, {"f.py": oid_ws}, stable=True) assert id_clean == id_ws def test_unstable_sensitive_to_whitespace(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_base = _write_obj(repo, b"x = 1\n") oid_clean = _write_obj(repo, b"x = 2\n") oid_ws = _write_obj(repo, b"x = 2 \n") base = {"f.py": oid_base} id_clean = _compute_patch_id(repo, base, {"f.py": oid_clean}, stable=False) id_ws = _compute_patch_id(repo, base, {"f.py": oid_ws}, stable=False) assert id_clean != id_ws def test_file_order_does_not_affect_id(self, tmp_path: pathlib.Path) -> None: """Files are sorted alphabetically so order of dict keys is irrelevant.""" repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"a\n") oid_b = _write_obj(repo, b"b\n") oid_a2 = _write_obj(repo, b"a2\n") oid_b2 = _write_obj(repo, b"b2\n") base1 = {"a.py": oid_a, "b.py": oid_b} base2 = {"b.py": oid_b, "a.py": oid_a} target1 = {"a.py": oid_a2, "b.py": oid_b2} target2 = {"b.py": oid_b2, "a.py": oid_a2} assert _compute_patch_id(repo, base1, target1, stable=False) == \ _compute_patch_id(repo, base2, target2, stable=False) def test_initial_commit_no_parent(self, tmp_path: pathlib.Path) -> None: """Initial commit: base_manifest={}, target has files.""" repo = _init_repo(tmp_path) oid = _write_obj(repo, b"hello\n") pid = _compute_patch_id(repo, {}, {"f.py": oid}, stable=False) assert pid.startswith("sha256:") and len(pid) == 71 def test_deleted_file_affects_id(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"bye\n") id_del = _compute_patch_id(repo, {"f.py": oid}, {}, stable=False) id_add = _compute_patch_id(repo, {}, {"f.py": oid}, stable=False) assert id_del != id_add def test_binary_content_included(self, tmp_path: pathlib.Path) -> None: """Binary files (non-UTF-8) still produce a stable patch-id.""" repo = _init_repo(tmp_path) binary_v1 = bytes(range(256)) binary_v2 = bytes(range(255, -1, -1)) oid1 = _write_obj(repo, binary_v1) oid2 = _write_obj(repo, binary_v2) pid1 = _compute_patch_id(repo, {"img.bin": oid1}, {"img.bin": oid2}, stable=False) pid2 = _compute_patch_id(repo, {"img.bin": oid1}, {"img.bin": oid2}, stable=False) assert pid1 == pid2 assert pid1.startswith("sha256:") and len(pid1) == 71 # --------------------------------------------------------------------------- # JSON output: duration_ms, exit_code, files_changed, stable [RED] # --------------------------------------------------------------------------- class TestJsonSupercharge: """[RED] New fields in --json output.""" def test_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "duration_ms" in d def test_json_duration_ms_non_negative(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["duration_ms"] >= 0.0 def test_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "exit_code" in d def test_json_exit_code_zero_on_success(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["exit_code"] == 0 def test_json_has_files_changed(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "files_changed" in d def test_json_files_changed_correct_count(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"a") oid_b = _write_obj(repo, b"b") _commit(repo, "init", {"a.py": oid_a, "b.py": oid_b}) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 2 def test_json_files_changed_is_int(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert isinstance(d["files_changed"], int) def test_json_has_stable_field(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "stable" in d def test_json_stable_false_by_default(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["stable"] is False def test_json_stable_true_with_flag(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json", "--stable")) assert d["stable"] is True def test_stable_flag_produces_different_patch_id_for_ws_diff( self, tmp_path: pathlib.Path ) -> None: """--stable and no-flag produce different IDs for trailing-whitespace diff.""" repo = _init_repo(tmp_path) oid_base = _write_obj(repo, b"x = 1\n") c1 = _commit(repo, "c1", {"f.py": oid_base}) oid_ws = _write_obj(repo, b"x = 1 \n") _commit(repo, "c2 ws", {"f.py": oid_ws}, parent=c1) d_normal = _json_out(_pid(repo, "--json")) d_stable = _json_out(_pid(repo, "--json", "--stable")) assert d_normal["patch_id"] != d_stable["patch_id"] # --------------------------------------------------------------------------- # Existing JSON fields still present [GREEN] # --------------------------------------------------------------------------- class TestJsonGreen: def test_commit_id_present(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "commit_id" in d def test_patch_id_present(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert "patch_id" in d def test_patch_id_is_64_hex(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["patch_id"].startswith("sha256:") and len(d["patch_id"]) == 71 assert all(c in "0123456789abcdef" for c in split_id(d["patch_id"])[1]) def test_subject_present(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "feat: hello world", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["subject"] == "feat: hello world" def test_commit_id_matches_head(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") cid = _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["commit_id"] == cid def test_same_diff_same_patch_id(self, tmp_path: pathlib.Path) -> None: """Cherry-pick detection: same logical change → same patch_id.""" repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"v1\n") c1 = _commit(repo, "c1", {"f.py": oid_a}) oid_b = _write_obj(repo, b"v2\n") _commit(repo, "c2", {"f.py": oid_b}, parent=c1) d1 = _json_out(_pid(repo, "--json")) # Second repo with identical diff repo2 = _init_repo(tmp_path / "repo2") _write_obj(repo2, b"v1\n") c1b = _commit(repo2, "c1", {"f.py": oid_a}) _write_obj(repo2, b"v2\n") _commit(repo2, "c2 clone", {"f.py": oid_b}, parent=c1b) d2 = _json_out(_pid(repo2, "--json")) assert d1["patch_id"] == d2["patch_id"] def test_different_diff_different_patch_id(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"v1\n") c1 = _commit(repo, "c1", {"f.py": oid_a}) oid_b = _write_obj(repo, b"v2\n") _commit(repo, "c2", {"f.py": oid_b}, parent=c1) d1 = _json_out(_pid(repo, "--json")) repo2 = _init_repo(tmp_path / "repo2") _write_obj(repo2, b"v1\n") c1b = _commit(repo2, "c1", {"f.py": oid_a}) oid_c = _write_obj(repo2, b"completely different content\n") _commit(repo2, "c2 different", {"f.py": oid_c}, parent=c1b) d2 = _json_out(_pid(repo2, "--json")) assert d1["patch_id"] != d2["patch_id"] def test_explicit_commit_id_ref(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") cid = _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, cid, "--json")) assert d["commit_id"] == cid def test_branch_name_ref(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "main", "--json")) assert "patch_id" in d # --------------------------------------------------------------------------- # Text output format [GREEN] # --------------------------------------------------------------------------- class TestTextOutput: def test_text_format_two_parts(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) r = _pid(repo) assert r.exit_code == 0 parts = r.output.strip().split() assert len(parts) == 2 def test_text_patch_id_is_hex(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) parts = _pid(repo).output.strip().split() assert parts[0].startswith("sha256:") and len(parts[0]) == 71 assert all(c in "0123456789abcdef" for c in split_id(parts[0])[1]) def test_text_commit_id_matches_json(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) text_parts = _pid(repo).output.strip().split() json_d = _json_out(_pid(repo, "--json")) assert text_parts[1] == json_d["commit_id"] assert text_parts[0] == json_d["patch_id"] # --------------------------------------------------------------------------- # files_changed correctness [RED] # --------------------------------------------------------------------------- class TestFilesChanged: def test_initial_commit_all_files_counted(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"a") oid_b = _write_obj(repo, b"b") oid_c = _write_obj(repo, b"c") _commit(repo, "init", {"a.py": oid_a, "b.py": oid_b, "c.py": oid_c}) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 3 def test_deletion_counted(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"gone") c1 = _commit(repo, "c1", {"old.py": oid}) _commit(repo, "c2 delete", {}, parent=c1) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 1 def test_modification_counted(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_v1 = _write_obj(repo, b"v1") c1 = _commit(repo, "c1", {"f.py": oid_v1}) oid_v2 = _write_obj(repo, b"v2") _commit(repo, "c2 mod", {"f.py": oid_v2}, parent=c1) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 1 def test_unchanged_files_not_counted(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_keep = _write_obj(repo, b"keep") oid_chg = _write_obj(repo, b"v1") c1 = _commit(repo, "c1", {"keep.py": oid_keep, "chg.py": oid_chg}) oid_chg2 = _write_obj(repo, b"v2") _commit(repo, "c2", {"keep.py": oid_keep, "chg.py": oid_chg2}, parent=c1) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 1 def test_no_op_commit_zero_files_changed(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"same") c1 = _commit(repo, "c1", {"f.py": oid}) _commit(repo, "c2 noop", {"f.py": oid}, parent=c1) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 0 # --------------------------------------------------------------------------- # Error paths [GREEN] # --------------------------------------------------------------------------- class TestErrors: def test_empty_repo_exits_nonzero(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) r = _pid(repo, "--json") assert r.exit_code != 0 def test_bad_ref_exits_nonzero(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) r = _pid(repo, "no-such-ref", "--json") assert r.exit_code != 0 def test_no_traceback_on_bad_ref(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) r = _pid(repo, "no-such-ref") assert "Traceback" not in r.output assert "Traceback" not in r.stderr def test_error_to_stderr_not_stdout(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) r = _pid(repo, "--json") assert r.exit_code != 0 assert "❌" in r.stderr or r.exit_code != 0 # --------------------------------------------------------------------------- # Security [GREEN] # --------------------------------------------------------------------------- class TestSecurity: def test_ansi_in_ref_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) assert _pid(repo, "\x1b[31mbad\x1b[0m").exit_code != 0 def test_null_byte_in_ref_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) assert _pid(repo, "main\x00malicious").exit_code != 0 def test_path_traversal_in_ref_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) assert _pid(repo, "../../etc/passwd").exit_code != 0 def test_very_long_ref_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) assert _pid(repo, "a" * 300).exit_code != 0 def test_no_traceback_on_ansi_ref(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) r = _pid(repo, "\x1b[31mbad\x1b[0m") assert "Traceback" not in r.output assert "Traceback" not in r.stderr # --------------------------------------------------------------------------- # Data integrity [GREEN] # --------------------------------------------------------------------------- class TestDataIntegrity: def test_patch_id_changes_when_content_changes(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_v1 = _write_obj(repo, b"version 1\n") c1 = _commit(repo, "c1", {"f.py": oid_v1}) oid_v2 = _write_obj(repo, b"version 2\n") _commit(repo, "c2", {"f.py": oid_v2}, parent=c1) d1 = _json_out(_pid(repo, c1, "--json")) # Change HEAD to c2 by making another commit oid_v3 = _write_obj(repo, b"version 3\n") c3 = _commit(repo, "c3", {"f.py": oid_v3}, parent=c1) # re-point HEAD ref directly (two different commits from same parent) (heads_dir(repo) / "main").write_text(c3) d3 = _json_out(_pid(repo, "--json")) assert d1["patch_id"] != d3["patch_id"] def test_adding_file_changes_patch_id(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_a = _write_obj(repo, b"a\n") c1 = _commit(repo, "c1", {"a.py": oid_a}) oid_b = _write_obj(repo, b"b\n") _commit(repo, "c2 add b", {"a.py": oid_a, "b.py": oid_b}, parent=c1) d = _json_out(_pid(repo, "--json")) assert d["files_changed"] == 1 assert d["patch_id"] is not None def test_patch_id_stable_vs_unstable_differ_for_ws(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid_base = _write_obj(repo, b"x = 1\n") c1 = _commit(repo, "c1", {"f.py": oid_base}) oid_ws = _write_obj(repo, b"x = 1 \n") _commit(repo, "c2", {"f.py": oid_ws}, parent=c1) d_normal = _json_out(_pid(repo, "--json")) d_stable = _json_out(_pid(repo, "--json", "--stable")) assert d_normal["patch_id"] != d_stable["patch_id"] # --------------------------------------------------------------------------- # Performance [GREEN] # --------------------------------------------------------------------------- class TestPerformance: def test_duration_ms_under_two_seconds(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) manifest: dict[str, str] = {} for i in range(20): content = f"# module {i}\n" .encode() * 50 oid = _write_obj(repo, content) manifest[f"src/file_{i:02d}.py"] = oid _commit(repo, "feat: 20 files", manifest) d = _json_out(_pid(repo, "--json")) assert d["duration_ms"] < 2000.0 def test_duration_ms_non_negative(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) oid = _write_obj(repo, b"x") _commit(repo, "init", {"f.py": oid}) d = _json_out(_pid(repo, "--json")) assert d["duration_ms"] >= 0.0 # --------------------------------------------------------------------------- # Stress [GREEN] # --------------------------------------------------------------------------- class TestStress: def test_10_distinct_commits_10_distinct_patch_ids(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) patch_ids: set[str] = set() parent: str | None = None for i in range(10): content = f"value = {i}\n".encode() oid = _write_obj(repo, content) cid = _commit(repo, f"c{i}", {"file.py": oid}, parent=parent) d = _json_out(_pid(repo, cid, "--json")) patch_ids.add(d["patch_id"]) parent = cid assert len(patch_ids) == 10 def test_50_file_commit(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) manifest: dict[str, str] = {} for i in range(50): oid = _write_obj(repo, f"file {i}\n".encode() * 20) manifest[f"f{i:03d}.py"] = oid _commit(repo, "feat: 50 files", manifest) r = _pid(repo, "--json") assert r.exit_code == 0 d = _json_out(r) assert d["files_changed"] == 50 # --------------------------------------------------------------------------- # TestRegisterFlags — argparse-level verification # --------------------------------------------------------------------------- class TestRegisterFlags: """Verify that register() wires --json / -j correctly.""" def _make_parser(self) -> "argparse.ArgumentParser": import argparse from muse.cli.commands.patch_id import register ap = argparse.ArgumentParser() subs = ap.add_subparsers() register(subs) return ap def test_json_flag_long(self) -> None: ns = self._make_parser().parse_args(["patch-id", "--json"]) assert ns.json_out is True def test_j_alias(self) -> None: ns = self._make_parser().parse_args(["patch-id", "-j"]) assert ns.json_out is True def test_default_is_text(self) -> None: ns = self._make_parser().parse_args(["patch-id"]) assert ns.json_out is False def test_dest_is_json_out(self) -> None: ns = self._make_parser().parse_args(["patch-id", "-j"]) assert hasattr(ns, "json_out") assert not hasattr(ns, "fmt")