forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_test.go
155 lines (125 loc) · 3.31 KB
/
player_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package spotify
import (
"net/http"
"testing"
)
func TestTransferPlaybackDeviceUnavailable(t *testing.T) {
client, server := testClientString(http.StatusAccepted, "")
defer server.Close()
err := client.TransferPlayback("newdevice", false)
if err == nil {
t.Error("expected error since auto retry is disabled")
}
}
func TestTransferPlayback(t *testing.T) {
client, server := testClientString(http.StatusNoContent, "")
defer server.Close()
err := client.TransferPlayback("newdevice", true)
if err != nil {
t.Error(err)
}
}
func TestVolume(t *testing.T) {
client, server := testClientString(http.StatusNoContent, "")
defer server.Close()
err := client.Volume(50)
if err != nil {
t.Error(err)
}
}
func TestPlayerDevices(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/player_available_devices.txt")
defer server.Close()
list, err := client.PlayerDevices()
if err != nil {
t.Error(err)
return
}
if len(list) != 2 {
t.Error("Expected two devices")
}
if list[0].Volume != 100 {
t.Error("Expected volume to be 100 percent")
}
if list[1].Volume != 0 {
t.Error("Expected null becomes 0")
}
}
func TestPlayerState(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/player_state.txt")
defer server.Close()
state, err := client.PlayerState()
if err != nil {
t.Error(err)
return
}
if len(state.PlaybackContext.ExternalURLs) != 1 {
t.Error("Expected one external url")
}
if state.Item == nil {
t.Error("Expected item to be a track")
}
if state.Timestamp != 1491302708055 {
t.Error("Expected timestamp to be 1491302708055")
}
if state.Progress != 102509 {
t.Error("Expected progress to be 102509")
}
if state.Playing {
t.Error("Expected not to be playing")
}
}
func TestPlayerCurrentlyPlaying(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/player_currently_playing.txt")
defer server.Close()
state, err := client.PlayerCurrentlyPlaying()
if err != nil {
t.Error(err)
return
}
if len(state.PlaybackContext.ExternalURLs) != 1 {
t.Error("Expected one external url")
}
if state.Item == nil {
t.Error("Expected item to be a track")
}
if state.Timestamp != 1491302708055 {
t.Error("Expected timestamp to be 1491302708055")
}
if state.Progress != 102509 {
t.Error("Expected progress to be 102509")
}
if state.Playing {
t.Error("Expected not to be playing")
}
}
func TestPlayerRecentlyPlayed(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/player_recently_played.txt")
defer server.Close()
items, err := client.PlayerRecentlyPlayed()
if err != nil {
t.Fatal(err)
}
if len(items) != 20 {
t.Error("Too few or too many items were returned")
}
actualTimePhrase := items[0].PlayedAt.Format("2006-01-02T15:04:05.999Z")
expectedTimePhrase := "2017-05-27T20:07:54.721Z"
if actualTimePhrase != expectedTimePhrase {
t.Errorf("Time of first track was not parsed correctly: [%s] != [%s]", actualTimePhrase, expectedTimePhrase)
}
}
func TestPlayArgsError(t *testing.T) {
json := `{
"error" : {
"status" : 400,
"message" : "Only one of either \"context_uri\" or \"uris\" can be specified"
}
}`
client, server := testClientString(http.StatusUnauthorized, json)
defer server.Close()
err := client.Play()
if err == nil {
t.Error("Expected an error")
}
}