test_lineage_algorithm.py
python
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6
chore(tests): add docstring to tests/__init__.py so rc14 tr…
Human
26 days ago
| 1 | """Comprehensive unit tests for build_lineage — the symbol provenance engine. |
| 2 | |
| 3 | build_lineage(address, commits) takes a pre-sorted list of CommitRecord objects |
| 4 | and returns the provenance chain. No repo, no disk I/O — pure in-memory. |
| 5 | |
| 6 | Coverage matrix |
| 7 | --------------- |
| 8 | Created — InsertOp with no prior live symbol sharing the content_id |
| 9 | Copied — InsertOp whose content_id matches a currently-live symbol |
| 10 | Renamed — InsertOp + DeleteOp in same commit with matching content_id, same file |
| 11 | Moved — InsertOp + DeleteOp in same commit with matching content_id, different file |
| 12 | Modified — ReplaceOp classified as signature_change / full_rewrite |
| 13 | Deleted — DeleteOp at the target address |
| 14 | Multi-event — symbol created, modified, deleted, re-created in the same history |
| 15 | Registry — incremental content_id registry enables accurate copy detection |
| 16 | across many commits without re-parsing blobs |
| 17 | No events — address absent from all commits → empty list |
| 18 | Empty list — no commits at all → empty list |
| 19 | """ |
| 20 | |
| 21 | from __future__ import annotations |
| 22 | |
| 23 | import datetime |
| 24 | |
| 25 | import pytest |
| 26 | |
| 27 | from muse.cli.commands.lineage import build_lineage |
| 28 | from muse.core.store import CommitRecord |
| 29 | from muse.domain import DeleteOp, DomainOp, InsertOp, ReplaceOp |
| 30 | |
| 31 | |
| 32 | # --------------------------------------------------------------------------- |
| 33 | # Helpers — pure in-memory, no disk I/O |
| 34 | # --------------------------------------------------------------------------- |
| 35 | |
| 36 | |
| 37 | _T0 = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 38 | _SEQ: list[int] = [0] |
| 39 | |
| 40 | |
| 41 | def _uid() -> str: |
| 42 | _SEQ[0] += 1 |
| 43 | return f"{_SEQ[0]:064d}" |
| 44 | |
| 45 | |
| 46 | def _commit( |
| 47 | ops: list[DomainOp], |
| 48 | *, |
| 49 | offset_days: int = 0, |
| 50 | message: str = "commit", |
| 51 | commit_id: str | None = None, |
| 52 | ) -> CommitRecord: |
| 53 | """Return a synthetic CommitRecord — no repo, no disk write.""" |
| 54 | return CommitRecord( |
| 55 | commit_id=commit_id or _uid(), |
| 56 | repo_id="test-repo", |
| 57 | branch="main", |
| 58 | snapshot_id="snap-" + (commit_id or _uid()), |
| 59 | message=message, |
| 60 | committed_at=_T0 + datetime.timedelta(days=offset_days), |
| 61 | structured_delta={"ops": ops}, |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | def _insert(address: str, content_id: str) -> InsertOp: |
| 66 | return InsertOp(op="insert", address=address, content_id=content_id, position=None, content_summary="") |
| 67 | |
| 68 | |
| 69 | def _delete(address: str, content_id: str) -> DeleteOp: |
| 70 | return DeleteOp(op="delete", address=address, content_id=content_id, position=None, content_summary="") |
| 71 | |
| 72 | |
| 73 | def _replace( |
| 74 | address: str, |
| 75 | old_cid: str, |
| 76 | new_cid: str, |
| 77 | old_summary: str = "impl changed", |
| 78 | new_summary: str = "impl changed", |
| 79 | ) -> ReplaceOp: |
| 80 | return ReplaceOp( |
| 81 | op="replace", |
| 82 | address=address, |
| 83 | old_content_id=old_cid, |
| 84 | new_content_id=new_cid, |
| 85 | old_summary=old_summary, |
| 86 | new_summary=new_summary, |
| 87 | position=None, |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | # --------------------------------------------------------------------------- |
| 92 | # Empty / no-op cases |
| 93 | # --------------------------------------------------------------------------- |
| 94 | |
| 95 | |
| 96 | class TestEmptyCases: |
| 97 | def test_no_commits_returns_empty(self) -> None: |
| 98 | assert build_lineage("src/billing.py::compute_total", []) == [] |
| 99 | |
| 100 | def test_address_not_in_any_commit(self) -> None: |
| 101 | c = _commit([_insert("src/auth.py::login", "cid-login")]) |
| 102 | assert build_lineage("src/billing.py::compute_total", [c]) == [] |
| 103 | |
| 104 | def test_commit_with_no_structured_delta(self) -> None: |
| 105 | record = CommitRecord( |
| 106 | commit_id="b" * 64, |
| 107 | repo_id="test", |
| 108 | branch="main", |
| 109 | snapshot_id="snap", |
| 110 | message="no delta", |
| 111 | committed_at=_T0, |
| 112 | structured_delta=None, |
| 113 | ) |
| 114 | assert build_lineage("src/main.py::main", [record]) == [] |
| 115 | |
| 116 | |
| 117 | # --------------------------------------------------------------------------- |
| 118 | # Created |
| 119 | # --------------------------------------------------------------------------- |
| 120 | |
| 121 | |
| 122 | class TestCreated: |
| 123 | def test_first_insert_is_created(self) -> None: |
| 124 | addr = "src/billing.py::compute_total" |
| 125 | c = _commit([_insert(addr, "cid-v1")]) |
| 126 | events = build_lineage(addr, [c]) |
| 127 | assert len(events) == 1 |
| 128 | assert events[0].kind == "created" |
| 129 | |
| 130 | def test_created_event_has_correct_commit_id(self) -> None: |
| 131 | addr = "src/main.py::main" |
| 132 | cid = "d" * 64 |
| 133 | c = _commit([_insert(addr, "cid-v1")], commit_id=cid) |
| 134 | events = build_lineage(addr, [c]) |
| 135 | assert events[0].commit_id == cid |
| 136 | |
| 137 | def test_created_event_records_content_id(self) -> None: |
| 138 | addr = "src/main.py::main" |
| 139 | c = _commit([_insert(addr, "cid-abc")]) |
| 140 | events = build_lineage(addr, [c]) |
| 141 | assert events[0].new_content_id == "cid-abc" |
| 142 | |
| 143 | |
| 144 | # --------------------------------------------------------------------------- |
| 145 | # Deleted |
| 146 | # --------------------------------------------------------------------------- |
| 147 | |
| 148 | |
| 149 | class TestDeleted: |
| 150 | def test_delete_after_insert_is_deleted(self) -> None: |
| 151 | addr = "src/api.py::get_user" |
| 152 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 153 | c2 = _commit([_delete(addr, "cid-v1")], offset_days=1) |
| 154 | events = build_lineage(addr, [c1, c2]) |
| 155 | assert events[-1].kind == "deleted" |
| 156 | |
| 157 | def test_deleted_event_records_content_id(self) -> None: |
| 158 | addr = "src/api.py::delete_user" |
| 159 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 160 | c2 = _commit([_delete(addr, "cid-v1")], offset_days=1) |
| 161 | events = build_lineage(addr, [c1, c2]) |
| 162 | assert events[-1].kind == "deleted" |
| 163 | assert events[-1].old_content_id == "cid-v1" |
| 164 | |
| 165 | |
| 166 | # --------------------------------------------------------------------------- |
| 167 | # Modified |
| 168 | # --------------------------------------------------------------------------- |
| 169 | |
| 170 | |
| 171 | class TestModified: |
| 172 | def test_replace_op_is_modified(self) -> None: |
| 173 | addr = "src/core.py::hash_content" |
| 174 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 175 | c2 = _commit([_replace(addr, "cid-v1", "cid-v2")], offset_days=1) |
| 176 | events = build_lineage(addr, [c1, c2]) |
| 177 | assert any(e.kind == "modified" for e in events) |
| 178 | |
| 179 | def test_modified_signature_change(self) -> None: |
| 180 | addr = "src/core.py::process" |
| 181 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 182 | c2 = _commit( |
| 183 | [_replace(addr, "cid-v1", "cid-v2", "signature changed", "signature changed")], |
| 184 | offset_days=1, |
| 185 | ) |
| 186 | events = build_lineage(addr, [c1, c2]) |
| 187 | modified = [e for e in events if e.kind == "modified"] |
| 188 | assert modified[0].detail == "signature_change" |
| 189 | |
| 190 | def test_modified_full_rewrite(self) -> None: |
| 191 | addr = "src/core.py::transform" |
| 192 | c1 = _commit([_insert(addr, "cid-aaaa")], offset_days=0) |
| 193 | c2 = _commit([_replace(addr, "cid-aaaa", "cid-bbbb")], offset_days=1) |
| 194 | events = build_lineage(addr, [c1, c2]) |
| 195 | modified = [e for e in events if e.kind == "modified"] |
| 196 | assert modified[0].detail == "full_rewrite" |
| 197 | |
| 198 | def test_multiple_modifications(self) -> None: |
| 199 | addr = "src/worker.py::run" |
| 200 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 201 | c2 = _commit([_replace(addr, "cid-v1", "cid-v2")], offset_days=1) |
| 202 | c3 = _commit([_replace(addr, "cid-v2", "cid-v3")], offset_days=2) |
| 203 | events = build_lineage(addr, [c1, c2, c3]) |
| 204 | assert len([e for e in events if e.kind == "modified"]) == 2 |
| 205 | |
| 206 | |
| 207 | # --------------------------------------------------------------------------- |
| 208 | # Renamed |
| 209 | # --------------------------------------------------------------------------- |
| 210 | |
| 211 | |
| 212 | class TestRenamed: |
| 213 | def test_insert_delete_same_file_is_renamed(self) -> None: |
| 214 | old_addr = "src/billing.py::_compute_total" |
| 215 | new_addr = "src/billing.py::compute_total" |
| 216 | c1 = _commit([_insert(old_addr, "cid-body")], offset_days=0) |
| 217 | c2 = _commit([_insert(new_addr, "cid-body"), _delete(old_addr, "cid-body")], offset_days=1) |
| 218 | events = build_lineage(new_addr, [c1, c2]) |
| 219 | assert any(e.kind == "renamed_from" for e in events) |
| 220 | |
| 221 | def test_renamed_from_detail_is_source_address(self) -> None: |
| 222 | old_addr = "src/billing.py::_inner" |
| 223 | new_addr = "src/billing.py::public_api" |
| 224 | c1 = _commit([_insert(old_addr, "cid-body")], offset_days=0) |
| 225 | c2 = _commit([_insert(new_addr, "cid-body"), _delete(old_addr, "cid-body")], offset_days=1) |
| 226 | events = build_lineage(new_addr, [c1, c2]) |
| 227 | renamed = [e for e in events if e.kind == "renamed_from"] |
| 228 | assert renamed[0].detail == old_addr |
| 229 | |
| 230 | |
| 231 | # --------------------------------------------------------------------------- |
| 232 | # Moved |
| 233 | # --------------------------------------------------------------------------- |
| 234 | |
| 235 | |
| 236 | class TestMoved: |
| 237 | def test_insert_delete_different_file_is_moved(self) -> None: |
| 238 | old_addr = "old/billing.py::compute_invoice_total" |
| 239 | new_addr = "src/billing.py::compute_invoice_total" |
| 240 | c1 = _commit([_insert(old_addr, "cid-body")], offset_days=0) |
| 241 | c2 = _commit([_insert(new_addr, "cid-body"), _delete(old_addr, "cid-body")], offset_days=1) |
| 242 | events = build_lineage(new_addr, [c1, c2]) |
| 243 | assert any(e.kind == "moved_from" for e in events) |
| 244 | |
| 245 | def test_moved_from_detail_is_original_address(self) -> None: |
| 246 | old_addr = "legacy/module.py::process" |
| 247 | new_addr = "src/processing.py::process" |
| 248 | c1 = _commit([_insert(old_addr, "cid-body")], offset_days=0) |
| 249 | c2 = _commit([_insert(new_addr, "cid-body"), _delete(old_addr, "cid-body")], offset_days=1) |
| 250 | events = build_lineage(new_addr, [c1, c2]) |
| 251 | moved = [e for e in events if e.kind == "moved_from"] |
| 252 | assert moved[0].detail == old_addr |
| 253 | |
| 254 | |
| 255 | # --------------------------------------------------------------------------- |
| 256 | # Copied |
| 257 | # --------------------------------------------------------------------------- |
| 258 | |
| 259 | |
| 260 | class TestCopied: |
| 261 | def test_insert_matching_live_symbol_is_copied(self) -> None: |
| 262 | original = "src/utils.py::helper" |
| 263 | copy = "src/other.py::helper" |
| 264 | shared = "cid-shared-body" |
| 265 | c1 = _commit([_insert(original, shared)], offset_days=0) |
| 266 | c2 = _commit([_insert(copy, shared)], offset_days=1) |
| 267 | events = build_lineage(copy, [c1, c2]) |
| 268 | assert any(e.kind == "copied_from" for e in events) |
| 269 | |
| 270 | def test_copied_from_detail_is_source_address(self) -> None: |
| 271 | original = "src/utils.py::helper" |
| 272 | copy = "src/other.py::helper" |
| 273 | shared = "cid-shared" |
| 274 | c1 = _commit([_insert(original, shared)], offset_days=0) |
| 275 | c2 = _commit([_insert(copy, shared)], offset_days=1) |
| 276 | events = build_lineage(copy, [c1, c2]) |
| 277 | copied = [e for e in events if e.kind == "copied_from"] |
| 278 | assert copied[0].detail == original |
| 279 | |
| 280 | def test_no_copy_if_source_is_dead(self) -> None: |
| 281 | """Source deleted before target inserted → 'created', not 'copied_from'.""" |
| 282 | original = "src/utils.py::helper" |
| 283 | copy = "src/other.py::helper" |
| 284 | shared = "cid-shared" |
| 285 | c1 = _commit([_insert(original, shared)], offset_days=0) |
| 286 | c2 = _commit([_delete(original, shared)], offset_days=1) |
| 287 | c3 = _commit([_insert(copy, shared)], offset_days=2) |
| 288 | events = build_lineage(copy, [c1, c2, c3]) |
| 289 | insert_events = [e for e in events if e.kind in ("created", "copied_from")] |
| 290 | assert insert_events[0].kind == "created" |
| 291 | |
| 292 | |
| 293 | # --------------------------------------------------------------------------- |
| 294 | # Complex multi-event sequences |
| 295 | # --------------------------------------------------------------------------- |
| 296 | |
| 297 | |
| 298 | class TestMultiEvent: |
| 299 | def test_create_modify_delete_sequence(self) -> None: |
| 300 | addr = "src/core.py::process" |
| 301 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 302 | c2 = _commit([_replace(addr, "cid-v1", "cid-v2")], offset_days=1) |
| 303 | c3 = _commit([_delete(addr, "cid-v2")], offset_days=2) |
| 304 | events = build_lineage(addr, [c1, c2, c3]) |
| 305 | assert [e.kind for e in events] == ["created", "modified", "deleted"] |
| 306 | |
| 307 | def test_delete_then_recreate(self) -> None: |
| 308 | addr = "src/api.py::endpoint" |
| 309 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 310 | c2 = _commit([_delete(addr, "cid-v1")], offset_days=1) |
| 311 | c3 = _commit([_insert(addr, "cid-v2")], offset_days=2) |
| 312 | events = build_lineage(addr, [c1, c2, c3]) |
| 313 | kinds = [e.kind for e in events] |
| 314 | assert kinds == ["created", "deleted", "created"] |
| 315 | |
| 316 | def test_ordered_by_commit_position_in_list(self) -> None: |
| 317 | """build_lineage processes commits in list order — caller is responsible for sorting.""" |
| 318 | addr = "src/main.py::main" |
| 319 | # Caller passes in chronological order (oldest-first) as documented. |
| 320 | c1 = _commit([_insert(addr, "cid-v1")], offset_days=0) |
| 321 | c2 = _commit([_replace(addr, "cid-v1", "cid-v2")], offset_days=2) |
| 322 | events = build_lineage(addr, [c1, c2]) |
| 323 | assert events[0].kind == "created" |
| 324 | assert events[1].kind == "modified" |
| 325 | |
| 326 | def test_many_commits_accumulate_all_events(self) -> None: |
| 327 | addr = "src/worker.py::run" |
| 328 | commits = [_commit([_insert(addr, "cid-0")], offset_days=0)] |
| 329 | prev = "cid-0" |
| 330 | for i in range(1, 10): |
| 331 | nxt = f"cid-{i}" |
| 332 | commits.append(_commit([_replace(addr, prev, nxt)], offset_days=i)) |
| 333 | prev = nxt |
| 334 | events = build_lineage(addr, commits) |
| 335 | assert len(events) == 10 |
| 336 | assert events[0].kind == "created" |
| 337 | assert all(e.kind == "modified" for e in events[1:]) |
| 338 | |
| 339 | def test_commit_message_propagated(self) -> None: |
| 340 | addr = "src/main.py::main" |
| 341 | c = _commit([_insert(addr, "cid-v1")], message="Initial commit") |
| 342 | events = build_lineage(addr, [c]) |
| 343 | assert events[0].message == "Initial commit" |
| 344 | |
| 345 | |
| 346 | # --------------------------------------------------------------------------- |
| 347 | # to_dict output shape |
| 348 | # --------------------------------------------------------------------------- |
| 349 | |
| 350 | |
| 351 | class TestJsonOutputShape: |
| 352 | def test_to_dict_has_expected_keys(self) -> None: |
| 353 | addr = "src/main.py::main" |
| 354 | c = _commit([_insert(addr, "cid-abc123")], commit_id="a" * 64) |
| 355 | events = build_lineage(addr, [c]) |
| 356 | d = events[0].to_dict() |
| 357 | assert "commit_id" in d |
| 358 | assert "committed_at" in d |
| 359 | assert "event" in d |
| 360 | assert "message" in d |
| 361 | assert d["event"] == "created" |
| 362 | |
| 363 | def test_to_dict_commit_id_is_full_sha(self) -> None: |
| 364 | """commit_id must be the full 64-character SHA — never truncated.""" |
| 365 | addr = "src/main.py::main" |
| 366 | full_cid = "f" * 64 |
| 367 | c = _commit([_insert(addr, "cid-abc")], commit_id=full_cid) |
| 368 | events = build_lineage(addr, [c]) |
| 369 | assert events[0].to_dict()["commit_id"] == full_cid |
| 370 | assert len(events[0].to_dict()["commit_id"]) == 64 |
| 371 | |
| 372 | |
| 373 | # --------------------------------------------------------------------------- |
| 374 | # Incremental registry — copy detection across many commits |
| 375 | # --------------------------------------------------------------------------- |
| 376 | |
| 377 | |
| 378 | class TestIncrementalRegistry: |
| 379 | def test_registry_tracks_all_live_symbols(self) -> None: |
| 380 | """Registry must track symbols in commits that don't touch the target.""" |
| 381 | shared_cid = "cid-shared" |
| 382 | c1 = _commit([_insert("src/a.py::foo", shared_cid)], offset_days=0) |
| 383 | c2 = _commit([_insert("src/b.py::foo", shared_cid)], offset_days=1) |
| 384 | events = build_lineage("src/b.py::foo", [c1, c2]) |
| 385 | assert events[0].kind == "copied_from" |
| 386 | assert events[0].detail == "src/a.py::foo" |
| 387 | |
| 388 | def test_registry_prunes_deleted_symbols(self) -> None: |
| 389 | """After deleting the original, its content_id leaves the live registry.""" |
| 390 | shared = "cid-shared" |
| 391 | original = "src/a.py::foo" |
| 392 | target = "src/b.py::foo" |
| 393 | c1 = _commit([_insert(original, shared)], offset_days=0) |
| 394 | c2 = _commit([_delete(original, shared)], offset_days=1) |
| 395 | c3 = _commit([_insert(target, shared)], offset_days=2) |
| 396 | events = build_lineage(target, [c1, c2, c3]) |
| 397 | assert events[0].kind == "created" |
| 398 | |
| 399 | def test_registry_survives_many_intermediate_commits(self) -> None: |
| 400 | """Original in commit 1; target copied in commit 12 — registry must persist.""" |
| 401 | shared = "cid-shared" |
| 402 | original = "src/lib.py::util" |
| 403 | target = "src/app.py::util" |
| 404 | commits = [_commit([_insert(original, shared)], offset_days=0)] |
| 405 | for i in range(1, 11): |
| 406 | commits.append(_commit([_insert(f"src/other_{i}.py::fn", f"cid-other-{i}")], offset_days=i)) |
| 407 | commits.append(_commit([_insert(target, shared)], offset_days=11)) |
| 408 | events = build_lineage(target, commits) |
| 409 | assert events[0].kind == "copied_from" |
File History
5 commits
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6
chore(tests): add docstring to tests/__init__.py so rc14 tr…
Human
26 days ago
sha256:d8316ffae901be06347e16ab55be11868eb519dd16ade3e8aa16a99e662f7e62
baseline: rc14 re-baseline after rc3 store corruption recovery
Human
patch
26 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e
Merge branch 'dev' into main
Human
29 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce
Merge branch 'dev' into main
Human
48 days ago
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa
feat: Muse — version control for the agent era
Human
100 days ago