#region ========================================================================= USING ===================================================================================== using Nemera.Plugins.Contracts.Core.Metadata; using Nemera.Plugins.LocalMusicArtwork.Common.DependencyInjection; using Nemera.Plugins.LocalMusicArtwork.Core; using Nemera.Plugins.LocalMusicArtwork.Core.Settings; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; #endregion namespace Nemera.Plugins.LocalMusicArtwork.UnitTests.Common.DependencyInjection; /// /// Contains unit tests for the class. /// [ExcludeFromCodeCoverage] public class LocalMusicArtworkServicesTests { [Fact] public void AddLocalMusicArtworkProviders_WhenServicesIsNull_ShouldThrowArgumentNullException() { // Arrange IServiceCollection services = null!; // Act Action act = () => services.AddLocalMusicArtworkProviders(pluginId: Guid.NewGuid()); // Assert Assert.Throws(act); } [Fact] public void AddLocalMusicArtworkProviders_WhenCalled_ShouldReturnTheSameServiceCollection() { // Arrange ServiceCollection services = new(); // Act IServiceCollection result = services.AddLocalMusicArtworkProviders(pluginId: Guid.NewGuid()); // Assert Assert.Same(services, result); } [Fact] public void AddLocalMusicArtworkProviders_WhenCalled_ShouldRegisterTheAlbumAndArtistProvidersForKeyedResolution() { // Arrange Guid pluginId = Guid.NewGuid(); ServiceCollection services = new(); // Act services.AddLocalMusicArtworkProviders(pluginId: pluginId); ServiceProvider serviceProvider = services.BuildServiceProvider(); // Assert IReadOnlyList providers = [.. serviceProvider.GetKeyedServices(pluginId)]; Assert.Equal(2, providers.Count); Assert.Contains(providers, provider => provider is LocalMusicAlbumArtworkProvider); Assert.Contains(providers, provider => provider is LocalMusicArtistArtworkProvider); Assert.Equal(2, providers.Select(provider => provider.GetType()).Distinct().Count()); Assert.Empty(serviceProvider.GetKeyedServices(Guid.NewGuid())); } [Fact] public void AddLocalMusicArtworkProviders_WhenCalled_ShouldRegisterTheSettingsProviderAsScoped() { // Arrange ServiceCollection services = new(); // Act services.AddLocalMusicArtworkProviders(pluginId: Guid.NewGuid()); // Assert ServiceDescriptor settingsProviderDescriptor = Assert.Single(services, descriptor => descriptor.ServiceType == typeof(LocalMusicArtworkSettingsProvider)); Assert.Equal(ServiceLifetime.Scoped, settingsProviderDescriptor.Lifetime); } [Fact] public void AddLocalMusicArtworkProviders_WhenCalledTwice_ShouldNotDuplicateTheTryAddSettingsProvider() { // Arrange Guid pluginId = Guid.NewGuid(); ServiceCollection services = new(); // Act services.AddLocalMusicArtworkProviders(pluginId: pluginId); services.AddLocalMusicArtworkProviders(pluginId: pluginId); // Assert // The settings provider is registered with TryAdd, so a second call must not add it again, while the artwork providers stay transient and are added once per call. Assert.Single(services, descriptor => descriptor.ServiceType == typeof(LocalMusicArtworkSettingsProvider)); Assert.Equal(4, services.Count(descriptor => descriptor.ServiceType == typeof(IArtworkProvider))); } }