"""TDD — init commit ID must be sha256:... not init-sha256:... (issue #60). IC-1 create_repo(initialize=True) produces a commit_id starting with 'sha256:'. IC-2 The init commit_id exists as a real row in musehub_commits. IC-3 wire_refs for a freshly initialized repo returns a branch head starting with 'sha256:'. IC-4 ensure_hub_seed must detect a never-pushed seed as stale (structural check). """ from __future__ import annotations import pytest from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from musehub.db import musehub_repo_models as db from musehub.core.genesis import compute_identity_id def _owner_user_id(handle: str) -> str: return compute_identity_id(handle.encode()) # ── IC-1 ────────────────────────────────────────────────────────────────────── @pytest.mark.asyncio async def test_ic1_init_commit_id_is_sha256(db_session: AsyncSession) -> None: """create_repo(initialize=True) must produce a sha256:... commit_id, not init-sha256:...""" from musehub.services.musehub_repository import create_repo repo = await create_repo( db_session, name="ic-test-1", owner="gabriel", owner_user_id=_owner_user_id("gabriel"), visibility="public", initialize=True, ) branch_q = await db_session.execute( select(db.MusehubBranch).where(db.MusehubBranch.repo_id == repo.repo_id) ) branch = branch_q.scalar_one_or_none() assert branch is not None, "branch not created" cid = branch.head_commit_id assert cid is not None, "head_commit_id is None" assert cid.startswith("sha256:"), f"got {cid!r} — must be sha256:..." assert not cid.startswith("init-"), f"got {cid!r} — init- prefix is not an algorithm" # ── IC-2 ────────────────────────────────────────────────────────────────────── @pytest.mark.asyncio async def test_ic2_init_commit_exists_in_musehub_commits(db_session: AsyncSession) -> None: """The init commit_id must exist as a real row in musehub_commits.""" from musehub.services.musehub_repository import create_repo repo = await create_repo( db_session, name="ic-test-2", owner="gabriel", owner_user_id=_owner_user_id("gabriel"), visibility="public", initialize=True, ) branch_q = await db_session.execute( select(db.MusehubBranch).where(db.MusehubBranch.repo_id == repo.repo_id) ) branch = branch_q.scalar_one() row = await db_session.get(db.MusehubCommit, branch.head_commit_id) assert row is not None, ( f"init commit {branch.head_commit_id!r} not found in musehub_commits" ) # ── IC-3 ────────────────────────────────────────────────────────────────────── @pytest.mark.asyncio async def test_ic3_wire_refs_head_is_valid_sha256(db_session: AsyncSession) -> None: """wire_refs for a freshly initialized repo returns a sha256:... branch head.""" from musehub.services.musehub_repository import create_repo from musehub.services.musehub_wire import wire_refs repo = await create_repo( db_session, name="ic-test-3", owner="gabriel", owner_user_id=_owner_user_id("gabriel"), visibility="public", initialize=True, ) result = await wire_refs(db_session, repo.repo_id) assert result is not None for branch_name, head in result.branch_heads.items(): assert head.startswith("sha256:"), ( f"branch {branch_name!r} head {head!r} is not a valid sha256: commit ID" ) assert not head.startswith("init-"), ( f"branch {branch_name!r} head {head!r} still uses the invalid init- prefix" ) # ── IC-4 ────────────────────────────────────────────────────────────────────── def test_ic4_ensure_hub_seed_checks_branch_head() -> None: """ensure_hub_seed must reject a seed whose branch head is the init placeholder. A repo created via wizard but never pushed to has head = sha256:. The bench must detect this and force a re-push, not silently reuse the empty repo. """ import inspect import sys sys.path.insert(0, "/Users/gabriel/ecosystem/musehub/tests") import bench_cli src = inspect.getsource(bench_cli.ensure_hub_seed) assert "branch_heads" in src or "head_commit" in src, ( "ensure_hub_seed must verify branch heads are real pushed commits — " "not just check repo existence and wire_hash" )