packages feed

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

{-# LANGUAGE OverloadedStrings #-}

module Test.Nostr.Nip11Spec (spec) where

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

spec :: Spec
spec = do
  describe "Nostr.Nip11" $ do
    
    describe "RelayInfo JSON Parsing" $ do
      it "parses complete RelayInfo from JSON" $ do
        let jsonStr = "{\"name\":\"Damus\",\"description\":\"A nostr relay\",\"pubkey\":\"hexpubkey\",\"contact\":\"admin@damus.io\",\"supported_nips\":[1,2,11,50],\"software\":\"nostr-rs-relay\",\"version\":\"0.8.9\"}"
        let expected = RelayInfo
              { riName = Just "Damus"
              , riDescription = Just "A nostr relay"
              , riPubkey = Just "hexpubkey"
              , riContact = Just "admin@damus.io"
              , riSupportedNips = Just [1, 2, 11, 50]
              , riSoftware = Just "nostr-rs-relay"
              , riVersion = Just "0.8.9"
              }
        decode jsonStr `shouldBe` Just expected
        
      it "parses empty JSON to all Nothing fields" $ do
        let jsonStr = "{}"
        let expected = RelayInfo Nothing Nothing Nothing Nothing Nothing Nothing Nothing
        decode jsonStr `shouldBe` Just expected

      it "serializes RelayInfo to JSON" $ do
        let info = RelayInfo
              { riName = Just "Damus"
              , riDescription = Nothing
              , riPubkey = Nothing
              , riContact = Nothing
              , riSupportedNips = Just [1]
              , riSoftware = Nothing
              , riVersion = Nothing
              }
        -- The order of keys is not guaranteed in Aeson by default, 
        -- so we just check decode . encode == Just info
        decode (encode info) `shouldBe` Just info