5777e641f6
- 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
79 lines
3.8 KiB
C#
79 lines
3.8 KiB
C#
using Microsoft.Extensions.Time.Testing;
|
|
using NetDaemon.HassModel.Entities;
|
|
using ShowerMonitor.Models;
|
|
using ShowerMonitorManager.Models;
|
|
|
|
namespace ShowerMonitor.Manager.Tests;
|
|
|
|
/// <summary>
|
|
/// Shared, deterministic fixtures for the shower monitor tests.
|
|
/// Keeping options, fan states and sample series here avoids copy-pasted setup
|
|
/// across test classes and keeps every timestamp independent of the wall clock.
|
|
/// </summary>
|
|
internal static class ShowerMonitorTestData
|
|
{
|
|
/// <summary>Fixed base time used by all timestamp-sensitive tests.</summary>
|
|
public static readonly DateTimeOffset BaseTimestamp = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
/// <summary>Creates a fake clock positioned at <see cref="BaseTimestamp"/> unless overridden.</summary>
|
|
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? start = null) => new(start ?? BaseTimestamp);
|
|
|
|
/// <summary>
|
|
/// Builds monitor options from the production defaults, overriding only the
|
|
/// values a test cares about.
|
|
/// </summary>
|
|
public static ShowerMonitorOptions CreateOptions(
|
|
string exhaustFanEntityId = ShowerMonitorConsts.ExhaustFanEntityId,
|
|
int? fanRunAfterShowerMinutes = null,
|
|
double spikeRateThreshold = ShowerMonitorConsts.DefaultSpikeRateThreshold,
|
|
double minDifferentialHumidity = ShowerMonitorConsts.DefaultMinDifferentialHumidity,
|
|
double peakHumidityThreshold = ShowerMonitorConsts.DefaultPeakHumidityThreshold,
|
|
int confirmationWindowMinutes = ShowerMonitorConsts.DefaultConfirmationWindowMinutes,
|
|
double recoveryDifferentialThreshold = ShowerMonitorConsts.DefaultRecoveryDifferentialThreshold,
|
|
int? maxShowerDurationMinutes = null,
|
|
int rateWindowSize = ShowerMonitorConsts.DefaultRateWindowSize)
|
|
{
|
|
return new ShowerMonitorOptions
|
|
{
|
|
ExhaustFanEntityId = exhaustFanEntityId,
|
|
FanRunAfterShowerMinutes = fanRunAfterShowerMinutes
|
|
?? (int)ShowerMonitorConsts.DefaultFanRunAfterShower.TotalMinutes,
|
|
SpikeRateThreshold = spikeRateThreshold,
|
|
MinDifferentialHumidity = minDifferentialHumidity,
|
|
PeakHumidityThreshold = peakHumidityThreshold,
|
|
ConfirmationWindowMinutes = confirmationWindowMinutes,
|
|
RecoveryDifferentialThreshold = recoveryDifferentialThreshold,
|
|
MaxShowerDurationMinutes = maxShowerDurationMinutes
|
|
?? (int)ShowerMonitorConsts.DefaultMaxShowerDuration.TotalMinutes,
|
|
RateWindowSize = rateWindowSize
|
|
};
|
|
}
|
|
|
|
/// <summary>Creates the HA state payload for the exhaust fan.</summary>
|
|
public static EntityState FanState(string? state) => new() { State = state };
|
|
|
|
/// <summary>A humidity sample with its offset from a test's base timestamp.</summary>
|
|
public readonly record struct HumiditySample(double Humidity, TimeSpan Offset);
|
|
|
|
/// <summary>Builds a sample series from (humidity, offset-in-seconds) pairs.</summary>
|
|
public static IReadOnlyList<HumiditySample> CreateHumiditySeries(
|
|
params (double Humidity, int OffsetSeconds)[] samples) =>
|
|
samples.Select(sample => new HumiditySample(sample.Humidity, TimeSpan.FromSeconds(sample.OffsetSeconds)))
|
|
.ToArray();
|
|
|
|
/// <summary>
|
|
/// Generates evenly spaced samples along a constant humidity slope, useful for
|
|
/// exercising every window size against a known rate.
|
|
/// </summary>
|
|
public static IReadOnlyList<HumiditySample> CreateLinearHumiditySeries(
|
|
double startHumidity,
|
|
double ratePerMinute,
|
|
int sampleCount,
|
|
int intervalSeconds = 60) =>
|
|
Enumerable.Range(0, sampleCount)
|
|
.Select(i => new HumiditySample(
|
|
startHumidity + (ratePerMinute * i * intervalSeconds / 60.0),
|
|
TimeSpan.FromSeconds(i * intervalSeconds)))
|
|
.ToArray();
|
|
}
|