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:
2026-11-29 21:30:13 +02:00
parent 1152462765
commit 5777e641f6
79 changed files with 7162 additions and 3196 deletions
@@ -5,164 +5,303 @@ namespace PowerMonitor.Manager.Tests;
public class CostAccumulatorTests
{
public enum CostWindow
{
FifteenMinutes,
Hour,
Day,
Month
}
#region AddCost and GetSnapshot
[Fact]
public void GetSnapshot_WhenNew_ShouldReturnZeroCosts()
{
var sut = new CostAccumulator(new DateTime(2026, 3, 17, 10, 0, 0));
// Arrange
var sut = new CostAccumulator(PowerMonitorTestData.FixedNow);
// Act
var snapshot = sut.GetSnapshot();
// Assert
snapshot.Cost15Min.Should().Be(0);
snapshot.Cost1Hour.Should().Be(0);
snapshot.CostDaily.Should().Be(0);
snapshot.CostMonthly.Should().Be(0);
snapshot.Previous15Min.Should().Be(0);
snapshot.Previous1Hour.Should().Be(0);
snapshot.PreviousDaily.Should().Be(0);
snapshot.PreviousMonthly.Should().Be(0);
snapshot.PartialWindow.Should().BeTrue();
}
[Fact]
public void AddCost_ShouldAccumulateInAllWindows()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
// Act
sut.AddCost(0.05, now.AddSeconds(15));
sut.AddCost(0.03, now.AddSeconds(30));
// Assert
var snapshot = sut.GetSnapshot();
snapshot.Cost15Min.Should().BeApproximately(0.08, 0.0001);
snapshot.Cost1Hour.Should().BeApproximately(0.08, 0.0001);
snapshot.CostDaily.Should().BeApproximately(0.08, 0.0001);
snapshot.CostMonthly.Should().BeApproximately(0.08, 0.0001);
snapshot.Cost15Min.Should().BeApproximately(0.08, 1e-9);
snapshot.Cost1Hour.Should().BeApproximately(0.08, 1e-9);
snapshot.CostDaily.Should().BeApproximately(0.08, 1e-9);
snapshot.CostMonthly.Should().BeApproximately(0.08, 1e-9);
snapshot.PartialWindow.Should().BeTrue();
}
#endregion
#region 15-minute window resets
[Fact]
public void AddCost_When15MinWindowCrosses_ShouldResetAndStorePrevious()
public void AddCost_WithGeneratedSeriesInSameWindow_ShouldSumEveryIncrement()
{
var now = new DateTime(2026, 3, 17, 10, 14, 0);
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
sut.AddCost(0.10, now);
var increments = PowerMonitorTestData.CreatePowerSampleSeries(50)
.Select(watts => watts / 100_000.0)
.ToArray();
sut.AddCost(0.02, new DateTime(2026, 3, 17, 10, 15, 0));
// Act
foreach (var increment in increments)
{
sut.AddCost(increment, now.AddSeconds(1));
}
// Assert
var expected = increments.Sum();
var snapshot = sut.GetSnapshot();
snapshot.Cost15Min.Should().BeApproximately(0.02, 0.0001);
snapshot.Previous15Min.Should().BeApproximately(0.10, 0.0001);
snapshot.PartialWindow.Should().BeFalse();
snapshot.Cost15Min.Should().BeApproximately(expected, 1e-9);
snapshot.Cost1Hour.Should().BeApproximately(expected, 1e-9);
snapshot.CostDaily.Should().BeApproximately(expected, 1e-9);
snapshot.CostMonthly.Should().BeApproximately(expected, 1e-9);
snapshot.PartialWindow.Should().BeTrue();
}
#endregion
#region Hourly window resets
[Fact]
public void AddCost_WhenHourWindowCrosses_ShouldResetAndStorePrevious()
public void AddCost_WithNegativeIncrement_ShouldReduceEveryWindow()
{
var now = new DateTime(2026, 3, 17, 10, 59, 0);
// Arrange - a meter that corrects itself downwards must not be clamped by the accumulator.
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
sut.AddCost(0.50, now);
sut.AddCost(0.01, new DateTime(2026, 3, 17, 11, 0, 0));
// Act
sut.AddCost(-0.20, now.AddSeconds(15));
// Assert
var snapshot = sut.GetSnapshot();
snapshot.Cost1Hour.Should().BeApproximately(0.01, 0.0001);
snapshot.Previous1Hour.Should().BeApproximately(0.50, 0.0001);
snapshot.Cost15Min.Should().BeApproximately(0.30, 1e-9);
snapshot.Cost1Hour.Should().BeApproximately(0.30, 1e-9);
snapshot.CostDaily.Should().BeApproximately(0.30, 1e-9);
snapshot.CostMonthly.Should().BeApproximately(0.30, 1e-9);
}
[Fact]
public void AddCost_WithVeryLargeIncrement_ShouldPreserveTheValue()
{
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
// Act
sut.AddCost(1_000_000.0, now);
// Assert
sut.GetSnapshot().CostMonthly.Should().Be(1_000_000.0);
}
[Fact]
public void AddCost_WithinSameWindows_ShouldNotPopulatePreviousValues()
{
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
// Act
sut.AddCost(0.01, now);
sut.AddCost(0.01, now.AddSeconds(15));
sut.AddCost(0.01, now.AddMinutes(14));
// Assert
var snapshot = sut.GetSnapshot();
snapshot.Previous15Min.Should().Be(0);
snapshot.Previous1Hour.Should().Be(0);
snapshot.PreviousDaily.Should().Be(0);
snapshot.PreviousMonthly.Should().Be(0);
snapshot.PartialWindow.Should().BeTrue();
}
#endregion
#region Daily window resets
#region Window rollovers
[Fact]
public void AddCost_WhenDayChanges_ShouldResetDailyAndStorePrevious()
public static IEnumerable<object[]> WindowRolloverCases =>
[
["15 minute slot ends", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 14, 0), new DateTime(2026, 3, 17, 10, 15, 0), 0.10, 0.02],
["15 minute slot ends across the hour", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.10, 0.02],
["15 minute slot ends at midnight", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.10, 0.02],
["15 minute slot ends on DST spring forward", CostWindow.FifteenMinutes, new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.10, 0.02],
["15 minute slot ends on DST fall back", CostWindow.FifteenMinutes, new DateTime(2026, 10, 25, 2, 45, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.10, 0.02],
["hour ends", CostWindow.Hour, new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.50, 0.01],
["hour ends at midnight", CostWindow.Hour, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.50, 0.01],
["hour ends on DST spring forward", CostWindow.Hour, new DateTime(2026, 3, 29, 2, 59, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.50, 0.01],
["hour ends on DST fall back", CostWindow.Hour, new DateTime(2026, 10, 25, 2, 59, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.50, 0.01],
["day changes", CostWindow.Day, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 5.00, 0.01],
["day changes at month end", CostWindow.Day, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 5.00, 0.01],
["day changes at year end", CostWindow.Day, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 5.00, 0.01],
["month changes", CostWindow.Month, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 100.00, 0.01],
["year changes", CostWindow.Month, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 100.00, 0.01],
["month changes on leap day", CostWindow.Month, new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), 100.00, 0.01]
];
[Theory]
[MemberData(nameof(WindowRolloverCases))]
public void AddCost_WhenWindowRollsOver_ShouldResetCurrentAndKeepPrevious(
string scenario,
CostWindow window,
DateTime firstTimestamp,
DateTime rolloverTimestamp,
double beforeRolloverCost,
double afterRolloverCost)
{
var now = new DateTime(2026, 3, 17, 23, 59, 0);
var sut = new CostAccumulator(now);
sut.AddCost(5.00, now);
// Arrange
var sut = new CostAccumulator(firstTimestamp);
sut.AddCost(beforeRolloverCost, firstTimestamp);
sut.AddCost(0.01, new DateTime(2026, 3, 18, 0, 0, 0));
// Act
sut.AddCost(afterRolloverCost, rolloverTimestamp);
// Assert
var snapshot = sut.GetSnapshot();
snapshot.CostDaily.Should().BeApproximately(0.01, 0.0001);
snapshot.PreviousDaily.Should().BeApproximately(5.00, 0.0001);
GetCurrent(snapshot, window).Should().BeApproximately(afterRolloverCost, 1e-9, $"scenario: {scenario}");
GetPrevious(snapshot, window).Should().BeApproximately(beforeRolloverCost, 1e-9, $"scenario: {scenario}");
snapshot.PartialWindow.Should().BeFalse($"scenario: {scenario}");
}
#endregion
[Fact]
public void AddCost_WhenAllWindowBoundariesCoincide_ShouldResetEveryWindow()
{
// Arrange
var lastSampleTime = new DateTime(2026, 3, 31, 23, 59, 0);
var rolloverTime = new DateTime(2026, 4, 1, 0, 0, 0);
var sut = new CostAccumulator(lastSampleTime);
sut.AddCost(12.34, lastSampleTime);
#region Monthly window resets
// Act
sut.AddCost(0.56, rolloverTime);
// Assert
var snapshot = sut.GetSnapshot();
snapshot.Cost15Min.Should().BeApproximately(0.56, 1e-9);
snapshot.Cost1Hour.Should().BeApproximately(0.56, 1e-9);
snapshot.CostDaily.Should().BeApproximately(0.56, 1e-9);
snapshot.CostMonthly.Should().BeApproximately(0.56, 1e-9);
snapshot.Previous15Min.Should().BeApproximately(12.34, 1e-9);
snapshot.Previous1Hour.Should().BeApproximately(12.34, 1e-9);
snapshot.PreviousDaily.Should().BeApproximately(12.34, 1e-9);
snapshot.PreviousMonthly.Should().BeApproximately(12.34, 1e-9);
}
[Fact]
public void AddCost_WhenMonthChanges_ShouldResetMonthlyAndStorePrevious()
public void AddCost_AfterFirstRollover_ShouldKeepPartialWindowFalse()
{
var now = new DateTime(2026, 3, 31, 23, 59, 0);
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
sut.AddCost(100.00, now);
sut.AddCost(0.10, now);
sut.AddCost(0.01, new DateTime(2026, 4, 1, 0, 0, 0));
// Act
sut.AddCost(0.10, now.AddMinutes(15));
sut.AddCost(0.10, now.AddMinutes(30));
var snapshot = sut.GetSnapshot();
snapshot.CostMonthly.Should().BeApproximately(0.01, 0.0001);
snapshot.PreviousMonthly.Should().BeApproximately(100.00, 0.0001);
// Assert
sut.GetSnapshot().PartialWindow.Should().BeFalse();
}
#endregion
#region Window boundary detection (static methods)
[Theory]
[InlineData(10, 0, 10, 14, false)]
[InlineData(10, 0, 10, 15, true)]
[InlineData(10, 14, 10, 15, true)]
[InlineData(10, 29, 10, 30, true)]
[InlineData(10, 44, 10, 45, true)]
[InlineData(10, 45, 10, 59, false)]
[InlineData(10, 45, 11, 0, true)]
public void IsNew15MinWindow_ShouldDetectBoundary(int lastHour, int lastMin, int nowHour, int nowMin, bool expected)
{
var lastReset = new DateTime(2026, 3, 17, lastHour, lastMin, 0);
var now = new DateTime(2026, 3, 17, nowHour, nowMin, 0);
public static IEnumerable<object[]> FifteenMinuteBoundaryCases =>
[
["same slot, first second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false],
["same slot, last second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 14, 59), false],
["slot boundary 14 -> 15", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 15, 0), true],
["slot boundary 29 -> 30", new DateTime(2026, 3, 17, 10, 29, 0), new DateTime(2026, 3, 17, 10, 30, 0), true],
["slot boundary 44 -> 45", new DateTime(2026, 3, 17, 10, 44, 0), new DateTime(2026, 3, 17, 10, 45, 0), true],
["same slot, late", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 59, 59), false],
["hour boundary", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), true],
["midnight boundary", new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
["clock moved backwards", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 44, 0), true],
["DST spring forward", new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), true]
];
CostAccumulator.IsNew15MinWindow(now, lastReset).Should().Be(expected);
[Theory]
[MemberData(nameof(FifteenMinuteBoundaryCases))]
public void IsNew15MinWindow_ShouldDetectBoundary(string scenario, DateTime lastReset, DateTime now, bool expected)
{
CostAccumulator.IsNew15MinWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
}
[Theory]
[InlineData(10, 10, false)]
[InlineData(10, 11, true)]
[InlineData(23, 0, true)]
public void IsNewHourWindow_ShouldDetectBoundary(int lastHour, int nowHour, bool expected)
{
var lastReset = new DateTime(2026, 3, 17, lastHour, 0, 0);
var now = new DateTime(2026, 3, 17, nowHour, 0, 0);
public static IEnumerable<object[]> HourBoundaryCases =>
[
["same hour, same minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false],
["same hour, later minute", new DateTime(2026, 3, 17, 10, 10, 0), new DateTime(2026, 3, 17, 10, 11, 0), false],
["same hour, last minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 59, 59), false],
["next hour", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 11, 0, 0), true],
["next hour, late minute", new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 59, 0), true],
["midnight boundary", new DateTime(2026, 3, 17, 23, 0, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
["clock moved backwards", new DateTime(2026, 3, 17, 11, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), true],
["DST spring forward", new DateTime(2026, 3, 29, 2, 0, 0), new DateTime(2026, 3, 29, 3, 0, 0), true],
["DST fall back", new DateTime(2026, 10, 25, 2, 0, 0), new DateTime(2026, 10, 25, 3, 0, 0), true]
];
CostAccumulator.IsNewHourWindow(now, lastReset).Should().Be(expected);
[Theory]
[MemberData(nameof(HourBoundaryCases))]
public void IsNewHourWindow_ShouldDetectBoundary(string scenario, DateTime lastReset, DateTime now, bool expected)
{
CostAccumulator.IsNewHourWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
}
[Theory]
[InlineData(17, 17, false)]
[InlineData(17, 18, true)]
public void IsNewDayWindow_ShouldDetectBoundary(int lastDay, int nowDay, bool expected)
{
var lastReset = new DateTime(2026, 3, lastDay, 0, 0, 0);
var now = new DateTime(2026, 3, nowDay, 0, 0, 0);
public static IEnumerable<object[]> DayBoundaryCases =>
[
["same timestamp", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 0, 0, 0), false],
["same day, last second", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 23, 59, 59), false],
["next day", new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
["month rollover", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true],
["year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true],
["leap day rollover", new DateTime(2028, 2, 28, 23, 59, 0), new DateTime(2028, 2, 29, 0, 0, 0), true],
["DST spring forward day", new DateTime(2026, 3, 28, 23, 59, 0), new DateTime(2026, 3, 29, 0, 0, 0), true],
["DST fall back day", new DateTime(2026, 10, 24, 23, 59, 0), new DateTime(2026, 10, 25, 0, 0, 0), true]
];
CostAccumulator.IsNewDayWindow(now, lastReset).Should().Be(expected);
[Theory]
[MemberData(nameof(DayBoundaryCases))]
public void IsNewDayWindow_ShouldDetectBoundary(string scenario, DateTime lastReset, DateTime now, bool expected)
{
CostAccumulator.IsNewDayWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
}
[Theory]
[InlineData(3, 3, false)]
[InlineData(3, 4, true)]
[InlineData(12, 1, true)]
public void IsNewMonthWindow_ShouldDetectBoundary(int lastMonth, int nowMonth, bool expected)
{
var lastReset = new DateTime(2026, lastMonth, 1, 0, 0, 0);
var now = new DateTime(2026, nowMonth, 1, 0, 0, 0);
public static IEnumerable<object[]> MonthBoundaryCases =>
[
["same month, first day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 1, 0, 0, 0), false],
["same month, last day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 59), false],
["next month", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true],
["clock moved into previous month", new DateTime(2026, 4, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 0), true],
["year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true],
["leap day rollover", new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), true]
];
CostAccumulator.IsNewMonthWindow(now, lastReset).Should().Be(expected);
[Theory]
[MemberData(nameof(MonthBoundaryCases))]
public void IsNewMonthWindow_ShouldDetectBoundary(string scenario, DateTime lastReset, DateTime now, bool expected)
{
CostAccumulator.IsNewMonthWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
}
#endregion
@@ -172,12 +311,14 @@ public class CostAccumulatorTests
[Fact]
public async Task AddCost_ConcurrentAccess_ShouldNotLoseData()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
// Arrange
var now = PowerMonitorTestData.FixedNow;
var sut = new CostAccumulator(now);
const double incrementPerCall = 0.001;
const int callsPerThread = 1000;
const int threadCount = 10;
// Act
var tasks = Enumerable.Range(0, threadCount)
.Select(_ => Task.Run(() =>
{
@@ -190,10 +331,29 @@ public class CostAccumulatorTests
await Task.WhenAll(tasks);
// Assert
var snapshot = sut.GetSnapshot();
var expected = incrementPerCall * callsPerThread * threadCount;
snapshot.CostMonthly.Should().BeApproximately(expected, 0.0001);
}
#endregion
private static double GetCurrent(CostSnapshot snapshot, CostWindow window) => window switch
{
CostWindow.FifteenMinutes => snapshot.Cost15Min,
CostWindow.Hour => snapshot.Cost1Hour,
CostWindow.Day => snapshot.CostDaily,
CostWindow.Month => snapshot.CostMonthly,
_ => throw new ArgumentOutOfRangeException(nameof(window))
};
private static double GetPrevious(CostSnapshot snapshot, CostWindow window) => window switch
{
CostWindow.FifteenMinutes => snapshot.Previous15Min,
CostWindow.Hour => snapshot.Previous1Hour,
CostWindow.Day => snapshot.PreviousDaily,
CostWindow.Month => snapshot.PreviousMonthly,
_ => throw new ArgumentOutOfRangeException(nameof(window))
};
}