odesli.js 1.3.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -85
- package/lib/index.d.ts +114 -17
- package/lib/index.js +893 -201
- package/lib/index.mjs +6 -3
- package/package.json +11 -2
- package/.eslintrc.js +0 -33
- package/.gitattributes +0 -1
- package/.github/dependabot.yml +0 -56
- package/.github/workflows/ci.yml +0 -94
- package/.github/workflows/dependabot-auto-merge.yml +0 -137
- package/.github/workflows/publish-github-packages.yml +0 -46
- package/.github/workflows/publish-npm.yml +0 -49
- package/.prettierignore +0 -31
- package/.prettierrc +0 -11
- package/API_IMPROVEMENTS.md +0 -318
- package/CHANGELOG.md +0 -246
- package/CODE_OF_CONDUCT.md +0 -125
- package/CONTRIBUTING.md +0 -104
- package/RELEASE_NOTES_1.1.0.md +0 -84
- package/SECURITY.md +0 -112
- package/examples/README.md +0 -442
- package/examples/advanced-features-example.js +0 -205
- package/examples/advanced-usage.js +0 -247
- package/examples/basic-usage.js +0 -100
- package/examples/cache-test.js +0 -119
- package/examples/rate-limiter-strategies.js +0 -83
- package/examples/test-apple-music-extraction.js +0 -60
- package/examples/user-agent-example.js +0 -46
- package/jest.config.js +0 -14
- package/jsdoc.json +0 -24
- package/scripts/version-bump.js +0 -41
- package/tests/advanced-features.test.js +0 -423
- package/tests/odesli.test.js +0 -742
- package/tests/setup.js +0 -22
- package/tsconfig.json +0 -21
package/tests/odesli.test.js
DELETED
|
@@ -1,742 +0,0 @@
|
|
|
1
|
-
jest.mock('node-fetch');
|
|
2
|
-
const fetch = require('node-fetch');
|
|
3
|
-
const Odesli = require('../lib/index.js');
|
|
4
|
-
|
|
5
|
-
// Helper to create a mock fetch response
|
|
6
|
-
function mockFetchResponse(data, status = 200) {
|
|
7
|
-
return {
|
|
8
|
-
ok: status >= 200 && status < 300,
|
|
9
|
-
status,
|
|
10
|
-
statusText: status === 200 ? 'OK' : 'Error',
|
|
11
|
-
json: jest.fn().mockResolvedValue(data),
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
describe('Odesli', () => {
|
|
16
|
-
let odesli;
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
// Disable caching for tests to avoid interference
|
|
20
|
-
odesli = new Odesli({ cache: false });
|
|
21
|
-
fetch.mockReset();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe('Constructor', () => {
|
|
25
|
-
test('should create instance with default options', () => {
|
|
26
|
-
const instance = new Odesli();
|
|
27
|
-
expect(instance.apiKey).toBeUndefined();
|
|
28
|
-
expect(instance.version).toBe('v1-alpha.1');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('should create instance with custom options', () => {
|
|
32
|
-
const instance = new Odesli({
|
|
33
|
-
apiKey: 'test-api-key',
|
|
34
|
-
version: 'v2-beta',
|
|
35
|
-
});
|
|
36
|
-
expect(instance.apiKey).toBe('test-api-key');
|
|
37
|
-
expect(instance.version).toBe('v2-beta');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('should create instance with partial options', () => {
|
|
41
|
-
const instance = new Odesli({ apiKey: 'test-key' });
|
|
42
|
-
expect(instance.apiKey).toBe('test-key');
|
|
43
|
-
expect(instance.version).toBe('v1-alpha.1');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('should handle empty options object', () => {
|
|
47
|
-
const instance = new Odesli({});
|
|
48
|
-
expect(instance.apiKey).toBeUndefined();
|
|
49
|
-
expect(instance.version).toBe('v1-alpha.1');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('should handle null/undefined options', () => {
|
|
53
|
-
const instance1 = new Odesli(null);
|
|
54
|
-
const instance2 = new Odesli(undefined);
|
|
55
|
-
expect(instance1.apiKey).toBeUndefined();
|
|
56
|
-
expect(instance1.version).toBe('v1-alpha.1');
|
|
57
|
-
expect(instance2.apiKey).toBeUndefined();
|
|
58
|
-
expect(instance2.version).toBe('v1-alpha.1');
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe('_request method', () => {
|
|
63
|
-
test('should make request without API key', async () => {
|
|
64
|
-
const mockResponse = {
|
|
65
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
66
|
-
title: 'Test Song',
|
|
67
|
-
artist: ['Test Artist'],
|
|
68
|
-
};
|
|
69
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
70
|
-
const result = await odesli._request('test-path');
|
|
71
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
72
|
-
'https://api.song.link/v1-alpha.1/test-path',
|
|
73
|
-
expect.objectContaining({
|
|
74
|
-
signal: expect.any(AbortSignal),
|
|
75
|
-
headers: expect.objectContaining({
|
|
76
|
-
'User-Agent': expect.any(String),
|
|
77
|
-
Accept: 'application/json',
|
|
78
|
-
}),
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
expect(result).toEqual(mockResponse);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('should make request with API key', async () => {
|
|
85
|
-
const odesliWithKey = new Odesli({ apiKey: 'test-key', cache: false });
|
|
86
|
-
const mockResponse = { success: true };
|
|
87
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
88
|
-
await odesliWithKey._request('test-path');
|
|
89
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
90
|
-
'https://api.song.link/v1-alpha.1/test-path&key=test-key',
|
|
91
|
-
expect.any(Object)
|
|
92
|
-
);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('should handle rate limiting error', async () => {
|
|
96
|
-
const errorResponse = { statusCode: 429, code: 'RATE_LIMITED' };
|
|
97
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(errorResponse, 200));
|
|
98
|
-
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
99
|
-
'429: RATE_LIMITED, You are being rate limited, No API Key is 10 Requests / Minute.'
|
|
100
|
-
);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test('should handle 4xx errors', async () => {
|
|
104
|
-
const errorResponse = { statusCode: 400, code: 'BAD_REQUEST' };
|
|
105
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(errorResponse, 200));
|
|
106
|
-
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
107
|
-
'400: BAD_REQUEST, Codes in the 4xx range indicate an error that failed given the information provided.'
|
|
108
|
-
);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test('should handle 5xx errors', async () => {
|
|
112
|
-
const errorResponse = { statusCode: 500, code: 'INTERNAL_ERROR' };
|
|
113
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(errorResponse, 200));
|
|
114
|
-
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
115
|
-
"500: INTERNAL_ERROR, Codes in the 5xx range indicate an error with Songlink's servers."
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('should handle unexpected API response', async () => {
|
|
120
|
-
fetch.mockResponseOnce('invalid json');
|
|
121
|
-
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
122
|
-
'invalid json response body'
|
|
123
|
-
);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('should handle network errors', async () => {
|
|
127
|
-
fetch.mockReject(new Error('Network error'));
|
|
128
|
-
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
129
|
-
'Network error'
|
|
130
|
-
);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test('should handle empty path', async () => {
|
|
134
|
-
const mockResponse = { success: true };
|
|
135
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
136
|
-
await odesli._request('');
|
|
137
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
138
|
-
'https://api.song.link/v1-alpha.1/',
|
|
139
|
-
expect.any(Object)
|
|
140
|
-
);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test('should handle special characters in path', async () => {
|
|
144
|
-
const mockResponse = { success: true };
|
|
145
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
146
|
-
await odesli._request('test/path?param=value&other=123');
|
|
147
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
148
|
-
'https://api.song.link/v1-alpha.1/test/path?param=value&other=123',
|
|
149
|
-
expect.any(Object)
|
|
150
|
-
);
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
describe('fetch method', () => {
|
|
155
|
-
test('should fetch song by URL', async () => {
|
|
156
|
-
const mockResponse = {
|
|
157
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
158
|
-
entitiesByUniqueId: {
|
|
159
|
-
'SPOTIFY_SONG::123': {
|
|
160
|
-
id: '123',
|
|
161
|
-
title: 'Test Song',
|
|
162
|
-
artistName: 'Test Artist, Featured Artist',
|
|
163
|
-
type: 'song',
|
|
164
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
169
|
-
const result = await odesli.fetch('https://open.spotify.com/track/123');
|
|
170
|
-
expect(result.title).toBe('Test Song');
|
|
171
|
-
expect(result.artist).toEqual(['Test Artist', 'Featured Artist']);
|
|
172
|
-
expect(result.type).toBe('song');
|
|
173
|
-
expect(result.thumbnail).toBe('https://example.com/thumb.jpg');
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test('should fetch multiple songs by array of URLs', async () => {
|
|
177
|
-
const mockResponse1 = {
|
|
178
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
179
|
-
entitiesByUniqueId: {
|
|
180
|
-
'SPOTIFY_SONG::123': {
|
|
181
|
-
id: '123',
|
|
182
|
-
title: 'Test Song 1',
|
|
183
|
-
artistName: 'Test Artist 1',
|
|
184
|
-
type: 'song',
|
|
185
|
-
thumbnailUrl: 'https://example.com/thumb1.jpg',
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
};
|
|
189
|
-
const mockResponse2 = {
|
|
190
|
-
entityUniqueId: 'SPOTIFY_SONG::456',
|
|
191
|
-
entitiesByUniqueId: {
|
|
192
|
-
'SPOTIFY_SONG::456': {
|
|
193
|
-
id: '456',
|
|
194
|
-
title: 'Test Song 2',
|
|
195
|
-
artistName: 'Test Artist 2',
|
|
196
|
-
type: 'song',
|
|
197
|
-
thumbnailUrl: 'https://example.com/thumb2.jpg',
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse1));
|
|
203
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse2));
|
|
204
|
-
|
|
205
|
-
const urls = [
|
|
206
|
-
'https://open.spotify.com/track/123',
|
|
207
|
-
'https://open.spotify.com/track/456',
|
|
208
|
-
];
|
|
209
|
-
|
|
210
|
-
const results = await odesli.fetch(urls);
|
|
211
|
-
|
|
212
|
-
expect(Array.isArray(results)).toBe(true);
|
|
213
|
-
expect(results).toHaveLength(2);
|
|
214
|
-
expect(results[0].title).toBe('Test Song 1');
|
|
215
|
-
expect(results[1].title).toBe('Test Song 2');
|
|
216
|
-
expect(fetch).toHaveBeenCalledTimes(2);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
test('should handle errors in batch fetch', async () => {
|
|
220
|
-
const urls = [
|
|
221
|
-
'https://open.spotify.com/track/123',
|
|
222
|
-
'https://open.spotify.com/track/invalid',
|
|
223
|
-
];
|
|
224
|
-
fetch.mockResponseOnce(
|
|
225
|
-
JSON.stringify({
|
|
226
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
227
|
-
entitiesByUniqueId: {
|
|
228
|
-
'SPOTIFY_SONG::123': { title: 'Test Song' },
|
|
229
|
-
},
|
|
230
|
-
})
|
|
231
|
-
);
|
|
232
|
-
fetch.mockRejectOnce(new Error('Network error'));
|
|
233
|
-
|
|
234
|
-
const results = await odesli.fetch(urls);
|
|
235
|
-
|
|
236
|
-
expect(Array.isArray(results)).toBe(true);
|
|
237
|
-
expect(results).toHaveLength(2);
|
|
238
|
-
expect(results[0].title).toBe('Test Song');
|
|
239
|
-
expect(results[1].error).toBe('Network error');
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
test('should fetch song with custom country', async () => {
|
|
243
|
-
const mockResponse = {
|
|
244
|
-
entityUniqueId: 'SPOTIFY_SONG::456',
|
|
245
|
-
entitiesByUniqueId: {
|
|
246
|
-
'SPOTIFY_SONG::456': {
|
|
247
|
-
id: '456',
|
|
248
|
-
title: 'Test Song 2',
|
|
249
|
-
artistName: 'Test Artist 2',
|
|
250
|
-
type: 'song',
|
|
251
|
-
thumbnailUrl: 'https://example.com/thumb2.jpg',
|
|
252
|
-
},
|
|
253
|
-
},
|
|
254
|
-
};
|
|
255
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
256
|
-
await odesli.fetch('https://open.spotify.com/track/456', 'GB');
|
|
257
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
258
|
-
'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F456&userCountry=GB',
|
|
259
|
-
expect.any(Object)
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test('should fetch batch with options', async () => {
|
|
264
|
-
const mockResponse = {
|
|
265
|
-
entityUniqueId: 'SPOTIFY_SONG::789',
|
|
266
|
-
entitiesByUniqueId: {
|
|
267
|
-
'SPOTIFY_SONG::789': {
|
|
268
|
-
id: '789',
|
|
269
|
-
title: 'Test Song 3',
|
|
270
|
-
artistName: 'Test Artist 3',
|
|
271
|
-
type: 'song',
|
|
272
|
-
thumbnailUrl: 'https://example.com/thumb3.jpg',
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
};
|
|
276
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
277
|
-
|
|
278
|
-
const urls = ['https://open.spotify.com/track/789'];
|
|
279
|
-
const results = await odesli.fetch(urls, {
|
|
280
|
-
country: 'CA',
|
|
281
|
-
concurrency: 1,
|
|
282
|
-
skipCache: true,
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
expect(Array.isArray(results)).toBe(true);
|
|
286
|
-
expect(results[0].title).toBe('Test Song 3');
|
|
287
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
288
|
-
'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F789&userCountry=CA',
|
|
289
|
-
expect.any(Object)
|
|
290
|
-
);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
test('should throw error when no URL provided', async () => {
|
|
294
|
-
await expect(odesli.fetch()).rejects.toThrow(
|
|
295
|
-
'No URL was provided to odesli.fetch()'
|
|
296
|
-
);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
test('should throw error when URL is null', async () => {
|
|
300
|
-
await expect(odesli.fetch(null)).rejects.toThrow(
|
|
301
|
-
'No URL was provided to odesli.fetch()'
|
|
302
|
-
);
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
test('should return empty array when URLs array is empty', async () => {
|
|
306
|
-
const results = await odesli.fetch([]);
|
|
307
|
-
expect(Array.isArray(results)).toBe(true);
|
|
308
|
-
expect(results).toHaveLength(0);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
test('should throw error when URLs is not an array', async () => {
|
|
312
|
-
await expect(odesli.fetch('not-an-array')).rejects.toThrow(
|
|
313
|
-
'Invalid URL format provided to odesli.fetch()'
|
|
314
|
-
);
|
|
315
|
-
});
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
describe('getByParams method', () => {
|
|
319
|
-
test('should get song by parameters', async () => {
|
|
320
|
-
const mockResponse = {
|
|
321
|
-
entityUniqueId: 'SPOTIFY_SONG::301',
|
|
322
|
-
entitiesByUniqueId: {
|
|
323
|
-
'SPOTIFY_SONG::301': {
|
|
324
|
-
id: '301',
|
|
325
|
-
title: 'Param Song',
|
|
326
|
-
artistName: 'Param Artist',
|
|
327
|
-
type: 'song',
|
|
328
|
-
thumbnailUrl: 'https://example.com/thumb5.jpg',
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
};
|
|
332
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
333
|
-
const result = await odesli.getByParams('spotify', 'song', '301');
|
|
334
|
-
expect(result.title).toBe('Param Song');
|
|
335
|
-
expect(result.artist).toEqual(['Param Artist']);
|
|
336
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
337
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=301&userCountry=US',
|
|
338
|
-
expect.any(Object)
|
|
339
|
-
);
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
test('should handle full ID format', async () => {
|
|
343
|
-
const mockResponse = {
|
|
344
|
-
entityUniqueId: 'SPOTIFY_SONG::302',
|
|
345
|
-
entitiesByUniqueId: {
|
|
346
|
-
'SPOTIFY_SONG::302': {
|
|
347
|
-
id: '302',
|
|
348
|
-
title: 'Full ID Song',
|
|
349
|
-
artistName: 'Full ID Artist',
|
|
350
|
-
type: 'song',
|
|
351
|
-
thumbnailUrl: 'https://example.com/thumb6.jpg',
|
|
352
|
-
},
|
|
353
|
-
},
|
|
354
|
-
};
|
|
355
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
356
|
-
await odesli.getByParams('spotify', 'song', 'SPOTIFY_SONG::302');
|
|
357
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
358
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=302&userCountry=US',
|
|
359
|
-
expect.any(Object)
|
|
360
|
-
);
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
test('should throw error when platform missing', async () => {
|
|
364
|
-
await expect(odesli.getByParams(null, 'song', '123')).rejects.toThrow(
|
|
365
|
-
'No `platform` was provided to odesli.getByParams()'
|
|
366
|
-
);
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
test('should throw error when type missing', async () => {
|
|
370
|
-
await expect(odesli.getByParams('spotify', null, '123')).rejects.toThrow(
|
|
371
|
-
'No `type` was provided to odesli.getByParams()'
|
|
372
|
-
);
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
test('should throw error when id missing', async () => {
|
|
376
|
-
await expect(odesli.getByParams('spotify', 'song', null)).rejects.toThrow(
|
|
377
|
-
'No `id` was provided to odesli.getByParams()'
|
|
378
|
-
);
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
test('should handle different platforms', async () => {
|
|
382
|
-
const mockResponse = {
|
|
383
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
384
|
-
entitiesByUniqueId: {
|
|
385
|
-
'SPOTIFY_SONG::123': {
|
|
386
|
-
id: '123',
|
|
387
|
-
title: 'Test Song',
|
|
388
|
-
artistName: 'Test Artist',
|
|
389
|
-
type: 'song',
|
|
390
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
391
|
-
},
|
|
392
|
-
},
|
|
393
|
-
};
|
|
394
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
395
|
-
await odesli.getByParams('spotify', 'song', '123');
|
|
396
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
397
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=123&userCountry=US',
|
|
398
|
-
expect.any(Object)
|
|
399
|
-
);
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
test('should handle different types', async () => {
|
|
403
|
-
const mockResponse = {
|
|
404
|
-
entityUniqueId: 'SPOTIFY_ALBUM::308',
|
|
405
|
-
entitiesByUniqueId: {
|
|
406
|
-
'SPOTIFY_ALBUM::308': {
|
|
407
|
-
id: '308',
|
|
408
|
-
title: 'Test Album',
|
|
409
|
-
artistName: 'Test Artist',
|
|
410
|
-
type: 'album',
|
|
411
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
412
|
-
},
|
|
413
|
-
},
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
417
|
-
const result = await odesli.getByParams('spotify', 'album', '308');
|
|
418
|
-
expect(result.type).toBe('album');
|
|
419
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
420
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=album&id=308&userCountry=US',
|
|
421
|
-
expect.any(Object)
|
|
422
|
-
);
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
test('should handle special characters in ID', async () => {
|
|
426
|
-
const mockResponse = {
|
|
427
|
-
entityUniqueId: 'SPOTIFY_SONG::309',
|
|
428
|
-
entitiesByUniqueId: {
|
|
429
|
-
'SPOTIFY_SONG::309': {
|
|
430
|
-
id: '309',
|
|
431
|
-
title: 'Special Char Song',
|
|
432
|
-
artistName: 'Special Artist',
|
|
433
|
-
type: 'song',
|
|
434
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
435
|
-
},
|
|
436
|
-
},
|
|
437
|
-
};
|
|
438
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
439
|
-
await odesli.getByParams('spotify', 'song', '123-456_789');
|
|
440
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
441
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=123-456_789&userCountry=US',
|
|
442
|
-
expect.any(Object)
|
|
443
|
-
);
|
|
444
|
-
});
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
describe('getById method', () => {
|
|
448
|
-
test('should get song by entity ID', async () => {
|
|
449
|
-
const mockResponse = {
|
|
450
|
-
entityUniqueId: 'SPOTIFY_SONG::401',
|
|
451
|
-
entitiesByUniqueId: {
|
|
452
|
-
'SPOTIFY_SONG::401': {
|
|
453
|
-
id: '401',
|
|
454
|
-
title: 'Entity ID Song',
|
|
455
|
-
artistName: 'Entity Artist',
|
|
456
|
-
type: 'song',
|
|
457
|
-
thumbnailUrl: 'https://example.com/thumb7.jpg',
|
|
458
|
-
},
|
|
459
|
-
},
|
|
460
|
-
};
|
|
461
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
462
|
-
const result = await odesli.getById('SPOTIFY_SONG::401');
|
|
463
|
-
expect(result.title).toBe('Entity ID Song');
|
|
464
|
-
expect(result.artist).toEqual(['Entity Artist']);
|
|
465
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
466
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=401&userCountry=US',
|
|
467
|
-
expect.any(Object)
|
|
468
|
-
);
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
test('should throw error when no ID provided', async () => {
|
|
472
|
-
await expect(odesli.getById()).rejects.toThrow(
|
|
473
|
-
'No `id` was provided to odesli.getById()'
|
|
474
|
-
);
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
test('should throw error when ID format is invalid', async () => {
|
|
478
|
-
await expect(odesli.getById('invalid-id')).rejects.toThrow(
|
|
479
|
-
'Provided Entity ID Does not match format. `<PLATFORM>_<SONG|ALBUM>::<UNIQUEID>`'
|
|
480
|
-
);
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
test('should handle different platform formats', async () => {
|
|
484
|
-
const mockResponse = {
|
|
485
|
-
entityUniqueId: 'APPLEMUSIC_SONG::402',
|
|
486
|
-
entitiesByUniqueId: {
|
|
487
|
-
'APPLEMUSIC_SONG::402': {
|
|
488
|
-
id: '402',
|
|
489
|
-
title: 'Apple Music Song',
|
|
490
|
-
artistName: 'Apple Artist',
|
|
491
|
-
type: 'song',
|
|
492
|
-
thumbnailUrl: 'https://example.com/thumb8.jpg',
|
|
493
|
-
},
|
|
494
|
-
},
|
|
495
|
-
};
|
|
496
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
497
|
-
const result = await odesli.getById('APPLEMUSIC_SONG::402');
|
|
498
|
-
expect(result.title).toBe('Apple Music Song');
|
|
499
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
500
|
-
'https://api.song.link/v1-alpha.1/links?platform=applemusic&type=song&id=402&userCountry=US',
|
|
501
|
-
expect.any(Object)
|
|
502
|
-
);
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
test('should handle album entity IDs', async () => {
|
|
506
|
-
const mockResponse = {
|
|
507
|
-
entityUniqueId: 'SPOTIFY_ALBUM::403',
|
|
508
|
-
entitiesByUniqueId: {
|
|
509
|
-
'SPOTIFY_ALBUM::403': {
|
|
510
|
-
id: '403',
|
|
511
|
-
title: 'Entity Album',
|
|
512
|
-
artistName: 'Entity Artist',
|
|
513
|
-
type: 'album',
|
|
514
|
-
thumbnailUrl: 'https://example.com/thumb9.jpg',
|
|
515
|
-
},
|
|
516
|
-
},
|
|
517
|
-
};
|
|
518
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
519
|
-
const result = await odesli.getById('SPOTIFY_ALBUM::403');
|
|
520
|
-
expect(result.type).toBe('album');
|
|
521
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
522
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=album&id=403&userCountry=US',
|
|
523
|
-
expect.any(Object)
|
|
524
|
-
);
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
test('should handle case insensitive platform names', async () => {
|
|
528
|
-
const mockResponse = {
|
|
529
|
-
entityUniqueId: 'SPOTIFY_SONG::404',
|
|
530
|
-
entitiesByUniqueId: {
|
|
531
|
-
'SPOTIFY_SONG::404': {
|
|
532
|
-
id: '404',
|
|
533
|
-
title: 'Case Insensitive Song',
|
|
534
|
-
artistName: 'Case Artist',
|
|
535
|
-
type: 'song',
|
|
536
|
-
thumbnailUrl: 'https://example.com/thumb10.jpg',
|
|
537
|
-
},
|
|
538
|
-
},
|
|
539
|
-
};
|
|
540
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
541
|
-
const result = await odesli.getById('spotify_song::404');
|
|
542
|
-
expect(result.title).toBe('Case Insensitive Song');
|
|
543
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
544
|
-
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=404&userCountry=US',
|
|
545
|
-
expect.any(Object)
|
|
546
|
-
);
|
|
547
|
-
});
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
describe('Edge Cases and Error Scenarios', () => {
|
|
551
|
-
test('should handle malformed API response', async () => {
|
|
552
|
-
const malformedResponse = {
|
|
553
|
-
entityUniqueId: 'SPOTIFY_SONG::501',
|
|
554
|
-
entitiesByUniqueId: {
|
|
555
|
-
'SPOTIFY_SONG::501': {
|
|
556
|
-
// Missing required fields
|
|
557
|
-
},
|
|
558
|
-
},
|
|
559
|
-
};
|
|
560
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(malformedResponse));
|
|
561
|
-
const result = await odesli.fetch('https://open.spotify.com/track/501');
|
|
562
|
-
expect(result.title).toBeUndefined();
|
|
563
|
-
expect(result.artist).toBeUndefined();
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
test('should handle multiple entities in response', async () => {
|
|
567
|
-
const mockResponse = {
|
|
568
|
-
entityUniqueId: 'SPOTIFY_SONG::502',
|
|
569
|
-
entitiesByUniqueId: {
|
|
570
|
-
'SPOTIFY_SONG::502': {
|
|
571
|
-
id: '502',
|
|
572
|
-
title: 'Main Song',
|
|
573
|
-
artistName: 'Main Artist',
|
|
574
|
-
type: 'song',
|
|
575
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
576
|
-
},
|
|
577
|
-
'APPLEMUSIC_SONG::503': {
|
|
578
|
-
id: '503',
|
|
579
|
-
title: 'Same Song',
|
|
580
|
-
artistName: 'Same Artist',
|
|
581
|
-
type: 'song',
|
|
582
|
-
thumbnailUrl: 'https://example.com/thumb2.jpg',
|
|
583
|
-
},
|
|
584
|
-
},
|
|
585
|
-
};
|
|
586
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
587
|
-
const result = await odesli.fetch('https://open.spotify.com/track/502');
|
|
588
|
-
expect(result.title).toBe('Main Song');
|
|
589
|
-
// Should only process the main entity, not all entities
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
test('should handle empty entitiesByUniqueId', async () => {
|
|
593
|
-
const mockResponse = {
|
|
594
|
-
entityUniqueId: 'SPOTIFY_SONG::504',
|
|
595
|
-
entitiesByUniqueId: {},
|
|
596
|
-
};
|
|
597
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
598
|
-
const result = await odesli.fetch('https://open.spotify.com/track/504');
|
|
599
|
-
expect(result.title).toBeUndefined();
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
test('should handle missing entityUniqueId in response', async () => {
|
|
603
|
-
const mockResponse = {
|
|
604
|
-
entitiesByUniqueId: {
|
|
605
|
-
'SPOTIFY_SONG::505': {
|
|
606
|
-
id: '505',
|
|
607
|
-
title: 'Missing Entity Song',
|
|
608
|
-
artistName: 'Missing Artist',
|
|
609
|
-
type: 'song',
|
|
610
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
611
|
-
},
|
|
612
|
-
},
|
|
613
|
-
};
|
|
614
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
615
|
-
const result = await odesli.fetch('https://open.spotify.com/track/505');
|
|
616
|
-
expect(result.title).toBeUndefined();
|
|
617
|
-
});
|
|
618
|
-
});
|
|
619
|
-
|
|
620
|
-
describe('getCountryOptions method', () => {
|
|
621
|
-
test('should return array of country options', () => {
|
|
622
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
623
|
-
expect(Array.isArray(countryOptions)).toBe(true);
|
|
624
|
-
expect(countryOptions.length).toBeGreaterThan(0);
|
|
625
|
-
});
|
|
626
|
-
|
|
627
|
-
test('should return objects with code and name properties', () => {
|
|
628
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
629
|
-
const firstCountry = countryOptions[0];
|
|
630
|
-
expect(firstCountry).toHaveProperty('code');
|
|
631
|
-
expect(firstCountry).toHaveProperty('name');
|
|
632
|
-
expect(typeof firstCountry.code).toBe('string');
|
|
633
|
-
expect(typeof firstCountry.name).toBe('string');
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
test('should include popular countries', () => {
|
|
637
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
638
|
-
const codes = countryOptions.map(c => c.code);
|
|
639
|
-
expect(codes).toContain('US');
|
|
640
|
-
expect(codes).toContain('GB');
|
|
641
|
-
expect(codes).toContain('CA');
|
|
642
|
-
expect(codes).toContain('AU');
|
|
643
|
-
expect(codes).toContain('DE');
|
|
644
|
-
});
|
|
645
|
-
|
|
646
|
-
test('should have valid ISO 3166-1 alpha-2 codes', () => {
|
|
647
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
648
|
-
countryOptions.forEach(country => {
|
|
649
|
-
expect(country.code).toMatch(/^[A-Z]{2}$/);
|
|
650
|
-
});
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
test('should have unique country codes', () => {
|
|
654
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
655
|
-
const codes = countryOptions.map(c => c.code);
|
|
656
|
-
const uniqueCodes = [...new Set(codes)];
|
|
657
|
-
expect(codes).toHaveLength(uniqueCodes.length);
|
|
658
|
-
});
|
|
659
|
-
|
|
660
|
-
test('should have non-empty country names', () => {
|
|
661
|
-
const countryOptions = Odesli.getCountryOptions();
|
|
662
|
-
countryOptions.forEach(country => {
|
|
663
|
-
expect(country.name.trim().length).toBeGreaterThan(0);
|
|
664
|
-
});
|
|
665
|
-
});
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
describe('Country code validation', () => {
|
|
669
|
-
test('should accept valid country codes', async () => {
|
|
670
|
-
const mockResponse = {
|
|
671
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
672
|
-
entitiesByUniqueId: {
|
|
673
|
-
'SPOTIFY_SONG::123': {
|
|
674
|
-
id: '123',
|
|
675
|
-
title: 'Test Song',
|
|
676
|
-
artistName: 'Test Artist',
|
|
677
|
-
type: 'song',
|
|
678
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
679
|
-
},
|
|
680
|
-
},
|
|
681
|
-
};
|
|
682
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
683
|
-
|
|
684
|
-
const result = await odesli.fetch('https://open.spotify.com/track/123', {
|
|
685
|
-
country: 'US',
|
|
686
|
-
});
|
|
687
|
-
expect(result.title).toBe('Test Song');
|
|
688
|
-
});
|
|
689
|
-
|
|
690
|
-
test('should accept multiple valid country codes', async () => {
|
|
691
|
-
const mockResponse = {
|
|
692
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
693
|
-
entitiesByUniqueId: {
|
|
694
|
-
'SPOTIFY_SONG::123': {
|
|
695
|
-
id: '123',
|
|
696
|
-
title: 'Test Song',
|
|
697
|
-
artistName: 'Test Artist',
|
|
698
|
-
type: 'song',
|
|
699
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
700
|
-
},
|
|
701
|
-
},
|
|
702
|
-
};
|
|
703
|
-
fetch.mockResolvedValue(mockFetchResponse(mockResponse));
|
|
704
|
-
|
|
705
|
-
const validCountries = ['US', 'GB', 'CA', 'AU', 'DE', 'FR', 'JP'];
|
|
706
|
-
|
|
707
|
-
for (const country of validCountries) {
|
|
708
|
-
await odesli.fetch('https://open.spotify.com/track/123', {
|
|
709
|
-
country,
|
|
710
|
-
});
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
// Should have made requests for each country
|
|
714
|
-
expect(fetch).toHaveBeenCalledTimes(validCountries.length);
|
|
715
|
-
});
|
|
716
|
-
|
|
717
|
-
test('should handle batch requests with country codes', async () => {
|
|
718
|
-
const mockResponse = {
|
|
719
|
-
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
720
|
-
entitiesByUniqueId: {
|
|
721
|
-
'SPOTIFY_SONG::123': {
|
|
722
|
-
id: '123',
|
|
723
|
-
title: 'Test Song',
|
|
724
|
-
artistName: 'Test Artist',
|
|
725
|
-
type: 'song',
|
|
726
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
727
|
-
},
|
|
728
|
-
},
|
|
729
|
-
};
|
|
730
|
-
fetch.mockResolvedValue(mockFetchResponse(mockResponse));
|
|
731
|
-
|
|
732
|
-
const urls = [
|
|
733
|
-
'https://open.spotify.com/track/123',
|
|
734
|
-
'https://music.apple.com/us/album/test/456?i=789',
|
|
735
|
-
];
|
|
736
|
-
|
|
737
|
-
const results = await odesli.fetch(urls, { country: 'GB' });
|
|
738
|
-
expect(results).toHaveLength(2);
|
|
739
|
-
expect(fetch).toHaveBeenCalledTimes(2);
|
|
740
|
-
});
|
|
741
|
-
});
|
|
742
|
-
});
|