Test alarm in existing test

This commit is contained in:
Sven van Heugten 2026-01-18 07:13:26 +01:00
parent 3d84bb6435
commit 23d3a7b601
2 changed files with 43 additions and 18 deletions

View file

@ -6,23 +6,25 @@ type PartOfDay =
| Day | Day
| Night | Night
let private getPartOfDay (dateTime: DateTime) = let startOfDay = TimeSpan.FromHours 6
let endOfAlarm = TimeSpan.FromHours 6.25
let endOfDay = TimeSpan.FromHours 20.5
let getPartOfDay (dateTime: DateTime) =
match dateTime with match dateTime with
| _ when | _ when dateTime.TimeOfDay >= startOfDay && dateTime.TimeOfDay < endOfDay -> Day
dateTime.TimeOfDay >= TimeSpan.FromHours 6
&& dateTime.TimeOfDay < TimeSpan.FromHours 20.5
->
Day
| _ -> Night | _ -> Night
let getPartOfDayAfterInteractions interactions = let getTimeAfterInteractions interactions =
interactions interactions
|> Seq.choose (fun interaction -> |> Seq.choose (fun interaction ->
match interaction with match interaction with
| Interaction.TimeChanged time -> Some time | Interaction.TimeChanged time -> Some time
| _ -> None) | _ -> None)
|> Seq.last |> Seq.last
|> getPartOfDay
let getPartOfDayAfterInteractions interactions =
interactions |> getTimeAfterInteractions |> getPartOfDay
let doesLightHavePowerAfterInteractions light interactions = let doesLightHavePowerAfterInteractions light interactions =
interactions interactions

View file

@ -41,21 +41,44 @@ type NightLightTests() =
|> Prop.label fakeHome.Label |> Prop.label fakeHome.Label
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0) |> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
[<Property(Arbitrary = [| typeof<ArbitraryInteractions> |])>] // TODO: Bias generator for alarm cases so that `MaxTest` can be reduced
[<Property(Arbitrary = [| typeof<ArbitraryInteractions> |], MaxTest = 10_000)>]
let ``All lights should either be off or have a brightness that fits its color`` (interactions: Interaction list) = let ``All lights should either be off or have a brightness that fits its color`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let time = getTimeAfterInteractions interactions |> _.TimeOfDay
let alarm =
hasNewDayStartedSince interactions (tryGetLastRemoteInteraction interactions)
&& startOfDay <= time
&& time <= endOfAlarm
let scaledForAlarm light brightness =
if light.ControlledWithRemote <> NonRemote && alarm then
float brightness * ((time - startOfDay) / (endOfAlarm - startOfDay)) |> byte
else
brightness
fakeHome.LightStates fakeHome.LightStates
|> Seq.forall (function |> Seq.forall (fun (light, state) ->
| _, Off let maybeExpectedBrightness =
| { Bulb = IkeaBulb }, On(254uy, White) -> true match light, state with
| { Bulb = IkeaBulb }, On(210uy, Yellow) -> true | _, Off -> None
| { Bulb = IkeaBulb }, On(254uy, Red) -> true | { Bulb = IkeaBulb }, On(_, White) -> Some 254uy
| { Bulb = PaulmannBulb }, On(35uy, White) -> true | { Bulb = IkeaBulb }, On(_, Yellow) -> Some 210uy
| { Bulb = PaulmannBulb }, On(35uy, Yellow) -> true | { Bulb = IkeaBulb }, On(_, Red) -> Some 254uy
| { Bulb = PaulmannBulb }, On(80uy, Red) -> true | { Bulb = PaulmannBulb }, On(_, White) -> Some 35uy
| _ -> false) | { Bulb = PaulmannBulb }, On(_, Yellow) -> Some 35uy
| { Bulb = 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.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
|> Prop.classify alarm "alarm"
|> Prop.label fakeHome.Label |> Prop.label fakeHome.Label
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0) |> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)