f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
94 lines
4.1 KiB
C#
94 lines
4.1 KiB
C#
#region
|
|
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using NetDaemon.HassModel.Entities;
|
|
using ShowerMonitor.Models;
|
|
using ShowerMonitorManager.Models;
|
|
|
|
#endregion
|
|
|
|
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>
|
|
private static readonly DateTimeOffset BaseTimestamp = new DateTimeOffset(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)
|
|
{
|
|
return new FakeTimeProvider(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)
|
|
{
|
|
return new EntityState { State = state };
|
|
}
|
|
|
|
/// <summary>Builds a sample series from (humidity, offset-in-seconds) pairs.</summary>
|
|
public static IReadOnlyList<HumiditySample> CreateHumiditySeries(
|
|
params (double Humidity, int OffsetSeconds)[] samples)
|
|
{
|
|
return
|
|
[
|
|
.. samples.Select(sample => new HumiditySample(sample.Humidity, TimeSpan.FromSeconds(sample.OffsetSeconds)))
|
|
];
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
return
|
|
[
|
|
.. Enumerable.Range(0, sampleCount)
|
|
.Select(i => new HumiditySample(
|
|
startHumidity + ratePerMinute * i * intervalSeconds / 60.0,
|
|
TimeSpan.FromSeconds(i * intervalSeconds)))
|
|
];
|
|
}
|
|
|
|
/// <summary>A humidity sample with its offset from a test's base timestamp.</summary>
|
|
public readonly record struct HumiditySample(double Humidity, TimeSpan Offset);
|
|
} |