From 6642f96d8ffc9a183bb2c48d6d277a812c762bd2 Mon Sep 17 00:00:00 2001 From: Amelia <77553571+Fesaa@users.noreply.github.com> Date: Mon, 16 Jun 2025 00:05:05 +0200 Subject: [PATCH] Some more unit tests, add access token errors to scrobble errors table to stop try-loop --- API.Tests/Services/ScrobblingServiceTests.cs | 26 +++++++++++++++++--- API/Services/Plus/ScrobblingService.cs | 9 +++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/API.Tests/Services/ScrobblingServiceTests.cs b/API.Tests/Services/ScrobblingServiceTests.cs index 470db0d92..ea1f9e870 100644 --- a/API.Tests/Services/ScrobblingServiceTests.cs +++ b/API.Tests/Services/ScrobblingServiceTests.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using API.Data.Repositories; using API.DTOs.Scrobbling; using API.Entities; using API.Entities.Enums; @@ -100,6 +101,18 @@ public class ScrobblingServiceTests : AbstractDbTest .WithPages(ChapterPages) .Build()]) .Build()) + .WithVolume(new VolumeBuilder("Volume 2") + .WithChapters([ + new ChapterBuilder("4") + .WithPages(ChapterPages) + .Build(), + new ChapterBuilder("5") + .WithPages(ChapterPages) + .Build(), + new ChapterBuilder("6") + .WithPages(ChapterPages) + .Build()]) + .Build()) .Build(); var library = new LibraryBuilder("Test Library", LibraryType.Manga) @@ -231,21 +244,28 @@ public class ScrobblingServiceTests : AbstractDbTest UnitOfWork.UserRepository.Update(user); await UnitOfWork.CommitAsync(); - var chapter = await UnitOfWork.ChapterRepository.GetChapterAsync(1); + var chapter = await UnitOfWork.ChapterRepository.GetChapterAsync(4); Assert.NotNull(chapter); + var volume = await UnitOfWork.VolumeRepository.GetVolumeAsync(1, VolumeIncludes.Chapters); + Assert.NotNull(volume); + await _service.ScrobbleReadingUpdate(1, 1); var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1); Assert.Single(events); - // Give it some read progress + // Give it some (more) read progress + await _readerService.MarkChaptersAsRead(user, 1, volume.Chapters); await _readerService.MarkChaptersAsRead(user, 1, [chapter]); await UnitOfWork.CommitAsync(); await _service.ProcessUpdatesSinceLastSync(); await _kavitaPlusApiService.Received(1).PostScrobbleUpdate( - Arg.Is(data => data.ChapterNumber == (int)chapter.MaxNumber), + Arg.Is(data => + data.ChapterNumber == (int)chapter.MaxNumber && + data.VolumeNumber == (int)volume.MaxNumber + ), Arg.Any()); } diff --git a/API/Services/Plus/ScrobblingService.cs b/API/Services/Plus/ScrobblingService.cs index b9a4546a2..604364f64 100644 --- a/API/Services/Plus/ScrobblingService.cs +++ b/API/Services/Plus/ScrobblingService.cs @@ -1065,6 +1065,15 @@ public class ScrobblingService : IScrobblingService _logger.LogCritical(ex, "Access Token for AppUserId: {AppUserId} needs to be regenerated/renewed to continue scrobbling", evt.AppUser.Id); evt.SetErrorMessage(AccessTokenErrorMessage); _unitOfWork.ScrobbleRepository.Update(evt); + + // Ensure series with this error do not get re-processed next sync + _unitOfWork.ScrobbleRepository.Attach(new ScrobbleError + { + Comment = AccessTokenErrorMessage, + Details = $"{evt.AppUser.UserName} has an invalid access token (K+ Error)", + LibraryId = evt.LibraryId, + SeriesId = evt.SeriesId, + }); } } catch (Exception ex)