-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.hs
83 lines (74 loc) · 2.58 KB
/
test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Exception (SomeException (..), try)
import Data.Aeson (KeyValue ((.=)))
import qualified Data.Aeson as Aeson
import qualified Data.Conduit.Binary
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import qualified System.Environment as Env
import qualified System.Exit as Exit
import Urbit.API
main :: IO ()
main = do
port <- Text.pack <$> Env.getEnv "PORT"
let ship = fakezod port
sess <- connect ship
testing "ship connection" $
connect ship >> return True
testing "poke ship" $
do
uuid <- UUID.nextRandom
_ <-
poke sess ship "zod" "chat-hook" "json" $
Aeson.object
[ "message"
.= Aeson.object
[ "path" .= Text.pack "/~/~zod/mc",
"envelope"
.= Aeson.object
[ "uid" .= UUID.toText uuid,
"number" .= (1 :: Int), -- FIXME: should this be lastEventId?
"author" .= Text.pack "~zod",
"when" .= (1602118786225 :: Int),
"letter" .= Aeson.object ["text" .= Text.pack "hello world from haskell!"]
]
]
]
return $ True
testing "ack" $
ack sess ship 1 >> return True
-- These tests are basically just checking that a connection happens and
-- doesn't throw, I need to pull in async in order to check for more
-- correctness. Ideally: subscribe, send a message, then read the message to
-- ensure its the same as the one sent. But maybe this is already tested in
-- urbit core?
testing "subscribe" $ do
_ <- subscribe sess ship "/mailbox/~/~zod/mc" Data.Conduit.Binary.sinkLbs
return True
fakezod :: Text -> Ship
fakezod port =
Ship
{ uid = "0123456789abcdef",
name = "zod",
lastEventId = 1,
url = "http://localhost:" <> port,
code = "lidlut-tabwed-pillex-ridrup"
}
-- | Poor man's testing framework
testing :: Text -> IO Bool -> IO ()
testing description f =
(putStrLn $ replicate 80 '-') >> try f >>= \case
Left (err :: SomeException) -> do
Text.IO.putStrLn $ "FAIL: " <> description
Exit.die $ show err
Right False -> do
Text.IO.putStrLn $ "FAIL: " <> description
Exit.die "expected True, got False"
Right True ->
Text.IO.putStrLn $ "PASS: " <> description