using System.Collections.ObjectModel; using FluentAssertions; using NUnit.Framework; using Recyclarr.TestLibrary.AutoFixture; using Recyclarr.TestLibrary.FluentAssertions; using Recyclarr.TrashLib.Config.Services; using Recyclarr.TrashLib.Services.CustomFormat.Models; using Recyclarr.TrashLib.Services.CustomFormat.Models.Cache; using Recyclarr.TrashLib.Services.CustomFormat.Processors.GuideSteps; using Recyclarr.TrashLib.TestLibrary; namespace Recyclarr.TrashLib.Tests.CustomFormat.Processors.GuideSteps; [TestFixture] [Parallelizable(ParallelScope.All)] public class CustomFormatStepTest { private sealed class Context { public List TestGuideData { get; } = new() { NewCf.Data("name1", "id1"), NewCf.Data("name2", "id2"), NewCf.Data("name3", "id3") }; } [Test, AutoMockData] public void Cfs_not_in_config_are_skipped(CustomFormatStep processor) { var ctx = new Context(); var testConfig = new List { new() {TrashIds = new List {"id1", "id3"}} }; processor.Process(ctx.TestGuideData, testConfig, new CustomFormatCache()); processor.DeletedCustomFormatsInCache.Should().BeEmpty(); processor.ProcessedCustomFormats.Should() .BeEquivalentTo(new List { NewCf.Processed("name1", "id1"), NewCf.Processed("name3", "id3") }); } [Test, AutoMockData] public void Config_cfs_in_different_sections_are_processed(CustomFormatStep processor) { var ctx = new Context(); var testConfig = new List { new() {TrashIds = new List {"id1", "id3"}}, new() {TrashIds = new List {"id2"}} }; processor.Process(ctx.TestGuideData, testConfig, new CustomFormatCache()); processor.DeletedCustomFormatsInCache.Should().BeEmpty(); processor.ProcessedCustomFormats.Should().BeEquivalentTo(new List { NewCf.Processed("name1", "id1"), NewCf.Processed("name2", "id2"), NewCf.Processed("name3", "id3") }, op => op.Using(new JsonEquivalencyStep())); } [Test, AutoMockData] public void Custom_format_is_deleted_if_in_config_and_cache_but_not_in_guide(CustomFormatStep processor) { var guideData = new List { NewCf.Data("name1", "id1") }; var testConfig = new List { new() {TrashIds = new List {"id1"}} }; var testCache = new CustomFormatCache { TrashIdMappings = new Collection {new("id1000", "", 1)} }; processor.Process(guideData, testConfig, testCache); processor.DeletedCustomFormatsInCache.Should() .BeEquivalentTo(new[] {new TrashIdMapping("id1000", "", 1)}); processor.ProcessedCustomFormats.Should().BeEquivalentTo(new List { NewCf.Processed("name1", "id1") }); } [Test, AutoMockData] public void Custom_format_is_deleted_if_not_in_config_but_in_cache_and_in_guide(CustomFormatStep processor) { var cache = new CustomFormatCache { TrashIdMappings = new Collection {new("id1", "", 9)} }; var guideCfs = new List { NewCf.Data("3D", "id1") }; processor.Process(guideCfs, Array.Empty(), cache); processor.DeletedCustomFormatsInCache.Should().BeEquivalentTo(new[] {cache.TrashIdMappings[0]}); processor.ProcessedCustomFormats.Should().BeEmpty(); } [Test, AutoMockData] public void Match_custom_format_using_trash_id(CustomFormatStep processor) { var guideData = new List { NewCf.Data("name1", "id1"), NewCf.Data("name2", "id2") }; var testConfig = new List { new() {TrashIds = new List {"id2"}} }; processor.Process(guideData, testConfig, null); processor.DeletedCustomFormatsInCache.Should().BeEmpty(); processor.ProcessedCustomFormats.Should() .BeEquivalentTo(new List { NewCf.Processed("name2", "id2") }); } [Test, AutoMockData] public void Non_existent_cfs_in_config_are_skipped(CustomFormatStep processor) { var ctx = new Context(); var testConfig = new List { new() {TrashIds = new List {"doesnt_exist"}} }; processor.Process(ctx.TestGuideData, testConfig, new CustomFormatCache()); processor.DeletedCustomFormatsInCache.Should().BeEmpty(); processor.ProcessedCustomFormats.Should().BeEmpty(); } }