Files
HomeAssistant/ShowerMonitor.Manager.Tests/HumidityRateCalculatorTests.cs
T
gitea_admin cf8c9434c8 Clear the residual Qodana findings
- Drop the leftover unused using, parameter and date-time attribute reader
- Merge the remaining enum guard into its pattern
- Make the NordPool price DTO init-only
- Replace the four non-serializable theory matrices with single tests that
  iterate the same rows, so xUnit can still report them
- Replace Array.Empty with collection expressions and drop unused locals,
  redundant default arguments and duplicated tuple names
- Throw from the unreachable shower default instead of an empty section
- Cover the configurable PowerMonitor option overrides in a test so the
  init setters have a caller
2026-11-29 21:49:21 +02:00

129 lines
4.8 KiB
C#

using FluentAssertions;
using Microsoft.Extensions.Time.Testing;
using ShowerMonitor.Models;
using ShowerMonitorManager;
namespace ShowerMonitor.Manager.Tests;
public class HumidityRateCalculatorTests
{
private const int DefaultWindowSize = ShowerMonitorConsts.DefaultRateWindowSize;
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
private DateTimeOffset Now => _timeProvider.GetUtcNow();
public static TheoryData<string, int, double[], int[], double> RateCases => new()
{
{ "no readings", 5, [], [], 0.0 },
{ "single reading", 5, [52.9], [0], 0.0 },
{ "rising humidity", 5, [52.9, 62.9], [0, 60], 10.0 },
{ "falling humidity", 5, [62.9, 52.9], [0, 120], -5.0 },
{ "same timestamp", 5, [52.9, 62.9], [0, 0], 0.0 },
{ "flat humidity", 5, [55.0, 55.0, 55.0], [0, 60, 120], 0.0 },
{ "linear three readings", 5, [50.0, 55.0, 60.0], [0, 60, 120], 5.0 },
{ "window drops stalest reading", 3, [52.9, 57.9, 62.9, 64.9], [0, 60, 120, 180], 3.5 },
{ "window keeps only last two readings", 2, [50.0, 55.0, 60.0], [0, 60, 120], 5.0 },
{ "single-sample window always returns zero", 1, [50.0, 55.0, 60.0], [0, 60, 120], 0.0 },
{ "zero-sized window always returns zero", 0, [50.0, 55.0, 60.0], [0, 60, 120], 0.0 },
{ "gap larger than window", 5, [20.0, 26.0], [0, 3_600], 0.1 },
{ "out of order timestamps", 5, [60.0, 50.0], [120, 0], 0.0 },
{ "very large rise", 5, [0.0, 10_000.0], [0, 60], 10_000.0 },
{ "negative humidity values", 5, [-20.0, 0.0], [0, 60], 20.0 },
{ "tiny rise", 5, [50.0, 50.001], [0, 60], 0.001 },
{ "shower spike over sixty seconds", 5, [52.9, 59.9, 64.9], [0, 30, 60], 12.0 },
{ "middle readings do not affect the window endpoints", 3, [40.0, 80.0, 60.0, 45.0], [0, 30, 60, 90], -35.0 },
{ "small change over fifteen minutes", 5, [50.0, 50.6], [0, 900], 0.04 }
};
[Theory]
[MemberData(nameof(RateCases))]
public void GetRatePerMinute_ShouldReturnExpectedRate(
string scenario,
int windowSize,
double[] humidities,
int[] offsetSeconds,
double expectedRate)
{
humidities.Should().HaveSameCount(offsetSeconds);
var calculator = new HumidityRateCalculator(windowSize);
for (var i = 0; i < humidities.Length; i++)
{
calculator.RecordReading(humidities[i], Now.AddSeconds(offsetSeconds[i]));
}
calculator.GetRatePerMinute().Should().BeApproximately(expectedRate, 1e-6, $"scenario: {scenario}");
}
[Fact]
public void GetRatePerMinute_WithGeneratedLinearSeries_ReturnsSlopeForEveryWindowSize()
{
const double ratePerMinute = 2.5;
var series = ShowerMonitorTestData.CreateLinearHumiditySeries(
startHumidity: 45.0,
ratePerMinute: ratePerMinute,
sampleCount: 12);
for (var windowSize = 2; windowSize <= series.Count; windowSize++)
{
var calculator = new HumidityRateCalculator(windowSize);
foreach (var sample in series)
{
calculator.RecordReading(sample.Humidity, Now + sample.Offset);
}
calculator.GetRatePerMinute()
.Should()
.BeApproximately(ratePerMinute, 1e-9, $"window size {windowSize}");
}
}
[Fact]
public void GetRatePerMinute_AfterReset_AndWithASingleNewReading_ReturnsZero()
{
var calculator = new HumidityRateCalculator(DefaultWindowSize);
calculator.RecordReading(52.9, Now);
calculator.RecordReading(62.9, Now.AddMinutes(1));
calculator.Reset();
calculator.RecordReading(70.0, Now.AddMinutes(2));
calculator.GetRatePerMinute().Should().Be(0.0);
}
[Fact]
public void PreviousReading_IsNullBeforeAnyReading()
{
var calculator = new HumidityRateCalculator(DefaultWindowSize);
calculator.PreviousReading.Should().BeNull();
}
[Fact]
public void PreviousReading_TracksMostRecentReadingEvenAfterWindowEviction()
{
var calculator = new HumidityRateCalculator(2);
calculator.RecordReading(52.9, Now);
calculator.RecordReading(57.9, Now.AddMinutes(1));
calculator.RecordReading(64.9, Now.AddMinutes(2));
calculator.PreviousReading.Should().Be(64.9);
}
[Fact]
public void Reset_ClearsReadingsAndPreviousReading()
{
var calculator = new HumidityRateCalculator(DefaultWindowSize);
calculator.RecordReading(52.9, Now);
calculator.RecordReading(62.9, Now.AddMinutes(1));
calculator.Reset();
calculator.GetRatePerMinute().Should().Be(0.0);
calculator.PreviousReading.Should().BeNull();
}
}