Files
Evergreen-Health/Admin.NET.Test/Health/RetiredDeviceTests.cs
T
2026-11-09 16:10:05 +08:00

57 lines
2.6 KiB
C#

using Evergreen.Health.Application.Health.Devices;
using Evergreen.Health.Application.Health.Devices.Discovery;
using Evergreen.Health.Application.Health.Integrations.HomeAssistant;
using Evergreen.Health.Application.Health.Scenes;
using Evergreen.Health.Application.Health.Devices.Contracts;
using Microsoft.Extensions.Options;
using Xunit;
namespace Admin.NET.Test.Health;
public sealed class RetiredDeviceTests
{
[Theory]
[InlineData(null)]
[InlineData("user")]
public void ExplicitRetiredBindingExcludesEntireDeviceEvenWhenBindingIsDisabled(string? disabledBy)
{
var catalog = new HomeAssistantDeviceCatalog(Options.Create(new HomeAssistantOptions
{
ExcludedDeviceEntityIds = ["number.old_heat"]
}), new DeviceModuleRegistry([]));
var registry = new HomeAssistantRegistries
{
Devices = [new() { Id = "retired", Name = "智能床垫" }, new() { Id = "new", Name = "智能床垫" }],
Entities = [new() { DeviceId = "retired", EntityId = "NUMBER.OLD_HEAT", DisabledBy = disabledBy },
new() { DeviceId = "retired", EntityId = "switch.old" },
new() { DeviceId = "new", EntityId = "sensor.presence" }]
};
var states = new Dictionary<string, HomeAssistantState>
{
["switch.old"] = new() { EntityId = "switch.old", State = "on" },
["sensor.presence"] = new() { EntityId = "sensor.presence", State = "on" }
};
var remaining = Assert.Single(catalog.BuildDiscoveredDevices(registry, states));
Assert.Equal("new", remaining.DeviceId);
Assert.Equal("", remaining.ModuleKey);
registry.Devices.RemoveAt(1);
Assert.Empty(catalog.BuildDiscoveredDevices(registry, states));
Assert.Empty(catalog.BuildDiscoveredDevices(null, states));
}
[Fact]
public async Task PrimaryLightCommandsStillTrackKnownOffStateWithoutMattressRules()
{
var state = new SceneLightSafetyState();
var gateway = new FakeHaGateway();
var runner = new DeviceActionRunner(gateway, TimeProvider.System, new(), [new PrimaryLightStateRule(state)]);
await runner.SendAsync(new("关灯", "light.room", "light", "turn_off")
{ Intent = DeviceCommandIntent.TurnRelaxationLightOff }, default);
Assert.NotNull(state.OffAt);
await runner.SendAsync(new("开灯", "light.room", "light", "turn_on")
{ Intent = DeviceCommandIntent.TurnRelaxationLightOn }, default);
Assert.Null(state.OffAt);
Assert.All(gateway.Calls, call => Assert.Equal("light.room", call.EntityId));
}
}