-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.test.ts
301 lines (255 loc) · 7.41 KB
/
client.test.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import {
assertEquals,
assertThrowsAsync,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { Client, Episode, RepeatState, SearchType, Track } from "./mod.ts";
const TOKEN = Deno.env.get("spotify_access_token") ?? "";
const spotify = new Client({
accessToken: TOKEN,
});
Deno.test("Fetch artist data", async () => {
const artist = await spotify.getArtist("Joji");
const expected = {
name: "Joji",
id: "3MZsBdqDrRTJihTHQrO6Dq",
genres: ["alternative r&b", "viral pop"],
type: "artist",
};
assertEquals(expected.name, artist.name);
assertEquals(expected.id, artist.id);
assertEquals(expected.genres, artist.genres);
assertEquals(expected.type, artist.type);
assertThrowsAsync(() => spotify.getArtist(""));
});
Deno.test("Fetch album data", async () => {
const artist = await spotify.getArtist("joji");
const albums = await artist.getAlbums({
market: "US",
});
const expected = {
id: "5EzDhyNZuO7kuaABHwbBKX",
name: "Nectar",
type: "album",
};
const result = albums[0];
assertEquals(result.id, expected.id);
assertEquals(result.name, expected.name);
assertEquals(result.type, expected.type);
assertThrowsAsync(() => spotify.getAlbum(""));
});
Deno.test("Get all album tracks", async () => {
const album = await spotify.getAlbum("Nectar");
const tracks = album.tracks;
const expected = [
"Ew",
"MODUS",
"Tick Tock",
"Daylight",
"Upgrade",
"Gimme Love",
"Run",
"Sanctuary",
"High Hopes (feat. Omar Apollo)",
"NITROUS",
"Pretty Boy (feat. Lil Yachty)",
"Normal People (feat. rei brown)",
"Afterthought",
"Mr. Hollywood",
"777",
"Reanimator (feat. Yves Tumor)",
"Like You Do",
"Your Man",
];
for (let i = 0; i < expected.length; i++) {
assertEquals(expected[i], tracks[i].name);
}
});
Deno.test("Get multiple albums", async () => {
const multipleAlbums = await spotify.getMultipleAlbums(["Nectar", "Ballads"]);
const expected = [
"Nectar",
"BALLADS 1",
];
const artist = "Joji";
for (let i = 0; i < multipleAlbums.length; i++) {
assertEquals(multipleAlbums[i].name, expected[i]);
assertEquals(multipleAlbums[i].artists[0].name, artist);
}
});
Deno.test("Get artist's top songs", async () => {
const artist = await spotify.getArtist("Joji");
const topTracks = await artist.getTopTracks();
const expected = [
"SLOW DANCING IN THE DARK",
"YEAH RIGHT",
"worldstar money (interlude)",
"Daylight",
"Your Man",
"Will He",
"CAN'T GET OVER YOU (feat. Clams Casino)",
"Gimme Love",
"Midsummer Madness",
"Sanctuary",
];
for (let i = 0; i < expected.length; i++) {
assertEquals(expected[i], topTracks[i].name);
}
});
Deno.test("Get albums and singles", async () => {
const artist = await spotify.getArtist("joji");
const albums = await artist.getAlbums({
market: "US",
});
const singles = await artist.getSingles({
market: "US",
});
const expectedAlbums = [
"Nectar",
"Nectar",
"BALLADS 1",
"BALLADS 1",
"In Tongues (Deluxe)",
"In Tongues Deluxe",
];
const expectedSingles = [
"Daylight",
"Daylight",
"Gimme Love",
"Gimme Love (Channel Tres Remix)",
"Run",
"Sanctuary",
"SLOW DANCING IN THE DARK (Loud Luxury Remix)",
"SLOW DANCING IN THE DARK (Mr. Mitch Remix)",
"SLOW DANCING IN THE DARK (Acoustic Remix)",
"Peach Jam",
"Yeah Right",
"YEAH RIGHT",
"In Tongues",
"In Tongues",
];
for (let i = 0; i < expectedAlbums.length; i++) {
assertEquals(expectedAlbums[i], albums[i].name);
}
for (let i = 0; i < expectedSingles.length; i++) {
assertEquals(expectedSingles[i], singles[i].name);
}
});
Deno.test("Get new releases", async () => {
const newReleases = await spotify.getNewReleases();
for (const release of newReleases) {
console.log(release.artists[0].name, " - ", release.name);
}
});
Deno.test("Get Tracks from a featured playlist", async () => {
const featuredPlaylists = await spotify.getFeaturedPlaylists();
const tracks = await featuredPlaylists[0].getTracks();
for (const track of tracks) {
console.log(track.name);
}
});
Deno.test("Get Recommendation genres", async () => {
const genres = await spotify.getRecommendationGenres();
for (const genre of genres) {
console.log(genre);
}
});
Deno.test("Get episodes", async () => {
const episode = await spotify.getEpisode("joe rogan");
console.log(episode.name);
});
Deno.test("Search", async () => {
const albums = await spotify.rawSearch({
q: "Nectar",
type: SearchType.Album,
});
console.log(albums);
});
Deno.test("Get Track", async () => {
const track = await spotify.getTrack("Peach Jam");
console.log(track.previewUrl);
});
Deno.test("Get Audio Features", async () => {
const track = await spotify.getTrack("Peach Jam");
const audioFeatures = await spotify.getAudioFeaturesForTrack({
id: track.id,
});
console.log(
audioFeatures.danceability,
audioFeatures.acousticness,
audioFeatures.liveness,
audioFeatures.timeSignature,
);
});
Deno.test("Get Player", async () => {
const player = spotify.getPlayer();
const devices = await player.getDevices();
for (const device of devices) {
console.log(device);
}
const playback = await player.getPlayback();
if (await playback.isPlaying()) {
const audio = await playback.getCurrentPlayingTrack();
if (audio instanceof Track) {
console.log(`Playing track: ${audio.artists[0].name} - ${audio.name}`);
} else if (audio instanceof Episode) {
console.log(`Playing episode: ${audio.name}`);
}
} else {
console.log(`Nothing is playing`);
}
});
Deno.test("Get current playing track", async () => {
const player = spotify.getPlayer();
const playback = await player.getPlayback();
const audio = await playback.getCurrentPlayingTrack();
if (audio) {
if (audio instanceof Track) {
console.log(
`Playing track: ${audio.artists[0].name} - ${audio.name}`,
);
} else if (audio instanceof Episode) {
console.log(
`Listening to ${audio.show.name} - ${audio.name}`,
);
}
return;
}
console.log("Nothing is playing.");
});
Deno.test("Test playback", async () => {
const player = await spotify.getPlayer();
const playback = await player.getPlayback();
if (await playback.isPlaying()) {
await playback.pause();
} else {
await playback.play();
}
console.log(playback.ctx?.repeatState, playback.ctx?.shuffleState);
});
Deno.test("Next Track", async () => {
const player = await spotify.getPlayer();
const playback = await player.getPlayback();
await playback.nextTrack();
});
Deno.test("Previous Track", async () => {
const player = await spotify.getPlayer();
const playback = await player.getPlayback();
await playback.previousTrack();
});
Deno.test("Set Repeat Mode", async () => {
const player = await spotify.getPlayer();
const playback = await player.getPlayback();
await playback.setRepeatMode(RepeatState.CONTEXT);
});
Deno.test("Get Audio Analysis", async () => {
const track = await spotify.getTrack("Peach Jam");
const analysis = await spotify.getAudioAnalysis(track.id);
console.log(analysis);
});
Deno.test("Get current user's playlists", async () => {
const currentUser = await spotify.getCurrentUser();
const playlists = await currentUser.getPlaylists();
for (const playlist of playlists) {
console.log(playlist.name);
}
});