Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
using LightMonitor.Models;
|
||||
using LightMonitorManager.Models;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
namespace LightMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic, reusable fixtures for the LightMonitor tests.
|
||||
/// Keeping the option builder, state builders and generated state tables here
|
||||
/// avoids copy-pasted setup across the test classes.
|
||||
/// </summary>
|
||||
internal static class LightMonitorTestData
|
||||
{
|
||||
public const string LightEntityId = "light.sonoff_1002214d40";
|
||||
public const string SunEntityId = "sun.sun";
|
||||
|
||||
public const string PrimaryPersonEntityId = "person.marius";
|
||||
public const string SecondaryPersonEntityId = "person.erika";
|
||||
public const string PhonePersonEntityId = "person.home_phone";
|
||||
|
||||
public static readonly string[] PersonEntityIds =
|
||||
[
|
||||
PrimaryPersonEntityId,
|
||||
SecondaryPersonEntityId,
|
||||
PhonePersonEntityId
|
||||
];
|
||||
|
||||
private static readonly string?[] PresenceStates =
|
||||
[
|
||||
LightMonitorConsts.PersonHomeState,
|
||||
"not_home",
|
||||
LightMonitorConsts.UnavailableState,
|
||||
null
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Builds light monitor options with production-compatible defaults.
|
||||
/// </summary>
|
||||
public static LightMonitorOptions CreateOptions(
|
||||
string lightEntityId = LightEntityId,
|
||||
string sunEntityId = SunEntityId,
|
||||
IEnumerable<string>? personEntityIds = null,
|
||||
bool enabled = true) => new()
|
||||
{
|
||||
Enabled = enabled,
|
||||
LightEntityId = lightEntityId,
|
||||
SunEntityId = sunEntityId,
|
||||
PersonEntityIds = personEntityIds?.ToArray() ?? [.. PersonEntityIds]
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Builds an <see cref="EntityState"/> carrying <paramref name="state"/>.
|
||||
/// </summary>
|
||||
public static EntityState CreateState(string? state) => new() { State = state };
|
||||
|
||||
/// <summary>
|
||||
/// Builds an <see cref="EntityState"/>, or <c>null</c> when the entity has no state at all
|
||||
/// (as returned by <c>IHaContext.GetState</c> for a missing entity).
|
||||
/// </summary>
|
||||
public static EntityState? CreateOptionalState(string? state) =>
|
||||
state is null ? null : CreateState(state);
|
||||
|
||||
/// <summary>
|
||||
/// Sun states that should (<c>true</c>) or should not (<c>false</c>) cause a light turn on.
|
||||
/// "below_horizon" is compared case-insensitively; unavailable, unknown, blank and
|
||||
/// missing states must never turn the light on.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, bool> SunStateTurnOnCases { get; } = new()
|
||||
{
|
||||
{ LightMonitorConsts.SunBelowHorizon, true },
|
||||
{ LightMonitorConsts.SunBelowHorizon.ToUpperInvariant(), true },
|
||||
{ "Below_Horizon", true },
|
||||
{ LightMonitorConsts.SunAboveHorizon, false },
|
||||
{ LightMonitorConsts.SunAboveHorizon.ToUpperInvariant(), false },
|
||||
{ LightMonitorConsts.UnavailableState, false },
|
||||
{ LightMonitorConsts.UnknownState, false },
|
||||
{ string.Empty, false },
|
||||
{ " ", false },
|
||||
{ null, false }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Light states evaluated while the sun is below the horizon. Only "on" (any casing)
|
||||
/// suppresses the turn on call; everything else, including missing state, turns it on.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, bool> LightStateTurnOnCases { get; } = new()
|
||||
{
|
||||
{ LightMonitorConsts.LightOnState, false },
|
||||
{ LightMonitorConsts.LightOnState.ToUpperInvariant(), false },
|
||||
{ "On", false },
|
||||
{ "off", true },
|
||||
{ "OFF", true },
|
||||
{ LightMonitorConsts.UnavailableState, true },
|
||||
{ LightMonitorConsts.UnknownState, true },
|
||||
{ string.Empty, true },
|
||||
{ " ", true },
|
||||
{ null, true }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Person states checked against the "arrived home" transition.
|
||||
/// Only "home" in any casing counts.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, bool> PersonNewStateCases { get; } = new()
|
||||
{
|
||||
{ LightMonitorConsts.PersonHomeState, true },
|
||||
{ "HOME", true },
|
||||
{ "Home", true },
|
||||
{ "hOmE", true },
|
||||
{ "not_home", false },
|
||||
{ "not_home ", false },
|
||||
{ LightMonitorConsts.UnavailableState, false },
|
||||
{ LightMonitorConsts.UnknownState, false },
|
||||
{ string.Empty, false },
|
||||
{ " ", false },
|
||||
{ null, false }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Old person states for a transition to "home". Recovery from unavailable, unknown or
|
||||
/// missing state must be ignored; every other old state is a real arrival.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, bool> PersonArrivalTransitions { get; } = new()
|
||||
{
|
||||
{ "not_home", true },
|
||||
{ "work", true },
|
||||
{ string.Empty, true },
|
||||
{ LightMonitorConsts.UnavailableState, false },
|
||||
{ LightMonitorConsts.UnknownState, false },
|
||||
{ null, false }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Old sun states for a transition to "below_horizon". Recovery from unavailable, unknown
|
||||
/// or missing state must be ignored.
|
||||
/// </summary>
|
||||
public static TheoryData<string?, bool> SunBelowHorizonTransitions { get; } = new()
|
||||
{
|
||||
{ LightMonitorConsts.SunAboveHorizon, true },
|
||||
{ string.Empty, true },
|
||||
{ LightMonitorConsts.UnavailableState, false },
|
||||
{ LightMonitorConsts.UnknownState, false },
|
||||
{ null, false }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Every 4^3 = 64 combination of home / not_home / unavailable / missing state across the
|
||||
/// three configured person entities.
|
||||
/// </summary>
|
||||
public static TheoryData<string?[]> PresenceStateMatrix { get; } = CreatePresenceStateMatrix();
|
||||
|
||||
/// <summary>
|
||||
/// Person id configurations that resolve to "nobody tracked".
|
||||
/// </summary>
|
||||
public static TheoryData<string[]> NoPersonEntityIdSets { get; } = new()
|
||||
{
|
||||
{ [] },
|
||||
{ [string.Empty] },
|
||||
{ [" "] }
|
||||
};
|
||||
|
||||
private static TheoryData<string?[]> CreatePresenceStateMatrix()
|
||||
{
|
||||
var matrix = new TheoryData<string?[]>();
|
||||
|
||||
foreach (var first in PresenceStates)
|
||||
{
|
||||
foreach (var second in PresenceStates)
|
||||
{
|
||||
foreach (var third in PresenceStates)
|
||||
{
|
||||
matrix.Add([first, second, third]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user