Improve types for remote button presses in tests

This commit is contained in:
Sven van Heugten 2026-03-15 11:33:26 +01:00
parent 3913522cc3
commit 1dc1faa16a
4 changed files with 25 additions and 27 deletions

View file

@ -6,11 +6,12 @@ open NightLight.Core.Models
open FsToolkit.ErrorHandling
open FSharp.Data
type RemoteInteraction =
type BedroomControllingRemoteInteraction =
| RemotePressedOnButton
| RemotePressedOffButton
| RemotePressedLeftButton
| RemotePressedRightButton
type LivingRoomControllingRemoteAction = | RemotePressedRightButton
type HumanInteraction =
| LightPoweredOn of Light
@ -18,7 +19,8 @@ type HumanInteraction =
type Interaction =
| HumanInteraction of HumanInteraction
| RemoteInteraction of RemoteInteraction
| BedroomControllingRemoteInteraction of BedroomControllingRemoteInteraction
| LivingRoomControllingRemoteInteraction of LivingRoomControllingRemoteAction
| TimeChanged of DateTime
type Color =
@ -123,22 +125,22 @@ type FakeHome() =
|> ReceivedZigbeeEvent
|> onEventPublished.Trigger
| HumanInteraction(LightPoweredOff light) -> friendlyNameToFakeLight[(lightProps light).FriendlyName].PowerOff()
| RemoteInteraction RemotePressedOnButton ->
| BedroomControllingRemoteInteraction RemotePressedOnButton ->
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
Payload = @"{ ""action"": ""on"" }" }
|> ReceivedZigbeeEvent
|> onEventPublished.Trigger
| RemoteInteraction RemotePressedOffButton ->
| BedroomControllingRemoteInteraction RemotePressedOffButton ->
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
Payload = @"{ ""action"": ""off"" }" }
|> ReceivedZigbeeEvent
|> onEventPublished.Trigger
| RemoteInteraction RemotePressedLeftButton ->
| BedroomControllingRemoteInteraction RemotePressedLeftButton ->
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
Payload = @"{ ""action"": ""arrow_left_click"" }" }
|> ReceivedZigbeeEvent
|> onEventPublished.Trigger
| RemoteInteraction RemotePressedRightButton ->
| LivingRoomControllingRemoteInteraction RemotePressedRightButton ->
{ Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}"
Payload = @"{ ""action"": ""arrow_right_click"" }" }
|> ReceivedZigbeeEvent

View file

@ -36,12 +36,11 @@ let private genHumanInteraction =
|> Gen.map Interaction.HumanInteraction
let private genRemoteInteraction =
Gen.elements
[ RemotePressedOnButton
RemotePressedOffButton
RemotePressedLeftButton
RemotePressedRightButton ]
|> Gen.map RemoteInteraction
Gen.oneof
[ Gen.elements [ RemotePressedOnButton; RemotePressedOffButton; RemotePressedLeftButton ]
|> Gen.map BedroomControllingRemoteInteraction
Gen.constant (LivingRoomControllingRemoteInteraction RemotePressedRightButton) ]
let private genInteraction =
Gen.frequency [ 4, genTimeChanged; 1, genHumanInteraction; 1, genRemoteInteraction ]

View file

@ -36,17 +36,13 @@ let doesLightHavePowerAfterInteractions light interactions =
|> Seq.tryLast
|> Option.defaultValue false
let tryGetLastBedroomRemoteInteraction interactions =
let tryGetLastBedroomControllingRemoteInteraction interactions =
interactions
|> Seq.indexed
|> Seq.choose (fun (index, interaction) ->
match interaction with
| Interaction.RemoteInteraction remoteInteraction ->
match remoteInteraction with
| RemotePressedOnButton
| RemotePressedOffButton
| RemotePressedLeftButton -> Some(index, remoteInteraction)
| RemotePressedRightButton -> None
| Interaction.BedroomControllingRemoteInteraction bedroomRemoteInteraction ->
Some(index, bedroomRemoteInteraction)
| _ -> None)
|> Seq.tryLast

View file

@ -47,7 +47,7 @@ type NightLightTests() =
let time = getTimeAfterInteractions interactions |> _.TimeOfDay
let alarm =
hasNewDayStartedSince interactions (tryGetLastBedroomRemoteInteraction interactions)
hasNewDayStartedSince interactions (tryGetLastBedroomControllingRemoteInteraction interactions)
&& startOfDay <= time
&& time <= endOfAlarm
@ -90,15 +90,16 @@ type NightLightTests() =
|> Seq.filter (fun (light, _) -> doesLightHavePowerAfterInteractions light interactions)
|> Seq.toList
let lastBedroomRemoteInteraction = tryGetLastBedroomRemoteInteraction interactions
let lastBedroomControllingRemoteInteraction =
tryGetLastBedroomControllingRemoteInteraction interactions
let newDayStartedSinceBedroomRemote =
hasNewDayStartedSince interactions lastBedroomRemoteInteraction
hasNewDayStartedSince interactions lastBedroomControllingRemoteInteraction
let hasPressedRight =
interactions
|> Seq.exists (function
| Interaction.RemoteInteraction RemotePressedRightButton -> true
| Interaction.LivingRoomControllingRemoteInteraction RemotePressedRightButton -> true
| _ -> false)
let isExpectedOn light =
@ -108,11 +109,10 @@ type NightLightTests() =
if newDayStartedSinceBedroomRemote then
true
else
match lastBedroomRemoteInteraction with
match lastBedroomControllingRemoteInteraction with
| Some(_, RemotePressedOffButton) -> false
| Some(_, RemotePressedLeftButton) -> light = LeftBedroomLamp
| Some(_, RemotePressedOnButton) -> true
| Some(_, RemotePressedRightButton) -> failwith "unexpected"
| None -> true
| LivingRoomWallLamp
| LivingRoomFloorLamp -> not hasPressedRight
@ -120,7 +120,8 @@ type NightLightTests() =
lightsWithPower
|> Seq.forall (fun (light, state) -> state.IsOn = isExpectedOn light)
|> Prop.collect $"last bedroom remote interaction is {lastBedroomRemoteInteraction |> Option.map snd}"
|> Prop.collect
$"last bedroom controlling remote interaction is {lastBedroomControllingRemoteInteraction |> Option.map snd}"
|> Prop.collect $"pressed right: {hasPressedRight}"
|> Prop.collect $"{lightsWithPower.Length} light(s) with power"
|> Prop.classify newDayStartedSinceBedroomRemote "new day since bedroom remote"