#region
using System.Text.Json;
using LeakMonitor.Models;
using LeakMonitorManager.Models;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
#endregion
namespace LeakMonitor.Manager.Tests;
///
/// 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.
///
internal static class LeakMonitorTestData
{
public const string AquariumSensorId = "binary_sensor.aquarium_leak_sensor";
public const string BoilerSensorId = "binary_sensor.boiler_moisture";
public static readonly string[] DefaultSensorIds = [AquariumSensorId, BoilerSensorId];
private static readonly DateTimeOffset DefaultStart = new DateTimeOffset(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
///
/// Every casing of the "water detected" state, across both configured sensors.
///
public static TheoryData WaterDetectedCases { get; } = new TheoryData
{
{ AquariumSensorId, LeakMonitorConsts.WaterDetectedState },
{ AquariumSensorId, "ON" },
{ AquariumSensorId, "On" },
{ AquariumSensorId, "oN" },
{ BoilerSensorId, LeakMonitorConsts.WaterDetectedState }
};
///
/// Every casing of the "water cleared" state, across both configured sensors.
///
public static TheoryData WaterClearedCases { get; } = new TheoryData
{
{ AquariumSensorId, LeakMonitorConsts.WaterClearedState },
{ AquariumSensorId, "OFF" },
{ AquariumSensorId, "Off" },
{ BoilerSensorId, "oFf" }
};
///
/// 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).
///
public static TheoryData NonActionableStateCases { get; } =
[
"unavailable",
"UNAVAILABLE",
"unknown",
"UNKNOWN",
string.Empty,
" ",
"\t",
"open",
"idle",
"detected",
"ON ",
" off",
"1",
"true",
null
];
///
/// Blank but non-null sensor ids. Production does not validate ids, so these are
/// subscribed and forwarded verbatim.
///
public static TheoryData BlankSensorIds { get; } =
[
string.Empty,
" ",
"\t"
];
///
/// (sensor id, expected notification id) rows for the id sanitising rules:
/// dots become underscores, other characters and blanks are preserved.
///
public static TheoryData NotificationIdCases { get; } = new TheoryData
{
{ AquariumSensorId, "leak_monitor_binary_sensor_aquarium_leak_sensor" },
{ BoilerSensorId, "leak_monitor_binary_sensor_boiler_moisture" },
{ "sensor.a.b.c", "leak_monitor_sensor_a_b_c" },
{ "no_dots", "leak_monitor_no_dots" },
{ "binary_sensor.already_snake_case", "leak_monitor_binary_sensor_already_snake_case" },
{ "binary_sensor.dash-and-dot.", "leak_monitor_binary_sensor_dash-and-dot_" },
{ string.Empty, "leak_monitor_" },
{ " ", "leak_monitor_ " }
};
///
/// (cooldown minutes, elapsed seconds before the second alert, expected notification count)
/// rows straddling the strict elapsed < cooldown boundary, plus zero, negative
/// and int-max cooldowns.
///
public static TheoryData CooldownBoundaryCases { get; } = new TheoryData
{
{ 5, 0, 1 },
{ 5, 299, 1 },
{ 5, 300, 2 },
{ 5, 301, 2 },
{ 10, 599, 1 },
{ 10, 600, 2 },
{ 1, 59, 1 },
{ 1, 60, 2 },
{ 0, 0, 2 },
{ -5, 0, 2 },
{ int.MaxValue, 600, 1 }
};
///
/// Repetition counts used to exercise duplicate suppression and zero-cooldown behaviour.
///
public static TheoryData RepeatedNotificationCounts { get; } =
[
2,
3,
5,
10
];
///
/// A deterministic detection/recovery sequence. The repeated "on" transition is filtered
/// out downstream, so the expected totals are two alerts and two dismissals.
///
public static (string? OldState, string? NewState)[] DetectionSequence { get; } =
[
("off", "on"),
("on", "on"),
("on", "off"),
("off", "unavailable"),
("unavailable", "on"),
("on", "off")
];
///
/// Builds leak monitor options with production-compatible defaults.
/// Pass [] for no sensors; omit the argument for the shared sensor set.
///
public static LeakMonitorOptions CreateOptions(
IEnumerable? sensorEntityIds = null,
int notificationCooldownMinutes = LeakMonitorConsts.DefaultNotificationCooldownMinutes,
bool enabled = true)
{
return new LeakMonitorOptions
{
Enabled = enabled,
SensorEntityIds = sensorEntityIds?.ToArray() ?? [.. DefaultSensorIds],
NotificationCooldownMinutes = notificationCooldownMinutes
};
}
///
/// Builds an , or null when the entity has no state at all.
///
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)
{
return new StateChange(new Entity(haContext, entityId), CreateOptionalState(oldState),
CreateOptionalState(newState));
}
public static IReadOnlyList CreateStateChangeSequence(
IHaContext haContext,
string entityId,
params (string? OldState, string? NewState)[] transitions)
{
return
[
.. transitions.Select(transition =>
CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))
];
}
///
/// Wires on the mock and returns the captured calls
/// in invocation order.
///
public static List CaptureServiceCalls(Mock haContextMock)
{
var calls = new List();
haContextMock
.Setup(h => h.CallService(
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny