c77f854682
Made-with: Cursor
138 lines
3.9 KiB
C#
138 lines
3.9 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.Options;
|
|
using PowerMonitorManager;
|
|
using PowerMonitorManager.Models;
|
|
|
|
namespace PowerMonitor.Manager.Tests;
|
|
|
|
public class OtherPowerCalculationServiceTests
|
|
{
|
|
private static OtherPowerCalculationService CreateSut(int smoothingWindowSize = 10)
|
|
{
|
|
var options = Options.Create(new PowerMonitorOptions
|
|
{
|
|
SmoothingWindowSize = smoothingWindowSize
|
|
});
|
|
return new OtherPowerCalculationService(options);
|
|
}
|
|
|
|
#region Basic calculation
|
|
|
|
[Fact]
|
|
public void Calculate_ShouldReturnOtherAsTotalMinusDevices()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 1);
|
|
var devices = new Dictionary<string, double>
|
|
{
|
|
["sensor.device_a_power"] = 500,
|
|
["sensor.device_b_power"] = 300
|
|
};
|
|
|
|
var result = sut.Calculate(1000, devices);
|
|
|
|
result.TotalPowerWatts.Should().Be(1000);
|
|
result.KnownDevicesSumWatts.Should().Be(800);
|
|
result.RawOtherPowerWatts.Should().Be(200);
|
|
result.DataQuality.Should().Be("ok");
|
|
}
|
|
|
|
[Fact]
|
|
public void Calculate_WhenDevicesSumExceedsTotal_ShouldClampToZero()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 1);
|
|
var devices = new Dictionary<string, double>
|
|
{
|
|
["sensor.device_a_power"] = 800,
|
|
["sensor.device_b_power"] = 500
|
|
};
|
|
|
|
var result = sut.Calculate(1000, devices);
|
|
|
|
result.RawOtherPowerWatts.Should().Be(0);
|
|
result.SmoothedOtherPowerWatts.Should().Be(0);
|
|
result.DataQuality.Should().Be("device_sum_exceeds_total");
|
|
}
|
|
|
|
[Fact]
|
|
public void Calculate_WithNoDevices_ShouldReturnTotalAsOther()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 1);
|
|
var devices = new Dictionary<string, double>();
|
|
|
|
var result = sut.Calculate(1500, devices);
|
|
|
|
result.RawOtherPowerWatts.Should().Be(1500);
|
|
result.KnownDevicesSumWatts.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Calculate_ShouldIncludeAllDeviceReadingsInResult()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 1);
|
|
var devices = new Dictionary<string, double>
|
|
{
|
|
["sensor.a"] = 100,
|
|
["sensor.b"] = 200
|
|
};
|
|
|
|
var result = sut.Calculate(500, devices);
|
|
|
|
result.DevicePowerReadings.Should().HaveCount(2);
|
|
result.DevicePowerReadings["sensor.a"].Should().Be(100);
|
|
result.DevicePowerReadings["sensor.b"].Should().Be(200);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region EMA smoothing
|
|
|
|
[Fact]
|
|
public void Calculate_WithSmoothing_ShouldApplyExponentialMovingAverage()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 4);
|
|
var devices = new Dictionary<string, double>();
|
|
|
|
var result1 = sut.Calculate(1000, devices);
|
|
result1.SmoothedOtherPowerWatts.Should().BeApproximately(1000, 0.1);
|
|
|
|
var result2 = sut.Calculate(500, devices);
|
|
result2.SmoothedOtherPowerWatts.Should().BeLessThan(1000);
|
|
result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(500);
|
|
}
|
|
|
|
[Fact]
|
|
public void Calculate_MultipleCallsWithSameValue_ShouldConverge()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 5);
|
|
var devices = new Dictionary<string, double>();
|
|
|
|
PowerCalculationResult? result = null;
|
|
for (var i = 0; i < 50; i++)
|
|
{
|
|
result = sut.Calculate(300, devices);
|
|
}
|
|
|
|
result!.SmoothedOtherPowerWatts.Should().BeApproximately(300, 0.1);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Reset
|
|
|
|
[Fact]
|
|
public void Reset_ShouldClearSmoothedState()
|
|
{
|
|
var sut = CreateSut(smoothingWindowSize: 10);
|
|
var devices = new Dictionary<string, double>();
|
|
sut.Calculate(1000, devices);
|
|
sut.Calculate(1000, devices);
|
|
|
|
sut.Reset();
|
|
var result = sut.Calculate(100, devices);
|
|
|
|
result.SmoothedOtherPowerWatts.Should().BeApproximately(100, 0.1);
|
|
}
|
|
|
|
#endregion
|
|
}
|