diff --git a/src/modules/history/tracks/history-tracks.service.spec.ts b/src/modules/history/tracks/history-tracks.service.spec.ts index 5d17d588..9f61eca1 100644 --- a/src/modules/history/tracks/history-tracks.service.spec.ts +++ b/src/modules/history/tracks/history-tracks.service.spec.ts @@ -97,33 +97,53 @@ describe('HistoryTracksService', () => { }) }) - test('should create history tracks from play history', async () => { - const playHistoryMock: PlayHistory[] = [ - { - played_at: new Date().toISOString(), - track: sdkTrackMock, - context: mock(), - }, - ] - - const findOrCreateTracks = vi - .spyOn(tracksService, 'findOrCreate') - .mockResolvedValue(trackEntitiesMock) - - const createHistoryTrackSpy = vi - .spyOn(historyTracksRepository, 'createHistoryTrack') - .mockResolvedValue(historyTrackMock) - - expect( - await historyTracksService.createHistoryTracksFromPlayHistory( - playHistoryMock, - userMock + describe('createHistoryTracksFromPlayHistory', () => { + test('should return empty array if no tracks are found or created', async () => { + const playHistoryMock = [] + + const findOrCreateTracks = vi + .spyOn(tracksService, 'findOrCreate') + .mockResolvedValue([]) + + expect( + await historyTracksService.createHistoryTracksFromPlayHistory( + playHistoryMock, + userMock + ) + ).toEqual([]) + expect(findOrCreateTracks).toHaveBeenCalledWith( + playHistoryMock.map(({ track }) => track) ) - ).toEqual(historyTracksMock) + }) + + test('should create history tracks from play history', async () => { + const playHistoryMock: PlayHistory[] = [ + { + played_at: new Date().toISOString(), + track: sdkTrackMock, + context: mock(), + }, + ] + + const findOrCreateTracks = vi + .spyOn(tracksService, 'findOrCreate') + .mockResolvedValue(trackEntitiesMock) + + const createHistoryTrackSpy = vi + .spyOn(historyTracksRepository, 'createHistoryTrack') + .mockResolvedValue(historyTrackMock) - expect(findOrCreateTracks).toHaveBeenCalledWith( - playHistoryMock.map(({ track }) => track) - ) - expect(createHistoryTrackSpy).toHaveBeenCalled() + expect( + await historyTracksService.createHistoryTracksFromPlayHistory( + playHistoryMock, + userMock + ) + ).toEqual(historyTracksMock) + + expect(findOrCreateTracks).toHaveBeenCalledWith( + playHistoryMock.map(({ track }) => track) + ) + expect(createHistoryTrackSpy).toHaveBeenCalled() + }) }) }) diff --git a/src/modules/history/tracks/history-tracks.service.ts b/src/modules/history/tracks/history-tracks.service.ts index 19f24448..0ea2e4c9 100644 --- a/src/modules/history/tracks/history-tracks.service.ts +++ b/src/modules/history/tracks/history-tracks.service.ts @@ -35,6 +35,8 @@ export class HistoryTracksService { playHistory.map(({ track }) => track) ) + if (tracks.length === 0) return [] + const historyTracks: HistoryTrack[] = [] for (const track of tracks) { diff --git a/src/modules/tracks/tracks.service.spec.ts b/src/modules/tracks/tracks.service.spec.ts index cac5bad9..25de6881 100644 --- a/src/modules/tracks/tracks.service.spec.ts +++ b/src/modules/tracks/tracks.service.spec.ts @@ -238,6 +238,14 @@ describe('TracksService', () => { expect(findOrCreateTracksFromDtosSpy).not.toHaveBeenCalled() }) + test('should return empty array if no data', async () => { + expect(await tracksService.findOrCreate([])).toEqual([]) + expect(findOrCreateTrackFromExternalIdSpy).not.toHaveBeenCalled() + expect(findOrCreateTrackFromDtoSpy).not.toHaveBeenCalled() + expect(findOrCreateTracksFromExternalIdsSpy).not.toHaveBeenCalled() + expect(findOrCreateTracksFromDtosSpy).not.toHaveBeenCalled() + }) + test('should find or create tracks from external ids', async () => { findOrCreateTracksFromExternalIdsSpy.mockResolvedValue(trackEntitiesMock) @@ -326,6 +334,14 @@ describe('TracksService', () => { findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate') }) + test('should return empty array if no external ids', async () => { + expect( + await tracksService.findOrCreateTracksFromExternalIds([], []) + ).toEqual([]) + expect(findTrackByExternalIdSpy).not.toHaveBeenCalled() + expect(findOrCreateAlbum).not.toHaveBeenCalled() + }) + test('should find tracks by external ids', async () => { findTrackByExternalIdSpy.mockResolvedValue(foundTracks) @@ -404,6 +420,12 @@ describe('TracksService', () => { findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate') }) + test('should return empty array if no tracks', async () => { + expect(await tracksService.findOrCreateTracksFromDtos([])).toEqual([]) + expect(findTrackByExternalIdSpy).not.toHaveBeenCalled() + expect(findOrCreateAlbum).not.toHaveBeenCalled() + }) + test('should find tracks by external ids', async () => { findTrackByExternalIdSpy.mockResolvedValue(foundTracks) diff --git a/src/modules/tracks/tracks.service.ts b/src/modules/tracks/tracks.service.ts index 2c89f243..341f23a1 100644 --- a/src/modules/tracks/tracks.service.ts +++ b/src/modules/tracks/tracks.service.ts @@ -132,13 +132,16 @@ export class TracksService implements ItemService { if ('id' in data) return this.findOrCreateTrackFromDto(data) - if (Array.isArray(data) && data.length > 0) + if (Array.isArray(data)) { + if (data.length === 0) return [] + return typeof data[0] === 'string' ? this.findOrCreateTracksFromExternalIds( data as string[], idOrIds as string[] ) : this.findOrCreateTracksFromDtos(data as SdkCreateTrack[]) + } } async findOrCreateTrackFromExternalId( @@ -158,6 +161,8 @@ export class TracksService implements ItemService { externalIds: string[], albumsExternalIds: string[] ): Promise { + if (externalIds.length === 0 || albumsExternalIds.length === 0) return [] + const filteredExternalIds = removeDuplicates(externalIds) const foundTracks = @@ -189,7 +194,9 @@ export class TracksService implements ItemService { return this.findOrCreateTrackFromDto(track) } - async findOrCreateTracksFromDtos(tracks: SdkCreateTrack[]) { + async findOrCreateTracksFromDtos(tracks: SdkCreateTrack[]): Promise { + if (tracks.length === 0) return [] + const externalIds = tracks.map(track => track.id) const foundTracks =