|
| 1 | +import { SoundcloudMetaProvider } from './SoundcloudPlugin'; |
| 2 | +import { SearchResultsSource } from '../plugins.types'; |
| 3 | + |
| 4 | +jest.mock('soundcloud.ts', () => { |
| 5 | + return function() { |
| 6 | + return { |
| 7 | + users: { |
| 8 | + search: jest.fn(), |
| 9 | + get: jest.fn(), |
| 10 | + tracks: jest.fn() |
| 11 | + }, |
| 12 | + tracks: { |
| 13 | + search: jest.fn() |
| 14 | + } |
| 15 | + }; |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +describe('SoundcloudMetaProvider', () => { |
| 20 | + let provider: SoundcloudMetaProvider; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + provider = new SoundcloudMetaProvider(); |
| 24 | + jest.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('searchForArtists', () => { |
| 28 | + it('should convert Soundcloud artists to SearchResultsArtist format', async () => { |
| 29 | + const mockArtists = { |
| 30 | + collection: [ |
| 31 | + { |
| 32 | + id: 123456, |
| 33 | + avatar_url: 'https://example.com/avatar.jpg', |
| 34 | + username: 'Test Artist', |
| 35 | + permalink_url: 'https://soundcloud.com/testartist' |
| 36 | + } |
| 37 | + ] |
| 38 | + }; |
| 39 | + |
| 40 | + provider.soundcloud.users.search = jest.fn().mockResolvedValue(mockArtists); |
| 41 | + |
| 42 | + const results = await provider.searchForArtists('Test Artist'); |
| 43 | + |
| 44 | + expect(provider.soundcloud.users.search).toHaveBeenCalledWith({ q: 'Test Artist' }); |
| 45 | + expect(results).toHaveLength(1); |
| 46 | + expect(results[0]).toEqual({ |
| 47 | + id: '123456', |
| 48 | + coverImage: 'https://example.com/avatar.jpg', |
| 49 | + thumb: 'https://example.com/avatar.jpg', |
| 50 | + name: 'Test Artist', |
| 51 | + image: 'https://example.com/avatar.jpg', |
| 52 | + url: 'https://soundcloud.com/testartist', |
| 53 | + resourceUrl: 'https://soundcloud.com/testartist', |
| 54 | + source: SearchResultsSource.Soundcloud |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('searchForTracks', () => { |
| 60 | + it('should convert Soundcloud tracks to SearchResultsTrack format', async () => { |
| 61 | + const mockTracks = { |
| 62 | + collection: [ |
| 63 | + { |
| 64 | + id: 987654, |
| 65 | + title: 'Test Track', |
| 66 | + user: { |
| 67 | + username: 'Test Artist' |
| 68 | + } |
| 69 | + } |
| 70 | + ] |
| 71 | + }; |
| 72 | + |
| 73 | + provider.soundcloud.tracks.search = jest.fn().mockResolvedValue(mockTracks); |
| 74 | + |
| 75 | + const results = await provider.searchForTracks('Test Track'); |
| 76 | + |
| 77 | + expect(provider.soundcloud.tracks.search).toHaveBeenCalledWith({ q: 'Test Track' }); |
| 78 | + expect(results).toHaveLength(1); |
| 79 | + expect(results[0]).toEqual({ |
| 80 | + id: '987654', |
| 81 | + title: 'Test Track', |
| 82 | + artist: 'Test Artist', |
| 83 | + source: SearchResultsSource.Soundcloud |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('fetchArtistDetails', () => { |
| 89 | + it('should fetch artist details and convert to ArtistDetails format', async () => { |
| 90 | + const mockArtist = { |
| 91 | + id: 123456, |
| 92 | + username: 'Test Artist', |
| 93 | + description: 'This is a test artist description', |
| 94 | + avatar_url: 'https://example.com/avatar.jpg' |
| 95 | + }; |
| 96 | + |
| 97 | + const mockTracks = [ |
| 98 | + { |
| 99 | + id: 987654, |
| 100 | + title: 'Top Track 1', |
| 101 | + user: { |
| 102 | + username: 'Test Artist' |
| 103 | + }, |
| 104 | + playback_count: 1000, |
| 105 | + likes_count: 500 |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 987655, |
| 109 | + title: 'Top Track 2', |
| 110 | + user: { |
| 111 | + username: 'Test Artist' |
| 112 | + }, |
| 113 | + playback_count: 800, |
| 114 | + likes_count: 400 |
| 115 | + } |
| 116 | + ]; |
| 117 | + |
| 118 | + provider.soundcloud.users.get = jest.fn().mockResolvedValue(mockArtist); |
| 119 | + provider.soundcloud.users.tracks = jest.fn().mockResolvedValue(mockTracks); |
| 120 | + |
| 121 | + const result = await provider.fetchArtistDetails('123456'); |
| 122 | + |
| 123 | + expect(provider.soundcloud.users.get).toHaveBeenCalledWith('123456'); |
| 124 | + expect(provider.soundcloud.users.tracks).toHaveBeenCalledWith(123456); |
| 125 | + |
| 126 | + expect(result).toEqual({ |
| 127 | + id: '123456', |
| 128 | + name: 'Test Artist', |
| 129 | + description: 'This is a test artist description', |
| 130 | + coverImage: 'https://example.com/avatar.jpg', |
| 131 | + similar: [], |
| 132 | + topTracks: [ |
| 133 | + { |
| 134 | + artist: { name: 'Test Artist' }, |
| 135 | + title: 'Top Track 1', |
| 136 | + playcount: 1000, |
| 137 | + listeners: 500 |
| 138 | + }, |
| 139 | + { |
| 140 | + artist: { name: 'Test Artist' }, |
| 141 | + title: 'Top Track 2', |
| 142 | + playcount: 800, |
| 143 | + listeners: 400 |
| 144 | + } |
| 145 | + ], |
| 146 | + source: SearchResultsSource.Soundcloud |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('fetchArtistDetailsByName', () => { |
| 152 | + it('should search for artist and then fetch details', async () => { |
| 153 | + const mockSearch = { |
| 154 | + collection: [ |
| 155 | + { |
| 156 | + id: 123456, |
| 157 | + username: 'Test Artist' |
| 158 | + } |
| 159 | + ] |
| 160 | + }; |
| 161 | + |
| 162 | + const mockArtistDetails = { |
| 163 | + id: '123456', |
| 164 | + name: 'Test Artist', |
| 165 | + description: 'Artist description', |
| 166 | + coverImage: 'https://example.com/avatar.jpg', |
| 167 | + similar: [], |
| 168 | + topTracks: [], |
| 169 | + source: SearchResultsSource.Soundcloud |
| 170 | + }; |
| 171 | + |
| 172 | + provider.soundcloud.users.search = jest.fn().mockResolvedValue(mockSearch); |
| 173 | + provider.fetchArtistDetails = jest.fn().mockResolvedValue(mockArtistDetails); |
| 174 | + |
| 175 | + const result = await provider.fetchArtistDetailsByName('Test Artist'); |
| 176 | + |
| 177 | + expect(provider.soundcloud.users.search).toHaveBeenCalledWith({ q: 'Test Artist' }); |
| 178 | + expect(provider.fetchArtistDetails).toHaveBeenCalledWith('123456'); |
| 179 | + expect(result).toEqual(mockArtistDetails); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should throw an error when artist is not found', async () => { |
| 183 | + const mockSearch = { |
| 184 | + collection: [] |
| 185 | + }; |
| 186 | + |
| 187 | + provider.soundcloud.users.search = jest.fn().mockResolvedValue(mockSearch); |
| 188 | + |
| 189 | + await expect(provider.fetchArtistDetailsByName('Nonexistent Artist')).rejects.toThrow('Artist not found'); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + describe('searchForReleases', () => { |
| 194 | + it('should return an empty array', async () => { |
| 195 | + const mockTracks = { |
| 196 | + collection: [ |
| 197 | + { |
| 198 | + id: 987654, |
| 199 | + title: 'Test Track', |
| 200 | + user: { |
| 201 | + username: 'Test Artist' |
| 202 | + } |
| 203 | + } |
| 204 | + ] |
| 205 | + }; |
| 206 | + |
| 207 | + provider.soundcloud.tracks.search = jest.fn().mockResolvedValue(mockTracks); |
| 208 | + |
| 209 | + const results = await provider.searchForReleases('Test Release'); |
| 210 | + |
| 211 | + expect(provider.soundcloud.tracks.search).toHaveBeenCalledWith({ q: 'Test Release' }); |
| 212 | + expect(results).toEqual([]); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + describe('fetchAlbumDetails and fetchAlbumDetailsByName', () => { |
| 217 | + it('should return null for fetchAlbumDetails', async () => { |
| 218 | + const result = await provider.fetchAlbumDetails('123', 'master'); |
| 219 | + expect(result).toBeNull(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should return null for fetchAlbumDetailsByName', async () => { |
| 223 | + const result = await provider.fetchAlbumDetailsByName('Test Album', 'master', 'Test Artist'); |
| 224 | + expect(result).toBeNull(); |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
0 commit comments