#region ========================================================================= USING ===================================================================================== using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Nemera.Application.Common.DataAccess.Entities.Themes; using Nemera.Application.Common.Infrastructure.Models.DTO.Themes; using Nemera.Application.Common.Infrastructure.Themes; using Nemera.Application.Common.Infrastructure.Time; using Nemera.Application.Fixtures.Common.Infrastructure.Models.DTO.Themes; using Nemera.DataAccess.Core.UoW; using Nemera.Domain.Common.Primitives; using Nemera.Infrastructure.Core.Themes; using NSubstitute; using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading; using System.Threading.Tasks; #endregion namespace Nemera.Infrastructure.IntegrationTests.Core.Themes; /// /// Contains integration tests for the class. /// [ExcludeFromCodeCoverage] public class ThemeSynchronizerTests : IDisposable { private readonly DbContextOptions _dbContextOptions; private readonly NemeraDbContext _dbContext; private readonly UnitOfWork _unitOfWork; private readonly IThemeService _mockThemeService; private readonly ILogger _mockLogger; private readonly ThemeManifestDtoFixture _themeManifestDtoFixture = new(); private readonly string _archivePath; /// /// Initializes a new instance of the class. /// public ThemeSynchronizerTests() { _dbContextOptions = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; _dbContext = new NemeraDbContext(_dbContextOptions); _unitOfWork = new UnitOfWork(_dbContext, Substitute.For()); _mockThemeService = Substitute.For(); _mockLogger = Substitute.For(); _mockLogger.IsEnabled(Arg.Any()).Returns(true); _archivePath = Path.Combine(Path.GetTempPath(), $"nemera-theme-sync-{Guid.NewGuid():N}.zip"); } [Fact] public async Task SynchronizeAsync_WhenANewBundledThemeIsInstalled_ShouldPersistTheActivatedTheme() { // Arrange ThemeManifestDto manifest = _themeManifestDtoFixture.Create(); File.WriteAllText(_archivePath, "theme-pack"); _mockThemeService.GetBundledThemeArchivePaths().Returns([_archivePath]); _mockThemeService.ReadManifestFromArchiveAsync(_archivePath, Arg.Any()).Returns(Result.From(manifest)); _mockThemeService.InstallAsync(Arg.Any(), Arg.Any()).Returns(Result.From(manifest)); _mockThemeService.HasThemePack(Arg.Any()).Returns(true); _mockThemeService.DefaultThemeId.Returns(manifest.Id); // Act await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _unitOfWork, _mockLogger, CancellationToken.None); // Assert // Activating an inserted theme must not attempt an update, which the repository would report as a failed activation warning. _mockLogger.DidNotReceive().Log( LogLevel.Warning, Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>()); // Read through a fresh context so the assertion proves SaveChanges wrote the activation, not only that the entity was tracked. using (NemeraDbContext verificationContext = new(_dbContextOptions)) { ThemeEntity persistedTheme = Assert.Single(verificationContext.Themes); Assert.True(persistedTheme.IsCurrent); } } /// /// Releases the database context and removes the temporary archive file. /// public void Dispose() { _dbContext.Dispose(); if (File.Exists(_archivePath)) File.Delete(_archivePath); } }