Reuse stable waiting tracked downloads

This commit is contained in:
Mika Cohen 2026-04-22 16:16:36 -06:00
parent 9226876792
commit 4d74ee73bd
No known key found for this signature in database
GPG key ID: D7415E8B2E80D893
2 changed files with 161 additions and 1 deletions

View file

@ -40,6 +40,140 @@ private void GivenDownloadHistory()
});
}
private static DownloadClientDefinition CreateDownloadClient()
{
return new DownloadClientDefinition()
{
Id = 1,
Protocol = DownloadProtocol.Usenet
};
}
private static DownloadClientItem CreateDownloadItem(DownloadItemStatus status)
{
return new DownloadClientItem()
{
Title = "A Movie 1998",
DownloadId = "35238",
Category = "radarr",
TotalSize = 1000,
RemainingSize = 500,
Status = status,
DownloadClientInfo = new DownloadClientItemClientInfo
{
Id = 1,
Type = "NZBGet",
Name = "NZBGet",
Protocol = DownloadProtocol.Usenet
}
};
}
private void GivenTrackedDownloadCanBeMapped()
{
Mocker.GetMock<IHistoryService>()
.Setup(s => s.FindByDownloadId(It.IsAny<string>()))
.Returns(new List<MovieHistory>());
Mocker.GetMock<IParsingService>()
.Setup(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<int>(), null))
.Returns(new RemoteMovie
{
Release = new ReleaseInfo { Title = "A Movie 1998" },
Movie = new Movie() { Id = 3 },
ParsedMovieInfo = new ParsedMovieInfo()
{
MovieTitles = new List<string> { "A Movie" },
Year = 1998
}
});
}
[Test]
public void should_reuse_stable_queued_downloading_tracked_download()
{
GivenTrackedDownloadCanBeMapped();
var client = CreateDownloadClient();
var item = CreateDownloadItem(DownloadItemStatus.Queued);
var updatedItem = CreateDownloadItem(DownloadItemStatus.Queued);
updatedItem.RemainingSize = 250;
var trackedDownload = Subject.TrackDownload(client, item);
var refreshedTrackedDownload = Subject.TrackDownload(client, updatedItem);
refreshedTrackedDownload.Should().BeSameAs(trackedDownload);
refreshedTrackedDownload.DownloadItem.Should().BeSameAs(updatedItem);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Once());
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<int>(), null), Times.Once());
}
[Test]
public void should_reuse_stable_paused_downloading_tracked_download()
{
GivenTrackedDownloadCanBeMapped();
var client = CreateDownloadClient();
var item = CreateDownloadItem(DownloadItemStatus.Paused);
var updatedItem = CreateDownloadItem(DownloadItemStatus.Paused);
updatedItem.RemainingSize = 250;
var trackedDownload = Subject.TrackDownload(client, item);
var refreshedTrackedDownload = Subject.TrackDownload(client, updatedItem);
refreshedTrackedDownload.Should().BeSameAs(trackedDownload);
refreshedTrackedDownload.DownloadItem.Should().BeSameAs(updatedItem);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Once());
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<int>(), null), Times.Once());
}
[Test]
public void should_reprocess_when_waiting_download_starts_downloading()
{
GivenTrackedDownloadCanBeMapped();
var client = CreateDownloadClient();
var item = CreateDownloadItem(DownloadItemStatus.Queued);
var updatedItem = CreateDownloadItem(DownloadItemStatus.Downloading);
Subject.TrackDownload(client, item);
Subject.TrackDownload(client, updatedItem);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Exactly(2));
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<int>(), null), Times.Exactly(2));
}
[Test]
public void should_reprocess_when_waiting_download_identity_changes()
{
GivenTrackedDownloadCanBeMapped();
var client = CreateDownloadClient();
var item = CreateDownloadItem(DownloadItemStatus.Queued);
var updatedItem = CreateDownloadItem(DownloadItemStatus.Queued);
updatedItem.TotalSize = 2000;
Subject.TrackDownload(client, item);
Subject.TrackDownload(client, updatedItem);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Exactly(2));
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), It.IsAny<int>(), null), Times.Exactly(2));
}
[Test]
public void should_track_downloads_using_the_source_title_if_it_cannot_be_found_using_the_download_title()
{

View file

@ -96,7 +96,7 @@ public TrackedDownload TrackDownload(DownloadClientDefinition downloadClient, Do
{
var existingItem = Find(downloadItem.DownloadId);
if (existingItem != null && existingItem.State != TrackedDownloadState.Downloading)
if (existingItem != null && CanReuseTrackedDownload(existingItem, downloadItem))
{
LogItemChange(existingItem, existingItem.DownloadItem, downloadItem);
@ -221,6 +221,32 @@ public void UpdateTrackable(List<TrackedDownload> trackedDownloads)
}
}
private static bool CanReuseTrackedDownload(TrackedDownload existingItem, DownloadClientItem downloadItem)
{
if (existingItem.State != TrackedDownloadState.Downloading)
{
return true;
}
return IsStableWaitingDownload(downloadItem) &&
HasSameDownloadIdentity(existingItem.DownloadItem, downloadItem);
}
private static bool IsStableWaitingDownload(DownloadClientItem downloadItem)
{
return downloadItem.Status == DownloadItemStatus.Queued ||
downloadItem.Status == DownloadItemStatus.Paused;
}
private static bool HasSameDownloadIdentity(DownloadClientItem existingItem, DownloadClientItem downloadItem)
{
return existingItem.DownloadId == downloadItem.DownloadId &&
existingItem.Title == downloadItem.Title &&
existingItem.Category == downloadItem.Category &&
existingItem.TotalSize == downloadItem.TotalSize &&
existingItem.DownloadClientInfo?.Id == downloadItem.DownloadClientInfo?.Id;
}
private void UpdateCachedItem(TrackedDownload trackedDownload)
{
var parsedMovieInfo = Parser.Parser.ParseMovieTitle(trackedDownload.DownloadItem.Title);