packages feed

nostr-1.3.0.0: test/Test/Nostr/RelaySpec.hs

{-# LANGUAGE OverloadedStrings #-}

module Test.Nostr.RelaySpec (spec) where

import Test.Hspec
import Nostr.Relay
import Nostr.Event
import Data.Aeson (encode, decode)
import qualified Data.Text as T

spec :: Spec
spec = do
  describe "Nostr.Relay" $ do
    
    describe "ClientMessage JSON Serialization" $ do
      it "serializes CMClose to JSON" $ do
        let subId = SubscriptionId "sub-123"
        let msg = CMClose subId
        encode msg `shouldBe` "[\"CLOSE\",\"sub-123\"]"
        
      it "deserializes CMClose from JSON" $ do
        let jsonStr = "[\"CLOSE\",\"sub-123\"]"
        decode jsonStr `shouldBe` Just (CMClose (SubscriptionId "sub-123"))

      it "serializes CMReq to JSON" $ do
        let subId = SubscriptionId "sub-req"
        let f = defaultFilter { filterLimit = Just 10 }
        let msg = CMReq subId [f]
        encode msg `shouldBe` "[\"REQ\",\"sub-req\",{\"limit\":10}]"

    describe "RelayMessage JSON Serialization" $ do
      it "serializes RMNotice to JSON" $ do
        let msg = RMNotice "Hello"
        encode msg `shouldBe` "[\"NOTICE\",\"Hello\"]"
        
      it "deserializes RMNotice from JSON" $ do
        let jsonStr = "[\"NOTICE\",\"Hello\"]"
        decode jsonStr `shouldBe` Just (RMNotice "Hello")

      it "serializes RMEOSE to JSON" $ do
        let subId = SubscriptionId "sub-1"
        let msg = RMEOSE subId
        encode msg `shouldBe` "[\"EOSE\",\"sub-1\"]"

      it "deserializes RMEOSE from JSON" $ do
        let jsonStr = "[\"EOSE\",\"sub-1\"]"
        decode jsonStr `shouldBe` Just (RMEOSE (SubscriptionId "sub-1"))

    describe "Filter JSON Serialization" $ do
      it "serializes empty filter" $ do
        encode defaultFilter `shouldBe` "{}"
        
      it "serializes filter with limit" $ do
        let f = defaultFilter { filterLimit = Just 42 }
        encode f `shouldBe` "{\"limit\":42}"
        
      it "serializes filter with kinds" $ do
        let f = defaultFilter { filterKinds = Just [1, 3] }
        encode f `shouldBe` "{\"kinds\":[1,3]}"