Refactor tests to prepare for stateful bedroom On button

This commit is contained in:
Sven van Heugten 2026-03-16 19:35:17 +01:00
parent 0c964719d9
commit 6e6a600e3c

View file

@ -7,6 +7,12 @@ open NightLight.Core.Models
open FsCheck.Xunit
open FsCheck.FSharp
type private BedroomLightsCycle =
| BothOff
| BothOn
| LeftOn
| RightOn
type NightLightTests() =
let createFakeHomeWithNightLightAndInteract (interactions: Interaction list) =
let mutable nightLightStateMachine = NightLightStateMachine()
@ -109,27 +115,37 @@ type NightLightTests() =
| LivingRoomRemotePressedOffButton -> false)
true
let bedroomLightsCycle =
interactions
|> Seq.choose (function
| Interaction.BedroomControllingRemoteInteraction interaction -> Some interaction
| _ -> None)
|> Seq.fold
(fun state interaction ->
match state, interaction with
| _, RemotePressedOffButton -> BothOff
| _, RemotePressedOnButton -> BothOn
| _, RemotePressedLeftButton -> LeftOn)
BothOn
let isExpectedOn light =
match light with
| LeftBedroomLamp
| LeftBedroomLamp ->
newDayStartedSinceLastBedroomControllingRemoteInteraction
|| bedroomLightsCycle = BothOn
|| bedroomLightsCycle = LeftOn
| RightBedroomLamp ->
if newDayStartedSinceLastBedroomControllingRemoteInteraction then
true
else
match lastBedroomControllingRemoteInteraction with
| Some(_, RemotePressedOffButton) -> false
| Some(_, RemotePressedLeftButton) -> light = LeftBedroomLamp
| Some(_, RemotePressedOnButton) -> true
| None -> true
newDayStartedSinceLastBedroomControllingRemoteInteraction
|| bedroomLightsCycle = BothOn
|| bedroomLightsCycle = RightOn
| LivingRoomWallLamp
| LivingRoomFloorLamp -> livingRoomLightsToggledOn
| BathroomCeilingLamp -> true
lightsWithPower
|> Seq.forall (fun (light, state) -> state.IsOn = isExpectedOn light)
|> Prop.collect
$"last bedroom controlling remote interaction is {lastBedroomControllingRemoteInteraction |> Option.map snd}"
|> Prop.collect $"{lightsWithPower.Length} light(s) with power"
|> Prop.collect $"bedroom lights cycle = {bedroomLightsCycle}"
|> Prop.classify livingRoomLightsToggledOn "living room lights toggled on"
|> Prop.classify
newDayStartedSinceLastBedroomControllingRemoteInteraction