17774b40b8
Replace untyped MemberData sources with TheoryData<...>, use collection expressions for array and list literals, drop redundant casts and null suppressions, and give log-assertion helpers an IsEnabled setup so guarded log calls stay verifiable.
360 lines
16 KiB
C#
360 lines
16 KiB
C#
using FluentAssertions;
|
|
using PowerMonitorManager.Models;
|
|
|
|
namespace PowerMonitor.Manager.Tests;
|
|
|
|
public class CostAccumulatorTests
|
|
{
|
|
public enum CostWindow
|
|
{
|
|
FifteenMinutes,
|
|
Hour,
|
|
Day,
|
|
Month
|
|
}
|
|
|
|
#region AddCost and GetSnapshot
|
|
|
|
[Fact]
|
|
public void GetSnapshot_WhenNew_ShouldReturnZeroCosts()
|
|
{
|
|
// 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()
|
|
{
|
|
// 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, 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();
|
|
}
|
|
|
|
[Fact]
|
|
public void AddCost_WithGeneratedSeriesInSameWindow_ShouldSumEveryIncrement()
|
|
{
|
|
// Arrange
|
|
var now = PowerMonitorTestData.FixedNow;
|
|
var sut = new CostAccumulator(now);
|
|
var increments = PowerMonitorTestData.CreatePowerSampleSeries(50)
|
|
.Select(watts => watts / 100_000.0)
|
|
.ToArray();
|
|
|
|
// 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(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();
|
|
}
|
|
|
|
[Fact]
|
|
public void AddCost_WithNegativeIncrement_ShouldReduceEveryWindow()
|
|
{
|
|
// 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);
|
|
|
|
// Act
|
|
sut.AddCost(-0.20, now.AddSeconds(15));
|
|
|
|
// Assert
|
|
var snapshot = sut.GetSnapshot();
|
|
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 Window rollovers
|
|
|
|
public static TheoryData<string, CostWindow, DateTime, DateTime, double, double> WindowRolloverCases => new()
|
|
{
|
|
{ "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)
|
|
{
|
|
// Arrange
|
|
var sut = new CostAccumulator(firstTimestamp);
|
|
sut.AddCost(beforeRolloverCost, firstTimestamp);
|
|
|
|
// Act
|
|
sut.AddCost(afterRolloverCost, rolloverTimestamp);
|
|
|
|
// Assert
|
|
var snapshot = sut.GetSnapshot();
|
|
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}");
|
|
}
|
|
|
|
[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);
|
|
|
|
// 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_AfterFirstRollover_ShouldKeepPartialWindowFalse()
|
|
{
|
|
// Arrange
|
|
var now = PowerMonitorTestData.FixedNow;
|
|
var sut = new CostAccumulator(now);
|
|
sut.AddCost(0.10, now);
|
|
|
|
// Act
|
|
sut.AddCost(0.10, now.AddMinutes(15));
|
|
sut.AddCost(0.10, now.AddMinutes(30));
|
|
|
|
// Assert
|
|
sut.GetSnapshot().PartialWindow.Should().BeFalse();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Window boundary detection (static methods)
|
|
|
|
public static TheoryData<string, DateTime, DateTime, bool> FifteenMinuteBoundaryCases => new()
|
|
{
|
|
{ "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 }
|
|
};
|
|
|
|
[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}");
|
|
}
|
|
|
|
public static TheoryData<string, DateTime, DateTime, bool> HourBoundaryCases => new()
|
|
{
|
|
{ "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 }
|
|
};
|
|
|
|
[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}");
|
|
}
|
|
|
|
public static TheoryData<string, DateTime, DateTime, bool> DayBoundaryCases => new()
|
|
{
|
|
{ "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 }
|
|
};
|
|
|
|
[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}");
|
|
}
|
|
|
|
public static TheoryData<string, DateTime, DateTime, bool> MonthBoundaryCases => new()
|
|
{
|
|
{ "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 }
|
|
};
|
|
|
|
[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
|
|
|
|
#region Thread safety
|
|
|
|
[Fact]
|
|
public async Task AddCost_ConcurrentAccess_ShouldNotLoseData()
|
|
{
|
|
// 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(() =>
|
|
{
|
|
for (var i = 0; i < callsPerThread; i++)
|
|
{
|
|
sut.AddCost(incrementPerCall, now.AddSeconds(1));
|
|
}
|
|
}))
|
|
.ToArray();
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
// Assert
|
|
var snapshot = sut.GetSnapshot();
|
|
const double 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))
|
|
};
|
|
}
|