Files
HomeAssistant/ShowerMonitor.Manager.Tests/ExhaustFanControlServiceTests.cs
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

283 lines
9.9 KiB
C#

#region
using FluentAssertions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using Moq;
using NetDaemon.HassModel;
using NetDaemon.HassModel.Entities;
using ShowerMonitor.Models.Enums;
using ShowerMonitorManager;
using ShowerMonitorManager.Models;
#endregion
namespace ShowerMonitor.Manager.Tests;
public class ExhaustFanControlServiceTests
{
private const string FanEntityId = "switch.exhaust_fan";
private readonly ShowerMonitorOptions _defaultOptions = ShowerMonitorTestData.CreateOptions();
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
private DateTimeOffset Now => _timeProvider.GetUtcNow();
public static TheoryData<string, ShowerState, string?, bool> ShowerProgressFanStateCases => new TheoryData<string, ShowerState, string?, bool>
{
{ "active while fan is off", ShowerState.Active, "off", true },
{ "active while fan is on", ShowerState.Active, "on", false },
{ "active while fan reports uppercase on", ShowerState.Active, "ON", false },
{ "active while fan is unavailable", ShowerState.Active, "unavailable", true },
{ "active while fan state is unknown", ShowerState.Active, "unknown", true },
{ "active while fan state is empty", ShowerState.Active, string.Empty, true },
{ "active while fan state is missing", ShowerState.Active, null, true },
{ "suspected while fan is off", ShowerState.Suspected, "off", true },
{ "suspected while fan is on", ShowerState.Suspected, "on", false },
{ "idle does not touch the fan", ShowerState.Idle, "off", false },
{ "declining does not turn the fan on", ShowerState.Declining, "off", false },
{ "completed does not turn the fan on", ShowerState.Completed, "off", false }
};
public static TheoryData<string, int, int, bool> FanRunAfterCases => new TheoryData<string, int, int, bool>
{
{ "well before run-after duration", 10, 60, false },
{ "one second before run-after duration", 10, 599, false },
{ "exactly at run-after duration", 10, 600, true },
{ "one second after run-after duration", 10, 601, true },
{ "well after run-after duration", 10, 660, true },
{ "zero-minute run-after turns off immediately", 0, 0, true },
{ "long run-after keeps fan running", 30, 300, false }
};
public static TheoryData<string, bool, string?, bool> ResetCases => new TheoryData<string, bool, string?, bool>
{
{ "fan on after we turned it on", true, "on", true },
{ "fan reports uppercase on", true, "ON", true },
{ "fan already off again", true, "off", false },
{ "fan unavailable", true, "unavailable", false },
{ "fan state missing", true, null, false },
{ "we never turned the fan on", false, "on", false }
};
[Theory]
[MemberData(nameof(ShowerProgressFanStateCases))]
public void Evaluate_WhileShowerInProgress_TurnsFanOnOnlyWhenNotAlreadyRunning(
string scenario,
ShowerState state,
string? fanState,
bool expectTurnOn)
{
SetupFanState(fanState);
var service = CreateService();
service.Evaluate(state, Now);
VerifySwitchCall("turn_on", expectTurnOn ? 1 : 0, because: $"scenario: {scenario}");
VerifySwitchCall("turn_off", 0, because: $"scenario: {scenario}");
}
[Theory]
[MemberData(nameof(FanRunAfterCases))]
public void Evaluate_AfterShowerEnds_TurnsFanOffOnlyOnceRunPeriodElapsed(
string scenario,
int runAfterMinutes,
int idleOffsetSecondsFromShowerEnd,
bool expectTurnOff)
{
SetupFanState("off");
var service = CreateService(ShowerMonitorTestData.CreateOptions(
fanRunAfterShowerMinutes: runAfterMinutes));
var showerEnd = Now.AddMinutes(5);
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(ShowerState.Completed, showerEnd);
service.Evaluate(ShowerState.Idle, showerEnd.AddSeconds(idleOffsetSecondsFromShowerEnd));
VerifySwitchCall("turn_on", 1, because: $"scenario: {scenario}");
VerifySwitchCall("turn_off", expectTurnOff ? 1 : 0, because: $"scenario: {scenario}");
}
[Theory]
[InlineData(ShowerState.Completed)]
[InlineData(ShowerState.Declining)]
public void Evaluate_EndingStates_StartRunAfterPeriod(ShowerState endingState)
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(endingState, Now.AddMinutes(5));
VerifySwitchCall("turn_off", 0);
service.Evaluate(ShowerState.Idle, Now.AddMinutes(15));
VerifySwitchCall("turn_off", 1);
VerifySwitchCall("turn_on", 1);
}
[Theory]
[InlineData(ShowerState.Idle)]
[InlineData(ShowerState.Declining)]
[InlineData(ShowerState.Completed)]
public void Evaluate_AfterShowerEnds_WhenFanWasNotTurnedOnByUs_DoesNotTurnFanOff(ShowerState state)
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(state, Now);
service.Evaluate(ShowerState.Idle, Now.AddHours(1));
VerifySwitchCall("turn_on", 0);
VerifySwitchCall("turn_off", 0);
}
[Fact]
public void Evaluate_WhenShowerStaysActive_TurnsFanOnOnlyOnce()
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(ShowerState.Active, Now.AddSeconds(30));
service.Evaluate(ShowerState.Suspected, Now.AddMinutes(1));
VerifySwitchCall("turn_on", 1);
VerifySwitchCall("turn_off", 0);
}
[Fact]
public void Evaluate_WhenShowerResumesDuringRunPeriod_CancelsPendingFanOff()
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(ShowerState.Completed, Now.AddMinutes(5));
service.Evaluate(ShowerState.Active, Now.AddMinutes(8));
service.Evaluate(ShowerState.Idle, Now.AddMinutes(30));
VerifySwitchCall("turn_on", 1);
VerifySwitchCall("turn_off", 0);
}
[Fact]
public void Evaluate_WhenAnewShowerStartsAfterFanStopped_TurnsFanOnAgain()
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(ShowerState.Completed, Now.AddMinutes(15));
service.Evaluate(ShowerState.Idle, Now.AddMinutes(26));
service.Evaluate(ShowerState.Active, Now.AddMinutes(40));
VerifySwitchCall("turn_on", 2);
VerifySwitchCall("turn_off", 1);
}
[Fact]
public void Evaluate_AfterShowerEnds_WhenFanWasAlreadyOn_DoesNotTurnItOff()
{
SetupFanState("on");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
service.Evaluate(ShowerState.Completed, Now.AddMinutes(5));
service.Evaluate(ShowerState.Idle, Now.AddMinutes(20));
VerifySwitchCall("turn_on", 0);
VerifySwitchCall("turn_off", 0);
}
[Theory]
[MemberData(nameof(ResetCases))]
public void Reset_TurnsFanOffOnlyWhenWeTurnedItOnAndItIsStillOn(
string scenario,
bool turnOnFirst,
string? currentFanState,
bool expectTurnOff)
{
SetupFanState("off");
var service = CreateService();
if (turnOnFirst)
{
service.Evaluate(ShowerState.Active, Now);
SetupFanState(currentFanState);
}
service.Reset();
VerifySwitchCall("turn_off", expectTurnOff ? 1 : 0, because: $"scenario: {scenario}");
}
[Fact]
public void Reset_IsIdempotent_AndPreventsLaterFanOff()
{
SetupFanState("off");
var service = CreateService();
service.Evaluate(ShowerState.Active, Now);
SetupFanState("on");
service.Reset();
service.Reset();
VerifySwitchCall("turn_off", 1);
SetupFanState("off");
service.Evaluate(ShowerState.Idle, Now.AddMinutes(20));
VerifySwitchCall("turn_off", 1);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("switch.unknown_fan")]
[InlineData(null)]
public void Evaluate_WithConfiguredFanEntityId_TargetsThatExactEntity(string? entityId)
{
var service = CreateService(ShowerMonitorTestData.CreateOptions(entityId!));
service.Evaluate(ShowerState.Active, Now);
VerifySwitchCall("turn_on", 1, entityId);
}
private ExhaustFanControlService CreateService(ShowerMonitorOptions? options = null)
{
return new ExhaustFanControlService(_haContextMock.Object, Options.Create(options ?? _defaultOptions));
}
private void SetupFanState(string? state)
{
_haContextMock
.Setup(x => x.GetState(_defaultOptions.ExhaustFanEntityId))
.Returns(ShowerMonitorTestData.FanState(state));
}
private void VerifySwitchCall(
string service,
int expectedCount,
string? expectedEntityId = FanEntityId,
string? because = null)
{
var targets = _haContextMock.Invocations
.Where(invocation => invocation.Method.Name == nameof(IHaContext.CallService) && (string?)invocation.Arguments[0] == "switch" && (string?)invocation.Arguments[1] == service)
.Select(invocation => (ServiceTarget?)invocation.Arguments[2])
.ToList();
targets.Should().HaveCount(expectedCount, because);
targets.Should()
.OnlyContain(
target => target != null && target.EntityIds != null && target.EntityIds.Count == 1 && target.EntityIds.Single() == expectedEntityId,
because);
}
}