odesli.js 1.1.1 → 1.3.1

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.
@@ -73,7 +73,7 @@ describe('Odesli', () => {
73
73
  expect.objectContaining({
74
74
  signal: expect.any(AbortSignal),
75
75
  headers: expect.objectContaining({
76
- 'User-Agent': 'odesli.js/1.0.3',
76
+ 'User-Agent': expect.any(String),
77
77
  Accept: 'application/json',
78
78
  }),
79
79
  })
@@ -117,16 +117,14 @@ describe('Odesli', () => {
117
117
  });
118
118
 
119
119
  test('should handle unexpected API response', async () => {
120
- fetch.mockRejectedValueOnce(
121
- new Error('Unexpected token < in JSON at position 0')
122
- );
120
+ fetch.mockResponseOnce('invalid json');
123
121
  await expect(odesli._request('test-path')).rejects.toThrow(
124
- 'API returned an unexpected result.'
122
+ 'invalid json response body'
125
123
  );
126
124
  });
127
125
 
128
126
  test('should handle network errors', async () => {
129
- fetch.mockRejectedValueOnce(new Error('Network error'));
127
+ fetch.mockReject(new Error('Network error'));
130
128
  await expect(odesli._request('test-path')).rejects.toThrow(
131
129
  'Network error'
132
130
  );
@@ -175,8 +173,20 @@ describe('Odesli', () => {
175
173
  expect(result.thumbnail).toBe('https://example.com/thumb.jpg');
176
174
  });
177
175
 
178
- test('should fetch song with custom country', async () => {
179
- const mockResponse = {
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 = {
180
190
  entityUniqueId: 'SPOTIFY_SONG::456',
181
191
  entitiesByUniqueId: {
182
192
  'SPOTIFY_SONG::456': {
@@ -188,95 +198,120 @@ describe('Odesli', () => {
188
198
  },
189
199
  },
190
200
  };
191
- fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
192
- await odesli.fetch('https://open.spotify.com/track/456', 'GB');
193
- expect(fetch).toHaveBeenCalledWith(
194
- 'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F456&userCountry=GB',
195
- expect.any(Object)
196
- );
197
- });
198
201
 
199
- test('should throw error when no URL provided', async () => {
200
- await expect(odesli.fetch()).rejects.toThrow(
201
- 'No URL was provided to odesli.fetch()'
202
- );
203
- });
202
+ fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse1));
203
+ fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse2));
204
204
 
205
- test('should throw error when URL is null', async () => {
206
- await expect(odesli.fetch(null)).rejects.toThrow(
207
- 'No URL was provided to odesli.fetch()'
208
- );
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);
209
217
  });
210
218
 
211
- test('should throw error when URL is empty string', async () => {
212
- await expect(odesli.fetch('')).rejects.toThrow(
213
- 'No URL was provided to odesli.fetch()'
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
+ })
214
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');
215
240
  });
216
241
 
217
- test('should handle single artist name', async () => {
242
+ test('should fetch song with custom country', async () => {
218
243
  const mockResponse = {
219
- entityUniqueId: 'SPOTIFY_SONG::789',
244
+ entityUniqueId: 'SPOTIFY_SONG::456',
220
245
  entitiesByUniqueId: {
221
- 'SPOTIFY_SONG::789': {
222
- id: '789',
223
- title: 'Single Artist Song',
224
- artistName: 'Single Artist',
246
+ 'SPOTIFY_SONG::456': {
247
+ id: '456',
248
+ title: 'Test Song 2',
249
+ artistName: 'Test Artist 2',
225
250
  type: 'song',
226
- thumbnailUrl: 'https://example.com/thumb3.jpg',
251
+ thumbnailUrl: 'https://example.com/thumb2.jpg',
227
252
  },
228
253
  },
229
254
  };
230
255
  fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
231
- const result = await odesli.fetch('https://open.spotify.com/track/789');
232
- expect(result.artist).toEqual(['Single Artist']);
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
+ );
233
261
  });
234
262
 
235
- test('should handle missing artist name', async () => {
263
+ test('should fetch batch with options', async () => {
236
264
  const mockResponse = {
237
- entityUniqueId: 'SPOTIFY_SONG::101',
265
+ entityUniqueId: 'SPOTIFY_SONG::789',
238
266
  entitiesByUniqueId: {
239
- 'SPOTIFY_SONG::101': {
240
- id: '101',
241
- title: 'No Artist Song',
267
+ 'SPOTIFY_SONG::789': {
268
+ id: '789',
269
+ title: 'Test Song 3',
270
+ artistName: 'Test Artist 3',
242
271
  type: 'song',
243
- thumbnailUrl: 'https://example.com/thumb4.jpg',
272
+ thumbnailUrl: 'https://example.com/thumb3.jpg',
244
273
  },
245
274
  },
246
275
  };
247
276
  fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
248
- const result = await odesli.fetch('https://open.spotify.com/track/101');
249
- expect(result.artist).toBeUndefined();
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
+ );
250
291
  });
251
292
 
252
- test('should handle different URL formats', async () => {
253
- const urls = [
254
- 'https://open.spotify.com/track/201',
255
- 'https://music.apple.com/us/album/test/202?i=456',
256
- 'https://www.youtube.com/watch?v=203',
257
- 'https://tidal.com/track/204',
258
- ];
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
+ });
259
298
 
260
- for (let i = 0; i < urls.length; i++) {
261
- const mockResponse = {
262
- entityUniqueId: `SPOTIFY_SONG::${205 + i}`,
263
- entitiesByUniqueId: {
264
- [`SPOTIFY_SONG::${205 + i}`]: {
265
- id: `${205 + i}`,
266
- title: `Test Song ${205 + i}`,
267
- artistName: 'Test Artist',
268
- type: 'song',
269
- thumbnailUrl: 'https://example.com/thumb.jpg',
270
- },
271
- },
272
- };
273
- fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
274
- await odesli.fetch(urls[i]);
275
- expect(fetch).toHaveBeenCalledWith(
276
- `https://api.song.link/v1-alpha.1/links?url=${encodeURIComponent(urls[i])}&userCountry=US`,
277
- expect.any(Object)
278
- );
279
- }
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
+ );
280
315
  });
281
316
  });
282
317
 
@@ -344,28 +379,24 @@ describe('Odesli', () => {
344
379
  });
345
380
 
346
381
  test('should handle different platforms', async () => {
347
- const platforms = ['spotify', 'appleMusic', 'youtube', 'tidal', 'deezer'];
348
- for (let i = 0; i < platforms.length; i++) {
349
- const platform = platforms[i];
350
- const mockResponse = {
351
- entityUniqueId: `SPOTIFY_SONG::${303 + i}`,
352
- entitiesByUniqueId: {
353
- [`SPOTIFY_SONG::${303 + i}`]: {
354
- id: `${303 + i}`,
355
- title: `Platform Song ${i}`,
356
- artistName: 'Platform Artist',
357
- type: 'song',
358
- thumbnailUrl: 'https://example.com/thumb.jpg',
359
- },
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',
360
391
  },
361
- };
362
- fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
363
- await odesli.getByParams(platform, 'song', `${303 + i}`);
364
- expect(fetch).toHaveBeenCalledWith(
365
- `https://api.song.link/v1-alpha.1/links?platform=${platform}&type=song&id=${303 + i}&userCountry=US`,
366
- expect.any(Object)
367
- );
368
- }
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
+ );
369
400
  });
370
401
 
371
402
  test('should handle different types', async () => {
@@ -585,4 +616,127 @@ describe('Odesli', () => {
585
616
  expect(result.title).toBeUndefined();
586
617
  });
587
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
+ });
588
742
  });
package/tests/setup.js CHANGED
@@ -18,3 +18,5 @@ global.console = {
18
18
  warn: jest.fn(),
19
19
  error: jest.fn(),
20
20
  };
21
+
22
+ require('jest-fetch-mock').enableMocks();