using System.Reactive.Concurrency; using BoilerMonitor.Models; using FluentAssertions; using HomeAssistantGenerated; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using NetDaemon.HassModel; using NordPool.Models; using NordPoolManager; using NordPoolManager.Interface; using NordPoolManager.Models; namespace NordPool.Manager.Tests; public class NordPoolInitTests { private readonly Mock> _loggerMock; private readonly Mock _priceServiceMock; private readonly Mock _entityServiceMock; private readonly Mock _schedulerMock; private readonly Mock _cheapestIntervalsFinderMock; private readonly Mock _boilerConfigProviderMock; private readonly Mock _haContextMock; private readonly Entities _entities; public NordPoolInitTests() { _loggerMock = new Mock>(); _priceServiceMock = new Mock(); _entityServiceMock = new Mock(); _schedulerMock = new Mock(); _schedulerMock.Setup(s => s.Now).Returns(DateTimeOffset.UtcNow); _cheapestIntervalsFinderMock = new Mock(); _boilerConfigProviderMock = new Mock(); _haContextMock = new Mock(); _entities = new Entities(_haContextMock.Object); _priceServiceMock.Setup(p => p.FetchPricesAsync()).Returns(Task.CompletedTask); _entityServiceMock.Setup(e => e.CleanupEntitiesAsync(It.IsAny())).Returns(Task.CompletedTask); _entityServiceMock.Setup(e => e.CreateEntitiesAsync(It.IsAny())).Returns(Task.CompletedTask); _entityServiceMock.Setup(e => e.ResetEntitiesAsync()).Returns(Task.CompletedTask); _entityServiceMock.Setup(e => e.UpdateEntitiesAsync(It.IsAny())).Returns(Task.CompletedTask); _priceServiceMock.Setup(p => p.GetCurrentPrice()).Returns(0.0); _priceServiceMock.Setup(p => p.ShouldFetchNewPrices(It.IsAny())).Returns(false); _priceServiceMock.SetupGet(p => p.DayQuartersIndex).Returns(0); _priceServiceMock.SetupGet(p => p.TodayNordPoolPrice).Returns((NordPoolPrice?)null); _priceServiceMock.SetupGet(p => p.TomorrowNordPoolPrice).Returns((NordPoolPrice?)null); _priceServiceMock.SetupGet(p => p.CurrentPrice).Returns(0.0); _priceServiceMock.SetupGet(p => p.LastUpdatedDate).Returns((DateTime?)null); _cheapestIntervalsFinderMock .Setup(c => c.IsCurrentIntervalCheap(It.IsAny>(), It.IsAny())) .Returns(false); _boilerConfigProviderMock.Setup(b => b.GetCurrentConfiguration()).Returns(new BoilerConfig { IntervalCount = 4, PriceCap = double.MaxValue, Deadline = DateTime.Today.AddDays(1), SkipPriceCap = false }); } private NordPoolInit CreateSut(NordPoolOptions? nordPoolOptions = null) { var options = Options.Create( nordPoolOptions ?? new NordPoolOptions { CleanupEntitiesOnStartup = false }); return new NordPoolInit( _entities, _haContextMock.Object, _loggerMock.Object, _priceServiceMock.Object, _entityServiceMock.Object, _schedulerMock.Object, _cheapestIntervalsFinderMock.Object, _boilerConfigProviderMock.Object, options, TimeProvider.System); } [Fact] public void Constructor_ShouldInitializeCorrectly() { // Arrange & Act var sut = CreateSut(); // Assert sut.Should().NotBeNull(); } [Theory] [InlineData(true)] [InlineData(false)] public async Task InitializeAsync_ShouldRespectCleanupOnStartupSetting(bool cleanupEntitiesOnStartup) { var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = cleanupEntitiesOnStartup }); await sut.InitializeAsync(); // The constructor also starts the same init flow in the background, so calls may happen more than once. _entityServiceMock.Verify( es => es.CleanupEntitiesAsync(It.IsAny()), cleanupEntitiesOnStartup ? Times.AtLeastOnce() : Times.Never()); _entityServiceMock.Verify(es => es.CreateEntitiesAsync(It.IsAny(), It.IsAny()), Times.AtLeastOnce); _entityServiceMock.Verify(es => es.ResetEntitiesAsync(It.IsAny()), Times.AtLeastOnce); _entityServiceMock.Verify(es => es.UpdateEntitiesAsync(It.IsAny(), It.IsAny()), Times.AtLeastOnce); } [Fact] public async Task InitializeAsync_WhenCreatingEntitiesFails_ShouldRethrow() { var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = false }); _entityServiceMock .Setup(es => es.CreateEntitiesAsync(It.IsAny(), It.IsAny())) .ThrowsAsync(new InvalidOperationException("create failed")); var act = sut.InitializeAsync; await act.Should().ThrowAsync().WithMessage("create failed"); _entityServiceMock.Verify(es => es.ResetEntitiesAsync(It.IsAny()), Times.Never); _entityServiceMock.Verify(es => es.UpdateEntitiesAsync(It.IsAny(), It.IsAny()), Times.Never); } }