fix magnet uri by download, and add torrent download service test

This commit is contained in:
iPromKnight
2024-02-08 15:10:21 +00:00
committed by iPromKnight
parent 373ca4e024
commit 8bba2aa284
3 changed files with 157 additions and 31 deletions

View File

@@ -21,7 +21,7 @@
"cache-manager": "^5.4.0",
"google-sr": "^3.2.1",
"inversify": "^6.0.2",
"magnet-uri": "^7.0.5",
"magnet-uri": "^6.2.0",
"moment": "^2.30.1",
"name-to-imdb": "^3.0.4",
"parse-torrent-title": "https://github.com/TheBeastLT/parse-torrent-title.git#022408972c2a040f846331a912a6a8487746a654",

View File

@@ -0,0 +1,141 @@
import "reflect-metadata"; // required
import { ILoggingService } from '@interfaces/logging_service';
import {IParsedTorrent} from "@interfaces/parsed_torrent";
import { TorrentDownloadService } from '@services/torrent_download_service';
import torrentStream from 'torrent-stream';
jest.mock('@services/logging_service', () => {
return {
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
}
})
jest.mock('torrent-stream', () => {
return jest.fn().mockImplementation(() => ({
on: jest.fn(),
files: [],
destroy: jest.fn(),
}));
});
describe('TorrentDownloadService', () => {
let torrentDownloadService: TorrentDownloadService,
mockLoggingService: ILoggingService;
beforeEach(() => {
mockLoggingService = jest.requireMock<ILoggingService>('@services/logging_service');
torrentDownloadService = new TorrentDownloadService(mockLoggingService);
});
it('should get torrent files', async () => {
const mockTorrent: IParsedTorrent = {
size: 123456789,
isPack: false,
imdbId: 'tt1234567',
kitsuId: 1234,
trackers: 'http://tracker1.com,http://tracker2.com',
provider: 'provider1',
infoHash: '1234567890abcdef',
type: 'movie',
uploadDate: new Date(),
seeders: 100,
torrentId: 'torrent1',
fileCollection: { },
title: 'Test Movie',
year: 2020,
season: 1,
episode: 1,
resolution: '1080p',
codec: 'H.264',
audio: 'AAC',
group: 'GRP',
extended: false,
hardcoded: false,
proper: false,
repack: false,
container: 'mp4',
unrated: false,
};
const mockFiles = [
{
name: 'file1.mp4',
path: '/path/to/file1.mp4',
length: 123456789,
fileIndex: 0,
select: jest.fn(),
deselect: jest.fn(),
createReadStream: jest.fn(),
},
{
name: 'file2.srt',
path: '/path/to/file2.srt',
length: 987654321,
fileIndex: 1,
select: jest.fn(),
deselect: jest.fn(),
createReadStream: jest.fn(),
},
];
const mockEngine = {
on: jest.fn((event, callback) => {
if (event === 'ready') {
callback();
}
}),
files: mockFiles,
destroy: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
block: jest.fn(),
remove: jest.fn(),
listen: jest.fn(),
swarm: {
downloaded: 2,
},
infoHash: 'mockInfoHash',
};
(torrentStream as jest.MockedFunction<typeof torrentStream>).mockReturnValue(mockEngine);
const result = await torrentDownloadService.getTorrentFiles(mockTorrent);
expect(result).toEqual({
contents: mockFiles.map(file => ({
path: file.path,
fileIndex: file.fileIndex,
infoHash: mockTorrent.infoHash,
size: file.length
})),
videos: mockFiles.filter(file => file.name.endsWith('.mp4')).map(file => ({
fileIndex: file.fileIndex,
infoHash: mockTorrent.infoHash,
path: file.path,
size: file.length,
title: file.name.split('.')[0],
extension: file.name.split('.')[1],
container: file.name.split('.')[1],
imdbId: mockTorrent.imdbId,
imdbSeason: mockTorrent.season,
imdbEpisode: mockTorrent.episode,
kitsuId: mockTorrent.kitsuId,
kitsuEpisode: mockTorrent.episode,
})),
subtitles: mockFiles.filter(file => file.name.endsWith('.srt')).map(file => ({
fileIndex: file.fileIndex,
infoHash: mockTorrent.infoHash,
path: file.path,
title: file.name,
fileId: file.fileIndex,
})),
});
expect(torrentStream).toHaveBeenCalledWith(expect.any(String), expect.any(Object));
expect(mockLoggingService.debug).toHaveBeenCalledWith(`Adding torrent with infoHash ${mockTorrent.infoHash} to torrent engine...`);
});
});

View File

@@ -978,13 +978,6 @@
dependencies:
"@sinonjs/commons" "^3.0.0"
"@thaunknown/thirty-two@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@thaunknown/thirty-two/-/thirty-two-1.0.3.tgz#64883031c4444c0fd75436219189ab5816b5b826"
integrity sha512-bD6PvWbaf53JC04O7WnGDjqZBDgja/KT2Jd/6I2vJBIy+DLmQfQJZZ/G+16nAkVq1yGTIkO4rfc4RlH0DmEEqA==
dependencies:
uint8-util "^2.1.9"
"@tirke/node-cache-manager-mongodb@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@tirke/node-cache-manager-mongodb/-/node-cache-manager-mongodb-1.6.0.tgz#e730623ae53d4b3e6c6328baab0cff49ed6cb4ce"
@@ -1580,11 +1573,6 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
base64-arraybuffer@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz#1c37589a7c4b0746e34bd1feb951da2df01c1bdc"
integrity sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==
base64-js@^1.3.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
@@ -1605,10 +1593,10 @@ bencode@^2.0.0:
resolved "https://registry.yarnpkg.com/bencode/-/bencode-2.0.3.tgz#89b9c80ea1b8573554915a7d0c15f62b0aa7fc52"
integrity sha512-D/vrAD4dLVX23NalHwb8dSvsUsxeRPO8Y7ToKA015JQYq69MLDOMkC0uGZYA/MPpltLO8rt8eqFC2j8DxjTZ/w==
bep53-range@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/bep53-range/-/bep53-range-2.0.0.tgz#a1770475661b4b814c4359e4b66f7cbd88de2b10"
integrity sha512-sMm2sV5PRs0YOVk0LTKtjuIprVzxgTQUsrGX/7Yph2Rm4FO2Fqqtq7hNjsOB5xezM4v4+5rljCgK++UeQJZguA==
bep53-range@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/bep53-range/-/bep53-range-1.1.1.tgz#20fd125b00a413254a77d42f63a43750ca7e64ac"
integrity sha512-ct6s33iiwRCUPp9KXnJ4QMWDgHIgaw36caK/5XEQ9L8dCzSQlJt1Vk6VmHh1VD4AlGCAI4C2zmtfItifBBPrhQ==
binary-extensions@^2.0.0:
version "2.2.0"
@@ -4118,14 +4106,13 @@ magnet-uri@^4.0.0:
thirty-two "^0.0.2"
xtend "^4.0.0"
magnet-uri@^7.0.5:
version "7.0.5"
resolved "https://registry.yarnpkg.com/magnet-uri/-/magnet-uri-7.0.5.tgz#7b5143fd5527f3f612959eeeae264d6f4aeff37b"
integrity sha512-Ke+dDiYHK1Rq/ZyGUAgk7NIkoypivxolTj/A0qr60ypP0FjeP+NTUNEhr965HsRan0zGxKEBK73+SsjRyJWkXg==
magnet-uri@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/magnet-uri/-/magnet-uri-6.2.0.tgz#10f7be050bf23452df210838239b118463c3eeff"
integrity sha512-O9AgdDwT771fnUj0giPYu/rACpz8173y8UXCSOdLITjOVfBenZ9H9q3FqQmveK+ORUMuD+BkKNSZP8C3+IMAKQ==
dependencies:
"@thaunknown/thirty-two" "^1.0.3"
bep53-range "^2.0.0"
uint8-util "^2.1.9"
bep53-range "^1.1.0"
thirty-two "^1.0.2"
make-dir@^4.0.0:
version "4.0.0"
@@ -5744,6 +5731,11 @@ thirty-two@^0.0.2:
resolved "https://registry.yarnpkg.com/thirty-two/-/thirty-two-0.0.2.tgz#4253e29d8cb058f0480267c5698c0e4927e54b6a"
integrity sha512-0j1A9eqbP8dSEtkqqEJGpYFN2lPgQR1d0qKS2KNAmIxkK6gV37D5hRa5b/mYzVL1fyAVWBkeUDIXybZdCLVBzA==
thirty-two@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/thirty-two/-/thirty-two-1.0.2.tgz#4ca2fffc02a51290d2744b9e3f557693ca6b627a"
integrity sha512-OEI0IWCe+Dw46019YLl6V10Us5bi574EvlJEOcAkB29IzQ/mYD1A6RyNHLjZPiHCmuodxvgF6U+vZO1L15lxVA==
thread-stream@^2.0.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/thread-stream/-/thread-stream-2.4.1.tgz#6d588b14f0546e59d3f306614f044bc01ce43351"
@@ -5983,13 +5975,6 @@ typescript@^5.3.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==
uint8-util@^2.1.9:
version "2.2.4"
resolved "https://registry.yarnpkg.com/uint8-util/-/uint8-util-2.2.4.tgz#4df57c7b25b821d02b1599e80f0f1f5f56bc90e1"
integrity sha512-uEI5lLozmKQPYEevfEhP9LY3Je5ZmrQhaWXrzTVqrLNQl36xsRh8NiAxYwB9J+2BAt99TRbmCkROQB2ZKhx4UA==
dependencies:
base64-arraybuffer "^1.0.2"
ultron@1.0.x:
version "1.0.2"
resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.0.2.tgz#ace116ab557cd197386a4e88f4685378c8b2e4fa"