Scrobbling Polish and Some Scanner fixes (#3638)

Co-authored-by: Fesaa <77553571+Fesaa@users.noreply.github.com>
This commit is contained in:
Joe Milazzo 2025-03-15 11:13:01 -05:00 committed by GitHub
parent 82e8f7fade
commit f281a63934
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 658 additions and 102 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using API.Helpers;
using Xunit;
@ -33,7 +34,7 @@ public class RateLimiterTests
}
[Fact]
public void AcquireTokens_Refill()
public async Task AcquireTokens_Refill()
{
// Arrange
var limiter = new RateLimiter(2, TimeSpan.FromSeconds(1));
@ -43,14 +44,14 @@ public class RateLimiterTests
limiter.TryAcquire("test_key");
// Wait for refill
System.Threading.Thread.Sleep(1100);
await Task.Delay(1100);
// Assert
Assert.True(limiter.TryAcquire("test_key"));
}
[Fact]
public void AcquireTokens_Refill_WithOff()
public async Task AcquireTokens_Refill_WithOff()
{
// Arrange
var limiter = new RateLimiter(2, TimeSpan.FromSeconds(10), false);
@ -60,7 +61,7 @@ public class RateLimiterTests
limiter.TryAcquire("test_key");
// Wait for refill
System.Threading.Thread.Sleep(2100);
await Task.Delay(2100);
// Assert
Assert.False(limiter.TryAcquire("test_key"));

View file

@ -11,8 +11,22 @@ public class StringHelperTests
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /> <br><br><br /> Every woman wishes for that happily ever after, but when time flies by and you've become a neglected housewife, what's a woman to do?</p>",
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /> Every woman wishes for that happily ever after, but when time flies by and you've become a neglected housewife, what's a woman to do?</p>"
)]
public void Test(string input, string expected)
public void TestSquashBreaklines(string input, string expected)
{
Assert.Equal(expected, StringHelper.SquashBreaklines(input));
}
[Theory]
[InlineData(
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /> (Source: Anime News Network)</p>",
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /></p>"
)]
[InlineData(
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /></p>(Source: Anime News Network)",
"<p>A Perfect Marriage Becomes a Perfect Affair!<br /></p>"
)]
public void TestRemoveSourceInDescription(string input, string expected)
{
Assert.Equal(expected, StringHelper.RemoveSourceInDescription(input));
}
}

View file

@ -451,4 +451,124 @@ public class ParseScannedFilesTests : AbstractDbTest
var changes = res.Count(sc => sc.HasChanged);
Assert.Equal(1, changes);
}
[Fact]
public async Task SubFoldersNoSubFolders_SkipAll()
{
const string testcase = "Subfolders and files at root - Manga.json";
var infos = new Dictionary<string, ComicInfo>();
var library = await _scannerHelper.GenerateScannerData(testcase, infos);
var testDirectoryPath = library.Folders.First().Path;
_unitOfWork.LibraryRepository.Update(library);
await _unitOfWork.CommitAsync();
var fs = new FileSystem();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fs);
var psf = new ParseScannedFiles(Substitute.For<ILogger<ParseScannedFiles>>(), ds,
new MockReadingItemService(ds, Substitute.For<IBookService>()), Substitute.For<IEventHub>());
var scanner = _scannerHelper.CreateServices(ds, fs);
await scanner.ScanLibrary(library.Id);
var postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
var spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(3, spiceAndWolf.Volumes.Count);
Assert.Equal(4, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
// Needs to be actual time as the write time is now, so if we set LastFolderChecked in the past
// it'll always a scan as it was changed since the last scan.
Thread.Sleep(1100); // Ensure at least one second has passed since library scan
var res = await psf.ScanFiles(testDirectoryPath, true,
await _unitOfWork.SeriesRepository.GetFolderPathMap(postLib.Id), postLib);
Assert.DoesNotContain(res, sc => sc.HasChanged);
}
[Fact]
public async Task SubFoldersNoSubFolders_ScanAllAfterAddInRoot()
{
const string testcase = "Subfolders and files at root - Manga.json";
var infos = new Dictionary<string, ComicInfo>();
var library = await _scannerHelper.GenerateScannerData(testcase, infos);
var testDirectoryPath = library.Folders.First().Path;
_unitOfWork.LibraryRepository.Update(library);
await _unitOfWork.CommitAsync();
var fs = new FileSystem();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fs);
var psf = new ParseScannedFiles(Substitute.For<ILogger<ParseScannedFiles>>(), ds,
new MockReadingItemService(ds, Substitute.For<IBookService>()), Substitute.For<IEventHub>());
var scanner = _scannerHelper.CreateServices(ds, fs);
await scanner.ScanLibrary(library.Id);
var postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
var spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(3, spiceAndWolf.Volumes.Count);
Assert.Equal(4, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
spiceAndWolf.LastFolderScanned = DateTime.Now.Subtract(TimeSpan.FromMinutes(2));
_context.Series.Update(spiceAndWolf);
await _context.SaveChangesAsync();
// Add file at series root
var spiceAndWolfDir = Path.Join(testDirectoryPath, "Spice and Wolf");
File.Copy(Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 1.cbz"),
Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 4.cbz"));
var res = await psf.ScanFiles(testDirectoryPath, true,
await _unitOfWork.SeriesRepository.GetFolderPathMap(postLib.Id), postLib);
var changes = res.Count(sc => sc.HasChanged);
Assert.Equal(2, changes);
}
[Fact]
public async Task SubFoldersNoSubFolders_ScanAllAfterAddInSubFolder()
{
const string testcase = "Subfolders and files at root - Manga.json";
var infos = new Dictionary<string, ComicInfo>();
var library = await _scannerHelper.GenerateScannerData(testcase, infos);
var testDirectoryPath = library.Folders.First().Path;
_unitOfWork.LibraryRepository.Update(library);
await _unitOfWork.CommitAsync();
var fs = new FileSystem();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fs);
var psf = new ParseScannedFiles(Substitute.For<ILogger<ParseScannedFiles>>(), ds,
new MockReadingItemService(ds, Substitute.For<IBookService>()), Substitute.For<IEventHub>());
var scanner = _scannerHelper.CreateServices(ds, fs);
await scanner.ScanLibrary(library.Id);
var postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
var spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(3, spiceAndWolf.Volumes.Count);
Assert.Equal(4, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
spiceAndWolf.LastFolderScanned = DateTime.Now.Subtract(TimeSpan.FromMinutes(2));
_context.Series.Update(spiceAndWolf);
await _context.SaveChangesAsync();
// Add file in subfolder
var spiceAndWolfDir = Path.Join(Path.Join(testDirectoryPath, "Spice and Wolf"), "Spice and Wolf Vol. 3");
File.Copy(Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 3 Ch. 0011.cbz"),
Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 3 Ch. 0013.cbz"));
var res = await psf.ScanFiles(testDirectoryPath, true,
await _unitOfWork.SeriesRepository.GetFolderPathMap(postLib.Id), postLib);
var changes = res.Count(sc => sc.HasChanged);
Assert.Equal(2, changes);
}
}

View file

@ -55,6 +55,28 @@ public class ScannerServiceTests : AbstractDbTest
await _context.SaveChangesAsync();
}
protected async Task SetAllSeriesLastScannedInThePast(Library library, TimeSpan? duration = null)
{
foreach (var series in library.Series)
{
await SetLastScannedInThePast(series, duration, false);
}
await _context.SaveChangesAsync();
}
protected async Task SetLastScannedInThePast(Series series, TimeSpan? duration = null, bool save = true)
{
duration ??= TimeSpan.FromMinutes(2);
series.LastFolderScanned = DateTime.Now.Subtract(duration.Value);
_context.Series.Update(series);
if (save)
{
await _context.SaveChangesAsync();
}
}
[Fact]
public async Task ScanLibrary_ComicVine_PublisherFolder()
{
@ -611,9 +633,7 @@ public class ScannerServiceTests : AbstractDbTest
File.Copy(Path.Join(root1PlushFolder, "Plush v02.cbz"), Path.Join(root1PlushFolder, "Plush v03.cbz"));
// Emulate time passage by updating lastFolderScan to be a min in the past
s.LastFolderScanned = DateTime.Now.Subtract(TimeSpan.FromMinutes(1));
_context.Series.Update(s);
await _context.SaveChangesAsync();
await SetLastScannedInThePast(s);
// Rescan to ensure nothing changes yet again
await scanner.ScanLibrary(library.Id, false);
@ -702,12 +722,7 @@ public class ScannerServiceTests : AbstractDbTest
Assert.Contains(postLib.Series, s => s.Name == "Plush");
// Emulate time passage by updating lastFolderScan to be a min in the past
foreach (var s in postLib.Series)
{
s.LastFolderScanned = DateTime.Now.Subtract(TimeSpan.FromMinutes(1));
_context.Series.Update(s);
}
await _context.SaveChangesAsync();
await SetAllSeriesLastScannedInThePast(postLib);
// Fourth Scan: Run again to check stability (should not remove Accel)
await scanner.ScanLibrary(library.Id);
@ -794,7 +809,7 @@ public class ScannerServiceTests : AbstractDbTest
Assert.Equal(2, executionerAndHerWayOfLife.Volumes.Count);
Assert.Equal(2, executionerAndHerWayOfLife.Volumes.Sum(v => v.Chapters.Count));
Thread.Sleep(1100); // Ensure at least one second has passed since library scan
await SetAllSeriesLastScannedInThePast(postLib);
// Add a new chapter to a volume of the series, and scan. Validate that no chapters were lost, and the new
// chapter was added
@ -822,4 +837,94 @@ public class ScannerServiceTests : AbstractDbTest
Assert.Equal(2, executionerAndHerWayOfLife.Volumes.Count);
Assert.Equal(3, executionerAndHerWayOfLife.Volumes.Sum(v => v.Chapters.Count)); // Incremented by 1
}
[Fact]
public async Task RemovalPickedUp_NoOtherChanges()
{
const string testcase = "Series removed when no other changes are made - Manga.json";
var infos = new Dictionary<string, ComicInfo>();
var library = await _scannerHelper.GenerateScannerData(testcase, infos);
var testDirectoryPath = library.Folders.First().Path;
_unitOfWork.LibraryRepository.Update(library);
await _unitOfWork.CommitAsync();
var scanner = _scannerHelper.CreateServices();
await scanner.ScanLibrary(library.Id);
var postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Equal(2, postLib.Series.Count);
var executionerCopyDir = Path.Join(testDirectoryPath, "The Executioner and Her Way of Life");
Directory.Delete(executionerCopyDir, true);
await scanner.ScanLibrary(library.Id);
await _unitOfWork.CommitAsync();
postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
Assert.Single(postLib.Series, s => s.Name == "Spice and Wolf");
Assert.Equal(2, postLib.Series.First().Volumes.Count);
}
[Fact]
public async Task SubFoldersNoSubFolders_CorrectPickupAfterAdd()
{
// This test case is used in multiple tests and can result in conflict if not separated
const string testcase = "Subfolders and files at root (2) - Manga.json";
var infos = new Dictionary<string, ComicInfo>();
var library = await _scannerHelper.GenerateScannerData(testcase, infos);
var testDirectoryPath = library.Folders.First().Path;
_unitOfWork.LibraryRepository.Update(library);
await _unitOfWork.CommitAsync();
var scanner = _scannerHelper.CreateServices();
await scanner.ScanLibrary(library.Id);
var postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
var spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(3, spiceAndWolf.Volumes.Count);
Assert.Equal(4, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
await SetLastScannedInThePast(spiceAndWolf);
// Add volume to Spice and Wolf series directory
var spiceAndWolfDir = Path.Join(testDirectoryPath, "Spice and Wolf");
File.Copy(Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 1.cbz"),
Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 4.cbz"));
await scanner.ScanLibrary(library.Id);
postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(4, spiceAndWolf.Volumes.Count);
Assert.Equal(5, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
await SetLastScannedInThePast(spiceAndWolf);
// Add file in subfolder
spiceAndWolfDir = Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 3");
File.Copy(Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 3 Ch. 0012.cbz"),
Path.Join(spiceAndWolfDir, "Spice and Wolf Vol. 3 Ch. 0013.cbz"));
await scanner.ScanLibrary(library.Id);
postLib = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(library.Id, LibraryIncludes.Series);
Assert.NotNull(postLib);
Assert.Single(postLib.Series);
spiceAndWolf = postLib.Series.First(x => x.Name == "Spice and Wolf");
Assert.Equal(4, spiceAndWolf.Volumes.Count);
Assert.Equal(6, spiceAndWolf.Volumes.Sum(v => v.Chapters.Count));
}
}

View file

@ -1,11 +1,208 @@
using API.Services.Plus;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs.Scrobbling;
using API.Entities.Enums;
using API.Helpers.Builders;
using API.Services;
using API.Services.Plus;
using API.SignalR;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services;
#nullable enable
public class ScrobblingServiceTests
public class ScrobblingServiceTests : AbstractDbTest
{
private readonly ScrobblingService _service;
private readonly ILicenseService _licenseService;
private readonly ILocalizationService _localizationService;
private readonly ILogger<ScrobblingService> _logger;
private readonly IEmailService _emailService;
public ScrobblingServiceTests()
{
_licenseService = Substitute.For<ILicenseService>();
_localizationService = Substitute.For<ILocalizationService>();
_logger = Substitute.For<ILogger<ScrobblingService>>();
_emailService = Substitute.For<IEmailService>();
_service = new ScrobblingService(_unitOfWork, Substitute.For<IEventHub>(), _logger, _licenseService, _localizationService, _emailService);
}
protected override async Task ResetDb()
{
_context.ScrobbleEvent.RemoveRange(_context.ScrobbleEvent.ToList());
_context.Series.RemoveRange(_context.Series.ToList());
_context.Library.RemoveRange(_context.Library.ToList());
_context.AppUser.RemoveRange(_context.AppUser.ToList());
await _unitOfWork.CommitAsync();
}
private async Task SeedData()
{
var series = new SeriesBuilder("Test Series")
.WithFormat(MangaFormat.Archive)
.WithMetadata(new SeriesMetadataBuilder().Build())
.Build();
var library = new LibraryBuilder("Test Library", LibraryType.Manga)
.WithAllowScrobbling(true)
.WithSeries(series)
.Build();
_context.Library.Add(library);
var user = new AppUserBuilder("testuser", "testuser")
//.WithPreferences(new UserPreferencesBuilder().WithAniListScrobblingEnabled(true).Build())
.Build();
user.UserPreferences.AniListScrobblingEnabled = true;
_unitOfWork.UserRepository.Add(user);
await _unitOfWork.CommitAsync();
}
#region ScrobbleWantToReadUpdate Tests
[Fact]
public async Task ScrobbleWantToReadUpdate_NoExistingEvents_WantToRead_ShouldCreateNewEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// Act
await _service.ScrobbleWantToReadUpdate(userId, seriesId, true);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.Equal(ScrobbleEventType.AddWantToRead, events[0].ScrobbleEventType);
Assert.Equal(userId, events[0].AppUserId);
}
[Fact]
public async Task ScrobbleWantToReadUpdate_NoExistingEvents_RemoveWantToRead_ShouldCreateNewEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// Act
await _service.ScrobbleWantToReadUpdate(userId, seriesId, false);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.Equal(ScrobbleEventType.RemoveWantToRead, events[0].ScrobbleEventType);
Assert.Equal(userId, events[0].AppUserId);
}
[Fact]
public async Task ScrobbleWantToReadUpdate_ExistingWantToReadEvent_WantToRead_ShouldNotCreateNewEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// First, let's create an event through the service
await _service.ScrobbleWantToReadUpdate(userId, seriesId, true);
// Act - Try to create the same event again
await _service.ScrobbleWantToReadUpdate(userId, seriesId, true);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.All(events, e => Assert.Equal(ScrobbleEventType.AddWantToRead, e.ScrobbleEventType));
}
[Fact]
public async Task ScrobbleWantToReadUpdate_ExistingWantToReadEvent_RemoveWantToRead_ShouldAddRemoveEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// First, let's create a want-to-read event through the service
await _service.ScrobbleWantToReadUpdate(userId, seriesId, true);
// Act - Now remove from want-to-read
await _service.ScrobbleWantToReadUpdate(userId, seriesId, false);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.Contains(events, e => e.ScrobbleEventType == ScrobbleEventType.RemoveWantToRead);
}
[Fact]
public async Task ScrobbleWantToReadUpdate_ExistingRemoveWantToReadEvent_RemoveWantToRead_ShouldNotCreateNewEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// First, let's create a remove-from-want-to-read event through the service
await _service.ScrobbleWantToReadUpdate(userId, seriesId, false);
// Act - Try to create the same event again
await _service.ScrobbleWantToReadUpdate(userId, seriesId, false);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.All(events, e => Assert.Equal(ScrobbleEventType.RemoveWantToRead, e.ScrobbleEventType));
}
[Fact]
public async Task ScrobbleWantToReadUpdate_ExistingRemoveWantToReadEvent_WantToRead_ShouldAddWantToReadEvent()
{
// Arrange
await SeedData();
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
const int userId = 1;
const int seriesId = 1;
// First, let's create a remove-from-want-to-read event through the service
await _service.ScrobbleWantToReadUpdate(userId, seriesId, false);
// Act - Now add to want-to-read
await _service.ScrobbleWantToReadUpdate(userId, seriesId, true);
// Assert
var events = await _unitOfWork.ScrobbleRepository.GetAllEventsForSeries(seriesId);
Assert.Single(events);
Assert.Contains(events, e => e.ScrobbleEventType == ScrobbleEventType.AddWantToRead);
}
#endregion
[Theory]
[InlineData("https://anilist.co/manga/35851/Byeontaega-Doeja/", 35851)]
[InlineData("https://anilist.co/manga/30105", 30105)]

View file

@ -0,0 +1,6 @@
[
"Spice and Wolf/Spice and Wolf Vol. 1.cbz",
"Spice and Wolf/Spice and Wolf Vol. 2.cbz",
"The Executioner and Her Way of Life/The Executioner and Her Way of Life Vol. 1.cbz",
"The Executioner and Her Way of Life/The Executioner and Her Way of Life Vol. 2.cbz"
]

View file

@ -0,0 +1,6 @@
[
"Spice and Wolf/Spice and Wolf Vol. 1.cbz",
"Spice and Wolf/Spice and Wolf Vol. 2.cbz",
"Spice and Wolf/Spice and Wolf Vol. 3/Spice and Wolf Vol. 3 Ch. 0011.cbz",
"Spice and Wolf/Spice and Wolf Vol. 3/Spice and Wolf Vol. 3 Ch. 0012.cbz"
]

View file

@ -0,0 +1,6 @@
[
"Spice and Wolf/Spice and Wolf Vol. 1.cbz",
"Spice and Wolf/Spice and Wolf Vol. 2.cbz",
"Spice and Wolf/Spice and Wolf Vol. 3/Spice and Wolf Vol. 3 Ch. 0011.cbz",
"Spice and Wolf/Spice and Wolf Vol. 3/Spice and Wolf Vol. 3 Ch. 0012.cbz"
]