night-light/NightLight.Core.Tests/NightLightTests.fs

129 lines
5.5 KiB
FSharp

namespace NightLight.Core.Tests
open NightLight.Core.Core
open NightLight.Core.Tests.InteractionListGenerators
open NightLight.Core.Tests.InteractionListHelpers
open NightLight.Core.Models
open FsCheck.Xunit
open FsCheck.FSharp
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(Arbitrary = [| typeof<ArbitraryInteractions> |])>]
let ``All lights should either be off or have the right color`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let partOfDay = getPartOfDayAfterInteractions interactions
fakeHome.LightStates
|> Seq.forall (function
| _, Off -> true
| _, On(_, color) ->
match partOfDay with
| Day -> color = White || color = Yellow
| Night -> color = Red)
|> Prop.collect partOfDay
|> Prop.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
|> Prop.label fakeHome.Label
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
[<Property(Arbitrary = [| typeof<ArbitraryInteractions> |], MaxTest = 500)>]
let ``All lights should either be off or have a brightness that fits its color`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let time = getTimeAfterInteractions interactions |> _.TimeOfDay
let alarm =
hasNewDayStartedSince interactions (tryGetLastBedroomControllingRemoteInteraction interactions)
&& startOfDay <= time
&& time <= endOfAlarm
let scaledForAlarm light brightness =
if (light = RightBedroomLamp || light = LeftBedroomLamp) && alarm then
float brightness * ((time - startOfDay) / (endOfAlarm - startOfDay)) |> byte
else
brightness
fakeHome.LightStates
|> Seq.forall (fun (light, state) ->
let maybeExpectedBrightness =
match (lightProps light).Bulb, state with
| _, Off -> None
| IkeaBulb, On(_, White) -> Some 254uy
| IkeaBulb, On(_, Yellow) -> Some 210uy
| IkeaBulb, On(_, Red) -> Some 254uy
| PaulmannBulb, On(_, White) -> Some 35uy
| PaulmannBulb, On(_, Yellow) -> Some 35uy
| PaulmannBulb, On(_, Red) -> Some 80uy
|> Option.map (scaledForAlarm light)
let maybeActualBrightness =
match state with
| Off -> None
| On(brightness, _) -> Some brightness
maybeExpectedBrightness = maybeActualBrightness)
|> Prop.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
|> Prop.classify alarm "alarm"
|> Prop.label fakeHome.Label
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
[<Property(Arbitrary = [| typeof<ArbitraryInteractions> |], MaxTest = 500)>]
let ``All lights with power should have the correct state`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let lightsWithPower =
fakeHome.LightStates
|> Seq.filter (fun (light, _) -> doesLightHavePowerAfterInteractions light interactions)
|> Seq.toList
let lastBedroomControllingRemoteInteraction =
tryGetLastBedroomControllingRemoteInteraction interactions
let newDayStartedSinceBedroomRemote =
hasNewDayStartedSince interactions lastBedroomControllingRemoteInteraction
let hasPressedRight =
interactions
|> Seq.exists (function
| Interaction.LivingRoomControllingRemoteInteraction RemotePressedRightButton -> true
| _ -> false)
let isExpectedOn light =
match light with
| LeftBedroomLamp
| RightBedroomLamp ->
if newDayStartedSinceBedroomRemote then
true
else
match lastBedroomControllingRemoteInteraction with
| Some(_, RemotePressedOffButton) -> false
| Some(_, RemotePressedLeftButton) -> light = LeftBedroomLamp
| Some(_, RemotePressedOnButton) -> true
| None -> true
| LivingRoomWallLamp
| LivingRoomFloorLamp -> not hasPressedRight
| 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 $"pressed right: {hasPressedRight}"
|> Prop.collect $"{lightsWithPower.Length} light(s) with power"
|> Prop.classify newDayStartedSinceBedroomRemote "new day since bedroom remote"
|> Prop.label fakeHome.Label
|> Prop.trivial (lightsWithPower.Length = 0)