using FluentAssertions; using Microsoft.Extensions.Options; using PowerMonitorManager; using PowerMonitorManager.Models; namespace PowerMonitor.Manager.Tests; public class OtherPowerCalculationServiceTests { private static OtherPowerCalculationService CreateSut(int smoothingWindowSize = 10) { var options = Options.Create(new PowerMonitorOptions { SmoothingWindowSize = smoothingWindowSize }); return new OtherPowerCalculationService(options); } #region Basic calculation [Fact] public void Calculate_ShouldReturnOtherAsTotalMinusDevices() { var sut = CreateSut(smoothingWindowSize: 1); var devices = new Dictionary { ["sensor.sonoff_100215c534_power"] = 8.55, ["sensor.sonoff_10018909cb_power"] = 36.93 }; var result = sut.Calculate(341.97, devices); result.TotalPowerWatts.Should().Be(341.97); result.KnownDevicesSumWatts.Should().BeApproximately(45.48, 0.01); result.RawOtherPowerWatts.Should().BeApproximately(296.49, 0.01); result.DataQuality.Should().Be("ok"); } [Fact] public void Calculate_WhenDevicesSumExceedsTotal_ShouldClampToZero() { var sut = CreateSut(smoothingWindowSize: 1); var devices = new Dictionary { ["sensor.sonoff_100215c534_power"] = 200.0, ["sensor.sonoff_10018909cb_power"] = 180.0 }; var result = sut.Calculate(341.97, devices); result.RawOtherPowerWatts.Should().Be(0); result.SmoothedOtherPowerWatts.Should().Be(0); result.DataQuality.Should().Be("device_sum_exceeds_total"); } [Fact] public void Calculate_WithNoDevices_ShouldReturnTotalAsOther() { var sut = CreateSut(smoothingWindowSize: 1); var devices = new Dictionary(); var result = sut.Calculate(341.97, devices); result.RawOtherPowerWatts.Should().Be(341.97); result.KnownDevicesSumWatts.Should().Be(0); } [Fact] public void Calculate_ShouldIncludeAllDeviceReadingsInResult() { var sut = CreateSut(smoothingWindowSize: 1); var devices = new Dictionary { ["sensor.sonoff_100215c534_power"] = 8.55, ["sensor.sonoff_10018909cb_power"] = 36.93 }; var result = sut.Calculate(341.97, devices); result.DevicePowerReadings.Should().HaveCount(2); result.DevicePowerReadings["sensor.sonoff_100215c534_power"].Should().Be(8.55); result.DevicePowerReadings["sensor.sonoff_10018909cb_power"].Should().Be(36.93); } #endregion #region EMA smoothing [Fact] public void Calculate_WithSmoothing_ShouldApplyExponentialMovingAverage() { var sut = CreateSut(smoothingWindowSize: 4); var devices = new Dictionary(); var result1 = sut.Calculate(341.97, devices); result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1); var result2 = sut.Calculate(115.0, devices); result2.SmoothedOtherPowerWatts.Should().BeLessThan(341.97); result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(115.0); } [Fact] public void Calculate_MultipleCallsWithSameValue_ShouldConverge() { var sut = CreateSut(smoothingWindowSize: 5); var devices = new Dictionary(); PowerCalculationResult? result = null; for (var i = 0; i < 50; i++) { result = sut.Calculate(200.0, devices); } result!.SmoothedOtherPowerWatts.Should().BeApproximately(200.0, 0.1); } #endregion #region Reset [Fact] public void Reset_ShouldClearSmoothedState() { var sut = CreateSut(smoothingWindowSize: 10); var devices = new Dictionary(); sut.Calculate(341.97, devices); sut.Calculate(341.97, devices); sut.Reset(); var result = sut.Calculate(115.0, devices); result.SmoothedOtherPowerWatts.Should().BeApproximately(115.0, 0.1); } #endregion }