Apply code formatting cleanup across the monitor projects

- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
2026-12-01 12:33:31 +02:00
parent 77458383d1
commit f919d6b25b
115 changed files with 3115 additions and 1959 deletions
@@ -1,7 +1,11 @@
#region
using FluentAssertions;
using PowerMonitorManager;
using PowerMonitorManager.Models.Consts;
#endregion
namespace PowerMonitor.Manager.Tests;
public class PowerCostCalculationServiceTests
@@ -12,7 +16,7 @@ public class PowerCostCalculationServiceTests
Other
}
private readonly PowerCostCalculationService _sut = new();
private readonly PowerCostCalculationService _sut = new PowerCostCalculationService();
#region Snapshot defaults
@@ -23,8 +27,8 @@ public class PowerCostCalculationServiceTests
{
// Act
var snapshot = bucket == CostBucket.Total
? _sut.GetTotalCostSnapshot()
: _sut.GetOtherCostSnapshot();
? _sut.GetTotalCostSnapshot()
: _sut.GetOtherCostSnapshot();
// Assert
snapshot.Cost15Min.Should().Be(0);
@@ -49,12 +53,14 @@ public class PowerCostCalculationServiceTests
// Act
var tasks = Enumerable.Range(0, threadCount)
.Select(_ => Task.Run(() =>
{
for (var i = 0; i < callsPerThread; i++)
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(1));
}))
.ToArray();
.Select(_ => Task.Run(() =>
{
for (var i = 0; i < callsPerThread; i++)
{
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(1));
}
}))
.ToArray();
await Task.WhenAll(tasks);
@@ -73,14 +79,12 @@ public class PowerCostCalculationServiceTests
double pricePerKwh,
int pollIntervalSeconds = PowerMonitorConsts.DefaultPollIntervalSeconds)
{
return powerWatts / PowerMonitorConsts.WattsPerKilowatt
* pricePerKwh
* ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
return powerWatts / PowerMonitorConsts.WattsPerKilowatt * pricePerKwh * ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
}
#region CalculateCostIncrement
public static TheoryData<string, double, double, int, double> CostIncrementCases => new()
public static TheoryData<string, double, double, int, double> CostIncrementCases => new TheoryData<string, double, double, int, double>
{
{ "default polling interval", 341.97, 0.1791, 15, 0.0002551951125 },
{ "one kilowatt for one hour", 1000.0, 1.0, PowerMonitorConsts.SecondsPerHour, 1.0 },
@@ -121,14 +125,17 @@ public class PowerCostCalculationServiceTests
// Act
var costs = samples
.Select(watts =>
PowerCostCalculationService.CalculateCostIncrement(watts, pricePerKwh, pollIntervalSeconds))
.ToArray();
.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
@@ -144,13 +151,14 @@ public class PowerCostCalculationServiceTests
// Act
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
now);
now);
// Assert
var expectedTotal =
ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
var expectedOther = ExpectedCost(PowerMonitorTestData.DefaultSmoothedOtherWatts,
PowerMonitorTestData.DefaultPricePerKwh);
PowerMonitorTestData.DefaultPricePerKwh);
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expectedTotal, 1e-12);
_sut.GetOtherCostSnapshot().CostDaily.Should().BeApproximately(expectedOther, 1e-12);
@@ -168,7 +176,7 @@ public class PowerCostCalculationServiceTests
// Act
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
now);
now);
// Assert
var snapshot = _sut.GetDeviceCostSnapshot(entityId);
@@ -187,7 +195,7 @@ public class PowerCostCalculationServiceTests
// Act
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
now);
now);
// Assert
var total = _sut.GetTotalCostSnapshot();
@@ -218,15 +226,16 @@ public class PowerCostCalculationServiceTests
{
// Arrange
var now = PowerMonitorTestData.FixedNow;
var firstResult = PowerMonitorTestData.CreateCalculation(
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
// Act
_sut.RecordCosts(firstResult, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 15,
now.AddSeconds(15));
now.AddSeconds(15));
// Assert
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
@@ -242,7 +251,8 @@ public class PowerCostCalculationServiceTests
// Act
var singleDeviceResult = PowerMonitorTestData.CreateCalculation(
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 101.0)));
_sut.RecordCosts(singleDeviceResult, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(15));
// Assert
@@ -270,10 +280,11 @@ public class PowerCostCalculationServiceTests
{
// Arrange
var now = PowerMonitorTestData.FixedNow;
var result = PowerMonitorTestData.CreateCalculation(
0,
0,
PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 0)));
0,
0,
PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 0)));
// Act
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
@@ -290,8 +301,9 @@ public class PowerCostCalculationServiceTests
{
// 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)));
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.glitch_power", -500.0)));
// Act
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
@@ -382,17 +394,19 @@ public class PowerCostCalculationServiceTests
const double pricePerKwh = 0.2;
var timestamps = PowerMonitorTestData.CreateSampleTimestamps(PowerMonitorTestData.FixedDayStart, 96);
var wattSamples = PowerMonitorTestData.CreatePowerSampleSeries();
var expectedPerSlot = timestamps
.Select((_, index) => ExpectedCost(wattSamples[index], pricePerKwh))
.ToArray();
.Select((_, index) => ExpectedCost(wattSamples[index], pricePerKwh))
.ToArray();
// Act
for (var slot = 0; slot < timestamps.Count; slot++)
{
var result = PowerMonitorTestData.CreateCalculation(
wattSamples[slot],
wattSamples[slot] / 2,
new Dictionary<string, double>());
wattSamples[slot],
wattSamples[slot] / 2,
new Dictionary<string, double>());
_sut.RecordCosts(result, pricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, timestamps[slot]);
}
@@ -416,10 +430,12 @@ public class PowerCostCalculationServiceTests
foreach (var (start, pricePerKwh) in windows)
{
var watts = 500.0 + start.Hour * 125.0;
var result = PowerMonitorTestData.CreateCalculation(
watts,
watts / 4,
PowerMonitorTestData.CreateReadings(("sensor.tv_power", watts / 2)));
watts,
watts / 4,
PowerMonitorTestData.CreateReadings(("sensor.tv_power", watts / 2)));
_sut.RecordCosts(result, pricePerKwh, pollIntervalSeconds, start);
expectedPerHour.Add(ExpectedCost(watts, pricePerKwh, pollIntervalSeconds));
}
@@ -476,8 +492,10 @@ public class PowerCostCalculationServiceTests
// 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);
}