Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -15,8 +15,8 @@ public class ExhaustFanControlServiceTests
|
||||
private const string FanEntityId = "switch.exhaust_fan";
|
||||
|
||||
private readonly ShowerMonitorOptions _defaultOptions = ShowerMonitorTestData.CreateOptions();
|
||||
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
|
||||
|
||||
private DateTimeOffset Now => _timeProvider.GetUtcNow();
|
||||
|
||||
@@ -237,7 +237,7 @@ public class ExhaustFanControlServiceTests
|
||||
[InlineData(null)]
|
||||
public void Evaluate_WithConfiguredFanEntityId_TargetsThatExactEntity(string? entityId)
|
||||
{
|
||||
var service = CreateService(ShowerMonitorTestData.CreateOptions(exhaustFanEntityId: entityId!));
|
||||
var service = CreateService(ShowerMonitorTestData.CreateOptions(entityId!));
|
||||
|
||||
service.Evaluate(ShowerState.Active, Now);
|
||||
|
||||
@@ -264,17 +264,17 @@ public class ExhaustFanControlServiceTests
|
||||
{
|
||||
var targets = _haContextMock.Invocations
|
||||
.Where(invocation => invocation.Method.Name == nameof(IHaContext.CallService)
|
||||
&& (string?)invocation.Arguments[0] == "switch"
|
||||
&& (string?)invocation.Arguments[1] == service)
|
||||
&& (string?)invocation.Arguments[0] == "switch"
|
||||
&& (string?)invocation.Arguments[1] == service)
|
||||
.Select(invocation => (ServiceTarget?)invocation.Arguments[2])
|
||||
.ToList();
|
||||
|
||||
targets.Should().HaveCount(expectedCount, because);
|
||||
targets.Should().OnlyContain(
|
||||
target => target != null
|
||||
&& target.EntityIds != null
|
||||
&& target.EntityIds.Count == 1
|
||||
&& target.EntityIds.Single() == expectedEntityId,
|
||||
&& target.EntityIds != null
|
||||
&& target.EntityIds.Count == 1
|
||||
&& target.EntityIds.Single() == expectedEntityId,
|
||||
because);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,9 +49,7 @@ public class HumidityRateCalculatorTests
|
||||
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}");
|
||||
}
|
||||
@@ -61,18 +59,15 @@ public class HumidityRateCalculatorTests
|
||||
{
|
||||
const double ratePerMinute = 2.5;
|
||||
var series = ShowerMonitorTestData.CreateLinearHumiditySeries(
|
||||
startHumidity: 45.0,
|
||||
ratePerMinute: ratePerMinute,
|
||||
sampleCount: 12);
|
||||
45.0,
|
||||
ratePerMinute,
|
||||
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);
|
||||
}
|
||||
foreach (var sample in series) calculator.RecordReading(sample.Humidity, Now + sample.Offset);
|
||||
|
||||
calculator.GetRatePerMinute()
|
||||
.Should()
|
||||
@@ -125,4 +120,4 @@ public class HumidityRateCalculatorTests
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
calculator.PreviousReading.Should().BeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,10 +158,7 @@ public class ShowerDetectionServiceTests
|
||||
service.TodaySessionCount.Should().Be(0);
|
||||
service.LastCompletedSession.Should().BeNull();
|
||||
|
||||
if (expectedState == ShowerState.Active)
|
||||
{
|
||||
service.CurrentSession.Should().NotBeNull();
|
||||
}
|
||||
if (expectedState == ShowerState.Active) service.CurrentSession.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -508,4 +505,4 @@ public class ShowerDetectionServiceTests
|
||||
service.Evaluate(56.0, 50.0, start.AddMinutes(12));
|
||||
service.Evaluate(55.0, 50.0, start.AddMinutes(13));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="10.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="11.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.11.0" />
|
||||
<PackageReference Include="Moq" Version="4.21.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="10.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="11.0.1"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0"/>
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.11.0"/>
|
||||
<PackageReference Include="Moq" Version="4.21.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShowerMonitor.Manager\ShowerMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\ShowerMonitor.Models\ShowerMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ShowerMonitor.Manager\ShowerMonitor.Manager.csproj"/>
|
||||
<ProjectReference Include="..\ShowerMonitor.Models\ShowerMonitor.Models.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,21 +6,24 @@ 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.
|
||||
/// 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(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>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.
|
||||
/// Builds monitor options from the production defaults, overriding only the
|
||||
/// values a test cares about.
|
||||
/// </summary>
|
||||
public static ShowerMonitorOptions CreateOptions(
|
||||
string exhaustFanEntityId = ShowerMonitorConsts.ExhaustFanEntityId,
|
||||
@@ -37,40 +40,53 @@ internal static class ShowerMonitorTestData
|
||||
{
|
||||
ExhaustFanEntityId = exhaustFanEntityId,
|
||||
FanRunAfterShowerMinutes = fanRunAfterShowerMinutes
|
||||
?? (int)ShowerMonitorConsts.DefaultFanRunAfterShower.TotalMinutes,
|
||||
?? (int)ShowerMonitorConsts.DefaultFanRunAfterShower.TotalMinutes,
|
||||
SpikeRateThreshold = spikeRateThreshold,
|
||||
MinDifferentialHumidity = minDifferentialHumidity,
|
||||
PeakHumidityThreshold = peakHumidityThreshold,
|
||||
ConfirmationWindowMinutes = confirmationWindowMinutes,
|
||||
RecoveryDifferentialThreshold = recoveryDifferentialThreshold,
|
||||
MaxShowerDurationMinutes = maxShowerDurationMinutes
|
||||
?? (int)ShowerMonitorConsts.DefaultMaxShowerDuration.TotalMinutes,
|
||||
?? (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);
|
||||
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) =>
|
||||
[.. samples.Select(sample => new HumiditySample(sample.Humidity, TimeSpan.FromSeconds(sample.OffsetSeconds)))];
|
||||
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.
|
||||
/// 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)))];
|
||||
}
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user