{-# LANGUAGE OverloadedStrings #-} module BusSpec (spec) where import Control.Concurrent.STM ( atomically , dupTChan , readTChan , writeTChan ) import Data.Aeson (Value (..)) import HomeAssistant.Controller (HASSEff (..), Service (..)) import HomeAssistant.Runtime.Bus import Test.Hspec spec :: Spec spec = describe "Bus" $ do it "broadcasts inbound messages to every dup'd channel in order" $ do bus <- newBus 0 p1 <- atomically $ dupTChan (busInbound bus) p2 <- atomically $ dupTChan (busInbound bus) atomically $ writeTChan (busInbound bus) (Number 1) atomically $ writeTChan (busInbound bus) (Number 2) r1 <- atomically $ (,) <$> readTChan p1 <*> readTChan p1 r2 <- atomically $ (,) <$> readTChan p2 <*> readTChan p2 r1 `shouldBe` (Number 1, Number 2) r2 `shouldBe` (Number 1, Number 2) it "channelHassEval writes CallService to the outbound channel" $ do bus <- newBus 0 let svc = Service "light" "turn_on" Nothing "light.bedroom_masse" channelHassEval bus (CallService svc) atomically (readTChan (busOutbound bus)) `shouldReturn` svc it "channelHassEval leaves Pure untouched" $ do bus <- newBus 0 channelHassEval bus (Pure 42) `shouldReturn` (42 :: Int) it "generates unique sequential call ids" $ do gen <- mkCallIdGen 0 a <- generateCallId gen b <- generateCallId gen (a, b) `shouldBe` (1, 2)