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:
@@ -1,34 +1,34 @@
|
||||
<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.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.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="..\LeakMonitor.Manager\LeakMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\LeakMonitor.Models\LeakMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LeakMonitor.Manager\LeakMonitor.Manager.csproj"/>
|
||||
<ProjectReference Include="..\LeakMonitor.Models\LeakMonitor.Models.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
using FluentAssertions;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
using LeakMonitorManager.Interface;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
public class LeakMonitorInitTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILeakNotificationService> _notificationServiceMock = new();
|
||||
private readonly Mock<ILogger<LeakMonitorInit>> _loggerMock = new();
|
||||
private readonly Mock<ILeakNotificationService> _notificationServiceMock = new();
|
||||
private readonly LeakMonitorTestData.TrackingObservable<StateChange> _stateChanges = new();
|
||||
|
||||
public LeakMonitorInitTests()
|
||||
@@ -145,7 +145,7 @@ public class LeakMonitorInitTests
|
||||
[Fact]
|
||||
public void InitializeAsync_WhenNoSensorsConfigured_ShouldNotSubscribeAndLogWarning()
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: []));
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions([]));
|
||||
|
||||
sut.Should().NotBeNull();
|
||||
_stateChanges.SubscriptionCount.Should().Be(0);
|
||||
@@ -169,7 +169,7 @@ public class LeakMonitorInitTests
|
||||
[MemberData(nameof(LeakMonitorTestData.BlankSensorIds), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WithBlankSensorId_ShouldStillSubscribeAndForward(string sensorEntityId)
|
||||
{
|
||||
CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: [sensorEntityId]));
|
||||
CreateSut(LeakMonitorTestData.CreateOptions([sensorEntityId]));
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(1);
|
||||
|
||||
@@ -186,7 +186,7 @@ public class LeakMonitorInitTests
|
||||
public void InitializeAsync_WithNullSensorEntry_ShouldStillSubscribeRemainingSensors()
|
||||
{
|
||||
CreateSut(LeakMonitorTestData.CreateOptions(
|
||||
sensorEntityIds: [null!, LeakMonitorTestData.BoilerSensorId]));
|
||||
[null!, LeakMonitorTestData.BoilerSensorId]));
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(2);
|
||||
|
||||
@@ -294,9 +294,7 @@ public class LeakMonitorInitTests
|
||||
_haContextMock.Object,
|
||||
LeakMonitorTestData.AquariumSensorId,
|
||||
LeakMonitorTestData.DetectionSequence))
|
||||
{
|
||||
_stateChanges.Publish(change);
|
||||
}
|
||||
|
||||
_notificationServiceMock.Verify(
|
||||
n => n.SendLeakAlert(LeakMonitorTestData.AquariumSensorId),
|
||||
@@ -352,4 +350,4 @@ public class LeakMonitorInitTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
using System.Text.Json;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Models;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic builders and datasets used by the LeakMonitor tests.
|
||||
/// Keeping entity/state construction, notification payload capture and generated
|
||||
/// input rows here means the test classes only describe the scenario under test.
|
||||
/// Shared, deterministic builders and datasets used by the LeakMonitor tests.
|
||||
/// Keeping entity/state construction, notification payload capture and generated
|
||||
/// input rows here means the test classes only describe the scenario under test.
|
||||
/// </summary>
|
||||
internal static class LeakMonitorTestData
|
||||
{
|
||||
@@ -23,21 +23,7 @@ internal static class LeakMonitorTestData
|
||||
private static readonly DateTimeOffset DefaultStart = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Builds leak monitor options with production-compatible defaults.
|
||||
/// Pass <c>[]</c> for no sensors; omit the argument for the shared sensor set.
|
||||
/// </summary>
|
||||
public static LeakMonitorOptions CreateOptions(
|
||||
IEnumerable<string>? sensorEntityIds = null,
|
||||
int notificationCooldownMinutes = LeakMonitorConsts.DefaultNotificationCooldownMinutes,
|
||||
bool enabled = true) => new()
|
||||
{
|
||||
Enabled = enabled,
|
||||
SensorEntityIds = sensorEntityIds?.ToArray() ?? [.. DefaultSensorIds],
|
||||
NotificationCooldownMinutes = notificationCooldownMinutes
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Every casing of the "water detected" state, across both configured sensors.
|
||||
/// Every casing of the "water detected" state, across both configured sensors.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string?> WaterDetectedCases { get; } = new()
|
||||
{
|
||||
@@ -49,7 +35,7 @@ internal static class LeakMonitorTestData
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Every casing of the "water cleared" state, across both configured sensors.
|
||||
/// Every casing of the "water cleared" state, across both configured sensors.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string?> WaterClearedCases { get; } = new()
|
||||
{
|
||||
@@ -60,8 +46,8 @@ internal static class LeakMonitorTestData
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// States that must never send or dismiss a leak notification. Covers unavailable,
|
||||
/// unknown, blank, null and near-miss strings (the comparison is exact apart from casing).
|
||||
/// States that must never send or dismiss a leak notification. Covers unavailable,
|
||||
/// unknown, blank, null and near-miss strings (the comparison is exact apart from casing).
|
||||
/// </summary>
|
||||
public static TheoryData<string?> NonActionableStateCases { get; } =
|
||||
[
|
||||
@@ -83,8 +69,8 @@ internal static class LeakMonitorTestData
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Blank but non-null sensor ids. Production does not validate ids, so these are
|
||||
/// subscribed and forwarded verbatim.
|
||||
/// Blank but non-null sensor ids. Production does not validate ids, so these are
|
||||
/// subscribed and forwarded verbatim.
|
||||
/// </summary>
|
||||
public static TheoryData<string> BlankSensorIds { get; } =
|
||||
[
|
||||
@@ -94,8 +80,8 @@ internal static class LeakMonitorTestData
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// (sensor id, expected notification id) rows for the id sanitising rules:
|
||||
/// dots become underscores, other characters and blanks are preserved.
|
||||
/// (sensor id, expected notification id) rows for the id sanitising rules:
|
||||
/// dots become underscores, other characters and blanks are preserved.
|
||||
/// </summary>
|
||||
public static TheoryData<string, string> NotificationIdCases { get; } = new()
|
||||
{
|
||||
@@ -110,9 +96,9 @@ internal static class LeakMonitorTestData
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// (cooldown minutes, elapsed seconds before the second alert, expected notification count)
|
||||
/// rows straddling the strict <c>elapsed < cooldown</c> boundary, plus zero, negative
|
||||
/// and int-max cooldowns.
|
||||
/// (cooldown minutes, elapsed seconds before the second alert, expected notification count)
|
||||
/// rows straddling the strict <c>elapsed < cooldown</c> boundary, plus zero, negative
|
||||
/// and int-max cooldowns.
|
||||
/// </summary>
|
||||
public static TheoryData<int, int, int> CooldownBoundaryCases { get; } = new()
|
||||
{
|
||||
@@ -130,7 +116,7 @@ internal static class LeakMonitorTestData
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Repetition counts used to exercise duplicate suppression and zero-cooldown behaviour.
|
||||
/// Repetition counts used to exercise duplicate suppression and zero-cooldown behaviour.
|
||||
/// </summary>
|
||||
public static TheoryData<int> RepeatedNotificationCounts { get; } =
|
||||
[
|
||||
@@ -141,8 +127,8 @@ internal static class LeakMonitorTestData
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// A deterministic detection/recovery sequence. The repeated "on" transition is filtered
|
||||
/// out downstream, so the expected totals are two alerts and two dismissals.
|
||||
/// A deterministic detection/recovery sequence. The repeated "on" transition is filtered
|
||||
/// out downstream, so the expected totals are two alerts and two dismissals.
|
||||
/// </summary>
|
||||
public static (string? OldState, string? NewState)[] DetectionSequence { get; } =
|
||||
[
|
||||
@@ -155,30 +141,55 @@ internal static class LeakMonitorTestData
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Builds an <see cref="EntityState"/>, or <c>null</c> when the entity has no state at all.
|
||||
/// Builds leak monitor options with production-compatible defaults.
|
||||
/// Pass <c>[]</c> for no sensors; omit the argument for the shared sensor set.
|
||||
/// </summary>
|
||||
private static EntityState? CreateOptionalState(string? state) =>
|
||||
state is null ? null : new EntityState { State = state };
|
||||
public static LeakMonitorOptions CreateOptions(
|
||||
IEnumerable<string>? sensorEntityIds = null,
|
||||
int notificationCooldownMinutes = LeakMonitorConsts.DefaultNotificationCooldownMinutes,
|
||||
bool enabled = true)
|
||||
{
|
||||
return new LeakMonitorOptions
|
||||
{
|
||||
Enabled = enabled,
|
||||
SensorEntityIds = sensorEntityIds?.ToArray() ?? [.. DefaultSensorIds],
|
||||
NotificationCooldownMinutes = notificationCooldownMinutes
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an <see cref="EntityState" />, or <c>null</c> when the entity has no state at all.
|
||||
/// </summary>
|
||||
private static EntityState? CreateOptionalState(string? state)
|
||||
{
|
||||
return state is null ? null : new EntityState { State = state };
|
||||
}
|
||||
|
||||
public static StateChange CreateStateChange(
|
||||
IHaContext haContext,
|
||||
string entityId,
|
||||
string? oldState,
|
||||
string? newState) =>
|
||||
new(new Entity(haContext, entityId), CreateOptionalState(oldState), CreateOptionalState(newState));
|
||||
string? newState)
|
||||
{
|
||||
return new StateChange(new Entity(haContext, entityId), CreateOptionalState(oldState),
|
||||
CreateOptionalState(newState));
|
||||
}
|
||||
|
||||
public static IReadOnlyList<StateChange> CreateStateChangeSequence(
|
||||
IHaContext haContext,
|
||||
string entityId,
|
||||
params (string? OldState, string? NewState)[] transitions) =>
|
||||
[.. transitions.Select(transition => CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))];
|
||||
|
||||
/// <summary>One captured <c>IHaContext.CallService</c> invocation.</summary>
|
||||
public sealed record ServiceCall(string Domain, string Service, ServiceTarget? Target, object? Data);
|
||||
params (string? OldState, string? NewState)[] transitions)
|
||||
{
|
||||
return
|
||||
[
|
||||
.. transitions.Select(transition =>
|
||||
CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))
|
||||
];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wires <see cref="IHaContext.CallService"/> on the mock and returns the captured calls
|
||||
/// in invocation order.
|
||||
/// Wires <see cref="IHaContext.CallService" /> on the mock and returns the captured calls
|
||||
/// in invocation order.
|
||||
/// </summary>
|
||||
public static List<ServiceCall> CaptureServiceCalls(Mock<IHaContext> haContextMock)
|
||||
{
|
||||
@@ -197,25 +208,40 @@ internal static class LeakMonitorTestData
|
||||
}
|
||||
|
||||
/// <summary>Serialises a captured service-call payload so tests can assert on it.</summary>
|
||||
public static JsonElement SerializePayload(object? data) => JsonSerializer.SerializeToElement(data);
|
||||
public static JsonElement SerializePayload(object? data)
|
||||
{
|
||||
return JsonSerializer.SerializeToElement(data);
|
||||
}
|
||||
|
||||
/// <summary>One captured <c>IHaContext.CallService</c> invocation.</summary>
|
||||
public sealed record ServiceCall(string Domain, string Service, ServiceTarget? Target, object? Data);
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic clock so cooldown boundaries can be crossed without real waiting.
|
||||
/// Deterministic clock so cooldown boundaries can be crossed without real waiting.
|
||||
/// </summary>
|
||||
public sealed class ManualTimeProvider(DateTimeOffset? start = null) : TimeProvider
|
||||
{
|
||||
private DateTimeOffset _utcNow = start ?? DefaultStart;
|
||||
|
||||
public override DateTimeOffset GetUtcNow() => _utcNow;
|
||||
public override DateTimeOffset GetUtcNow()
|
||||
{
|
||||
return _utcNow;
|
||||
}
|
||||
|
||||
public void Advance(TimeSpan duration) => _utcNow += duration;
|
||||
public void Advance(TimeSpan duration)
|
||||
{
|
||||
_utcNow += duration;
|
||||
}
|
||||
|
||||
public void AdvanceSeconds(int seconds) => Advance(TimeSpan.FromSeconds(seconds));
|
||||
public void AdvanceSeconds(int seconds)
|
||||
{
|
||||
Advance(TimeSpan.FromSeconds(seconds));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Observable that records subscriptions and lets tests push state changes through the
|
||||
/// same pipeline the NetDaemon runtime uses.
|
||||
/// Observable that records subscriptions and lets tests push state changes through the
|
||||
/// same pipeline the NetDaemon runtime uses.
|
||||
/// </summary>
|
||||
public sealed class TrackingObservable<T> : IObservable<T>
|
||||
{
|
||||
@@ -232,15 +258,15 @@ internal static class LeakMonitorTestData
|
||||
|
||||
public void Publish(T value)
|
||||
{
|
||||
foreach (var observer in _observers.ToArray())
|
||||
{
|
||||
observer.OnNext(value);
|
||||
}
|
||||
foreach (var observer in _observers.ToArray()) observer.OnNext(value);
|
||||
}
|
||||
|
||||
private sealed class Subscription(List<IObserver<T>> observers, IObserver<T> observer) : IDisposable
|
||||
{
|
||||
public void Dispose() => observers.Remove(observer);
|
||||
public void Dispose()
|
||||
{
|
||||
observers.Remove(observer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using FluentAssertions;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
using LeakMonitorManager.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
@@ -156,10 +156,7 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
@@ -172,10 +169,7 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(notificationCooldownMinutes: 0));
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
|
||||
VerifyMobilePush(Times.Exactly(repeats));
|
||||
VerifyPersistentCreate(Times.Exactly(repeats));
|
||||
@@ -220,10 +214,7 @@ public class LeakNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds) sut.SendLeakAlert(sensorId);
|
||||
|
||||
VerifyMobilePush(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
VerifyPersistentCreate(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
@@ -354,4 +345,4 @@ public class LeakNotificationServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user