From 50c82393ed6457c1d0bd62c090a57a2ad480a4c5 Mon Sep 17 00:00:00 2001 From: Mats Rauhala Date: Fri, 21 Aug 2026 16:01:15 +0300 Subject: [PATCH] Clean tests --- test/RuntimeSpec.hs | 71 ++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/test/RuntimeSpec.hs b/test/RuntimeSpec.hs index d28d0fb..953f58e 100644 --- a/test/RuntimeSpec.hs +++ b/test/RuntimeSpec.hs @@ -1,43 +1,40 @@ -{-# LANGUAGE OverloadedStrings #-} module RuntimeSpec (spec) where -import Control.Concurrent (threadDelay) -import Control.Concurrent.Async (async, race) -import Control.Concurrent.STM (atomically, isEmptyTChan, readTChan, writeTChan) -import Data.Aeson (Value, object, (.=)) -import Data.Text (Text) -import HomeAssistant.Controller (light, lightController, Target (..)) -import HomeAssistant.Runtime (Controller (..), runController) -import HomeAssistant.Runtime.Bus import Test.Hspec -import Katip (Severity(..)) spec :: Spec -spec = describe "runController" $ do - it "feeds inbound events through the machine and forwards service calls" $ withBus InfoS $ \bus -> do - _ <- async (runController bus (Controller "test" lightController True)) - putStrLn "Before the delay" - threadDelay 100000 -- let the controller dup its inbound channel - putStrLn "After the delay" - atomically $ writeTChan (busInbound bus) (doorEvent "on") -- initial value: no change event - atomically $ writeTChan (busInbound bus) (doorEvent "off") -- door closes: lights on - atomically $ writeTChan (busInbound bus) (doorEvent "on") -- door opens: lights off - putStrLn "After the writes" - Right (_, svc1) <- boundedRead (busOutbound bus) - Right (_, svc2) <- boundedRead (busOutbound bus) - putStrLn "After the reads" - svc1 `shouldBe` light [EntityId "light.bedroom_masse"] True - svc2 `shouldBe` light [EntityId "light.bedroom_masse"] False - atomically (isEmptyTChan (busOutbound bus)) `shouldReturn` True - where - boundedRead channel = race (threadDelay 10000) (atomically (readTChan channel)) +spec = pure () -doorEvent :: Text -> Value -doorEvent state = object - [ "event" .= object - [ "data" .= object - [ "entity_id" .= ("binary_sensor.makuuhuone_ovi_contact" :: Text) - , "new_state" .= object ["state" .= state] - ] - ] - ] +-- I'm commenting out the test as that specific controller +-- was just a dummy for seeing how the data flows (dead code) +-- But leaving it commented as it retains some code sample on how and what to test +-- +-- spec :: Spec +-- spec = describe "runController" $ do +-- it "feeds inbound events through the machine and forwards service calls" $ withBus InfoS $ \bus -> do +-- _ <- async (runController bus (Controller "test" lightController True)) +-- putStrLn "Before the delay" +-- threadDelay 100000 -- let the controller dup its inbound channel +-- putStrLn "After the delay" +-- atomically $ writeTChan (busInbound bus) (doorEvent "on") -- initial value: no change event +-- atomically $ writeTChan (busInbound bus) (doorEvent "off") -- door closes: lights on +-- atomically $ writeTChan (busInbound bus) (doorEvent "on") -- door opens: lights off +-- putStrLn "After the writes" +-- Right (_, svc1) <- boundedRead (busOutbound bus) +-- Right (_, svc2) <- boundedRead (busOutbound bus) +-- putStrLn "After the reads" +-- svc1 `shouldBe` light [EntityId "light.bedroom_masse"] True +-- svc2 `shouldBe` light [EntityId "light.bedroom_masse"] False +-- atomically (isEmptyTChan (busOutbound bus)) `shouldReturn` True +-- where +-- boundedRead channel = race (threadDelay 10000) (atomically (readTChan channel)) +-- +-- doorEvent :: Text -> Value +-- doorEvent state = object +-- [ "event" .= object +-- [ "data" .= object +-- [ "entity_id" .= ("binary_sensor.makuuhuone_ovi_contact" :: Text) +-- , "new_state" .= object ["state" .= state] +-- ] +-- ] +-- ]