gabriel / musehub public
test_dag_symbol_counts.py python
68 lines 3.2 KB
Raw
sha256:69161439990eeb42a0502d2417d8492b076c88e5ca0fb842dbb1bfb1ef8fca7c fix(objects): P0 object availability — serve mpack-stored o… Opus 5.5 minor ⚠ breaking 2 days ago
1 """The DAG's per-commit symbol insert/delete counts are computed in the database (jsonpath) instead of shipping every commit's structured_delta to Python.
2 They must equal what the Python loop counted, including on malformed deltas."""
3 from __future__ import annotations
4
5 import typing
6 from datetime import datetime, timezone
7
8 import pytest
9 from sqlalchemy.ext.asyncio import AsyncSession
10
11 from musehub.db.musehub_repo_models import MusehubBranch, MusehubCommit, MusehubCommitRef
12 from muse.core.types import blob_id
13 from musehub.services.musehub_repository import list_commits_dag
14 from tests.factories import create_repo
15
16
17 def _python_counts(delta: object) -> tuple[int, int]:
18 """The loop this replaced, verbatim in behaviour."""
19 added = removed = 0
20 d = delta if isinstance(delta, dict) else {}
21 for file_op in (d.get("ops") or []):
22 for child_op in (file_op.get("child_ops") or []) if isinstance(file_op, dict) else []:
23 if not isinstance(child_op, dict):
24 continue
25 if child_op.get("op") == "insert":
26 added += 1
27 elif child_op.get("op") == "delete":
28 removed += 1
29 return added, removed
30
31
32 DELTAS: list[typing.Any] = [
33 None,
34 {},
35 {"ops": []},
36 {"ops": [{"op": "patch", "child_ops": [{"op": "insert"}, {"op": "insert"}, {"op": "delete"}, {"op": "replace"}]}]},
37 {"ops": [{"op": "patch", "child_ops": [{"op": "insert"}]}, {"op": "insert"}, {"op": "patch", "child_ops": [{"op": "delete"}, {"op": "delete"}]}]},
38 {"ops": [{"op": "patch", "child_ops": ["junk", 3, {"op": "insert"}]}, "not-a-dict"]},
39 {"ops": [{"op": "patch", "child_ops": []}], "summary": "x"},
40 # Shapes lax jsonpath would count but the Python loop skipped (review of ab1924fc):
41 {"ops": {"op": "patch", "child_ops": [{"op": "insert"}]}},
42 {"ops": [{"op": "patch", "child_ops": {"op": "insert"}}]},
43 {"ops": [{"op": "patch", "child_ops": [[{"op": "insert"}], {"op": "delete"}]}]},
44 [{"ops": [{"child_ops": [{"op": "insert"}]}]}],
45 {"ops": [{"op": "patch", "child_ops": [{"op": ["insert"]}, {"op": "insert"}]}]},
46 {"ops": [{"op": "insert"}, {"op": "patch", "child_ops": [{"op": "insert"}, {"kind": "no-op-key"}]}]},
47 ]
48
49
50 @pytest.mark.asyncio
51 async def test_dag_symbol_counts_match_the_python_semantics(db_session: AsyncSession) -> None:
52 repo = await create_repo(db_session, owner="gabriel", visibility="public")
53 expected: dict[str, tuple[int, int]] = {}
54 for i, delta in enumerate(DELTAS):
55 cid = blob_id(f"dag-sym-{i}".encode())
56 db_session.add(MusehubCommit(
57 commit_id=cid, branch="dev", parent_ids=[], message=f"feat: {i}", author="gabriel",
58 timestamp=datetime(2026, 11, 1, 12, i, tzinfo=timezone.utc), structured_delta=delta,
59 ))
60 db_session.add(MusehubCommitRef(repo_id=repo.repo_id, commit_id=cid))
61 expected[cid] = _python_counts(delta)
62 db_session.add(MusehubBranch(branch_id=blob_id(b"dag-sym-br"), repo_id=repo.repo_id, name="dev", head_commit_id=cid))
63 await db_session.commit()
64
65 dag = await list_commits_dag(db_session, repo.repo_id)
66 got = {n.commit_id: (n.sym_added, n.sym_removed) for n in dag.nodes}
67 assert got == expected
68 assert expected[blob_id(b"dag-sym-4")] == (1, 2) # the fixture really exercises nesting
File History 2 commits
sha256:69161439990eeb42a0502d2417d8492b076c88e5ca0fb842dbb1bfb1ef8fca7c fix(objects): P0 object availability — serve mpack-stored o… Opus 5.5 minor ⚠ 2 days ago
sha256:30c51e90dfe2486f79752f7305be1faf2f086d5407ee5e1307167b5a8c61549d perf(db): SQL audit pass 2 + Tier A review fixes; local tim… Opus 5.5 minor ⚠ 2 days ago