From 2752d3325c68862cfdd584384557c9d9282e8143 Mon Sep 17 00:00:00 2001 From: Fabian Sylvester Date: Sat, 12 Oct 2024 21:11:09 +0000 Subject: [PATCH] tests: add rest test for play (#39) --- pkg/rest/play_test.go | 41 ++++++++++++++ ...yer1_media_player_play_media_response.json | 56 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 pkg/rest/play_test.go create mode 100644 pkg/rest/testdata/player1_media_player_play_media_response.json diff --git a/pkg/rest/play_test.go b/pkg/rest/play_test.go new file mode 100644 index 0000000..0b1eedb --- /dev/null +++ b/pkg/rest/play_test.go @@ -0,0 +1,41 @@ +// Copyright 2024 Fabian `xx4h` Sylvester +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package rest + +import ( + "testing" +) + +func Test_PlayMusic(t *testing.T) { + ms := mockServer(t) + h := &Hass{ + APIURL: ms.URL, + Token: "test_token", + } + defer ms.Close() + obj, action, sub, err := h.PlayMusic("player1", "testdata/fake.mp3", "fake.mp3") + if err != nil { + t.Errorf("Error playing: %v", err) + } + if obj != "player1" { + t.Errorf("got %s, want player1", obj) + } + if action != "playing fake.mp3" { + t.Errorf("got %s, want 'playing fake.mp3'", action) + } + if sub != "media_player" { + t.Errorf("got %s, want media_player", sub) + } +} diff --git a/pkg/rest/testdata/player1_media_player_play_media_response.json b/pkg/rest/testdata/player1_media_player_play_media_response.json new file mode 100644 index 0000000..79dea56 --- /dev/null +++ b/pkg/rest/testdata/player1_media_player_play_media_response.json @@ -0,0 +1,56 @@ +[ + { + "entity_id": "media_player.player1", + "state": "idle", + "attributes": { + "volume_level": 0.30000001192092896, + "is_volume_muted": false, + "media_content_id": "http://10.10.10.10:1337/fake.mp3", + "media_content_type": "music", + "media_duration": 3.160816, + "media_position": 0, + "media_position_updated_at": "2024-10-12T20:57:22.888305+00:00", + "app_id": "ABCDE123", + "app_name": "Default Media Receiver", + "entity_picture_local": null, + "device_class": "speaker", + "friendly_name": "Player 1", + "supported_features": 152463 + }, + "last_changed": "2024-10-12T20:56:54.692016+00:00", + "last_reported": "2024-10-12T20:57:22.888625+00:00", + "last_updated": "2024-10-12T20:57:22.888625+00:00", + "context": { + "id": "ABDCDEFGHIJKLMNOPQRSTUVW01", + "parent_id": null, + "user_id": "someuseridhash" + } + }, + { + "entity_id": "media_player.player1", + "state": "playing", + "attributes": { + "volume_level": 0.30000001192092896, + "is_volume_muted": false, + "media_content_id": "http://10.10.10.10:1337/fake.mp3", + "media_content_type": "music", + "media_duration": 3.160816, + "media_position": 0, + "media_position_updated_at": "2024-10-12T20:57:23.012272+00:00", + "app_id": "ABCDE123", + "app_name": "Default Media Receiver", + "entity_picture_local": null, + "device_class": "speaker", + "friendly_name": "Player 1", + "supported_features": 152463 + }, + "last_changed": "2024-10-12T20:57:23.012695+00:00", + "last_reported": "2024-10-12T20:57:23.012695+00:00", + "last_updated": "2024-10-12T20:57:23.012695+00:00", + "context": { + "id": "ABDCDEFGHIJKLMNOPQRSTUVW01", + "parent_id": null, + "user_id": "someuseridhash" + } + } +]