Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -12,8 +13,8 @@ namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerLimitEnforcementServiceTests
|
||||
{
|
||||
private const string BoilerSwitch = "switch.sonoff_1002230e32";
|
||||
private const string AutoManagement = "switch.boilermonitor_auto_management";
|
||||
private const string BoilerSwitch = PowerMonitorTestData.BoilerSwitchEntityId;
|
||||
private const string AutoManagement = PowerMonitorTestData.AutoManagementEntityId;
|
||||
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<PowerLimitEnforcementService>> _loggerMock;
|
||||
@@ -24,16 +25,8 @@ public class PowerLimitEnforcementServiceTests
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<PowerLimitEnforcementService>>();
|
||||
_timeProvider = new FakeTimeProvider(DateTimeOffset.UtcNow);
|
||||
_options = new PowerMonitorOptions
|
||||
{
|
||||
EnforceLimitEnabled = true,
|
||||
PowerLimitWatts = 5500,
|
||||
RecoverySeconds = 600,
|
||||
LimitCooldownSeconds = 30,
|
||||
EnforceSwitchOffEntityIds = [BoilerSwitch],
|
||||
PauseAndRestoreEntityIds = [AutoManagement]
|
||||
};
|
||||
_timeProvider = new FakeTimeProvider(PowerMonitorTestData.FixedUtcNow);
|
||||
_options = PowerMonitorTestData.CreateLimitEnforcementOptions();
|
||||
}
|
||||
|
||||
private PowerLimitEnforcementService CreateSut() =>
|
||||
@@ -43,63 +36,96 @@ public class PowerLimitEnforcementServiceTests
|
||||
Options.Create(_options),
|
||||
_timeProvider);
|
||||
|
||||
private static bool IsTurnOffCall(string domain, string service, string entityId) =>
|
||||
domain == "switch" && service == "turn_off";
|
||||
private void VerifyNoServiceCalls() =>
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
|
||||
private static bool IsTurnOnCall(string domain, string service, string entityId) =>
|
||||
domain == "switch" && service == "turn_on";
|
||||
private void VerifyTurnOff(string entityId, int expectedCalls) =>
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
|
||||
Times.Exactly(expectedCalls));
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenDisabled_ShouldNotCallAnyService()
|
||||
private void VerifyTurnOn(string entityId, int expectedCalls) =>
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(entityId)), null),
|
||||
Times.Exactly(expectedCalls));
|
||||
|
||||
private void VerifyNotificationCreated(int expectedCalls) =>
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "create", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Exactly(expectedCalls));
|
||||
|
||||
private void VerifyNotificationDismissed(int expectedCalls) =>
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Exactly(expectedCalls));
|
||||
|
||||
#region EnforceLimit
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(5500.0)]
|
||||
[InlineData(6000.0)]
|
||||
[InlineData(1_000_000.0)]
|
||||
public void EnforceLimit_WhenDisabled_ShouldNotCallAnyService(double powerWatts)
|
||||
{
|
||||
// Arrange
|
||||
_options.EnforceLimitEnabled = false;
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.EnforceLimit(6000);
|
||||
// Act
|
||||
sut.EnforceLimit(powerWatts);
|
||||
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Assert
|
||||
VerifyNoServiceCalls();
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenPowerBelowLimit_ShouldNotEnforce()
|
||||
[Theory]
|
||||
[InlineData(-1000.0)]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(0.5)]
|
||||
[InlineData(2749.99)]
|
||||
[InlineData(5499.99)]
|
||||
[InlineData(5500.0)]
|
||||
public void EnforceLimit_WhenPowerAtOrBelowLimit_ShouldNotEnforce(double powerWatts)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.EnforceLimit(5000);
|
||||
// Act
|
||||
sut.EnforceLimit(powerWatts);
|
||||
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeFalse("power equal to the limit must not trigger (strict greater-than)");
|
||||
VerifyNoServiceCalls();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenPowerExceedsLimit_ShouldTurnOffSwitchesAndPauseMonitors()
|
||||
[Theory]
|
||||
[InlineData(5500.01)]
|
||||
[InlineData(6000.0)]
|
||||
[InlineData(15_000.0)]
|
||||
[InlineData(1_000_000.0)]
|
||||
public void EnforceLimit_WhenPowerExceedsLimit_ShouldTurnOffSwitchesAndPauseMonitors(double powerWatts)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
sut.EnforceLimit(6000);
|
||||
// Act
|
||||
sut.EnforceLimit(powerWatts);
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
// turn_off called for boiler switch + auto_management
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(BoilerSwitch)), null),
|
||||
Times.Once);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(AutoManagement)), null),
|
||||
Times.Once);
|
||||
// notification created
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "create", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once);
|
||||
VerifyTurnOff(BoilerSwitch, 1);
|
||||
VerifyTurnOff(AutoManagement, 1);
|
||||
VerifyNotificationCreated(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenBreached_ShouldTurnOffBoilerBeforeAutoManagement()
|
||||
{
|
||||
// Arrange
|
||||
var calls = new List<(string domain, string service, string? entityId)>();
|
||||
_haContextMock
|
||||
.Setup(x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()))
|
||||
@@ -107,8 +133,10 @@ public class PowerLimitEnforcementServiceTests
|
||||
calls.Add((d, s, t?.EntityIds?.FirstOrDefault())));
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
sut.EnforceLimit(6000);
|
||||
|
||||
// Assert
|
||||
var turnOffCalls = calls.Where(c => c.service == "turn_off").ToList();
|
||||
turnOffCalls.Should().HaveCount(2);
|
||||
turnOffCalls[0].entityId.Should().Be(BoilerSwitch, "boiler switch must be turned off before auto_management (race-safe order)");
|
||||
@@ -116,230 +144,304 @@ public class PowerLimitEnforcementServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenAlreadyActiveAndBeforeRecoveryTime_ShouldDoNothing()
|
||||
public void EnforceLimit_WithMultipleConfiguredEntities_ShouldTurnOffEveryEntityInConfiguredOrder()
|
||||
{
|
||||
// Arrange
|
||||
_options.EnforceSwitchOffEntityIds = ["switch.high_load_a", "switch.high_load_b"];
|
||||
_options.PauseAndRestoreEntityIds = ["switch.monitor_c"];
|
||||
var calls = new List<string?>();
|
||||
_haContextMock
|
||||
.Setup(x => x.CallService(It.IsAny<string>(), "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()))
|
||||
.Callback<string, string, ServiceTarget?, object?>((_, _, target, _) =>
|
||||
calls.Add(target?.EntityIds?.FirstOrDefault()));
|
||||
var sut = CreateSut();
|
||||
sut.EnforceLimit(6000);
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
// advance less than recovery
|
||||
_timeProvider.Advance(TimeSpan.FromMinutes(5));
|
||||
sut.EnforceLimit(5000);
|
||||
// Act
|
||||
sut.EnforceLimit(7000);
|
||||
|
||||
// Assert
|
||||
calls.Should().Equal("switch.high_load_a", "switch.high_load_b", "switch.monitor_c");
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenRecoveryTimeReachedAndPowerBelowLimit_ShouldRestoreMonitorsAndNotTurnBoilerOn()
|
||||
public void EnforceLimit_WithNoConfiguredEntities_ShouldStillNotifyAndActivate()
|
||||
{
|
||||
// Arrange
|
||||
_options.EnforceSwitchOffEntityIds = [];
|
||||
_options.PauseAndRestoreEntityIds = [];
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
sut.EnforceLimit(6000);
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(4000);
|
||||
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
// auto_management turned back ON
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
VerifyNotificationCreated(1);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(AutoManagement)), null),
|
||||
Times.Once);
|
||||
// boiler switch should NOT be turned back on
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(BoilerSwitch)), null),
|
||||
x => x.CallService("switch", It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// notification dismissed
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenRecoveryTimeReachedButPowerStillHigh_ShouldStayEnforcedAndResetTimer()
|
||||
#endregion
|
||||
|
||||
#region Recovery
|
||||
|
||||
public static IEnumerable<object[]> RecoveryCases =>
|
||||
[
|
||||
["not yet due", 300, 3000.0, true, 0],
|
||||
["one second before due", 599, 3000.0, true, 0],
|
||||
["exactly due", 600, 3000.0, false, 1],
|
||||
["just under limit", 601, 5499.99, false, 1],
|
||||
["zero power", 900, 0.0, false, 1],
|
||||
["negative power", 601, -10.0, false, 1],
|
||||
["exactly at limit stays enforced", 601, 5500.0, true, 0],
|
||||
["above limit stays enforced", 601, 5800.0, true, 0],
|
||||
["very high power stays enforced", 601, 25_000.0, true, 0]
|
||||
];
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecoveryCases))]
|
||||
public void EnforceLimit_AfterEnforcement_ShouldEvaluateRecovery(
|
||||
string scenario,
|
||||
int secondsAfterEnforcement,
|
||||
double powerWatts,
|
||||
bool expectedLimitActive,
|
||||
int expectedRecoveryActions)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.EnforceLimit(6000);
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(5800);
|
||||
// Act
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(secondsAfterEnforcement));
|
||||
sut.EnforceLimit(powerWatts);
|
||||
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
// no restore, no dismiss
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().Be(expectedLimitActive, $"scenario: {scenario}");
|
||||
VerifyTurnOn(AutoManagement, expectedRecoveryActions);
|
||||
VerifyTurnOn(BoilerSwitch, 0);
|
||||
VerifyNotificationDismissed(expectedRecoveryActions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenRecoveryTimeReachedButPowerStillHigh_ShouldAllowAnotherRecoveryCheckAfterWait()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.EnforceLimit(6000);
|
||||
|
||||
// first recovery check - still high, timer resets
|
||||
// Act - first recovery check finds power still high and resets the timer
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(5800);
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
|
||||
// immediately after - not enough time, no-op
|
||||
sut.EnforceLimit(3000);
|
||||
var immediatelyAfter = sut;
|
||||
immediatelyAfter.EnforceLimit(3000);
|
||||
|
||||
// Assert - not enough time has passed for another check
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
|
||||
// after another full wait - power dropped, recovers
|
||||
// Act - after another full wait the power has dropped
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(3000);
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
VerifyTurnOn(AutoManagement, 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenCooldownActive_ShouldNotReEnforce()
|
||||
#endregion
|
||||
|
||||
#region Cooldown
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, false)]
|
||||
[InlineData(29, false)]
|
||||
[InlineData(30, true)]
|
||||
[InlineData(31, true)]
|
||||
[InlineData(600, true)]
|
||||
public void EnforceLimit_AfterRecovery_ShouldReEnforceOnlyOnceCooldownElapsed(int secondsAfterRecovery, bool expectedLimitActive)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.EnforceLimit(6000);
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
|
||||
// recover so we can re-trigger
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(3000);
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
// immediately breach again - within cooldown, should NOT enforce
|
||||
// Act
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(secondsAfterRecovery));
|
||||
sut.EnforceLimit(6000);
|
||||
sut.IsLimitActive.Should().BeFalse("cooldown should block immediate re-enforcement");
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().Be(expectedLimitActive);
|
||||
|
||||
if (expectedLimitActive)
|
||||
{
|
||||
VerifyTurnOff(BoilerSwitch, 1);
|
||||
VerifyTurnOff(AutoManagement, 1);
|
||||
VerifyNotificationCreated(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
VerifyNoServiceCalls();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenCooldownPassed_ShouldReEnforce()
|
||||
public void EnforceLimit_ImmediatelyAfterManualReset_ShouldNotReEnforceWithinCooldown()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.EnforceLimit(6000);
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(601));
|
||||
sut.EnforceLimit(3000);
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
|
||||
// wait past cooldown
|
||||
_timeProvider.Advance(TimeSpan.FromSeconds(31));
|
||||
sut.TriggerLimitBreach();
|
||||
sut.ResetLimitBreach();
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
// Act
|
||||
sut.EnforceLimit(6000);
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.AtLeastOnce);
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeFalse("the manual reset starts the cooldown window");
|
||||
VerifyNoServiceCalls();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region BuildLimitMessage
|
||||
|
||||
[Theory]
|
||||
[InlineData("typical breach", 6.0, 5.5, "6.00 kW", "5.5 kW")]
|
||||
[InlineData("rounds current to two decimals", 5.999, 5.549, "6.00 kW", "5.5 kW")]
|
||||
[InlineData("zero current", 0.0, 5.5, "0.00 kW", "5.5 kW")]
|
||||
[InlineData("negative current", -1.0, 5.5, "-1.00 kW", "5.5 kW")]
|
||||
[InlineData("large values", 123.456, 100.0, "123.46 kW", "100.0 kW")]
|
||||
public void BuildLimitMessage_ShouldContainCurrentAndLimitInKw(
|
||||
string scenario,
|
||||
double currentKw,
|
||||
double limitKw,
|
||||
string expectedCurrentText,
|
||||
string expectedLimitText)
|
||||
{
|
||||
// Act
|
||||
var message = PowerLimitEnforcementService.BuildLimitMessage(currentKw, limitKw);
|
||||
|
||||
// Assert
|
||||
message.Should().Contain(expectedCurrentText, $"scenario: {scenario}");
|
||||
message.Should().Contain(expectedLimitText, $"scenario: {scenario}");
|
||||
message.Should().Contain("exceeded the limit");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnforceLimit_WhenExactlyAtLimit_ShouldNotEnforce()
|
||||
public void BuildLimitMessage_WithCommaDecimalCulture_ShouldStillUseInvariantFormatting()
|
||||
{
|
||||
var sut = CreateSut();
|
||||
// Arrange
|
||||
var originalCulture = CultureInfo.CurrentCulture;
|
||||
|
||||
sut.EnforceLimit(5500);
|
||||
try
|
||||
{
|
||||
CultureInfo.CurrentCulture = new CultureInfo("de-DE");
|
||||
|
||||
sut.IsLimitActive.Should().BeFalse("power equal to limit should not trigger (strict greater-than)");
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Act
|
||||
var message = PowerLimitEnforcementService.BuildLimitMessage(6.0, 5.5);
|
||||
|
||||
// Assert
|
||||
message.Should().Contain("6.00 kW");
|
||||
message.Should().Contain("5.5 kW");
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = originalCulture;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildLimitMessage_ShouldContainCurrentAndLimitInKw()
|
||||
{
|
||||
var message = PowerLimitEnforcementService.BuildLimitMessage(6.0, 5.5);
|
||||
#endregion
|
||||
|
||||
message.Should().Contain("6.00 kW");
|
||||
message.Should().Contain("5.5 kW");
|
||||
}
|
||||
#region Debug triggers
|
||||
|
||||
[Fact]
|
||||
public void TriggerLimitBreach_WhenNotActive_ShouldEnforceRegardlessOfPower()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
sut.TriggerLimitBreach();
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(BoilerSwitch)), null),
|
||||
Times.Once);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(AutoManagement)), null),
|
||||
Times.Once);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "create", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once);
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Exactly(3));
|
||||
VerifyTurnOff(BoilerSwitch, 1);
|
||||
VerifyTurnOff(AutoManagement, 1);
|
||||
VerifyNotificationCreated(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TriggerLimitBreach_WhenAlreadyActive_ShouldDoNothing()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.TriggerLimitBreach();
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
// Act
|
||||
sut.TriggerLimitBreach();
|
||||
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Assert
|
||||
VerifyNoServiceCalls();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetLimitBreach_WhenActive_ShouldRecoverWithoutWaiting()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
sut.TriggerLimitBreach();
|
||||
_haContextMock.Invocations.Clear();
|
||||
|
||||
// Act
|
||||
sut.ResetLimitBreach();
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeFalse();
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(AutoManagement)), null),
|
||||
Times.Once);
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("persistent_notification", "dismiss", It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once);
|
||||
// boiler switch should NOT be turned on
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_on", It.Is<ServiceTarget>(t => t.EntityIds!.Contains(BoilerSwitch)), null),
|
||||
Times.Never);
|
||||
VerifyTurnOn(AutoManagement, 1);
|
||||
VerifyTurnOn(BoilerSwitch, 0);
|
||||
VerifyNotificationDismissed(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetLimitBreach_WhenNotActive_ShouldDoNothing()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
sut.ResetLimitBreach();
|
||||
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.Never);
|
||||
// Assert
|
||||
VerifyNoServiceCalls();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TriggerLimitBreach_WhenDisabled_ShouldStillEnforce()
|
||||
{
|
||||
// Arrange
|
||||
_options.EnforceLimitEnabled = false;
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
sut.TriggerLimitBreach();
|
||||
|
||||
// Assert
|
||||
sut.IsLimitActive.Should().BeTrue("debug trigger bypasses EnforceLimitEnabled so the button always works");
|
||||
_haContextMock.Verify(
|
||||
x => x.CallService("switch", "turn_off", It.IsAny<ServiceTarget>(), It.IsAny<object?>()),
|
||||
Times.AtLeastOnce);
|
||||
VerifyTurnOff(BoilerSwitch, 1);
|
||||
VerifyNotificationCreated(1);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user