From e69098d6337f5bcf1a848a9e4aa9e98805030332 Mon Sep 17 00:00:00 2001 From: JSC Date: Thu, 31 Jul 2025 21:33:00 +0200 Subject: [PATCH] refactor: Update player seek functionality to use consistent position field across schemas and services --- app/api/v1/player.py | 6 +++--- app/schemas/player.py | 6 +++--- app/services/player.py | 4 ++-- tests/api/v1/test_player_endpoints.py | 20 ++++++++++---------- tests/services/test_player.py | 15 +++++++-------- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/api/v1/player.py b/app/api/v1/player.py index a498d9b..bde3f3c 100644 --- a/app/api/v1/player.py +++ b/app/api/v1/player.py @@ -137,10 +137,10 @@ async def seek( """Seek to specific position in current track.""" try: player = get_player_service() - await player.seek(request.position_ms) - return MessageResponse(message=f"Seeked to position {request.position_ms}ms") + await player.seek(request.position) + return MessageResponse(message=f"Seeked to position {request.position}ms") except Exception as e: - logger.exception("Error seeking to position %s", request.position_ms) + logger.exception("Error seeking to position %s", request.position) raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to seek", diff --git a/app/schemas/player.py b/app/schemas/player.py index 2c2f868..6e25924 100644 --- a/app/schemas/player.py +++ b/app/schemas/player.py @@ -10,7 +10,7 @@ from app.services.player import PlayerMode class PlayerSeekRequest(BaseModel): """Request model for seek operation.""" - position_ms: int = Field(ge=0, description="Position in milliseconds") + position: int = Field(ge=0, description="Position in milliseconds") class PlayerVolumeRequest(BaseModel): @@ -35,8 +35,8 @@ class PlayerStateResponse(BaseModel): playlist: dict[str, Any] | None = Field( None, description="Current playlist information" ) - position_ms: int = Field(description="Current position in milliseconds") - duration_ms: int | None = Field( + position: int = Field(description="Current position in milliseconds") + duration: int | None = Field( None, description="Total duration in milliseconds", ) volume: int = Field(description="Current volume (0-100)") diff --git a/app/services/player.py b/app/services/player.py index 433e20f..27383f7 100644 --- a/app/services/player.py +++ b/app/services/player.py @@ -64,8 +64,8 @@ class PlayerState: "status": self.status.value, "mode": self.mode.value, "volume": self.volume, - "position_ms": self.current_sound_position or 0, - "duration_ms": self.current_sound_duration, + "position": self.current_sound_position or 0, + "duration": self.current_sound_duration, "index": self.current_sound_index, "current_sound": self._serialize_sound(self.current_sound), "playlist": { diff --git a/tests/api/v1/test_player_endpoints.py b/tests/api/v1/test_player_endpoints.py index 457f3dc..5fa0b08 100644 --- a/tests/api/v1/test_player_endpoints.py +++ b/tests/api/v1/test_player_endpoints.py @@ -278,17 +278,17 @@ class TestPlayerEndpoints: mock_player_service, ): """Test seeking to position successfully.""" - position_ms = 5000 + position = 5000 response = await authenticated_client.post( "/api/v1/player/seek", - json={"position_ms": position_ms}, + json={"position": position}, ) assert response.status_code == 200 data = response.json() - assert data["message"] == f"Seeked to position {position_ms}ms" + assert data["message"] == f"Seeked to position {position}ms" - mock_player_service.seek.assert_called_once_with(position_ms) + mock_player_service.seek.assert_called_once_with(position) @pytest.mark.asyncio async def test_seek_invalid_position( @@ -300,7 +300,7 @@ class TestPlayerEndpoints: """Test seeking with invalid position.""" response = await authenticated_client.post( "/api/v1/player/seek", - json={"position_ms": -1000}, # Negative position + json={"position": -1000}, # Negative position ) assert response.status_code == 422 # Validation error @@ -310,7 +310,7 @@ class TestPlayerEndpoints: """Test seeking without authentication.""" response = await client.post( "/api/v1/player/seek", - json={"position_ms": 5000}, + json={"position": 5000}, ) assert response.status_code == 401 @@ -326,7 +326,7 @@ class TestPlayerEndpoints: response = await authenticated_client.post( "/api/v1/player/seek", - json={"position_ms": 5000}, + json={"position": 5000}, ) assert response.status_code == 500 @@ -516,8 +516,8 @@ class TestPlayerEndpoints: "status": PlayerStatus.PLAYING.value, "mode": PlayerMode.CONTINUOUS.value, "volume": 50, - "position_ms": 5000, - "duration_ms": 30000, + "position": 5000, + "duration": 30000, "index": 0, "current_sound": { "id": 1, @@ -625,7 +625,7 @@ class TestPlayerEndpoints: """Test seeking to position zero.""" response = await authenticated_client.post( "/api/v1/player/seek", - json={"position_ms": 0}, + json={"position": 0}, ) assert response.status_code == 200 diff --git a/tests/services/test_player.py b/tests/services/test_player.py index 84979f4..7c159b8 100644 --- a/tests/services/test_player.py +++ b/tests/services/test_player.py @@ -65,14 +65,13 @@ class TestPlayerState: assert result["status"] == "playing" assert result["mode"] == "loop" assert result["volume"] == 75 - assert result["current_sound_id"] == 1 - assert result["current_sound_index"] == 0 - assert result["current_sound_position"] == 5000 - assert result["current_sound_duration"] == 30000 - assert result["playlist_id"] == 1 - assert result["playlist_name"] == "Test Playlist" - assert result["playlist_length"] == 5 - assert result["playlist_duration"] == 150000 + assert result["position"] == 5000 + assert result["duration"] == 30000 + assert result["index"] == 0 + assert result["playlist"]["id"] == 1 + assert result["playlist"]["name"] == "Test Playlist" + assert result["playlist"]["length"] == 5 + assert result["playlist"]["duration"] == 150000 def test_serialize_sound_with_sound_object(self): """Test serializing a sound object."""