haskell-urbit-api/test.hs

80 lines
2.3 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-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-06 04:19:08 +03:00
import qualified Network.Wreq as Wreq
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-05 23:17:59 +03:00
let ship = fakezod
2020-10-06 04:19:08 +03:00
testing "ship connection" $ isJust <$> do
r <- connect ship
return $ r ^? Wreq.responseBody
2020-10-05 23:17:59 +03:00
2020-10-06 04:19:08 +03:00
testing "poke ship" $ isJust <$> do
r <- (poke ship "zod" "chat-hook" "json" $
Aeson.object
[ "message"
.= Aeson.object
[ "path" .= Text.pack "/~/~zod/mc",
"envelope"
.= Aeson.object
[ "uid" .= Text.pack "FIXME",
"number" .= lastEventId ship,
"author" .= Text.pack "~zod",
"when" .= Text.pack "FIXME", -- int(time.time() * 1000)
"letter" .= Aeson.object ["text" .= Text.pack "hello world!"]
]
]
])
return $ r ^? Wreq.responseBody
2020-10-05 23:17:59 +03:00
fakezod :: Ship
fakezod =
Ship
{ session = Nothing,
2020-10-05 23:52:58 +03:00
name = "zod",
2020-10-05 23:17:59 +03:00
lastEventId = 0,
url = "http://localhost:8081",
code = "lidlut-tabwed-pillex-ridrup",
sseClient = False
}
-- | 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
{-
s = baseconvert.base(random.getrandbits(128), 10, 32, string=True).lower()
uid = '0v' + '.'.join(s[i:i+5] for i in range(0, len(s), 5))[::-1]
"path": "/~/~zod/mc",
"envelope": {"uid": uid,
"number": 1,
"author": "~zod",
"when": ,
"letter": {"text": "hello world!"}}
-}