Files
HomeAssistant/PowerMonitor.Manager.Tests/CostAccumulatorTests.cs
T

200 lines
6.1 KiB
C#

using FluentAssertions;
using PowerMonitorManager.Models;
namespace PowerMonitor.Manager.Tests;
public class CostAccumulatorTests
{
#region AddCost and GetSnapshot
[Fact]
public void GetSnapshot_WhenNew_ShouldReturnZeroCosts()
{
var sut = new CostAccumulator(new DateTime(2026, 3, 17, 10, 0, 0));
var snapshot = sut.GetSnapshot();
snapshot.Cost15Min.Should().Be(0);
snapshot.Cost1Hour.Should().Be(0);
snapshot.CostDaily.Should().Be(0);
snapshot.CostMonthly.Should().Be(0);
snapshot.PartialWindow.Should().BeTrue();
}
[Fact]
public void AddCost_ShouldAccumulateInAllWindows()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var sut = new CostAccumulator(now);
sut.AddCost(0.05, now.AddSeconds(15));
sut.AddCost(0.03, now.AddSeconds(30));
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);
}
#endregion
#region 15-minute window resets
[Fact]
public void AddCost_When15MinWindowCrosses_ShouldResetAndStorePrevious()
{
var now = new DateTime(2026, 3, 17, 10, 14, 0);
var sut = new CostAccumulator(now);
sut.AddCost(0.10, now);
sut.AddCost(0.02, new DateTime(2026, 3, 17, 10, 15, 0));
var snapshot = sut.GetSnapshot();
snapshot.Cost15Min.Should().BeApproximately(0.02, 0.0001);
snapshot.Previous15Min.Should().BeApproximately(0.10, 0.0001);
snapshot.PartialWindow.Should().BeFalse();
}
#endregion
#region Hourly window resets
[Fact]
public void AddCost_WhenHourWindowCrosses_ShouldResetAndStorePrevious()
{
var now = new DateTime(2026, 3, 17, 10, 59, 0);
var sut = new CostAccumulator(now);
sut.AddCost(0.50, now);
sut.AddCost(0.01, new DateTime(2026, 3, 17, 11, 0, 0));
var snapshot = sut.GetSnapshot();
snapshot.Cost1Hour.Should().BeApproximately(0.01, 0.0001);
snapshot.Previous1Hour.Should().BeApproximately(0.50, 0.0001);
}
#endregion
#region Daily window resets
[Fact]
public void AddCost_WhenDayChanges_ShouldResetDailyAndStorePrevious()
{
var now = new DateTime(2026, 3, 17, 23, 59, 0);
var sut = new CostAccumulator(now);
sut.AddCost(5.00, now);
sut.AddCost(0.01, new DateTime(2026, 3, 18, 0, 0, 0));
var snapshot = sut.GetSnapshot();
snapshot.CostDaily.Should().BeApproximately(0.01, 0.0001);
snapshot.PreviousDaily.Should().BeApproximately(5.00, 0.0001);
}
#endregion
#region Monthly window resets
[Fact]
public void AddCost_WhenMonthChanges_ShouldResetMonthlyAndStorePrevious()
{
var now = new DateTime(2026, 3, 31, 23, 59, 0);
var sut = new CostAccumulator(now);
sut.AddCost(100.00, now);
sut.AddCost(0.01, new DateTime(2026, 4, 1, 0, 0, 0));
var snapshot = sut.GetSnapshot();
snapshot.CostMonthly.Should().BeApproximately(0.01, 0.0001);
snapshot.PreviousMonthly.Should().BeApproximately(100.00, 0.0001);
}
#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);
CostAccumulator.IsNew15MinWindow(now, lastReset).Should().Be(expected);
}
[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);
CostAccumulator.IsNewHourWindow(now, lastReset).Should().Be(expected);
}
[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);
CostAccumulator.IsNewDayWindow(now, lastReset).Should().Be(expected);
}
[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);
CostAccumulator.IsNewMonthWindow(now, lastReset).Should().Be(expected);
}
#endregion
#region Thread safety
[Fact]
public async Task AddCost_ConcurrentAccess_ShouldNotLoseData()
{
var now = new DateTime(2026, 3, 17, 10, 0, 0);
var sut = new CostAccumulator(now);
const double incrementPerCall = 0.001;
const int callsPerThread = 1000;
const int threadCount = 10;
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);
var snapshot = sut.GetSnapshot();
var expected = incrementPerCall * callsPerThread * threadCount;
snapshot.CostMonthly.Should().BeApproximately(expected, 0.0001);
}
#endregion
}