using FluentAssertions; using PowerMonitorManager; using PowerMonitorManager.Models; using PowerMonitorManager.Models.Consts; namespace PowerMonitor.Manager.Tests; public class PowerCostCalculationServiceTests { private readonly PowerCostCalculationService _sut = new(); private const double DefaultTotalWatts = 341.97; private const double DefaultSmoothedOtherWatts = 131.04; private const double DefaultPricePerKwh = 0.1791; private static PowerCalculationResult CreateResult( double totalWatts = DefaultTotalWatts, double smoothedOtherWatts = DefaultSmoothedOtherWatts, Dictionary? deviceReadings = null) { var readings = deviceReadings ?? new Dictionary { ["sensor.sonoff_100215c534_power"] = 101.0, ["sensor.sonoff_10018909cb_power"] = 109.93 }; return new PowerCalculationResult { TotalPowerWatts = totalWatts, KnownDevicesSumWatts = readings.Values.Sum(), RawOtherPowerWatts = smoothedOtherWatts, SmoothedOtherPowerWatts = smoothedOtherWatts, DataQuality = "ok", DevicePowerReadings = readings }; } #region CalculateCostIncrement [Fact] public void CalculateCostIncrement_ShouldComputeCorrectCost() { var cost = PowerCostCalculationService.CalculateCostIncrement( powerWatts: 341.97, pricePerKwh: 0.1791, pollIntervalSeconds: PowerMonitorConsts.DefaultPollIntervalSeconds); var expected = (341.97 / PowerMonitorConsts.WattsPerKilowatt) * 0.1791 * ((double)PowerMonitorConsts.DefaultPollIntervalSeconds / PowerMonitorConsts.SecondsPerHour); cost.Should().BeApproximately(expected, 0.000001); } [Fact] public void CalculateCostIncrement_WithZeroPower_ShouldReturnZero() { var cost = PowerCostCalculationService.CalculateCostIncrement(0, DefaultPricePerKwh, 15); cost.Should().Be(0); } [Fact] public void CalculateCostIncrement_WithZeroPrice_ShouldReturnZero() { var cost = PowerCostCalculationService.CalculateCostIncrement(341.97, 0, 15); cost.Should().Be(0); } #endregion #region RecordCosts [Fact] public void RecordCosts_ShouldCreateTotalAndOtherSnapshots() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(); _sut.RecordCosts(result, DefaultPricePerKwh, 15, now); var totalSnapshot = _sut.GetTotalCostSnapshot(); totalSnapshot.CostDaily.Should().BeGreaterThan(0); var otherSnapshot = _sut.GetOtherCostSnapshot(); otherSnapshot.CostDaily.Should().BeGreaterThan(0); } [Fact] public void RecordCosts_ShouldCreateDeviceSnapshots() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(); _sut.RecordCosts(result, DefaultPricePerKwh, 15, now); var ac = _sut.GetDeviceCostSnapshot("sensor.sonoff_100215c534_power"); ac.CostDaily.Should().BeGreaterThan(0); var induction = _sut.GetDeviceCostSnapshot("sensor.sonoff_10018909cb_power"); induction.CostDaily.Should().BeGreaterThan(0); } [Fact] public void RecordCosts_TotalCostShouldBeHigherThanOtherCost() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts); _sut.RecordCosts(result, DefaultPricePerKwh, 15, now); var total = _sut.GetTotalCostSnapshot(); var other = _sut.GetOtherCostSnapshot(); total.CostDaily.Should().BeGreaterThan(other.CostDaily); } [Fact] public void RecordCosts_MultipleCalls_ShouldAccumulate() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(); _sut.RecordCosts(result, DefaultPricePerKwh, 15, now); var firstCost = _sut.GetTotalCostSnapshot().CostDaily; _sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(15)); var secondCost = _sut.GetTotalCostSnapshot().CostDaily; secondCost.Should().BeApproximately(firstCost * 2, 0.000001); } [Fact] public void RecordCosts_NewDeviceAppearing_ShouldCreateAccumulator() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result1 = CreateResult(deviceReadings: new Dictionary { ["sensor.sonoff_100215c534_power"] = 101.0 }); _sut.RecordCosts(result1, DefaultPricePerKwh, 15, now); _sut.GetAllDeviceCostSnapshots().Should().HaveCount(1); var result2 = CreateResult(deviceReadings: new Dictionary { ["sensor.sonoff_100215c534_power"] = 101.0, ["sensor.sonoff_10018909cb_power"] = 109.93 }); _sut.RecordCosts(result2, DefaultPricePerKwh, 15, now.AddSeconds(15)); _sut.GetAllDeviceCostSnapshots().Should().HaveCount(2); } #endregion #region Price management [Fact] public void RecordCosts_WithValidPrice_ShouldUpdateLastKnownPrice() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(); _sut.RecordCosts(result, 0.2492, 15, now); _sut.LastKnownPrice.Should().Be(0.2492); _sut.PriceSource.Should().Be("live"); } [Fact] public void RecordCosts_WithZeroPrice_ShouldUseFallback() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(); _sut.RecordCosts(result, DefaultPricePerKwh, 15, now); _sut.RecordCosts(result, 0, 15, now.AddSeconds(15)); _sut.LastKnownPrice.Should().Be(DefaultPricePerKwh); _sut.PriceSource.Should().Be("live"); } [Fact] public void SetPrice_ShouldUpdatePriceAndMarkAsStale() { _sut.SetPrice(0.1891); _sut.LastKnownPrice.Should().Be(0.1891); _sut.PriceSource.Should().Be("stale"); } #endregion #region GetAllDeviceCostSnapshots [Fact] public void GetAllDeviceCostSnapshots_WhenEmpty_ShouldReturnEmpty() { _sut.GetAllDeviceCostSnapshots().Should().BeEmpty(); } [Fact] public void GetDeviceCostSnapshot_UnknownDevice_ShouldReturnEmptySnapshot() { var snapshot = _sut.GetDeviceCostSnapshot("sensor.unknown"); snapshot.CostDaily.Should().Be(0); snapshot.PartialWindow.Should().BeTrue(); } #endregion #region Snapshot defaults [Fact] public void GetTotalCostSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot() { var snapshot = _sut.GetTotalCostSnapshot(); snapshot.CostDaily.Should().Be(0); snapshot.PartialWindow.Should().BeTrue(); } [Fact] public void GetOtherCostSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot() { var snapshot = _sut.GetOtherCostSnapshot(); snapshot.CostDaily.Should().Be(0); snapshot.PartialWindow.Should().BeTrue(); } #endregion #region Thread safety [Fact] public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData() { var now = new DateTime(2026, 3, 17, 10, 0, 0); var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts); const int callsPerThread = 100; const int threadCount = 10; var tasks = Enumerable.Range(0, threadCount) .Select(_ => Task.Run(() => { for (var i = 0; i < callsPerThread; i++) { _sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(1)); } })) .ToArray(); await Task.WhenAll(tasks); var total = _sut.GetTotalCostSnapshot(); total.CostDaily.Should().BeGreaterThan(0); var devices = _sut.GetAllDeviceCostSnapshots(); devices.Should().HaveCount(2); } #endregion }