Update
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
using System.Reactive.Subjects;
|
||||
using LightMonitor.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using LightMonitorManager;
|
||||
using LightMonitorManager.Interface;
|
||||
using LightMonitorManager.Models;
|
||||
|
||||
namespace LightMonitor.Manager.Tests;
|
||||
|
||||
public class LightMonitorInitTests
|
||||
{
|
||||
private const string SunEntityId = "sun.sun";
|
||||
private const string PersonEntityId = "person.marius";
|
||||
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<IPresenceService> _presenceServiceMock = new();
|
||||
private readonly Mock<ILightControlService> _lightControlServiceMock = new();
|
||||
private readonly Subject<StateChange> _stateChangesSubject = new();
|
||||
|
||||
private readonly LightMonitorOptions _defaultOptions = new()
|
||||
{
|
||||
Enabled = true,
|
||||
SunEntityId = SunEntityId,
|
||||
PersonEntityIds = [PersonEntityId]
|
||||
};
|
||||
|
||||
private LightMonitorInit CreateSut(LightMonitorOptions? options = null)
|
||||
{
|
||||
_haContextMock.Setup(x => x.StateAllChanges())
|
||||
.Returns(_stateChangesSubject);
|
||||
|
||||
return new LightMonitorInit(
|
||||
_haContextMock.Object,
|
||||
Mock.Of<ILogger<LightMonitorInit>>(),
|
||||
_presenceServiceMock.Object,
|
||||
_lightControlServiceMock.Object,
|
||||
Options.Create(options ?? _defaultOptions));
|
||||
}
|
||||
|
||||
private void EmitStateChange(string entityId, string? oldState, string? newState)
|
||||
{
|
||||
var entity = new Entity(_haContextMock.Object, entityId);
|
||||
var old = oldState is not null ? new EntityState { State = oldState } : null;
|
||||
var @new = newState is not null ? new EntityState { State = newState } : null;
|
||||
|
||||
_stateChangesSubject.OnNext(new StateChange(entity, old, @new));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonStateChange_FromNotHomeToHome_ShouldCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(PersonEntityId, "not_home", LightMonitorConsts.PersonHomeState);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonStateChange_FromUnavailableToHome_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(PersonEntityId, LightMonitorConsts.UnavailableState, LightMonitorConsts.PersonHomeState);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonStateChange_FromUnknownToHome_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(PersonEntityId, LightMonitorConsts.UnknownState, LightMonitorConsts.PersonHomeState);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonStateChange_FromNullToHome_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(PersonEntityId, null, LightMonitorConsts.PersonHomeState);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersonStateChange_FromNotHomeToNotHome_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(PersonEntityId, "not_home", "not_home");
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromAboveHorizonToBelowHorizon_WhenSomeoneIsHome_ShouldCallTurnOnLight()
|
||||
{
|
||||
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromAboveHorizonToBelowHorizon_WhenNoOneIsHome_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(false);
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, LightMonitorConsts.SunAboveHorizon, LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromUnavailableToBelowHorizon_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, LightMonitorConsts.UnavailableState, LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromUnknownToBelowHorizon_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, LightMonitorConsts.UnknownState, LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromNullToBelowHorizon_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
_presenceServiceMock.Setup(x => x.IsAnyoneHome()).Returns(true);
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, null, LightMonitorConsts.SunBelowHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SunStateChange_FromBelowHorizonToAboveHorizon_ShouldNotCallTurnOnLight()
|
||||
{
|
||||
CreateSut();
|
||||
|
||||
EmitStateChange(SunEntityId, LightMonitorConsts.SunBelowHorizon, LightMonitorConsts.SunAboveHorizon);
|
||||
|
||||
_lightControlServiceMock.Verify(x => x.TurnOnLight(), Times.Never);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user