From 9c5c4f37d181af1fead3a32031e008f928f89d57 Mon Sep 17 00:00:00 2001 From: Sergey Borovkov Date: Thu, 14 Aug 2025 10:42:40 +0400 Subject: [PATCH] Fix append not working with empty playlist --- src/commands/playlist.rs | 53 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/commands/playlist.rs b/src/commands/playlist.rs index 02e2f00..c1f6937 100644 --- a/src/commands/playlist.rs +++ b/src/commands/playlist.rs @@ -118,10 +118,14 @@ impl PlaylistCommand { )?; let playlist_items = serde_json::from_value::>(response)?; - if playlist_items.len() != 1 { + let position = if playlist_items.is_empty() { + POSITION_MULTIPLIER + } else if playlist_items.len() == 1 { + playlist_items[0].position + POSITION_MULTIPLIER + } else { return Err(CommandError::MissingField); - } - let position = playlist_items[0].position + POSITION_MULTIPLIER; + }; + let payload = json!([{ "playlist_id": playlist_uuid, "asset_id": asset_uuid, @@ -439,6 +443,49 @@ mod tests { assert!(result.is_ok()); } + #[test] + fn test_append_asset_to_empty_playlist_should_send_correct_request() { + let items_request = json!([ + { + "position": 100000, + "duration": 50, + "asset_id": "test-asset-id", + "playlist_id": "test-playlist-id" + }, + ]); + + let mock_server = MockServer::start(); + // request to get the highest position from empty playlist + let get_mock = mock_server.mock(|when, then| { + when.method(GET) + .path("/v4/playlist-items") + .query_param("playlist_id", "eq.test-playlist-id") + .query_param("select", "position,asset_id,duration") + .query_param("order", "position.desc") + .query_param("limit", "1") + .header("Authorization", "Token token"); + then.status(200).json_body(json!([])); // empty array for empty playlist + }); + + // then post request to create new playlist items + let post_mock = mock_server.mock(|when, then| { + when.method(POST) + .path("/v4/playlist-items") + .header("Authorization", "Token token") + .json_body(items_request); + then.status(201).json_body(json!({})); + }); + + let config = Config::new(mock_server.base_url()); + let authentication = Authentication::new_with_config(config, "token"); + let command = PlaylistCommand::new(authentication); + let result = command.append_asset("test-playlist-id", "test-asset-id", 50); + + post_mock.assert(); + get_mock.assert(); + assert!(result.is_ok()); + } + #[test] fn test_prepend_asset_to_playlist_should_send_correct_request() { let _updated_playlist = json!({