using System.Text.Json;
using BoilerMonitor.Models;
using BoilerMonitorManager.Models;
using Microsoft.Extensions.Time.Testing;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using static BoilerMonitor.Models.BoilerMonitorConsts;
namespace BoilerMonitor.Manager.Tests;
///
/// Shared, deterministic fixtures used by the BoilerMonitor tests: config/options builders,
/// entity-state sets, parse samples and a fixed clock. Keeping them here avoids copy-pasted
/// setup and makes every timestamp/DST assertion independent of the real clock.
///
internal static class BoilerMonitorTestData
{
public const string PowerEntityId = "sensor.sonoff_1002230e32_power";
public const string SwitchEntityId = "switch.sonoff_1002230e32";
/// Fixed "now" (UTC) used by every time based test.
public static readonly DateTimeOffset BaseTime = new(2026, 3, 31, 12, 0, 0, TimeSpan.Zero);
public static FakeTimeProvider CreateTimeProvider(DateTimeOffset? utcNow = null) =>
new(utcNow ?? BaseTime);
public static BoilerMonitorConfig CreateConfig(
string? boilerPowerEntityId = PowerEntityId,
string? boilerSwitchEntityId = SwitchEntityId,
int? pollIntervalSeconds = DefaultPollIntervalSeconds,
double? heatingPowerThresholdWatts = DefaultHeatingPowerThresholdWatts) =>
new()
{
BoilerPowerEntityId = boilerPowerEntityId,
BoilerSwitchEntityId = boilerSwitchEntityId,
PollIntervalSeconds = pollIntervalSeconds,
HeatingPowerThresholdWatts = heatingPowerThresholdWatts
};
public static BoilerMonitorOptions CreateOptions(
bool enabled = true,
bool cleanupEntitiesOnStartup = true) =>
new()
{
Enabled = enabled,
CleanupEntitiesOnStartup = cleanupEntitiesOnStartup
};
public static PersistedBoilerState CreatePersistedState(
TimeSpan? accumulatedRunTime = null,
int sessionCount = 3,
string? skipPriceCapState = "ON",
string? budgetOverrideState = "OFF") =>
new()
{
AccumulatedRunTime = accumulatedRunTime ?? TimeSpan.FromHours(2),
SessionCount = sessionCount,
SkipPriceCapState = skipPriceCapState,
BudgetOverrideState = budgetOverrideState
};
public static BoilerMonitorUpdateContext CreateUpdateContext(
TimeSpan? todayRunTime = null,
TimeSpan? remainingTime = null,
double budgetPercentage = 50.0,
bool budgetExhausted = false,
int sessionCount = 1,
double budgetHours = 2.0,
bool waterReady = false,
string? boilerSwitchState = "on",
double? boilerPowerWatts = 0.0,
DateTimeOffset? cooldownSince = null,
bool isNowCheap = true) =>
new()
{
TodayRunTime = todayRunTime ?? TimeSpan.FromHours(1),
RemainingTime = remainingTime ?? TimeSpan.FromHours(1),
BudgetPercentage = budgetPercentage,
BudgetExhausted = budgetExhausted,
SessionCount = sessionCount,
BudgetHours = budgetHours,
IsCurrentlyHeating = false,
WaterReady = waterReady,
BoilerSwitchState = boilerSwitchState,
BoilerPowerWatts = boilerPowerWatts,
CooldownSince = cooldownSince,
IsNowCheap = isNowCheap,
NextCheapIntervalStart = null
};
///
/// The four entity states ReadPersistedState reads, with valid defaults.
/// A null value means the entity does not exist in Home Assistant.
///
public static Dictionary CreatePersistedEntityStates(
string? todayRuntime = "7200",
string? sessionCount = "3",
string? skipPriceCap = "OFF",
string? budgetOverride = "OFF") =>
new()
{
[TodayRuntimeEntityId] = todayRuntime,
[SessionCountEntityId] = sessionCount,
[SkipPriceCapEntityId] = skipPriceCap,
[BudgetOverrideEntityId] = budgetOverride
};
public static EntityState CreateEntityState(string state) => new() { State = state };
public static EntityState CreateEntityStateWithAttributes(IReadOnlyDictionary attributes) =>
new() { State = "yes", AttributesJson = JsonSerializer.SerializeToElement(attributes) };
public static EntityState CreateWaterReadyEntityStateWithCooldown(object? cooldownSince) =>
CreateEntityStateWithAttributes(new Dictionary { ["cooldown_since"] = cooldownSince });
public static void SetupEntityStates(
this Mock haContextMock,
IReadOnlyDictionary states)
{
foreach (var (entityId, state) in states)
{
var entityState = state is null ? null : CreateEntityState(state);
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
}
}
public static void SetupEntityState(
this Mock haContextMock,
string entityId,
EntityState? entityState) =>
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
///
/// Entity-state samples for the today-runtime sensor: valid values with the expected
/// , plus unavailable/negative/overflow values that must be rejected.
///
public static IEnumerable