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