From e04f2d01a4fc9c0ed09bf22c73c2d67b5fb51a40 Mon Sep 17 00:00:00 2001 From: iPromKnight Date: Fri, 9 Feb 2024 16:18:00 +0000 Subject: [PATCH] Start of some file service tests. final service that needed to be tested We need a lot more tests in this - its the main processor for the consumer really --- .../src/lib/services/torrent_file_service.ts | 8 +- .../test/torrent_file_service.test.ts | 216 ++++++++++++++++++ .../test/torrent_subtitle_service.test.ts | 2 +- 3 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 src/node/consumer/test/torrent_file_service.test.ts diff --git a/src/node/consumer/src/lib/services/torrent_file_service.ts b/src/node/consumer/src/lib/services/torrent_file_service.ts index 5d40160..f8ab06d 100644 --- a/src/node/consumer/src/lib/services/torrent_file_service.ts +++ b/src/node/consumer/src/lib/services/torrent_file_service.ts @@ -46,6 +46,10 @@ export class TorrentFileService implements ITorrentFileService { return Promise.reject(new Error('Torrent title is missing')); } + if (!torrent.infoHash) { + return Promise.reject(new Error('Torrent infoHash is missing')); + } + const parsedTorrentName = parse(torrent.title); const query: IMetaDataQuery = { id: torrent.kitsuId || torrent.imdbId, @@ -115,7 +119,7 @@ export class TorrentFileService implements ITorrentFileService { const parsedVideos = filteredVideos.map(video => ({ infoHash: torrent.infoHash, fileIndex: video.fileIndex, - title: video.path || video.title || video.fileName || '', + title: video.title || video.path || video.fileName || '', size: video.size || torrent.size, imdbId: torrent.imdbId?.toString() || metadata && metadata.imdbId?.toString(), kitsuId: parseInt(torrent.kitsuId?.toString() || metadata && metadata.kitsuId?.toString() || '0') @@ -129,7 +133,7 @@ export class TorrentFileService implements ITorrentFileService { .then(videos => videos.map((video: IFileAttributes) => ({ infoHash: torrent.infoHash, fileIndex: video.fileIndex, - title: video.path || video.title, + title: video.title || video.path, size: video.size, imdbId: video.imdbId, }))); diff --git a/src/node/consumer/test/torrent_file_service.test.ts b/src/node/consumer/test/torrent_file_service.test.ts new file mode 100644 index 0000000..292e079 --- /dev/null +++ b/src/node/consumer/test/torrent_file_service.test.ts @@ -0,0 +1,216 @@ +import "reflect-metadata"; // required +import {TorrentType} from "@enums/torrent_types"; +import {ILoggingService} from "@interfaces/logging_service"; +import {IMetadataService} from "@interfaces/metadata_service"; +import {IParsedTorrent} from "@interfaces/parsed_torrent"; +import {ITorrentDownloadService} from "@interfaces/torrent_download_service"; +import {TorrentFileService} from "@services/torrent_file_service"; + +jest.mock('@services/logging_service', () => { + return { + error: jest.fn(), + info: jest.fn(), + debug: jest.fn(), + } +}) + +jest.mock('@services/torrent_download_service', () => { + return { + getTorrentFiles: jest.fn().mockImplementation(() => Promise.resolve({ + contents: [], + videos: [], + subtitles: [], + })), + }; +}); + +jest.mock('@services/metadata_service', () => { + return { + getMetadata: jest.fn().mockImplementation(() => Promise.resolve(undefined)), + } +}); + +describe('TorrentFileService tests', () => { + let torrentFileService: TorrentFileService, + mockLoggingService: ILoggingService, + mockDownloadService: ITorrentDownloadService, + mockMetadataService: IMetadataService; + + beforeEach(() => { + jest.clearAllMocks(); + mockLoggingService = jest.requireMock('@services/logging_service'); + mockDownloadService = jest.requireMock('@services/torrent_download_service'); + mockMetadataService = jest.requireMock('@services/metadata_service'); + torrentFileService = new TorrentFileService(mockMetadataService, mockDownloadService, mockLoggingService); + }); + + + it('should parse torrent files correctly', () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Movie, + infoHash: '1234567890abcdef', + }; + + const result = torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toBeInstanceOf(Promise); + + result.then(res => { + expect(res).toHaveProperty('videos'); + expect(res).toHaveProperty('subtitles'); + expect(res).toHaveProperty('contents'); + }); + }); + + it('should reject when torrent has no title', async () => { + const mockTorrent: IParsedTorrent = { + kitsuId: 123, + type: TorrentType.Movie, + infoHash: '1234567890abcdef', + }; + + await expect(torrentFileService.parseTorrentFiles(mockTorrent)).rejects.toThrow('Torrent title is missing'); + }); + + it('should handle torrent with no kitsuId', async () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + type: TorrentType.Movie, + infoHash: '1234567890abcdef', + }; + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); + + it('should handle torrent of type Series', async () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Series, + infoHash: '1234567890abcdef', + }; + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); + + it('should reject when torrent has no infoHash', async () => { + const mockTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Movie, + } as IParsedTorrent; + + await expect(torrentFileService.parseTorrentFiles(mockTorrent)).rejects.toThrow('Torrent infoHash is missing'); + }); + + it('should handle torrent with no type', async () => { + const mockTorrent = { + title: 'test', + kitsuId: 123, + infoHash: '1234567890abcdef', + } as IParsedTorrent; + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); + + it('should handle torrent of type Anime', async () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Anime, + infoHash: '1234567890abcdef', + }; + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); + + it('should handle torrent with a single video', async () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Movie, + infoHash: '1234567890abcdef', + }; + + (mockDownloadService.getTorrentFiles as jest.Mock).mockImplementation(() => Promise.resolve({ + contents: [], + videos: [{ + title: 'video1', + path: 'path/to/video1', + size: 123456789, + fileIndex: 0, + }], + subtitles: [], + })); + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result.videos).toHaveLength(1); + expect(result.videos[0]).toHaveProperty('title', 'video1'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); + + it('should handle torrent with multiple videos', async () => { + const mockTorrent: IParsedTorrent = { + title: 'test', + kitsuId: 123, + type: TorrentType.Movie, + infoHash: '1234567890abcdef', + }; + + (mockDownloadService.getTorrentFiles as jest.Mock).mockImplementation(() => Promise.resolve({ + contents: [], + videos: [ + { + title: 'video1', + path: 'path/to/video1', + size: 123456789, + fileIndex: 0, + }, + { + title: 'video2', + path: 'path/to/video2', + size: 123456789, + fileIndex: 1, + }, + { + title: 'video3', + path: 'path/to/video3', + size: 123456789, + fileIndex: 2, + } + ], + subtitles: [], + })); + + const result = await torrentFileService.parseTorrentFiles(mockTorrent); + + expect(result).toHaveProperty('videos'); + expect(result.videos).toHaveLength(3); + expect(result.videos[0]).toHaveProperty('title', 'video1'); + expect(result.videos[1]).toHaveProperty('title', 'video2'); + expect(result.videos[2]).toHaveProperty('title', 'video3'); + expect(result).toHaveProperty('subtitles'); + expect(result).toHaveProperty('contents'); + }); +}); \ No newline at end of file diff --git a/src/node/consumer/test/torrent_subtitle_service.test.ts b/src/node/consumer/test/torrent_subtitle_service.test.ts index d4c097d..5eff584 100644 --- a/src/node/consumer/test/torrent_subtitle_service.test.ts +++ b/src/node/consumer/test/torrent_subtitle_service.test.ts @@ -2,7 +2,7 @@ import "reflect-metadata"; // required import {ITorrentFileCollection} from "@interfaces/torrent_file_collection"; import {TorrentSubtitleService} from "@services/torrent_subtitle_service"; -describe('TrrentSubtitleService tests', () => { +describe('TorrentSubtitleService tests', () => { let torrentSubtitleService: TorrentSubtitleService; beforeEach(() => {