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 = "light.sonoff_1002214d40"; private const string SunEntityId = "sun.sun"; private readonly LightMonitorOptions _defaultOptions = new() { LightEntityId = LightEntityId, SunEntityId = SunEntityId }; private readonly Mock _haContextMock = new(); private LightControlService CreateService(LightMonitorOptions? options = null) { return new LightControlService( _haContextMock.Object, Mock.Of>(), Options.Create(options ?? _defaultOptions)); } [Fact] public void TurnOnLight_WhenDarkAndLightIsOff_ShouldTurnOnLight() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon }); _haContextMock.Setup(x => x.GetState(LightEntityId)) .Returns(new EntityState { State = "off" }); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Once); } [Fact] public void TurnOnLight_WhenDarkAndLightIsAlreadyOn_ShouldNotCallService() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon }); _haContextMock.Setup(x => x.GetState(LightEntityId)) .Returns(new EntityState { State = "on" }); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Never); } [Fact] public void TurnOnLight_WhenSunIsAboveHorizon_ShouldNotTurnOnLight() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns(new EntityState { State = LightMonitorConsts.SunAboveHorizon }); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Never); } [Fact] public void TurnOnLight_WhenSunStateIsNull_ShouldNotTurnOnLight() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns((EntityState?)null); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Never); } [Fact] public void TurnOnLight_WhenSunStateIsUnavailable_ShouldNotTurnOnLight() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns(new EntityState { State = "unavailable" }); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Never); } [Fact] public void TurnOnLight_WhenLightStateIsNull_ShouldTurnOnLight() { _haContextMock.Setup(x => x.GetState(SunEntityId)) .Returns(new EntityState { State = LightMonitorConsts.SunBelowHorizon }); _haContextMock.Setup(x => x.GetState(LightEntityId)) .Returns((EntityState?)null); var service = CreateService(); service.TurnOnLight(); _haContextMock.Verify(x => x.CallService("light", "turn_on", It.IsAny(), It.IsAny()), Times.Once); } }