Files
home-assistant-controller/test/BusSpec.hs
T
2026-08-21 13:15:31 +03:00

45 lines
1.5 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module BusSpec (spec) where
import AFRP (Request (..))
import Control.Concurrent.STM
( atomically
, dupTChan
, readTChan
, writeTChan
)
import Data.Aeson (Value (..))
import Data.Time (UTCTime (..))
import Data.UUID (nil)
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
import HomeAssistant.Runtime.Bus
import Katip (Namespace (Namespace), runKatipContextT)
import Test.Hspec
spec :: Spec
spec = describe "Bus" $ do
it "broadcasts inbound messages to every dup'd channel in order" $ withBus $ \bus -> do
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" $ withBus $ \bus -> do
let req = Request (UTCTime (toEnum 0) 0) nil
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
channelHassEval bus (CallService req svc)
atomically (readTChan (busOutbound bus)) `shouldReturn` svc
it "generates unique sequential call ids" $ do
gen <- mkCallIdGen 0
a <- generateCallId gen
b <- generateCallId gen
(a, b) `shouldBe` (1, 2)