From b55a66dabc5f07428d44ce9e92b246bc3ba2e221 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Tue, 27 Oct 2026 10:40:53 +0200 Subject: [PATCH] added lidarr cf sync coverage tests --- .../Jobs/CustomFormatScoreSyncerTests.cs | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/code/backend/Cleanuparr.Infrastructure.Tests/Features/Jobs/CustomFormatScoreSyncerTests.cs b/code/backend/Cleanuparr.Infrastructure.Tests/Features/Jobs/CustomFormatScoreSyncerTests.cs index bb756918..67681ac9 100644 --- a/code/backend/Cleanuparr.Infrastructure.Tests/Features/Jobs/CustomFormatScoreSyncerTests.cs +++ b/code/backend/Cleanuparr.Infrastructure.Tests/Features/Jobs/CustomFormatScoreSyncerTests.cs @@ -1270,6 +1270,95 @@ public class CustomFormatScoreSyncerTests : IDisposable (await _fixture.EventsContext.CustomFormatScoreHistory.ToListAsync()).ShouldHaveSingleItem().EpisodeId.ShouldBe(50); } + [Fact] + public async Task ExecuteAsync_LidarrSync_FlushesFullArtistChunks() + { + // Arrange + ArrInstance lidarrInstance = await AddLidarrWithSyncAsync(); + List artists = []; + for (int i = 1; i <= ChunkSize + 1; i++) + { + artists.Add(new SearchableArtist { Id = i, ArtistName = $"Artist {i}", Monitored = true, QualityProfileId = 1 }); + } + + _lidarrClient + .StreamAllArtistsAsync(lidarrInstance, Arg.Any()) + .Returns(_ => ToAsyncEnumerable(artists)); + _lidarrClient + .GetAlbumsAsync(lidarrInstance, Arg.Any(), Arg.Any()) + .Returns(callInfo => Task.FromResult(new List { LidarrAlbum(callInfo.Arg() * 10, trackFileCount: 1, trackCount: 1) })); + _lidarrClient + .GetTrackFilesAsync(lidarrInstance, Arg.Any(), Arg.Any()) + .Returns(callInfo => Task.FromResult(new List { new() { Id = callInfo.Arg() * 100, AlbumId = callInfo.Arg() * 10, CustomFormatScore = 120 } })); + + CustomFormatScoreSyncer sut = CreateSut(); + + // Act + await sut.ExecuteAsync(); + + // Assert + (await _fixture.EventsContext.CustomFormatScoreEntries.CountAsync()).ShouldBe(ChunkSize + 1); + } + + [Fact] + public async Task ExecuteAsync_LidarrCompleteAlbumWithoutTrackFile_KeepsEntryAndSkips() + { + // Arrange + ArrInstance lidarrInstance = await AddLidarrWithSyncAsync(); + DateTimeOffset oldSync = new(1999, 1, 1, 0, 0, 0, TimeSpan.Zero); + _fixture.EventsContext.CustomFormatScoreEntries.Add(new CustomFormatScoreEntry + { + ArrInstanceId = lidarrInstance.Id, + ExternalItemId = 5, + EpisodeId = 50, + ItemType = InstanceType.Lidarr, + Title = "Test Artist - Album 50", + FileId = 901, + CurrentScore = 120, + CutoffScore = 300, + QualityProfileName = "Lossless", + LastSyncedAt = oldSync + }); + await _fixture.EventsContext.SaveChangesAsync(); + StubLidarrLibrary( + lidarrInstance, + LidarrArtist(), + [LidarrAlbum(50, trackFileCount: 3, trackCount: 3)], + []); + + CustomFormatScoreSyncer sut = CreateSut(); + + // Act + await sut.ExecuteAsync(); + + // Assert + CustomFormatScoreEntry entry = (await _fixture.EventsContext.CustomFormatScoreEntries.AsNoTracking().ToListAsync()).ShouldHaveSingleItem(); + entry.CurrentScore.ShouldBe(120); + entry.LastSyncedAt.ShouldBeGreaterThan(oldSync); + } + + [Fact] + public async Task ExecuteAsync_LidarrArtistProfileMissing_UsesUnknownProfile() + { + // Arrange + ArrInstance lidarrInstance = await AddLidarrWithSyncAsync(); + StubLidarrLibrary( + lidarrInstance, + new SearchableArtist { Id = 5, ArtistName = "Test Artist", Monitored = true, QualityProfileId = 99 }, + [LidarrAlbum(50, trackFileCount: 1, trackCount: 1)], + [new ArrTrackFile { Id = 901, AlbumId = 50, CustomFormatScore = 120 }]); + + CustomFormatScoreSyncer sut = CreateSut(); + + // Act + await sut.ExecuteAsync(); + + // Assert + CustomFormatScoreEntry entry = (await _fixture.EventsContext.CustomFormatScoreEntries.ToListAsync()).ShouldHaveSingleItem(); + entry.CutoffScore.ShouldBe(0); + entry.QualityProfileName.ShouldBe("Unknown"); + } + #endregion #region Score Unchanged Tests