33 lines
1.2 KiB
Haskell
33 lines
1.2 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module ConnectionSpec (spec) where
|
|
|
|
import Data.Aeson (object, (.=))
|
|
import Data.Text (Text)
|
|
import HomeAssistant.Controller (Service (..))
|
|
import HomeAssistant.Runtime.Connection (encodeService)
|
|
import Test.Hspec
|
|
|
|
spec :: Spec
|
|
spec = describe "encodeService" $ do
|
|
it "encodes a call_service message" $
|
|
encodeService 7 (Service "light" "turn_on" Nothing "light.bedroom_masse")
|
|
`shouldBe` object
|
|
[ "id" .= (7 :: Int)
|
|
, "type" .= ("call_service" :: Text)
|
|
, "domain" .= ("light" :: Text)
|
|
, "service" .= ("turn_on" :: Text)
|
|
, "target" .= object ["entity_id" .= ("light.bedroom_masse" :: Text)]
|
|
]
|
|
|
|
it "includes service_data when present" $
|
|
encodeService 8 (Service "light" "turn_on" (Just (object ["brightness" .= (200 :: Int)])) "light.bedroom_masse")
|
|
`shouldBe` object
|
|
[ "id" .= (8 :: Int)
|
|
, "type" .= ("call_service" :: Text)
|
|
, "domain" .= ("light" :: Text)
|
|
, "service" .= ("turn_on" :: Text)
|
|
, "target" .= object ["entity_id" .= ("light.bedroom_masse" :: Text)]
|
|
, "service_data" .= object ["brightness" .= (200 :: Int)]
|
|
]
|