using System.IO.Abstractions.TestingHelpers; using System.Text; using Moq; using RdtClient.Data.Enums; using RdtClient.Data.Models.Data; using TestSettings = RdtClient.Service.Services.TestSettings; using TorrentRunnerState = RdtClient.Service.Services.TorrentRunnerState; using TorrentsService = RdtClient.Service.Services.Torrents; namespace RdtClient.Service.Test.Services; public class NzbTorrentsTest { private readonly MockFileSystem _fileSystem; private readonly Mocks _mocks; private readonly TorrentsService _torrents; public NzbTorrentsTest() { _mocks = new(); _fileSystem = new(); _torrents = new(_mocks.TorrentsLoggerMock.Object, _mocks.TorrentDataMock.Object, _mocks.DownloadsMock.Object, _mocks.ProcessFactoryMock.Object, _fileSystem, _mocks.EnricherMock.Object, null!, null!, null!, null!, null!, new TestSettings(), new TorrentRunnerState()); } [Fact] public async Task AddNzbLinkToDebridQueue_ValidLink_AddsToQueue() { // Arrange var nzbLink = "http://example.com/test.nzb"; var torrent = new Torrent { DownloadClient = DownloadClient.Bezzad }; _mocks.TorrentDataMock.Setup(t => t.GetByHash(It.IsAny())).ReturnsAsync((Torrent)null!); _mocks.TorrentDataMock.Setup(t => t.Add(null, It.IsAny(), nzbLink, false, DownloadType.Nzb, torrent.DownloadClient, It.IsAny())) .ReturnsAsync(new Torrent { Hash = "mockHash", RdName = "test.nzb" }); // Act var result = await _torrents.AddNzbLinkToDebridQueue(nzbLink, torrent); // Assert Assert.NotNull(result); Assert.Equal("test.nzb", torrent.RdName); Assert.Equal(TorrentStatus.Queued, torrent.RdStatus); _mocks.TorrentDataMock.Verify(t => t.Add(null, It.IsAny(), nzbLink, false, DownloadType.Nzb, torrent.DownloadClient, torrent), Times.Once); } [Fact] public async Task AddNzbLinkToDebridQueue_InvalidLink_ThrowsException() { // Arrange var nzbLink = "invalid-link"; var torrent = new Torrent(); // Act & Assert await Assert.ThrowsAsync(() => _torrents.AddNzbLinkToDebridQueue(nzbLink, torrent)); } [Fact] public async Task AddNzbFileToDebridQueue_ValidFile_AddsToQueue() { // Arrange var nzbContent = "\r\n\r\n \r\n Test NZB Title\r\n \r\n"; var bytes = Encoding.UTF8.GetBytes(nzbContent); var torrent = new Torrent { DownloadClient = DownloadClient.Bezzad }; _mocks.TorrentDataMock.Setup(t => t.GetByHash(It.IsAny())).ReturnsAsync((Torrent)null!); _mocks.TorrentDataMock.Setup(t => t.Add(null, It.IsAny(), It.IsAny(), true, DownloadType.Nzb, torrent.DownloadClient, It.IsAny())) .ReturnsAsync(new Torrent { Hash = "mockHash", RdName = "Test NZB Title" }); // Act var result = await _torrents.AddNzbFileToDebridQueue(bytes, "filename.nzb", torrent); // Assert Assert.NotNull(result); Assert.Equal("Test NZB Title", torrent.RdName); Assert.Equal(TorrentStatus.Queued, torrent.RdStatus); _mocks.TorrentDataMock.Verify(t => t.Add(null, It.IsAny(), Convert.ToBase64String(bytes), true, DownloadType.Nzb, torrent.DownloadClient, torrent), Times.Once); } [Fact] public async Task AddNzbFileToDebridQueue_InvalidXml_ThrowsException() { // Arrange var bytes = Encoding.UTF8.GetBytes("not xml"); var torrent = new Torrent(); // Act & Assert await Assert.ThrowsAsync(() => _torrents.AddNzbFileToDebridQueue(bytes, "filename.nzb", torrent)); } }