Get rid of NightLightStateMachine in the tests

This commit is contained in:
Sven van Heugten 2026-01-04 10:23:36 +01:00
parent bd550bfcd9
commit 285874777a
3 changed files with 17 additions and 41 deletions

View file

@ -3,6 +3,7 @@ namespace NightLight.Core.Tests
open System
open System.Text.RegularExpressions
open NightLight.Core.Models
open NightLight.Core.Core
open FsToolkit.ErrorHandling
open FSharp.Data
@ -33,7 +34,12 @@ type FakeLight(light: Light) =
brightness <- newBrightness
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 =
lights
@ -57,6 +63,13 @@ type FakeHome(now: DateTime) =
}
|> 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) =
@ -70,9 +83,7 @@ type FakeHome(now: DateTime) =
""type"": ""device_announce"",
""data"": {{ ""friendly_name"": ""{light.FriendlyName}"" }}
}}" }
|> nightLightStateMachine.SendMessage
|> ReceivedZigbeeEvent
|> sendEvent
| HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff()
| TimeChanged time -> nightLightStateMachine.ChangeTime time
nightLightStateMachine.TransmittedCommands |> Seq.iter processCommand
nightLightStateMachine.ClearTransmittedCommands()
| TimeChanged time -> time |> Event.TimeChanged |> sendEvent

View file

@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="NightLightStateMachine.fs" />
<Compile Include="FakeHome.fs" />
<Compile Include="Arbitraries.fs" />
<Compile Include="NightLightTests.fs" />

View file

@ -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()