2020-10-04 16:27:38 +03:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2020-09-17 04:05:14 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-10-04 16:27:38 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2020-09-17 04:05:14 +03:00
|
|
|
|
2020-09-17 03:49:49 +03:00
|
|
|
module Main where
|
|
|
|
|
2020-10-04 16:27:38 +03:00
|
|
|
import Control.Exception (SomeException (..), try)
|
2020-10-06 04:19:08 +03:00
|
|
|
import Control.Lens ((^?))
|
2020-10-04 16:27:38 +03:00
|
|
|
import Data.Aeson (KeyValue ((.=)))
|
|
|
|
import qualified Data.Aeson as Aeson
|
2020-10-06 04:19:08 +03:00
|
|
|
import Data.Maybe (isJust)
|
2020-10-04 16:27:38 +03:00
|
|
|
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
|
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-12 18:53:52 +03:00
|
|
|
import qualified System.Environment as Env
|
2020-10-12 18:58:34 +03:00
|
|
|
import qualified System.Exit as Exit
|
2020-09-17 04:05:14 +03:00
|
|
|
import Urbit.Airlock
|
2020-09-17 03:49:49 +03:00
|
|
|
|
2020-10-04 16:27:38 +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-04 16:27:38 +03:00
|
|
|
|
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-12 18:59:20 +03:00
|
|
|
uuid <- 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-12 18:59:20 +03:00
|
|
|
[ "uid" .= UUID.toText uuid,
|
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-20 23:56:15 +03:00
|
|
|
"letter" .= Aeson.object ["text" .= Text.pack "hello world from haskell!"]
|
2020-10-06 04:21:48 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
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-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-21 00:12:52 +03:00
|
|
|
code = "lidlut-tabwed-pillex-ridrup"
|
2020-10-05 23:17:59 +03:00
|
|
|
}
|
2020-10-04 16:27:38 +03:00
|
|
|
|
|
|
|
-- | Poor man's testing framework
|
2020-10-05 23:17:59 +03:00
|
|
|
testing :: Text -> IO Bool -> IO ()
|
2020-10-04 16:27:38 +03:00
|
|
|
testing description f =
|
|
|
|
(putStrLn $ replicate 80 '-') >> try f >>= \case
|
|
|
|
Left (err :: SomeException) -> do
|
|
|
|
Text.IO.putStrLn $ "FAIL: " <> description
|
2020-10-12 18:58:34 +03:00
|
|
|
Exit.die $ show err
|
2020-10-05 23:17:59 +03:00
|
|
|
Right False -> do
|
|
|
|
Text.IO.putStrLn $ "FAIL: " <> description
|
2020-10-12 18:58:34 +03:00
|
|
|
Exit.die "expected True, got False"
|
2020-10-05 23:17:59 +03:00
|
|
|
Right True ->
|
2020-10-04 16:27:38 +03:00
|
|
|
Text.IO.putStrLn $ "PASS: " <> description
|