Get rid of NightLightStateMachine in the tests
This commit is contained in:
parent
bd550bfcd9
commit
285874777a
3 changed files with 17 additions and 41 deletions
|
|
@ -3,6 +3,7 @@ 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
|
||||||
|
|
||||||
|
|
@ -33,7 +34,12 @@ type FakeLight(light: Light) =
|
||||||
brightness <- newBrightness
|
brightness <- newBrightness
|
||||||
|
|
||||||
type FakeHome(now: DateTime) =
|
type FakeHome(now: DateTime) =
|
||||||
let nightLightStateMachine = NightLightStateMachine now
|
let mutable nightLightStateMachine = State now
|
||||||
|
|
||||||
|
let assertIsOkAndGet result =
|
||||||
|
match result with
|
||||||
|
| Ok value -> value
|
||||||
|
| Error error -> failwith $"Expected Ok, got Error {error}"
|
||||||
|
|
||||||
let friendlyNameToFakeLight =
|
let friendlyNameToFakeLight =
|
||||||
lights
|
lights
|
||||||
|
|
@ -57,6 +63,13 @@ type FakeHome(now: DateTime) =
|
||||||
}
|
}
|
||||||
|> 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 _.LightStates = friendlyNameToFakeLight.Values |> Seq.map _.LightWithState
|
||||||
|
|
||||||
member _.Interact(interaction: Interaction) =
|
member _.Interact(interaction: Interaction) =
|
||||||
|
|
@ -70,9 +83,7 @@ type FakeHome(now: DateTime) =
|
||||||
""type"": ""device_announce"",
|
""type"": ""device_announce"",
|
||||||
""data"": {{ ""friendly_name"": ""{light.FriendlyName}"" }}
|
""data"": {{ ""friendly_name"": ""{light.FriendlyName}"" }}
|
||||||
}}" }
|
}}" }
|
||||||
|> nightLightStateMachine.SendMessage
|
|> ReceivedZigbeeEvent
|
||||||
|
|> sendEvent
|
||||||
| HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff()
|
| HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff()
|
||||||
| TimeChanged time -> nightLightStateMachine.ChangeTime time
|
| TimeChanged time -> time |> Event.TimeChanged |> sendEvent
|
||||||
|
|
||||||
nightLightStateMachine.TransmittedCommands |> Seq.iter processCommand
|
|
||||||
nightLightStateMachine.ClearTransmittedCommands()
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="NightLightStateMachine.fs" />
|
|
||||||
<Compile Include="FakeHome.fs" />
|
<Compile Include="FakeHome.fs" />
|
||||||
<Compile Include="Arbitraries.fs" />
|
<Compile Include="Arbitraries.fs" />
|
||||||
<Compile Include="NightLightTests.fs" />
|
<Compile Include="NightLightTests.fs" />
|
||||||
|
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
namespace NightLight.Core.Tests
|
|
||||||
|
|
||||||
open System
|
|
||||||
open System.Collections.Generic
|
|
||||||
open FsToolkit.ErrorHandling
|
|
||||||
open NightLight.Core.Models
|
|
||||||
open NightLight.Core.Core
|
|
||||||
|
|
||||||
type NightLightStateMachine(now: DateTime) =
|
|
||||||
let mutable state = State now
|
|
||||||
|
|
||||||
let transmittedCommands = new List<Message>()
|
|
||||||
|
|
||||||
let assertIsOk (result: Result<unit, 'a>) : unit =
|
|
||||||
match result with
|
|
||||||
| Ok() -> ()
|
|
||||||
| Error error -> failwith $"Expected Ok, got Error {error}"
|
|
||||||
|
|
||||||
let sendEvent event =
|
|
||||||
result {
|
|
||||||
let! newState, commands = state.OnEventReceived event
|
|
||||||
state <- newState
|
|
||||||
transmittedCommands.AddRange commands
|
|
||||||
}
|
|
||||||
|> assertIsOk
|
|
||||||
|
|
||||||
member _.TransmittedCommands = transmittedCommands.AsReadOnly()
|
|
||||||
|
|
||||||
member _.SendMessage message =
|
|
||||||
ReceivedZigbeeEvent message |> sendEvent
|
|
||||||
|
|
||||||
member _.ChangeTime time = TimeChanged time |> sendEvent
|
|
||||||
|
|
||||||
member _.ClearTransmittedCommands() = transmittedCommands.Clear()
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue