Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -7,62 +7,66 @@ 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<string, double>? deviceReadings = null)
|
||||
public enum CostBucket
|
||||
{
|
||||
var readings = deviceReadings ?? new Dictionary<string, double>
|
||||
{
|
||||
["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
|
||||
};
|
||||
Total,
|
||||
Other
|
||||
}
|
||||
|
||||
private readonly PowerCostCalculationService _sut = new();
|
||||
|
||||
#region CalculateCostIncrement
|
||||
|
||||
[Fact]
|
||||
public void CalculateCostIncrement_ShouldComputeCorrectCost()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(
|
||||
powerWatts: 341.97,
|
||||
pricePerKwh: 0.1791,
|
||||
pollIntervalSeconds: PowerMonitorConsts.DefaultPollIntervalSeconds);
|
||||
public static IEnumerable<object[]> CostIncrementCases =>
|
||||
[
|
||||
["default polling interval", 341.97, 0.1791, 15, 0.0002551951125],
|
||||
["one kilowatt for one hour", 1000.0, 1.0, PowerMonitorConsts.SecondsPerHour, 1.0],
|
||||
["limit load for one minute", 5500.0, 0.25, 60, 0.0229166666666667],
|
||||
["very large load", 1_000_000.0, 0.5, 900, 125.0],
|
||||
["negative power", -500.0, 0.2, PowerMonitorConsts.SecondsPerHour, -0.1],
|
||||
["zero power", 0.0, 0.1791, 15, 0.0],
|
||||
["zero price", 341.97, 0.0, 15, 0.0],
|
||||
["zero interval", 1000.0, 0.2, 0, 0.0],
|
||||
["tiny values", 0.001, 0.0001, 1, 2.77777777777778e-14]
|
||||
];
|
||||
|
||||
var expected = (341.97 / PowerMonitorConsts.WattsPerKilowatt)
|
||||
* 0.1791
|
||||
* ((double)PowerMonitorConsts.DefaultPollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
cost.Should().BeApproximately(expected, 0.000001);
|
||||
[Theory]
|
||||
[MemberData(nameof(CostIncrementCases))]
|
||||
public void CalculateCostIncrement_ShouldComputeExpectedCost(
|
||||
string scenario,
|
||||
double powerWatts,
|
||||
double pricePerKwh,
|
||||
int pollIntervalSeconds,
|
||||
double expectedCost)
|
||||
{
|
||||
// Act
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(powerWatts, pricePerKwh, pollIntervalSeconds);
|
||||
|
||||
// Assert
|
||||
var precision = expectedCost == 0 ? 1e-12 : Math.Abs(expectedCost) * 1e-9;
|
||||
cost.Should().BeApproximately(expectedCost, precision, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateCostIncrement_WithZeroPower_ShouldReturnZero()
|
||||
public void CalculateCostIncrement_ForGeneratedPowerSeries_ShouldBeProportionalToPower()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(0, DefaultPricePerKwh, 15);
|
||||
cost.Should().Be(0);
|
||||
}
|
||||
// Arrange
|
||||
const double pricePerKwh = 0.25;
|
||||
const int pollIntervalSeconds = 15;
|
||||
var samples = PowerMonitorTestData.CreatePowerSampleSeries(25);
|
||||
var unitCost = PowerCostCalculationService.CalculateCostIncrement(1_000.0, pricePerKwh, pollIntervalSeconds);
|
||||
|
||||
[Fact]
|
||||
public void CalculateCostIncrement_WithZeroPrice_ShouldReturnZero()
|
||||
{
|
||||
var cost = PowerCostCalculationService.CalculateCostIncrement(341.97, 0, 15);
|
||||
cost.Should().Be(0);
|
||||
// Act
|
||||
var costs = samples
|
||||
.Select(watts => PowerCostCalculationService.CalculateCostIncrement(watts, pricePerKwh, pollIntervalSeconds))
|
||||
.ToArray();
|
||||
|
||||
// Assert
|
||||
costs.Should().BeInAscendingOrder("every generated sample is larger than the previous one");
|
||||
for (var i = 0; i < samples.Count; i++)
|
||||
{
|
||||
costs[i].Should().BeApproximately(samples[i] / 1_000.0 * unitCost, 1e-15);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -72,41 +76,53 @@ public class PowerCostCalculationServiceTests
|
||||
[Fact]
|
||||
public void RecordCosts_ShouldCreateTotalAndOtherSnapshots()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, now);
|
||||
|
||||
var totalSnapshot = _sut.GetTotalCostSnapshot();
|
||||
totalSnapshot.CostDaily.Should().BeGreaterThan(0);
|
||||
// Assert
|
||||
var expectedTotal = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
var expectedOther = ExpectedCost(PowerMonitorTestData.DefaultSmoothedOtherWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
|
||||
var otherSnapshot = _sut.GetOtherCostSnapshot();
|
||||
otherSnapshot.CostDaily.Should().BeGreaterThan(0);
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expectedTotal, 1e-12);
|
||||
_sut.GetOtherCostSnapshot().CostDaily.Should().BeApproximately(expectedOther, 1e-12);
|
||||
_sut.GetTotalCostSnapshot().PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_ShouldCreateDeviceSnapshots()
|
||||
[Theory]
|
||||
[InlineData("sensor.sonoff_100215c534_power")]
|
||||
[InlineData("sensor.sonoff_10018909cb_power")]
|
||||
public void RecordCosts_ShouldCreatePositiveDeviceSnapshotForEveryReading(string entityId)
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, 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);
|
||||
// Assert
|
||||
var snapshot = _sut.GetDeviceCostSnapshot(entityId);
|
||||
var expected = ExpectedCost(result.DevicePowerReadings[entityId], PowerMonitorTestData.DefaultPricePerKwh);
|
||||
snapshot.CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
snapshot.CostDaily.Should().BeGreaterThan(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_TotalCostShouldBeHigherThanOtherCost()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, now);
|
||||
|
||||
// Assert
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
var other = _sut.GetOtherCostSnapshot();
|
||||
total.CostDaily.Should().BeGreaterThan(other.CostDaily);
|
||||
@@ -115,78 +131,334 @@ public class PowerCostCalculationServiceTests
|
||||
[Fact]
|
||||
public void RecordCosts_MultipleCalls_ShouldAccumulate()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
var firstCost = _sut.GetTotalCostSnapshot().CostDaily;
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
var secondCost = _sut.GetTotalCostSnapshot().CostDaily;
|
||||
|
||||
// Assert
|
||||
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<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 101.0
|
||||
});
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var firstResult = PowerMonitorTestData.CreateCalculation(
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
|
||||
|
||||
_sut.RecordCosts(result1, DefaultPricePerKwh, 15, now);
|
||||
// Act
|
||||
_sut.RecordCosts(firstResult, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
|
||||
|
||||
var result2 = CreateResult(deviceReadings: new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 101.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 109.93
|
||||
});
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
|
||||
_sut.RecordCosts(result2, DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
// Assert
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WhenDeviceDisappears_ShouldKeepItsPreviousAccumulator()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
var beforeDisappearance = _sut.GetDeviceCostSnapshot("sensor.sonoff_10018909cb_power").CostDaily;
|
||||
|
||||
// Act
|
||||
var singleDeviceResult = PowerMonitorTestData.CreateCalculation(
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
|
||||
_sut.RecordCosts(singleDeviceResult, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
|
||||
// Assert
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
_sut.GetDeviceCostSnapshot("sensor.sonoff_10018909cb_power").CostDaily.Should().Be(beforeDisappearance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithNoDeviceReadings_ShouldStillTrackTotals()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation(deviceReadings: new Dictionary<string, double>());
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
|
||||
// Assert
|
||||
_sut.GetAllDeviceCostSnapshots().Should().BeEmpty();
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeGreaterThan(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithZeroPower_ShouldCreateZeroCostSnapshots()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: 0,
|
||||
smoothedOtherWatts: 0,
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 0)));
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
|
||||
// Assert
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().Be(0);
|
||||
_sut.GetOtherCostSnapshot().CostDaily.Should().Be(0);
|
||||
_sut.GetDeviceCostSnapshot("sensor.sonoff_100215c534_power").CostDaily.Should().Be(0);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithNegativeDevicePower_ShouldProduceNegativeDeviceCost()
|
||||
{
|
||||
// Arrange - a sensor glitch must flow through the pure cost math unchanged.
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.glitch_power", -500.0)));
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
|
||||
// Assert
|
||||
var expected = ExpectedCost(-500.0, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
_sut.GetDeviceCostSnapshot("sensor.glitch_power").CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
_sut.GetDeviceCostSnapshot("sensor.glitch_power").CostDaily.Should().BeNegative();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithZeroPollInterval_ShouldTrackDevicesWithoutCost()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 0, now);
|
||||
|
||||
// Assert
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().Be(0);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
_sut.GetAllDeviceCostSnapshots().Values.Should().OnlyContain(snapshot => snapshot.CostMonthly == 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_When15MinWindowCrosses_ShouldResetAndStorePrevious()
|
||||
{
|
||||
// Arrange
|
||||
var firstTimestamp = new DateTime(2026, 3, 17, 10, 14, 0);
|
||||
var rolloverTimestamp = new DateTime(2026, 3, 17, 10, 15, 0);
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
var expected = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, firstTimestamp);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, rolloverTimestamp);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.Cost15Min.Should().BeApproximately(expected, 1e-12);
|
||||
snapshot.Previous15Min.Should().BeApproximately(expected, 1e-12);
|
||||
snapshot.PartialWindow.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WhenDayChanges_ShouldResetDailyWindow()
|
||||
{
|
||||
// Arrange
|
||||
var beforeMidnight = new DateTime(2026, 3, 17, 23, 50, 0);
|
||||
var afterMidnight = new DateTime(2026, 3, 18, 0, 5, 0);
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
var expected = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, beforeMidnight);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, afterMidnight);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
snapshot.PreviousDaily.Should().BeApproximately(expected, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WhenMonthChanges_ShouldResetMonthlyWindow()
|
||||
{
|
||||
// Arrange
|
||||
var beforeMonthEnd = new DateTime(2026, 3, 31, 23, 59, 0);
|
||||
var afterMonthStart = new DateTime(2026, 4, 1, 0, 0, 0);
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
var expected = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, beforeMonthEnd);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, afterMonthStart);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.CostMonthly.Should().BeApproximately(expected, 1e-12);
|
||||
snapshot.PreviousMonthly.Should().BeApproximately(expected, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_ForGeneratedQuarterHourDay_ShouldResetEverySlot()
|
||||
{
|
||||
// Arrange
|
||||
const double pricePerKwh = 0.2;
|
||||
var timestamps = PowerMonitorTestData.CreateSampleTimestamps(PowerMonitorTestData.FixedDayStart, 96);
|
||||
var wattSamples = PowerMonitorTestData.CreatePowerSampleSeries(96);
|
||||
var expectedPerSlot = timestamps
|
||||
.Select((_, index) => ExpectedCost(wattSamples[index], pricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds))
|
||||
.ToArray();
|
||||
|
||||
// Act
|
||||
for (var slot = 0; slot < timestamps.Count; slot++)
|
||||
{
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: wattSamples[slot],
|
||||
smoothedOtherWatts: wattSamples[slot] / 2,
|
||||
deviceReadings: new Dictionary<string, double>());
|
||||
_sut.RecordCosts(result, pricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, timestamps[slot]);
|
||||
}
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.Cost15Min.Should().BeApproximately(expectedPerSlot[^1], 1e-12);
|
||||
snapshot.Previous15Min.Should().BeApproximately(expectedPerSlot[^2], 1e-12);
|
||||
snapshot.CostDaily.Should().BeApproximately(expectedPerSlot.Sum(), 1e-9);
|
||||
snapshot.PreviousDaily.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_ForGeneratedHourlyDay_ShouldAccumulateDailyAndMonthlyTotals()
|
||||
{
|
||||
// Arrange
|
||||
const int pollIntervalSeconds = PowerMonitorConsts.SecondsPerHour;
|
||||
var windows = PowerMonitorTestData.CreateHourlyTariffWindows(PowerMonitorTestData.FixedDayStart);
|
||||
var expectedPerHour = new List<double>();
|
||||
|
||||
// Act
|
||||
foreach (var (start, pricePerKwh) in windows)
|
||||
{
|
||||
var watts = 500.0 + (start.Hour * 125.0);
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: watts,
|
||||
smoothedOtherWatts: watts / 4,
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.tv_power", watts / 2)));
|
||||
_sut.RecordCosts(result, pricePerKwh, pollIntervalSeconds, start);
|
||||
expectedPerHour.Add(ExpectedCost(watts, pricePerKwh, pollIntervalSeconds));
|
||||
}
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.CostDaily.Should().BeApproximately(expectedPerHour.Sum(), 1e-9);
|
||||
snapshot.CostMonthly.Should().BeApproximately(expectedPerHour.Sum(), 1e-9);
|
||||
snapshot.Cost1Hour.Should().BeApproximately(expectedPerHour[^1], 1e-12);
|
||||
snapshot.Previous1Hour.Should().BeApproximately(expectedPerHour[^2], 1e-12);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Price management
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithValidPrice_ShouldUpdateLastKnownPrice()
|
||||
public void InitialPrice_ShouldBeZeroAndLive()
|
||||
{
|
||||
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.LastKnownPrice.Should().Be(0);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithZeroPrice_ShouldUseFallback()
|
||||
[Theory]
|
||||
[InlineData(0.0001)]
|
||||
[InlineData(0.1791)]
|
||||
[InlineData(0.2492)]
|
||||
[InlineData(1.5)]
|
||||
[InlineData(100.0)]
|
||||
public void RecordCosts_WithPositivePrice_ShouldStoreLivePrice(double price)
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult();
|
||||
// Act
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), price, 15, PowerMonitorTestData.FixedNow);
|
||||
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now);
|
||||
_sut.RecordCosts(result, 0, 15, now.AddSeconds(15));
|
||||
|
||||
_sut.LastKnownPrice.Should().Be(DefaultPricePerKwh);
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(price);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetPrice_ShouldUpdatePriceAndMarkAsStale()
|
||||
[Theory]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(-0.0001)]
|
||||
[InlineData(-5.0)]
|
||||
public void RecordCosts_WithNonPositivePrice_ShouldFallBackToLastKnownPrice(double invalidPrice)
|
||||
{
|
||||
_sut.SetPrice(0.1891);
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
|
||||
_sut.LastKnownPrice.Should().Be(0.1891);
|
||||
// Act
|
||||
_sut.RecordCosts(result, invalidPrice, 15, now.AddSeconds(15));
|
||||
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(PowerMonitorTestData.DefaultPricePerKwh);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
var expected = 2 * ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_WithZeroPriceAndNoPriorPrice_ShouldRecordZeroCost()
|
||||
{
|
||||
// Arrange
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, 0, 15, PowerMonitorTestData.FixedNow);
|
||||
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(0);
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().Be(0);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.1891)]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(-1.25)]
|
||||
[InlineData(12.5)]
|
||||
public void SetPrice_ShouldUpdatePriceAndMarkAsStale(double price)
|
||||
{
|
||||
// Act
|
||||
_sut.SetPrice(price);
|
||||
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(price);
|
||||
_sut.PriceSource.Should().Be("stale");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordCosts_AfterSetPrice_WithZeroPrice_ShouldKeepStalePriceAndSource()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
_sut.SetPrice(0.5);
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), 0, 15, now);
|
||||
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(0.5);
|
||||
_sut.PriceSource.Should().Be("stale");
|
||||
var expected = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, 0.5);
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetAllDeviceCostSnapshots
|
||||
@@ -197,11 +469,20 @@ public class PowerCostCalculationServiceTests
|
||||
_sut.GetAllDeviceCostSnapshots().Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDeviceCostSnapshot_UnknownDevice_ShouldReturnEmptySnapshot()
|
||||
[Theory]
|
||||
[InlineData("sensor.unknown")]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("SENSOR.SONOFF_100215C534_POWER")]
|
||||
public void GetDeviceCostSnapshot_ForUnknownDevice_ShouldReturnEmptySnapshot(string entityId)
|
||||
{
|
||||
var snapshot = _sut.GetDeviceCostSnapshot("sensor.unknown");
|
||||
// Act
|
||||
var snapshot = _sut.GetDeviceCostSnapshot(entityId);
|
||||
|
||||
// Assert
|
||||
snapshot.Cost15Min.Should().Be(0);
|
||||
snapshot.CostDaily.Should().Be(0);
|
||||
snapshot.CostMonthly.Should().Be(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
@@ -209,19 +490,21 @@ public class PowerCostCalculationServiceTests
|
||||
|
||||
#region Snapshot defaults
|
||||
|
||||
[Fact]
|
||||
public void GetTotalCostSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot()
|
||||
[Theory]
|
||||
[InlineData(CostBucket.Total)]
|
||||
[InlineData(CostBucket.Other)]
|
||||
public void GetSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot(CostBucket bucket)
|
||||
{
|
||||
var snapshot = _sut.GetTotalCostSnapshot();
|
||||
snapshot.CostDaily.Should().Be(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
// Act
|
||||
var snapshot = bucket == CostBucket.Total
|
||||
? _sut.GetTotalCostSnapshot()
|
||||
: _sut.GetOtherCostSnapshot();
|
||||
|
||||
[Fact]
|
||||
public void GetOtherCostSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot()
|
||||
{
|
||||
var snapshot = _sut.GetOtherCostSnapshot();
|
||||
// Assert
|
||||
snapshot.Cost15Min.Should().Be(0);
|
||||
snapshot.Cost1Hour.Should().Be(0);
|
||||
snapshot.CostDaily.Should().Be(0);
|
||||
snapshot.CostMonthly.Should().Be(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
@@ -232,23 +515,26 @@ public class PowerCostCalculationServiceTests
|
||||
[Fact]
|
||||
public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
var now = new DateTime(2026, 3, 17, 10, 0, 0);
|
||||
var result = CreateResult(totalWatts: DefaultTotalWatts, smoothedOtherWatts: DefaultSmoothedOtherWatts);
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
const int callsPerThread = 100;
|
||||
const int threadCount = 10;
|
||||
|
||||
// Act
|
||||
var tasks = Enumerable.Range(0, threadCount)
|
||||
.Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++)
|
||||
{
|
||||
_sut.RecordCosts(result, DefaultPricePerKwh, 15, now.AddSeconds(1));
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(1));
|
||||
}
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
total.CostDaily.Should().BeGreaterThan(0);
|
||||
|
||||
@@ -257,4 +543,12 @@ public class PowerCostCalculationServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static double ExpectedCost(
|
||||
double powerWatts,
|
||||
double pricePerKwh,
|
||||
int pollIntervalSeconds = PowerMonitorConsts.DefaultPollIntervalSeconds) =>
|
||||
powerWatts / PowerMonitorConsts.WattsPerKilowatt
|
||||
* pricePerKwh
|
||||
* ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user