haskell-urbit-api/test.hs

88 lines
2.5 KiB
Haskell
Raw Normal View History

{-# LANGUAGE LambdaCase #-}
2020-09-17 04:05:14 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
2020-09-17 04:05:14 +03:00
2020-09-17 03:49:49 +03:00
module Main where
import Control.Exception (SomeException (..), try)
2020-10-06 04:19:08 +03:00
import Control.Lens ((^?))
import Data.Aeson (KeyValue ((.=)))
import qualified Data.Aeson as Aeson
2020-10-09 23:23:08 +03:00
import qualified Data.Char as Char
2020-10-06 04:19:08 +03:00
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
2020-10-10 16:06:11 +03:00
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import qualified Data.Word as Word
2020-10-06 04:19:08 +03:00
import qualified Network.Wreq as Wreq
2020-10-10 16:06:11 +03:00
import qualified Network.Wreq.Session as Session
2020-10-09 23:23:08 +03:00
import qualified Numeric
2020-10-12 18:53:52 +03:00
import qualified System.Environment as Env
2020-10-09 23:23:08 +03:00
import qualified System.Random as Random
2020-09-17 04:05:14 +03:00
import Urbit.Airlock
2020-09-17 03:49:49 +03:00
main :: IO ()
2020-09-17 03:49:49 +03:00
main = do
2020-10-12 18:53:52 +03:00
port <- Env.getEnv "PORT"
let ship = fakezod port
2020-10-10 16:06:11 +03:00
sess <- Session.newSession
2020-10-06 04:21:48 +03:00
testing "ship connection" $
isJust <$> do
2020-10-10 16:06:11 +03:00
r <- connect sess ship
2020-10-06 04:21:48 +03:00
return $ r ^? Wreq.responseBody
2020-10-05 23:17:59 +03:00
2020-10-06 04:21:48 +03:00
testing "poke ship" $
isJust <$> do
2020-10-10 16:06:11 +03:00
uid <- UUID.nextRandom
2020-10-06 04:21:48 +03:00
r <-
2020-10-10 16:06:11 +03:00
poke sess ship "zod" "chat-hook" "json" $
2020-10-06 04:21:48 +03:00
Aeson.object
[ "message"
.= Aeson.object
[ "path" .= Text.pack "/~/~zod/mc",
"envelope"
.= Aeson.object
2020-10-10 16:06:11 +03:00
[ "uid" .= UUID.toText uid,
2020-10-12 17:11:33 +03:00
"number" .= (1 :: Int), -- FIXME: should this be lastEventId?
2020-10-06 04:21:48 +03:00
"author" .= Text.pack "~zod",
2020-10-12 17:11:33 +03:00
"when" .= (1602118786225 :: Int),
2020-10-06 04:21:48 +03:00
"letter" .= Aeson.object ["text" .= Text.pack "hello world!"]
]
]
]
return $ r ^? Wreq.responseBody
testing "ack" $
isJust <$> do
2020-10-10 16:06:11 +03:00
r <- ack sess ship 1
2020-10-06 04:21:48 +03:00
return $ r ^? Wreq.responseBody
2020-10-05 23:17:59 +03:00
2020-10-10 16:06:11 +03:00
2020-10-12 18:53:52 +03:00
fakezod :: String -> Ship
fakezod port =
2020-10-05 23:17:59 +03:00
Ship
2020-10-10 16:06:11 +03:00
{ uid = "0123456789abcdef",
2020-10-05 23:52:58 +03:00
name = "zod",
2020-10-12 17:11:33 +03:00
lastEventId = 1,
2020-10-12 18:53:52 +03:00
url = "http://localhost:" ++ port,
2020-10-05 23:17:59 +03:00
code = "lidlut-tabwed-pillex-ridrup",
sseClient = False
}
2020-10-10 16:06:11 +03:00
-- | Poor man's testing framework
2020-10-05 23:17:59 +03:00
testing :: Text -> IO Bool -> IO ()
testing description f =
(putStrLn $ replicate 80 '-') >> try f >>= \case
Left (err :: SomeException) -> do
Text.IO.putStrLn $ "FAIL: " <> description
putStrLn $ show err
2020-10-05 23:17:59 +03:00
Right False -> do
Text.IO.putStrLn $ "FAIL: " <> description
putStrLn $ "expected True, got False"
Right True ->
Text.IO.putStrLn $ "PASS: " <> description