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,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user