"""Unit tests for service layer — band and annotation services.""" import uuid from unittest.mock import AsyncMock, MagicMock, patch import pytest from rehearsalhub.services.annotation import AnnotationService from rehearsalhub.services.band import BandService @pytest.mark.asyncio async def test_create_band_raises_on_duplicate_slug(mock_session): from rehearsalhub.db.models import Band from rehearsalhub.schemas.band import BandCreate existing_band = MagicMock(spec=Band) existing_band.slug = "taken" with patch( "rehearsalhub.repositories.band.BandRepository.get_by_slug", new_callable=AsyncMock, return_value=existing_band, ): svc = BandService(mock_session) with pytest.raises(ValueError, match="Slug already taken"): await svc.create_band( BandCreate(name="Test", slug="taken"), creator_id=uuid.uuid4(), ) @pytest.mark.asyncio async def test_assert_membership_raises_when_not_member(mock_session): band_id = uuid.uuid4() member_id = uuid.uuid4() with patch( "rehearsalhub.repositories.band.BandRepository.get_member_role", new_callable=AsyncMock, return_value=None, ): svc = BandService(mock_session) with pytest.raises(PermissionError, match="Not a member"): await svc.assert_membership(band_id, member_id) @pytest.mark.asyncio async def test_assert_admin_raises_when_member_role(mock_session): band_id = uuid.uuid4() member_id = uuid.uuid4() with patch( "rehearsalhub.repositories.band.BandRepository.get_member_role", new_callable=AsyncMock, return_value="member", ): svc = BandService(mock_session) with pytest.raises(PermissionError, match="Admin role required"): await svc.assert_admin(band_id, member_id) @pytest.mark.asyncio async def test_create_range_annotation_enqueues_job(mock_session): from rehearsalhub.db.models import Annotation from rehearsalhub.schemas.annotation import AnnotationCreate annotation = MagicMock(spec=Annotation) annotation.id = uuid.uuid4() mock_queue = AsyncMock() mock_queue.enqueue = AsyncMock(return_value=uuid.uuid4()) with patch( "rehearsalhub.repositories.annotation.AnnotationRepository.create", new_callable=AsyncMock, return_value=annotation, ): svc = AnnotationService(mock_session, job_queue=mock_queue) await svc.create_annotation( version_id=uuid.uuid4(), author_id=uuid.uuid4(), data=AnnotationCreate( type="range", timestamp_ms=1000, range_end_ms=5000, tags=["hook"], ), ) mock_queue.enqueue.assert_called_once_with( "analyse_range", { "annotation_id": str(annotation.id), "version_id": unittest_any_str(), "start_ms": 1000, "end_ms": 5000, }, ) @pytest.mark.asyncio async def test_create_point_annotation_does_not_enqueue(mock_session): from rehearsalhub.db.models import Annotation from rehearsalhub.schemas.annotation import AnnotationCreate annotation = MagicMock(spec=Annotation) annotation.id = uuid.uuid4() mock_queue = AsyncMock() mock_queue.enqueue = AsyncMock() with patch( "rehearsalhub.repositories.annotation.AnnotationRepository.create", new_callable=AsyncMock, return_value=annotation, ): svc = AnnotationService(mock_session, job_queue=mock_queue) await svc.create_annotation( version_id=uuid.uuid4(), author_id=uuid.uuid4(), data=AnnotationCreate(type="point", timestamp_ms=2000), ) mock_queue.enqueue.assert_not_called() @pytest.mark.asyncio async def test_delete_annotation_by_non_author_raises(mock_session): from rehearsalhub.db.models import Annotation author_id = uuid.uuid4() other_id = uuid.uuid4() annotation = MagicMock(spec=Annotation) annotation.id = uuid.uuid4() annotation.author_id = author_id svc = AnnotationService(mock_session) with pytest.raises(PermissionError, match="Only the author"): await svc.delete_annotation(annotation, other_id) def unittest_any_str(): """Helper that matches any string in assert_called_with.""" class AnyStr: def __eq__(self, other): return isinstance(other, str) return AnyStr()