4
4
-- | The matrix client specification tests
5
5
module Main (main ) where
6
6
7
- import Control.Monad (void )
8
7
import qualified Data.Aeson.Encode.Pretty as Aeson
9
8
import qualified Data.ByteString.Lazy as BS
10
9
import Data.Either (isLeft )
@@ -14,6 +13,7 @@ import Network.Matrix.Client
14
13
import Network.Matrix.Internal
15
14
import System.Environment (lookupEnv )
16
15
import Test.Hspec
16
+ import Control.Monad.Except
17
17
18
18
main :: IO ()
19
19
main = do
@@ -32,9 +32,8 @@ integration :: ClientSession -> ClientSession -> Spec
32
32
integration sess1 sess2 = do
33
33
describe " integration tests" $ do
34
34
it " create room" $ do
35
- resp <-
35
+ resp <- runMatrixM sess1 $ do
36
36
createRoom
37
- sess1
38
37
( RoomCreateRequest
39
38
{ rcrPreset = PublicChat ,
40
39
rcrRoomAliasName = " test" ,
@@ -44,25 +43,51 @@ integration sess1 sess2 = do
44
43
)
45
44
case resp of
46
45
Left err -> meError err `shouldBe` " Alias already exists"
47
- Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty )
48
- it " join room" $ do
49
- resp <- joinRoom sess1 " #test:localhost"
46
+ Right (RoomID roomID' ) -> roomID' `shouldSatisfy` (/= mempty )
47
+ it " join room" $ do
48
+ resp <- runMatrixM sess1 $ joinRoom " #test:localhost"
50
49
case resp of
51
50
Left err -> error (show err)
52
- Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty )
53
- resp' <- joinRoom sess2 " #test:localhost"
51
+ Right (RoomID roomID' ) -> roomID' `shouldSatisfy` (/= mempty )
52
+ resp' <- runMatrixM sess2 $ joinRoom " #test:localhost"
54
53
case resp' of
55
54
Left err -> error (show err)
56
- Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty )
55
+ Right (RoomID roomID' ) -> roomID' `shouldSatisfy` (/= mempty )
57
56
it " send message and reply" $ do
58
- -- Flush previous events
59
- Right sr <- sync sess2 Nothing Nothing Nothing Nothing
60
- Right [room] <- getJoinedRooms sess1
61
- let msg body = RoomMessageText $ MessageText body TextType Nothing Nothing
62
- let since = srNextBatch sr
63
- Right eventID <- sendMessage sess1 room (EventRoomMessage $ msg " Hello" ) (TxnID since)
64
- Right reply <- sendMessage sess2 room (EventRoomReply eventID $ msg " Hi!" ) (TxnID since)
65
- reply `shouldNotBe` eventID
57
+ result <- runMatrixM sess2 $ do
58
+ -- Flush previous events
59
+ sr <- sync Nothing Nothing Nothing Nothing
60
+ [room] <- getJoinedRooms
61
+ let msg body = RoomMessageText $ MessageText body TextType Nothing Nothing
62
+ let since = srNextBatch sr
63
+ eventID <- sendMessage room (EventRoomMessage $ msg " Hello" ) (TxnID since)
64
+ reply <- sendMessage room (EventRoomReply eventID $ msg " Hi!" ) (TxnID since)
65
+ pure (reply, eventID)
66
+ case result of
67
+ Left err -> error (show err)
68
+ Right (reply, eventID) -> reply `shouldNotBe` eventID
69
+ it " does not retry on success" $
70
+ checkPause (<=) $ do
71
+ res <- runMatrixM sess1 $ retry (pure True )
72
+ res `shouldBe` pure True
73
+ it " does not retry on regular failure" $
74
+ checkPause (<=) $ do
75
+ let resp = MatrixError " test" " error" Nothing
76
+ res <- runMatrixM sess1 $ retry (throwError resp :: MatrixIO Int )
77
+ res `shouldBe` Left resp
78
+ it " retry on rate limit failure" $
79
+ checkPause (>=) $ do
80
+ let resp = MatrixError " M_LIMIT_EXCEEDED" " error" (Just 1000 )
81
+ (runMatrixM sess1 $ retryWithLog 1 (const $ pure () ) (throwError resp))
82
+ `shouldThrow` rateLimitSelector
83
+ where
84
+ rateLimitSelector :: MatrixException -> Bool
85
+ rateLimitSelector MatrixRateLimit = True
86
+ checkPause op action = do
87
+ MkSystemTime start' _ <- getSystemTime
88
+ void action
89
+ MkSystemTime end' _ <- getSystemTime
90
+ (end' - start') `shouldSatisfy` (`op` 1 )
66
91
67
92
spec :: Spec
68
93
spec = describe " unit tests" $ do
@@ -93,29 +118,7 @@ spec = describe "unit tests" $ do
93
118
it " encode room message" $
94
119
encodePretty (RoomMessageText (MessageText " Hello" TextType Nothing Nothing ))
95
120
`shouldBe` " {\" body\" :\" Hello\" ,\" msgtype\" :\" m.text\" }"
96
- it " does not retry on success" $
97
- checkPause (<=) $ do
98
- let resp = Right True
99
- res <- retry (pure resp)
100
- res `shouldBe` resp
101
- it " does not retry on regular failre" $
102
- checkPause (<=) $ do
103
- let resp = Left $ MatrixError " test" " error" Nothing
104
- res <- (retry (pure resp) :: MatrixIO Int )
105
- res `shouldBe` resp
106
- it " retry on rate limit failure" $
107
- checkPause (>=) $ do
108
- let resp = Left $ MatrixError " M_LIMIT_EXCEEDED" " error" (Just 1000 )
109
- (retryWithLog 1 (const $ pure () ) (pure resp) :: MatrixIO Int )
110
- `shouldThrow` rateLimitSelector
111
121
where
112
- rateLimitSelector :: MatrixException -> Bool
113
- rateLimitSelector MatrixRateLimit = True
114
- checkPause op action = do
115
- MkSystemTime start _ <- getSystemTime
116
- void action
117
- MkSystemTime end _ <- getSystemTime
118
- (end - start) `shouldSatisfy` (`op` 1 )
119
122
encodePretty =
120
123
Aeson. encodePretty'
121
124
( Aeson. defConfig {Aeson. confIndent = Aeson. Spaces 0 , Aeson. confCompare = compare @ Text }
0 commit comments