|
| 1 | +import { API } from "@/API"; |
| 2 | + |
| 3 | +const TRACK_URL = "https://open.spotify.com/track/6Fbsn9471Xd0vVsMWwhePh?si=f992e1fe1f714674"; |
| 4 | +const ALBUM_URL = "https://open.spotify.com/album/5Gu0Ldddj2f6a0q5gitIok?si=781b3339b2bc4015"; |
| 5 | +const PLAYLIST_URL = "https://open.spotify.com/playlist/1IetTTs69jS1MQ3U6yxofB?si=182a1e01ff374fc8"; |
| 6 | +const ARTIST_URL = "https://open.spotify.com/artist/3FwYnbtGNt8hJfjNuOfpeG?si=d4c1d832636c4bb1"; |
| 7 | +const LONG_PLAYLIST_URL = "https://open.spotify.com/playlist/0HlfmkivifBxTcNeoev41s?si=8f7aa0dac31a406d"; |
| 8 | +const LONG_ALBUM_URL = "https://open.spotify.com/album/1fnZbc8ZrgyW6RpFvwXkwl"; |
| 9 | + |
| 10 | +const PRIVATE_ID = "1CReGsrMUgMQXinhawwIKX"; |
| 11 | + |
| 12 | +const expectTrack = { |
| 13 | + type: "track", |
| 14 | + name: expect.any(String), |
| 15 | + artists: expect.arrayContaining([expect.objectContaining({ name: expect.any(String) })]), |
| 16 | +}; |
| 17 | + |
| 18 | +const expectList = (type: "track" | "album" | "playlist" | "artist" | string) => |
| 19 | + type === "track" |
| 20 | + ? expectTrack |
| 21 | + : { |
| 22 | + type, |
| 23 | + name: expect.any(String), |
| 24 | + thumbnail: expect.any(String), |
| 25 | + url: expect.any(String), |
| 26 | + tracks: expect.arrayContaining([expect.objectContaining(expectTrack)]), |
| 27 | + }; |
| 28 | + |
| 29 | +describe.each([ |
| 30 | + ["Without", undefined, undefined], |
| 31 | + ["With", process.env.SPOTIFY_CLIENT_ID, process.env.SPOTIFY_CLIENT_SECRET], |
| 32 | +])("%s client credentials", (_name, clientId, clientSecret) => { |
| 33 | + describe.each([ |
| 34 | + ["Using spotify api", true], |
| 35 | + ["Using spotify embed", false], |
| 36 | + ])("%s", (_name, useAPI) => { |
| 37 | + const api = new API(clientId, clientSecret, "VN"); |
| 38 | + if (useAPI) { |
| 39 | + test("refreshToken", async () => { |
| 40 | + await api.refreshToken(); |
| 41 | + expect(api["_tokenAvailable"]).toBe(true); |
| 42 | + expect(api["_hasCredentials"]).toBe(!!(clientId && clientSecret)); |
| 43 | + expect(api["_expirationTime"]).toBeGreaterThan(Date.now()); |
| 44 | + }); |
| 45 | + } else { |
| 46 | + api["_tokenAvailable"] = false; |
| 47 | + api["_hasCredentials"] = false; |
| 48 | + api["_expirationTime"] = Infinity; |
| 49 | + } |
| 50 | + |
| 51 | + describe("getData", () => { |
| 52 | + test.each([ |
| 53 | + ["track", TRACK_URL], |
| 54 | + ["album", ALBUM_URL], |
| 55 | + ["playlist", PLAYLIST_URL], |
| 56 | + ["artist", ARTIST_URL], |
| 57 | + ])("get %s", async (type, url) => { |
| 58 | + const data = await api.getData(url); |
| 59 | + expect(data).toMatchObject(expectList(type)); |
| 60 | + if (data.type !== "track") { |
| 61 | + expect(data.tracks.length).toBeGreaterThanOrEqual(1); |
| 62 | + expect(data.tracks).not.toContain(null); |
| 63 | + expect(data.tracks).not.toContainEqual(expect.objectContaining({ type: expect.not.stringMatching("track") })); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + test.each([ |
| 68 | + ["playlist", LONG_PLAYLIST_URL, 900], |
| 69 | + ["album", LONG_ALBUM_URL, 50], |
| 70 | + ])( |
| 71 | + "get long %s", |
| 72 | + async (type, url, length) => { |
| 73 | + const data = await api.getData(url); |
| 74 | + expect(data).toMatchObject(expectList(type)); |
| 75 | + expect(data.type).toBe(type); |
| 76 | + if (data.type !== "track") { |
| 77 | + expect(data.tracks.length).toBeGreaterThanOrEqual(useAPI ? length : 100); |
| 78 | + } |
| 79 | + }, |
| 80 | + 15_000, |
| 81 | + ); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("API error handling", () => { |
| 87 | + const api = new API(); |
| 88 | + |
| 89 | + test("refreshToken", async () => { |
| 90 | + await api.refreshToken(); |
| 91 | + expect(api["_tokenAvailable"]).toBe(true); |
| 92 | + expect(api["_hasCredentials"]).toBe(false); |
| 93 | + expect(api["_expirationTime"]).toBeGreaterThan(Date.now()); |
| 94 | + }); |
| 95 | + |
| 96 | + test.each([ |
| 97 | + ["track", `https://open.spotify.com/track/${PRIVATE_ID}`], |
| 98 | + ["album", `https://open.spotify.com/album/${PRIVATE_ID}`], |
| 99 | + ["playlist", `https://open.spotify.com/playlist/${PRIVATE_ID}`], |
| 100 | + ["artist", `https://open.spotify.com/artist/${PRIVATE_ID}`], |
| 101 | + ])("private %s", async (_type, url) => { |
| 102 | + await expect(api.getData(url)).rejects.toThrow("The URL is private or unavailable."); |
| 103 | + }); |
| 104 | +}); |
0 commit comments