using FluentAssertions; using LightMonitor.Models; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using NetDaemon.HassModel; using NetDaemon.HassModel.Entities; using LightMonitorManager; namespace LightMonitor.Manager.Tests; public class PresenceServiceTests { private readonly Mock _haContextMock = new(); private readonly LightMonitorOptions _defaultOptions = LightMonitorTestData.CreateOptions(); private PresenceService CreateService(LightMonitorOptions? options = null) { return new PresenceService( _haContextMock.Object, Mock.Of>(), Options.Create(options ?? _defaultOptions)); } private void SetupStates(string?[] personStates) { for (var index = 0; index < personStates.Length; index++) { var entityId = LightMonitorTestData.PersonEntityIds[index]; var state = LightMonitorTestData.CreateOptionalState(personStates[index]); _haContextMock.Setup(x => x.GetState(entityId)) .Returns(state); } } [Theory] [MemberData(nameof(LightMonitorTestData.PresenceStateMatrix), MemberType = typeof(LightMonitorTestData))] public void IsAnyoneHome_ForEveryPresenceCombination_ShouldReturnTrueOnlyWhenSomeoneIsHome(string?[] personStates) { SetupStates(personStates); var expected = personStates.Any(state => string.Equals(state, LightMonitorConsts.PersonHomeState, StringComparison.OrdinalIgnoreCase)); CreateService().IsAnyoneHome().Should().Be(expected); } [Theory] [MemberData(nameof(LightMonitorTestData.PersonNewStateCases), MemberType = typeof(LightMonitorTestData))] public void IsAnyoneHome_ShouldOnlyTreatHomeStateAsPresent(string? state, bool expected) { _haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId)) .Returns(LightMonitorTestData.CreateOptionalState(state)); var options = LightMonitorTestData.CreateOptions( personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]); CreateService(options).IsAnyoneHome().Should().Be(expected); } [Theory] [MemberData(nameof(LightMonitorTestData.NoPersonEntityIdSets), MemberType = typeof(LightMonitorTestData))] public void IsAnyoneHome_WithNoUsablePersonEntityIds_ShouldReturnFalse(string[] personEntityIds) { var options = LightMonitorTestData.CreateOptions(personEntityIds: personEntityIds); CreateService(options).IsAnyoneHome().Should().BeFalse(); } [Fact] public void IsAnyoneHome_WithEmptyPersonList_ShouldNotQueryAnyEntity() { var options = LightMonitorTestData.CreateOptions(personEntityIds: []); CreateService(options).IsAnyoneHome().Should().BeFalse(); _haContextMock.Verify(x => x.GetState(It.IsAny()), Times.Never); } [Fact] public void IsAnyoneHome_ShouldStopCheckingAfterTheFirstHomePerson() { _haContextMock.Setup(x => x.GetState(LightMonitorTestData.PrimaryPersonEntityId)) .Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState)); CreateService().IsAnyoneHome().Should().BeTrue(); _haContextMock.Verify(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId), Times.Never); _haContextMock.Verify(x => x.GetState(LightMonitorTestData.PhonePersonEntityId), Times.Never); } [Fact] public void IsAnyoneHome_ShouldCheckEveryConfiguredPersonWhenNobodyIsHome() { CreateService().IsAnyoneHome().Should().BeFalse(); foreach (var personEntityId in LightMonitorTestData.PersonEntityIds) { _haContextMock.Verify(x => x.GetState(personEntityId), Times.Once); } } [Fact] public void IsAnyoneHome_ShouldIgnorePersonsThatAreNotConfigured() { _haContextMock.Setup(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId)) .Returns(LightMonitorTestData.CreateState(LightMonitorConsts.PersonHomeState)); var options = LightMonitorTestData.CreateOptions( personEntityIds: [LightMonitorTestData.PrimaryPersonEntityId]); CreateService(options).IsAnyoneHome().Should().BeFalse(); _haContextMock.Verify(x => x.GetState(LightMonitorTestData.SecondaryPersonEntityId), Times.Never); } [Fact] public void IsAnyoneHome_WithDefaultOptions_ShouldQueryAllDefaultPersonEntities() { var service = CreateService(new LightMonitorOptions()); service.IsAnyoneHome().Should().BeFalse(); foreach (var personEntityId in LightMonitorConsts.DefaultPersonEntityIds) { _haContextMock.Verify(x => x.GetState(personEntityId), Times.Once); } } }