Add comprehensive tests for player API endpoints and player service functionality

- Implemented tests for player API endpoints including play, pause, stop, next, previous, seek, set volume, set mode, reload playlist, and get state.
- Added mock player service for testing API interactions.
- Created tests for player service methods including play, pause, stop playback, next, previous, seek, set volume, set mode, and reload playlist.
- Ensured proper handling of success, error, and edge cases in both API and service tests.
- Verified state management and serialization in player state tests.
This commit is contained in:
JSC
2025-07-30 01:22:24 +02:00
parent 5ed19c8f0f
commit 1b0d291ad3
11 changed files with 2291 additions and 98 deletions

View File

@@ -158,7 +158,10 @@ class ExtractionService:
service_info["service"], service_info["service_id"]
)
if existing and existing.id != extraction_id:
error_msg = f"Extraction already exists for {service_info['service']}:{service_info['service_id']}"
error_msg = (
f"Extraction already exists for "
f"{service_info['service']}:{service_info['service_id']}"
)
logger.warning(error_msg)
raise ValueError(error_msg)
@@ -204,10 +207,10 @@ class ExtractionService:
sound_id = sound.id
# Normalize the sound
await self._normalize_sound(sound)
await self._normalize_sound(sound_id)
# Add to main playlist
await self._add_to_main_playlist(sound, user_id)
await self._add_to_main_playlist(sound_id, user_id)
# Update extraction with success
await self.extraction_repo.update(
@@ -427,39 +430,45 @@ class ExtractionService:
return sound
async def _normalize_sound(self, sound: Sound) -> None:
async def _normalize_sound(self, sound_id: int) -> None:
"""Normalize the extracted sound."""
try:
# Get fresh sound object from database for normalization
sound = await self.sound_repo.get_by_id(sound_id)
if not sound:
logger.warning("Sound %d not found for normalization", sound_id)
return
normalizer_service = SoundNormalizerService(self.session)
result = await normalizer_service.normalize_sound(sound)
if result["status"] == "error":
logger.warning(
"Failed to normalize sound %d: %s",
sound.id,
sound_id,
result.get("error"),
)
else:
logger.info("Successfully normalized sound %d", sound.id)
logger.info("Successfully normalized sound %d", sound_id)
except Exception as e:
logger.exception("Error normalizing sound %d: %s", sound.id, e)
logger.exception("Error normalizing sound %d: %s", sound_id, e)
# Don't fail the extraction if normalization fails
async def _add_to_main_playlist(self, sound: Sound, user_id: int) -> None:
async def _add_to_main_playlist(self, sound_id: int, user_id: int) -> None:
"""Add the sound to the user's main playlist."""
try:
await self.playlist_service.add_sound_to_main_playlist(sound.id, user_id)
await self.playlist_service.add_sound_to_main_playlist(sound_id, user_id)
logger.info(
"Added sound %d to main playlist for user %d",
sound.id,
sound_id,
user_id,
)
except Exception:
logger.exception(
"Error adding sound %d to main playlist for user %d",
sound.id,
sound_id,
user_id,
)
# Don't fail the extraction if playlist addition fails