gabriel / musehub public

test_init_commit_id.py file-level

at sha256:7 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 πŸ’₯ blast risk
sha256:0 fix: fall back to any indexed mpack in read_object_bytes when push mpac… · gabriel · Jun 17, 2026
1 """TDD β€” init commit ID must be sha256:... not init-sha256:... (issue #60).
2
3 IC-1 create_repo(initialize=True) produces a commit_id starting with 'sha256:'.
4 IC-2 The init commit_id exists as a real row in musehub_commits.
5 IC-3 wire_refs for a freshly initialized repo returns a branch head starting with 'sha256:'.
6 IC-4 ensure_hub_seed must detect a never-pushed seed as stale (structural check).
7 """
8 from __future__ import annotations
9
10 import pytest
11 from sqlalchemy.ext.asyncio import AsyncSession
12 from sqlalchemy import select
13 from musehub.db import musehub_repo_models as db
14 from musehub.core.genesis import compute_identity_id
15
16
17 def _owner_user_id(handle: str) -> str:
18 return compute_identity_id(handle.encode())
19
20
21 # ── IC-1 ──────────────────────────────────────────────────────────────────────
22
23 @pytest.mark.asyncio
24 async def test_ic1_init_commit_id_is_sha256(db_session: AsyncSession) -> None:
25 """create_repo(initialize=True) must produce a sha256:... commit_id, not init-sha256:..."""
26 from musehub.services.musehub_repository import create_repo
27
28 repo = await create_repo(
29 db_session,
30 name="ic-test-1",
31 owner="gabriel",
32 owner_user_id=_owner_user_id("gabriel"),
33 visibility="public",
34 initialize=True,
35 )
36
37 branch_q = await db_session.execute(
38 select(db.MusehubBranch).where(db.MusehubBranch.repo_id == repo.repo_id)
39 )
40 branch = branch_q.scalar_one_or_none()
41 assert branch is not None, "branch not created"
42 cid = branch.head_commit_id
43 assert cid is not None, "head_commit_id is None"
44 assert cid.startswith("sha256:"), f"got {cid!r} β€” must be sha256:..."
45 assert not cid.startswith("init-"), f"got {cid!r} β€” init- prefix is not an algorithm"
46
47
48 # ── IC-2 ──────────────────────────────────────────────────────────────────────
49
50 @pytest.mark.asyncio
51 async def test_ic2_init_commit_exists_in_musehub_commits(db_session: AsyncSession) -> None:
52 """The init commit_id must exist as a real row in musehub_commits."""
53 from musehub.services.musehub_repository import create_repo
54
55 repo = await create_repo(
56 db_session,
57 name="ic-test-2",
58 owner="gabriel",
59 owner_user_id=_owner_user_id("gabriel"),
60 visibility="public",
61 initialize=True,
62 )
63
64 branch_q = await db_session.execute(
65 select(db.MusehubBranch).where(db.MusehubBranch.repo_id == repo.repo_id)
66 )
67 branch = branch_q.scalar_one()
68 row = await db_session.get(db.MusehubCommit, branch.head_commit_id)
69 assert row is not None, (
70 f"init commit {branch.head_commit_id!r} not found in musehub_commits"
71 )
72
73
74 # ── IC-3 ──────────────────────────────────────────────────────────────────────
75
76 @pytest.mark.asyncio
77 async def test_ic3_wire_refs_head_is_valid_sha256(db_session: AsyncSession) -> None:
78 """wire_refs for a freshly initialized repo returns a sha256:... branch head."""
79 from musehub.services.musehub_repository import create_repo
80 from musehub.services.musehub_wire import wire_refs
81
82 repo = await create_repo(
83 db_session,
84 name="ic-test-3",
85 owner="gabriel",
86 owner_user_id=_owner_user_id("gabriel"),
87 visibility="public",
88 initialize=True,
89 )
90
91 result = await wire_refs(db_session, repo.repo_id)
92 assert result is not None
93 for branch_name, head in result.branch_heads.items():
94 assert head.startswith("sha256:"), (
95 f"branch {branch_name!r} head {head!r} is not a valid sha256: commit ID"
96 )
97 assert not head.startswith("init-"), (
98 f"branch {branch_name!r} head {head!r} still uses the invalid init- prefix"
99 )
100
101
102 # ── IC-4 ──────────────────────────────────────────────────────────────────────
103
104 def test_ic4_ensure_hub_seed_checks_branch_head() -> None:
105 """ensure_hub_seed must reject a seed whose branch head is the init placeholder.
106
107 A repo created via wizard but never pushed to has head = sha256:<init-hash>.
108 The bench must detect this and force a re-push, not silently reuse the empty repo.
109 """
110 import inspect
111 import sys
112 sys.path.insert(0, "/Users/gabriel/ecosystem/musehub/tests")
113 import bench_cli
114
115 src = inspect.getsource(bench_cli.ensure_hub_seed)
116 assert "branch_heads" in src or "head_commit" in src, (
117 "ensure_hub_seed must verify branch heads are real pushed commits β€” "
118 "not just check repo existence and wire_hash"
119 )