Make FakeHome independent of NightLightStateMachine

This commit is contained in:
Sven van Heugten 2026-01-04 20:17:35 +01:00
parent 9531dee52b
commit 15bf8db9f4
2 changed files with 30 additions and 26 deletions

View file

@ -1,6 +1,7 @@
namespace NightLight.Core.Tests
open System
open NightLight.Core.Core
open FsCheck.Xunit
open FsCheck.FSharp
@ -26,24 +27,37 @@ module InteractionsHelpers =
[<Properties(Arbitrary = [| typeof<Arbitraries> |])>]
type NightLightTests() =
let createFakeHomeWithNightLightAndInteract (interactions: Interaction list) =
let mutable nightLightStateMachine = NightLightStateMachine()
let fakeHome = FakeHome()
fakeHome.OnEventPublished.Add(fun event ->
match event |> nightLightStateMachine.OnEventReceived with
| Ok(newState, commands) ->
commands |> Seq.iter fakeHome.ProcessCommand
nightLightStateMachine <- newState
| Error error -> failwith $"Unexpected error {error}")
fakeHome.Interact interactions
fakeHome
[<Property>]
let ``Brightness should always be under 255`` (interactions: Interaction list) =
let fakeHome = FakeHome()
fakeHome.Interact interactions
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy)
[<Property>]
let ``Lights should be red during the night`` (interactions: Interaction list) =
let fakeHome = FakeHome()
fakeHome.Interact interactions
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
InteractionsHelpers.isNightAfter interactions
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
[<Property>]
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
let fakeHome = FakeHome()
fakeHome.Interact interactions
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
InteractionsHelpers.isDayAfter interactions
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)