From 6e6a600e3c217406b7037bfa49c77ade97d69d43 Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Mon, 16 Mar 2026 19:35:17 +0100 Subject: [PATCH] Refactor tests to prepare for stateful bedroom On button --- NightLight.Core.Tests/NightLightTests.fs | 38 +++++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index 5a99241..cfbc25f 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -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