"""Unit tests for musehub/services/musehub_symlog.py (muse#53 item 4). Tests the service layer directly (no HTTP): batched multi-symbol store/list round-trip, per-symbol isolation, dedup via content-hash entry_id, and default handling for missing/empty fields. """ from __future__ import annotations from datetime import datetime, timezone from sqlalchemy.ext.asyncio import AsyncSession from musehub.services import musehub_symlog as symlog_svc from tests.factories import create_repo def _entry_dict(old_cid: str, new_cid: str, commit_id: str, **overrides: str) -> dict: base = { "old_content_id": old_cid, "new_content_id": new_cid, "commit_id": commit_id, "author": "gabriel", "operation": "symbol-created: compute_total", "timestamp": datetime.now(timezone.utc).isoformat(), } base.update(overrides) return base async def test_store_and_list_round_trip(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") stored, skipped = await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::compute_total": [_entry_dict("0" * 64, "a" * 64, "c" * 64)]}, ) await db_session.commit() assert stored == 1 assert skipped == 0 symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) assert "billing.py::compute_total" in symbols entries = symbols["billing.py::compute_total"] assert len(entries) == 1 assert entries[0]["old_content_id"] == "0" * 64 assert entries[0]["new_content_id"] == "a" * 64 assert entries[0]["commit_id"] == "c" * 64 async def test_repush_same_entry_is_skipped(db_session: AsyncSession) -> None: """Re-pushing an identical entry (same content hash) is a true no-op -- it must not duplicate the row.""" repo = await create_repo(db_session, visibility="public") entry = _entry_dict("0" * 64, "a" * 64, "c" * 64, timestamp="2026-01-01T00:00:00+00:00") await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::compute_total": [entry]}, ) await db_session.commit() stored, skipped = await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::compute_total": [entry]}, ) await db_session.commit() assert stored == 0 assert skipped == 1 symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) assert len(symbols["billing.py::compute_total"]) == 1 async def test_batched_multi_symbol_push(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") stored, skipped = await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), { "billing.py::compute_total": [_entry_dict("0" * 64, "a" * 64, "c" * 64)], "billing.py::apply_discount": [_entry_dict("0" * 64, "b" * 64, "c" * 64)], }, ) await db_session.commit() assert stored == 2 assert skipped == 0 symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) assert set(symbols.keys()) == {"billing.py::compute_total", "billing.py::apply_discount"} async def test_entries_isolated_per_symbol(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::a": [_entry_dict("0" * 64, "a" * 64, "c" * 64)]}, ) await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::b": [_entry_dict("0" * 64, "b" * 64, "c" * 64)]}, ) await db_session.commit() symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) assert len(symbols["billing.py::a"]) == 1 assert len(symbols["billing.py::b"]) == 1 assert symbols["billing.py::a"][0]["new_content_id"] == "a" * 64 assert symbols["billing.py::b"][0]["new_content_id"] == "b" * 64 async def test_list_ordered_oldest_first_within_symbol(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::compute_total": [ _entry_dict("0" * 64, "a" * 64, "c1" + "0" * 62, timestamp="2026-01-01T00:00:00+00:00"), _entry_dict("a" * 64, "b" * 64, "c2" + "0" * 62, timestamp="2026-01-02T00:00:00+00:00"), ]}, ) await db_session.commit() symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) entries = symbols["billing.py::compute_total"] assert len(entries) == 2 assert entries[0]["new_content_id"] == "a" * 64 assert entries[1]["new_content_id"] == "b" * 64 async def test_empty_batch_is_noop(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") stored, skipped = await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {}, ) assert (stored, skipped) == (0, 0) async def test_entry_missing_commit_id_is_skipped_silently(db_session: AsyncSession) -> None: repo = await create_repo(db_session, visibility="public") stored, skipped = await symlog_svc.store_symlog_entries( db_session, str(repo.repo_id), {"billing.py::compute_total": [_entry_dict("0" * 64, "a" * 64, "")]}, ) await db_session.commit() assert stored == 0 assert skipped == 0 symbols = await symlog_svc.list_symlog_entries(db_session, str(repo.repo_id)) assert symbols == {}