using LightMonitor.Models; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitorManager; using LightMonitorManager.Models; namespace LightMonitor.Manager.Tests; public class LightControlServiceTests { private const string LightEntityId = LightMonitorTestData.LightEntityId; private const string SunEntityId = LightMonitorTestData.SunEntityId; private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions(); private readonly Mock _haContextMock = new(); private LightControlService CreateService(LightMonitorOptions? options = null) { return new LightControlService( _haContextMock.Object, Mock.Of>(), Options.Create(options ?? _defaultOptions)); } private void SetupState(string entityId, string? state) { _haContextMock.Setup(x => x.GetState(entityId)) .Returns(LightMonitorTestData.CreateOptionalState(state)); } private void VerifyTurnOn(string entityId, bool shouldTurnOn) { if (!shouldTurnOn) { _haContextMock.Verify(x => x.CallService( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); return; } _haContextMock.Verify(x => x.CallService("light", "turn_on", It.Is(target => target.EntityIds != null && target.EntityIds.Contains(entityId)), It.IsAny()), Times.Once); } [Theory] [MemberData(nameof(LightMonitorTestData.SunStateTurnOnCases), MemberType = typeof(LightMonitorTestData))] public void TurnOnLight_ShouldOnlyTurnOnWhenSunIsBelowHorizon(string? sunState, bool shouldTurnOn) { SetupState(SunEntityId, sunState); SetupState(LightEntityId, "off"); CreateService().TurnOnLight(); VerifyTurnOn(LightEntityId, shouldTurnOn); } [Theory] [MemberData(nameof(LightMonitorTestData.LightStateTurnOnCases), MemberType = typeof(LightMonitorTestData))] public void TurnOnLight_WhenSunIsBelowHorizon_ShouldOnlyTurnOnWhenLightIsNotAlreadyOn( string? lightState, bool shouldTurnOn) { SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon); SetupState(LightEntityId, lightState); CreateService().TurnOnLight(); VerifyTurnOn(LightEntityId, shouldTurnOn); } [Fact] public void TurnOnLight_ShouldTargetTheConfiguredLightEntity() { const string customLightEntityId = "light.kitchen_ceiling"; var options = LightMonitorTestData.CreateOptions(lightEntityId: customLightEntityId); SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon); SetupState(customLightEntityId, "off"); CreateService(options).TurnOnLight(); VerifyTurnOn(customLightEntityId, true); } [Fact] public void TurnOnLight_WithDefaultOptions_ShouldTargetTheDefaultLightEntity() { var options = new LightMonitorOptions(); SetupState(LightMonitorConsts.DefaultSunEntityId, LightMonitorConsts.SunBelowHorizon); SetupState(LightMonitorConsts.DefaultLightEntityId, null); CreateService(options).TurnOnLight(); VerifyTurnOn(LightMonitorConsts.DefaultLightEntityId, true); } [Fact] public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotReadTheLightState() { SetupState(SunEntityId, LightMonitorConsts.SunAboveHorizon); CreateService().TurnOnLight(); _haContextMock.Verify(x => x.GetState(LightEntityId), Times.Never); } [Theory] [InlineData("")] [InlineData(" ")] public void TurnOnLight_WhenSunEntityIdIsBlank_ShouldNotTurnOnLight(string sunEntityId) { var options = LightMonitorTestData.CreateOptions(sunEntityId: sunEntityId); SetupState(LightEntityId, "off"); CreateService(options).TurnOnLight(); VerifyTurnOn(LightEntityId, false); } [Fact] public void TurnOnLight_ShouldReadEachStateAtMostOnce() { SetupState(SunEntityId, LightMonitorConsts.SunBelowHorizon); SetupState(LightEntityId, LightMonitorConsts.LightOnState); CreateService().TurnOnLight(); _haContextMock.Verify(x => x.GetState(SunEntityId), Times.Once); _haContextMock.Verify(x => x.GetState(LightEntityId), Times.Once); VerifyTurnOn(LightEntityId, false); } }