Only reuse healthy waiting tracked downloads

This commit is contained in:
Mika Cohen 2026-04-23 12:16:32 -06:00
parent c87c96ff1e
commit 7cfbc88fb8
No known key found for this signature in database
GPG key ID: D7415E8B2E80D893
2 changed files with 69 additions and 1 deletions

View file

@ -175,6 +175,66 @@ public void should_reprocess_when_waiting_download_identity_changes()
.Verify(s => s.Map(It.IsAny<ParsedEpisodeInfo>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), null), Times.Exactly(2));
}
[Test]
public void should_reprocess_when_waiting_download_has_warning_status()
{
GivenTrackedDownloadCanBeMapped();
var client = CreateDownloadClient();
var item = CreateDownloadItem(DownloadItemStatus.Queued);
var updatedItem = CreateDownloadItem(DownloadItemStatus.Queued);
updatedItem.RemainingSize = 250;
var trackedDownload = Subject.TrackDownload(client, item);
trackedDownload.Warn("Temporary warning");
var refreshedTrackedDownload = Subject.TrackDownload(client, updatedItem);
refreshedTrackedDownload.Should().NotBeSameAs(trackedDownload);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Exactly(2));
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedEpisodeInfo>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), null), Times.Exactly(2));
}
[Test]
public void should_reprocess_when_waiting_download_is_not_mapped()
{
Mocker.GetMock<IHistoryService>()
.Setup(s => s.FindByDownloadId(It.IsAny<string>()))
.Returns(new List<EpisodeHistory>());
Mocker.GetMock<IParsingService>()
.Setup(s => s.Map(It.IsAny<ParsedEpisodeInfo>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), null))
.Returns(new RemoteEpisode
{
ParsedEpisodeInfo = new ParsedEpisodeInfo
{
SeriesTitle = "TV Series",
SeasonNumber = 1,
EpisodeNumbers = new[] { 1 }
}
});
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().NotBeSameAs(trackedDownload);
Mocker.GetMock<IHistoryService>()
.Verify(s => s.FindByDownloadId(It.IsAny<string>()), Times.Exactly(2));
Mocker.GetMock<IParsingService>()
.Verify(s => s.Map(It.IsAny<ParsedEpisodeInfo>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), 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

@ -230,7 +230,8 @@ private static bool CanReuseTrackedDownload(TrackedDownload existingItem, Downlo
}
return IsStableWaitingDownload(downloadItem) &&
HasSameDownloadIdentity(existingItem.DownloadItem, downloadItem);
HasSameDownloadIdentity(existingItem.DownloadItem, downloadItem) &&
HasHealthyWaitingCache(existingItem);
}
private static bool IsStableWaitingDownload(DownloadClientItem downloadItem)
@ -239,6 +240,13 @@ private static bool IsStableWaitingDownload(DownloadClientItem downloadItem)
downloadItem.Status == DownloadItemStatus.Paused;
}
private static bool HasHealthyWaitingCache(TrackedDownload existingItem)
{
return existingItem.Status == TrackedDownloadStatus.Ok &&
existingItem.RemoteEpisode?.Series != null &&
existingItem.RemoteEpisode.Episodes?.Any() == true;
}
private static bool HasSameDownloadIdentity(DownloadClientItem existingItem, DownloadClientItem downloadItem)
{
return existingItem.DownloadId == downloadItem.DownloadId &&