using System.Globalization; using System.Text.Json; using BatteryMonitor.Models; using Moq; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; namespace BatteryMonitor.Manager.Tests; /// /// Shared, deterministic builders and datasets used by the BatteryMonitor tests. /// Keeping entity/state construction and generated inputs here means the test /// classes only describe the scenario under test. /// internal static class BatteryMonitorTestData { public const string DefaultFriendlyName = "Battery Device"; /// /// Describes one entity exposed through . /// public sealed record EntitySpec { public required string EntityId { get; init; } public string? State { get; init; } = "50"; public string? DeviceClass { get; init; } = BatteryMonitorConsts.BatteryDeviceClass; public string? FriendlyName { get; init; } = DefaultFriendlyName; /// When false the context returns no for the entity. public bool HasState { get; init; } = true; } /// A battery device-class sensor. public static EntitySpec Battery(string entityId, string? state, string? friendlyName = DefaultFriendlyName) { return new EntitySpec { EntityId = entityId, State = state, FriendlyName = friendlyName }; } /// A sensor with an arbitrary (or missing) device class. public static EntitySpec Sensor(string entityId, string? deviceClass, string? state, string? friendlyName = DefaultFriendlyName) { return new EntitySpec { EntityId = entityId, DeviceClass = deviceClass, State = state, FriendlyName = friendlyName }; } public static EntityState CreateEntityState(EntitySpec spec) { var attributes = new Dictionary(StringComparer.Ordinal); if (spec.DeviceClass is not null) { attributes["device_class"] = spec.DeviceClass; } if (spec.FriendlyName is not null) { attributes["friendly_name"] = spec.FriendlyName; } return new EntityState { State = spec.State, AttributesJson = JsonSerializer.SerializeToElement(attributes) }; } /// /// Wires and /// on the mock and returns the created entities in declaration order. /// public static IReadOnlyList ConfigureHaContext(Mock haContextMock, params EntitySpec[] entities) { var entityList = new List(entities.Length); foreach (var spec in entities) { if (spec.HasState) { haContextMock.Setup(h => h.GetState(spec.EntityId)).Returns(CreateEntityState(spec)); } entityList.Add(new Entity(haContextMock.Object, spec.EntityId)); } haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList); return entityList; } public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null) { return new BatterySensorInfo { EntityId = entityId, FriendlyName = friendlyName ?? entityId, BatteryLevel = batteryLevel }; } public static List CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors) { return sensors .Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel)) .ToList(); } public static List CreateSensorInfos(params (string EntityId, string FriendlyName, double BatteryLevel)[] sensors) { return sensors .Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName)) .ToList(); } /// /// Generates battery sensors with every integer level in 0..count-1. /// public static EntitySpec[] CreateGeneratedBatteryEntities(int count) { return Enumerable.Range(0, count) .Select(i => Battery( $"sensor.generated_battery_{i:D3}", i.ToString(CultureInfo.InvariantCulture), $"Generated Battery {i:D3}")) .ToArray(); } /// /// Generates notification devices with deterministic, non-monotonic levels so /// ordering behaviour is exercised over more than a handful of rows. /// public static IReadOnlyList CreateGeneratedSensorInfos(int count) { return Enumerable.Range(0, count) .Select(i => CreateSensorInfo( $"sensor.generated_{i:D3}", (i * 37) % 101, $"Generated Device {i:D3}")) .ToList(); } /// /// (threshold, state, expected) rows straddling the strict level < threshold /// boundary, including zero/negative thresholds and int min/max thresholds. /// public static TheoryData ThresholdBoundaryRows { get; } = new() { { 5, "4", true }, { 5, "5", false }, { 5, "6", false }, { 5, "0", true }, { 5, "-3", true }, { 5, "4.9", true }, { 5, "5.0", false }, { 5, "100", false }, { 1, "0.5", true }, { 1, "1", false }, { 20, "19", true }, { 20, "19.99", true }, { 20, "20", false }, { 0, "-1", true }, { 0, "0", false }, { 0, "1", false }, { -5, "-6", true }, { -5, "-5", false }, { -5, "-4", false }, { 100, "99.9", true }, { 100, "100", false }, { 101, "100", true }, { int.MaxValue, "100", true }, { int.MinValue, "-2147483648", false }, { int.MinValue, "-2147483649", true } }; /// /// States that must never produce a , including /// whitespace/malformed strings and a null state. /// public static TheoryData UnusableStateRows { get; } = new() { "unavailable", "unknown", "UNAVAILABLE", "UNKNOWN", "", " ", "\t", "not_a_number", "3%", "--1", "low", "1 2", (string?)null }; /// /// Cross-product of every excluded keyword (lower and upper case) plus /// non-excluded look-alikes that must not be filtered. /// public static TheoryData ExcludedEntityIdRows() { var data = new TheoryData(); foreach (var keyword in BatteryMonitorConsts.ExcludedKeywords) { data.Add($"sensor.my_{keyword}_battery_level", true); data.Add($"SENSOR.MY_{keyword.ToUpperInvariant()}_BATTERY_LEVEL", true); } data.Add("sensor.iphone", true); data.Add("sensor.x_phone_x", true); data.Add("sensor.bathroom_battery", false); data.Add("sensor.boiler_temp_battery", false); data.Add("sensor.aquarium_leak_sensor_battery", false); data.Add("sensor.temperature", false); data.Add("", false); data.Add(" ", false); return data; } /// Device counts used to exercise notification payload generation. public static TheoryData NotifyDeviceCounts { get; } = new() { 1, 2, 3, 5, 10, 50 }; }