"""TDD — ui_repo commit enrichment must not read commit_meta.""" from __future__ import annotations import typing from collections.abc import Mapping from datetime import datetime, timezone import pytest from httpx import AsyncClient from muse.core.types import blob_id from sqlalchemy.ext.asyncio import AsyncSession from musehub.db import musehub_repo_models as db from tests.factories import create_repo def _utc() -> datetime: return datetime.now(tz=timezone.utc) async def _add_commit(session: AsyncSession, repo_id: str, seed: str, **kwargs: typing.Any) -> db.MusehubCommit: row = db.MusehubCommit( commit_id=blob_id(seed.encode()), branch="dev", parent_ids=[], message=f"feat: {seed}", author="gabriel", timestamp=_utc(), **kwargs, ) session.add(row) session.add(db.MusehubCommitRef(repo_id=repo_id, commit_id=row.commit_id)) await session.commit() return row # --------------------------------------------------------------------------- # R1 — ORM row has first-class columns, not commit_meta # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_r1_musehub_commit_has_no_commit_meta_attribute( db_session: AsyncSession, ) -> None: """MusehubCommit ORM row must expose agent_id/sem_ver_bump/etc. and not commit_meta.""" repo = await create_repo(db_session, owner="gabriel", visibility="public") row = await _add_commit( db_session, repo.repo_id, "repo-col-check", agent_id="claude-code", model_id="claude-sonnet-4-6", sem_ver_bump="minor", breaking_changes=[], toolchain_id="muse-1", test_runs=2, reviewed_by=[], ) await db_session.refresh(row) assert not hasattr(row, "commit_meta"), "commit_meta must not exist on ORM row" assert row.agent_id == "claude-code" assert row.model_id == "claude-sonnet-4-6" assert row.sem_ver_bump == "minor" assert row.test_runs == 2 assert row.toolchain_id == "muse-1" # --------------------------------------------------------------------------- # R2 — route GET /owner/slug does not crash with AttributeError # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_r2_repo_route_no_commit_meta_error( db_session: AsyncSession, client: AsyncClient, auth_headers: Mapping[str, str], ) -> None: """Repo page must not raise AttributeError on commit_meta when enriching commits.""" repo = await create_repo(db_session, owner="gabriel", visibility="public") commit = await _add_commit( db_session, repo.repo_id, "repo-route-c1", agent_id="claude-code", model_id="claude-sonnet-4-6", sem_ver_bump="patch", ) db_session.add(db.MusehubBranch( branch_id=blob_id(b"repo-route-branch"), repo_id=repo.repo_id, name="dev", head_commit_id=commit.commit_id, )) await db_session.commit() resp = await client.get( f"/gabriel/{repo.slug}", headers=auth_headers, follow_redirects=True, ) assert resp.status_code == 200