using FluentAssertions; using HomeAssistant.Entities.Interfaces; using Microsoft.Extensions.Logging; using Moq; using PowerMonitor.Models; using PowerMonitorManager; using PowerMonitorManager.Models; namespace PowerMonitor.Manager.Tests; public class PowerMonitorEntityServiceTests { private readonly Mock _entityManagerMock; private readonly Mock> _loggerMock; private readonly PowerMonitorEntityService _sut; public PowerMonitorEntityServiceTests() { _entityManagerMock = new Mock(); _loggerMock = new Mock>(); _sut = new PowerMonitorEntityService(_entityManagerMock.Object, _loggerMock.Object); } #region CreateEntityAsync [Fact] public async Task CreateEntityAsync_ShouldCreateMqttEntity() { // Arrange _entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.CreateEntityAsync(); // Assert _entityManagerMock.Verify( em => em.CreateEntityAsync(It.Is(e => e.EntityId == "sensor.other_power_usage" && e.Domain == "sensor" && e.Identifier == "other_power_usage")), Times.Once); } [Fact] public async Task CreateEntityAsync_WhenCreateFails_ShouldNotThrow() { // Arrange _entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny())) .ThrowsAsync(new InvalidOperationException("Entity creation failed")); // Act var act = () => _sut.CreateEntityAsync(); // Assert await act.Should().NotThrowAsync(); } #endregion #region ResetEntityAsync [Fact] public async Task ResetEntityAsync_ShouldSetStateToZero() { // Arrange _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.ResetEntityAsync(); // Assert _entityManagerMock.Verify( em => em.SetEntityState("sensor.other_power_usage", "0"), Times.Once); } [Fact] public async Task ResetEntityAsync_ShouldSetDefaultAttributes() { // Arrange _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.ResetEntityAsync(); // Assert _entityManagerMock.Verify( em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny()), Times.Once); } #endregion #region UpdateEntityAsync [Fact] public async Task UpdateEntityAsync_ShouldSetStateWithFormattedPower() { // Arrange var context = new PowerUpdateContext { TotalHomePower = 3000.0, TotalMonitoredPower = 1500.0, OtherPower = 1500.0, MonitoredSensorCount = 21 }; _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.UpdateEntityAsync(context); // Assert _entityManagerMock.Verify( em => em.SetEntityState("sensor.other_power_usage", "1500.00"), Times.Once); } [Fact] public async Task UpdateEntityAsync_ShouldSetAttributes() { // Arrange var context = new PowerUpdateContext { TotalHomePower = 5000.0, TotalMonitoredPower = 2000.0, OtherPower = 3000.0, MonitoredSensorCount = 15 }; _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.UpdateEntityAsync(context); // Assert _entityManagerMock.Verify( em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny()), Times.Once); } [Fact] public async Task UpdateEntityAsync_WithZeroPower_ShouldSetZeroState() { // Arrange var context = new PowerUpdateContext { TotalHomePower = 0.0, TotalMonitoredPower = 0.0, OtherPower = 0.0, MonitoredSensorCount = 10 }; _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.UpdateEntityAsync(context); // Assert _entityManagerMock.Verify( em => em.SetEntityState("sensor.other_power_usage", "0.00"), Times.Once); } #endregion #region CleanupEntityAsync [Fact] public async Task CleanupEntityAsync_ShouldRemoveEntity() { // Arrange _entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.CleanupEntityAsync(); // Assert _entityManagerMock.Verify( em => em.RemoveEntity("sensor.other_power_usage"), Times.Once); } [Fact] public async Task CleanupEntityAsync_WhenRemoveFails_ShouldRethrow() { // Arrange _entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny())) .ThrowsAsync(new InvalidOperationException("Remove failed")); // Act var act = () => _sut.CleanupEntityAsync(); // Assert await act.Should().ThrowAsync() .WithMessage("Remove failed"); } #endregion #region SetEntityStateSafe [Fact] public async Task SetEntityStateSafe_ShouldCallSetEntityState() { // Arrange _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.SetEntityStateSafe("sensor.test", "42"); // Assert _entityManagerMock.Verify(em => em.SetEntityState("sensor.test", "42"), Times.Once); } [Fact] public async Task SetEntityStateSafe_WhenExceptionThrown_ShouldNotRethrow() { // Arrange _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) .ThrowsAsync(new InvalidOperationException("State update failed")); // Act var act = () => _sut.SetEntityStateSafe("sensor.test", "42"); // Assert await act.Should().NotThrowAsync(); } #endregion #region SetEntityAttributesSafe [Fact] public async Task SetEntityAttributesSafe_ShouldCallSetEntityAttributes() { // Arrange var attributes = new { unit_of_measurement = "W" }; _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); // Act await _sut.SetEntityAttributesSafe("sensor.test", attributes); // Assert _entityManagerMock.Verify( em => em.SetEntityAttributes("sensor.test", It.IsAny()), Times.Once); } [Fact] public async Task SetEntityAttributesSafe_WhenExceptionThrown_ShouldNotRethrow() { // Arrange var attributes = new { unit_of_measurement = "W" }; _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) .ThrowsAsync(new InvalidOperationException("Attributes update failed")); // Act var act = () => _sut.SetEntityAttributesSafe("sensor.test", attributes); // Assert await act.Should().NotThrowAsync(); } #endregion }