Make FakeHome independent of NightLightStateMachine
This commit is contained in:
parent
9531dee52b
commit
15bf8db9f4
2 changed files with 30 additions and 26 deletions
|
|
@ -3,7 +3,6 @@ namespace NightLight.Core.Tests
|
||||||
open System
|
open System
|
||||||
open System.Text.RegularExpressions
|
open System.Text.RegularExpressions
|
||||||
open NightLight.Core.Models
|
open NightLight.Core.Models
|
||||||
open NightLight.Core.Core
|
|
||||||
open FsToolkit.ErrorHandling
|
open FsToolkit.ErrorHandling
|
||||||
open FSharp.Data
|
open FSharp.Data
|
||||||
|
|
||||||
|
|
@ -44,19 +43,19 @@ type FakeLight(light: Light) =
|
||||||
color <- newColor
|
color <- newColor
|
||||||
|
|
||||||
type FakeHome() =
|
type FakeHome() =
|
||||||
let mutable nightLightStateMachine = NightLightStateMachine()
|
|
||||||
|
|
||||||
let assertIsOkAndGet result =
|
|
||||||
match result with
|
|
||||||
| Ok value -> value
|
|
||||||
| Error error -> failwith $"Expected Ok, got Error {error}"
|
|
||||||
|
|
||||||
let friendlyNameToFakeLight =
|
let friendlyNameToFakeLight =
|
||||||
lights
|
lights
|
||||||
|> Seq.map (fun light -> light.FriendlyName, FakeLight light)
|
|> Seq.map (fun light -> light.FriendlyName, FakeLight light)
|
||||||
|> Map.ofSeq
|
|> Map.ofSeq
|
||||||
|
|
||||||
let processCommand command =
|
let onEventPublished = new Event<Event>()
|
||||||
|
|
||||||
|
member _.LightStates = friendlyNameToFakeLight.Values |> Seq.map _.LightWithState
|
||||||
|
|
||||||
|
[<CLIEvent>]
|
||||||
|
member _.OnEventPublished = onEventPublished.Publish
|
||||||
|
|
||||||
|
member _.ProcessCommand(command: Message) =
|
||||||
option {
|
option {
|
||||||
let! friendlyName =
|
let! friendlyName =
|
||||||
let m = Regex.Match(command.Topic, "^zigbee2mqtt/(.+)/set$")
|
let m = Regex.Match(command.Topic, "^zigbee2mqtt/(.+)/set$")
|
||||||
|
|
@ -86,15 +85,6 @@ type FakeHome() =
|
||||||
}
|
}
|
||||||
|> ignore
|
|> ignore
|
||||||
|
|
||||||
let sendEvent event =
|
|
||||||
let newState, commands =
|
|
||||||
event |> nightLightStateMachine.OnEventReceived |> assertIsOkAndGet
|
|
||||||
|
|
||||||
commands |> Seq.iter processCommand
|
|
||||||
nightLightStateMachine <- newState
|
|
||||||
|
|
||||||
member _.LightStates = friendlyNameToFakeLight.Values |> Seq.map _.LightWithState
|
|
||||||
|
|
||||||
member _.Interact(interaction: Interaction) =
|
member _.Interact(interaction: Interaction) =
|
||||||
match interaction with
|
match interaction with
|
||||||
| HumanInteraction(LightTurnedOn light) ->
|
| HumanInteraction(LightTurnedOn light) ->
|
||||||
|
|
@ -107,9 +97,9 @@ type FakeHome() =
|
||||||
""data"": {{ ""friendly_name"": ""{light.FriendlyName}"" }}
|
""data"": {{ ""friendly_name"": ""{light.FriendlyName}"" }}
|
||||||
}}" }
|
}}" }
|
||||||
|> ReceivedZigbeeEvent
|
|> ReceivedZigbeeEvent
|
||||||
|> sendEvent
|
|> onEventPublished.Trigger
|
||||||
| HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff()
|
| HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff()
|
||||||
| TimeChanged newTime -> newTime |> Event.TimeChanged |> sendEvent
|
| TimeChanged newTime -> newTime |> Event.TimeChanged |> onEventPublished.Trigger
|
||||||
|
|
||||||
type FakeHome with
|
type FakeHome with
|
||||||
member this.Interact(interactions: Interaction seq) = interactions |> Seq.iter this.Interact
|
member this.Interact(interactions: Interaction seq) = interactions |> Seq.iter this.Interact
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
namespace NightLight.Core.Tests
|
namespace NightLight.Core.Tests
|
||||||
|
|
||||||
open System
|
open System
|
||||||
|
open NightLight.Core.Core
|
||||||
open FsCheck.Xunit
|
open FsCheck.Xunit
|
||||||
open FsCheck.FSharp
|
open FsCheck.FSharp
|
||||||
|
|
||||||
|
|
@ -26,24 +27,37 @@ module InteractionsHelpers =
|
||||||
|
|
||||||
[<Properties(Arbitrary = [| typeof<Arbitraries> |])>]
|
[<Properties(Arbitrary = [| typeof<Arbitraries> |])>]
|
||||||
type NightLightTests() =
|
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>]
|
[<Property>]
|
||||||
let ``Brightness should always be under 255`` (interactions: Interaction list) =
|
let ``Brightness should always be under 255`` (interactions: Interaction list) =
|
||||||
let fakeHome = FakeHome()
|
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
||||||
fakeHome.Interact interactions
|
|
||||||
fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy)
|
fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy)
|
||||||
|
|
||||||
[<Property>]
|
[<Property>]
|
||||||
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
||||||
let fakeHome = FakeHome()
|
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
||||||
fakeHome.Interact interactions
|
|
||||||
|
|
||||||
InteractionsHelpers.isNightAfter interactions
|
InteractionsHelpers.isNightAfter interactions
|
||||||
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
||||||
|
|
||||||
[<Property>]
|
[<Property>]
|
||||||
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
||||||
let fakeHome = FakeHome()
|
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
||||||
fakeHome.Interact interactions
|
|
||||||
|
|
||||||
InteractionsHelpers.isDayAfter interactions
|
InteractionsHelpers.isDayAfter interactions
|
||||||
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)
|
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue