2021-07-07 22:46:38 +01:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2021-08-05 20:51:48 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2021-07-07 22:46:38 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2021-08-05 20:51:48 +01:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2021-07-07 22:46:38 +01:00
|
|
|
|
|
|
|
module ChatClient where
|
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
import Control.Concurrent (ThreadId, forkIOWithUnmask, killThread)
|
2021-07-07 22:46:38 +01:00
|
|
|
import Control.Concurrent.Async
|
2021-08-05 20:51:48 +01:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Exception (bracket, bracket_)
|
2021-07-07 22:46:38 +01:00
|
|
|
import Control.Monad.Except
|
2021-08-05 20:51:48 +01:00
|
|
|
import Data.List (dropWhileEnd)
|
|
|
|
import Network.Socket
|
2021-07-07 22:46:38 +01:00
|
|
|
import Simplex.Chat
|
2021-09-04 07:32:56 +01:00
|
|
|
import Simplex.Chat.Controller (ChatConfig (..), ChatController (..))
|
2021-07-07 22:46:38 +01:00
|
|
|
import Simplex.Chat.Options
|
|
|
|
import Simplex.Chat.Store
|
|
|
|
import Simplex.Chat.Types (Profile)
|
2021-08-02 20:10:24 +01:00
|
|
|
import Simplex.Messaging.Agent.Env.SQLite
|
2021-08-14 21:04:51 +01:00
|
|
|
import Simplex.Messaging.Agent.RetryInterval
|
2021-08-05 20:51:48 +01:00
|
|
|
import Simplex.Messaging.Server (runSMPServerBlocking)
|
|
|
|
import Simplex.Messaging.Server.Env.STM
|
|
|
|
import Simplex.Messaging.Transport
|
2021-07-16 07:40:55 +01:00
|
|
|
import System.Directory (createDirectoryIfMissing, removeDirectoryRecursive)
|
2021-07-07 22:46:38 +01:00
|
|
|
import qualified System.Terminal as C
|
2021-08-05 20:51:48 +01:00
|
|
|
import System.Terminal.Internal (VirtualTerminal (..), VirtualTerminalSettings (..), withVirtualTerminal)
|
|
|
|
import System.Timeout (timeout)
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-07-24 10:26:28 +01:00
|
|
|
testDBPrefix :: FilePath
|
|
|
|
testDBPrefix = "tests/tmp/test"
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
serverPort :: ServiceName
|
2021-12-27 15:15:35 +04:00
|
|
|
serverPort = "5001"
|
2021-08-05 20:51:48 +01:00
|
|
|
|
2021-07-07 22:46:38 +01:00
|
|
|
opts :: ChatOpts
|
|
|
|
opts =
|
|
|
|
ChatOpts
|
|
|
|
{ dbFile = undefined,
|
2022-01-20 20:23:21 +00:00
|
|
|
smpServers = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001"],
|
|
|
|
logging = False
|
2021-07-07 22:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
termSettings :: VirtualTerminalSettings
|
|
|
|
termSettings =
|
|
|
|
VirtualTerminalSettings
|
|
|
|
{ virtualType = "xterm",
|
|
|
|
virtualWindowSize = pure C.Size {height = 24, width = 1000},
|
|
|
|
virtualEvent = retry,
|
|
|
|
virtualInterrupt = retry
|
|
|
|
}
|
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
data TestCC = TestCC
|
|
|
|
{ chatController :: ChatController,
|
|
|
|
virtualTerminal :: VirtualTerminal,
|
|
|
|
chatAsync :: Async (),
|
|
|
|
termAsync :: Async (),
|
|
|
|
termQ :: TQueue String
|
|
|
|
}
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-08-02 20:10:24 +01:00
|
|
|
aCfg :: AgentConfig
|
|
|
|
aCfg = agentConfig defaultChatConfig
|
|
|
|
|
|
|
|
cfg :: ChatConfig
|
|
|
|
cfg =
|
|
|
|
defaultChatConfig
|
|
|
|
{ agentConfig =
|
2022-01-11 08:50:44 +00:00
|
|
|
aCfg {reconnectInterval = (reconnectInterval aCfg) {initialInterval = 50000}}
|
2021-08-02 20:10:24 +01:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:46:38 +01:00
|
|
|
virtualSimplexChat :: FilePath -> Profile -> IO TestCC
|
|
|
|
virtualSimplexChat dbFile profile = do
|
2022-01-11 21:23:57 +00:00
|
|
|
st <- createStore (dbFile <> "_chat.db") 1
|
2021-07-07 22:46:38 +01:00
|
|
|
void . runExceptT $ createUser st profile True
|
|
|
|
t <- withVirtualTerminal termSettings pure
|
2021-08-02 20:10:24 +01:00
|
|
|
cc <- newChatController cfg opts {dbFile} t . const $ pure () -- no notifications
|
2021-08-05 20:51:48 +01:00
|
|
|
chatAsync <- async $ runSimplexChat cc
|
|
|
|
termQ <- newTQueueIO
|
|
|
|
termAsync <- async $ readTerminalOutput t termQ
|
|
|
|
pure TestCC {chatController = cc, virtualTerminal = t, chatAsync, termAsync, termQ}
|
|
|
|
|
|
|
|
readTerminalOutput :: VirtualTerminal -> TQueue String -> IO ()
|
|
|
|
readTerminalOutput t termQ = do
|
|
|
|
let w = virtualWindow t
|
|
|
|
winVar <- atomically $ newTVar . init =<< readTVar w
|
|
|
|
forever . atomically $ do
|
|
|
|
win <- readTVar winVar
|
|
|
|
win' <- init <$> readTVar w
|
|
|
|
if win' == win
|
|
|
|
then retry
|
|
|
|
else do
|
|
|
|
let diff = getDiff win' win
|
|
|
|
forM_ diff $ writeTQueue termQ
|
|
|
|
writeTVar winVar win'
|
|
|
|
where
|
|
|
|
getDiff :: [String] -> [String] -> [String]
|
|
|
|
getDiff win win' = getDiff_ 1 (length win) win win'
|
|
|
|
getDiff_ :: Int -> Int -> [String] -> [String] -> [String]
|
|
|
|
getDiff_ n len win' win =
|
|
|
|
let diff = drop (len - n) win'
|
|
|
|
in if drop n win <> diff == win'
|
|
|
|
then map (dropWhileEnd (== ' ')) diff
|
|
|
|
else getDiff_ (n + 1) len win' win
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-07-24 10:26:28 +01:00
|
|
|
testChatN :: [Profile] -> ([TestCC] -> IO ()) -> IO ()
|
|
|
|
testChatN ps test =
|
|
|
|
bracket_
|
|
|
|
(createDirectoryIfMissing False "tests/tmp")
|
|
|
|
(removeDirectoryRecursive "tests/tmp")
|
|
|
|
$ do
|
|
|
|
let envs = zip ps $ map ((testDBPrefix <>) . show) [(1 :: Int) ..]
|
|
|
|
tcs <- getTestCCs envs []
|
|
|
|
test tcs
|
|
|
|
where
|
|
|
|
getTestCCs [] tcs = pure tcs
|
|
|
|
getTestCCs ((p, db) : envs') tcs = (:) <$> virtualSimplexChat db p <*> getTestCCs envs' tcs
|
|
|
|
|
2021-07-07 22:46:38 +01:00
|
|
|
testChat2 :: Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
|
2021-07-24 10:26:28 +01:00
|
|
|
testChat2 p1 p2 test = testChatN [p1, p2] test_
|
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2] = test tc1 tc2
|
|
|
|
test_ _ = error "expected 2 chat clients"
|
|
|
|
|
|
|
|
testChat3 :: Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChat3 p1 p2 p3 test = testChatN [p1, p2, p3] test_
|
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2, tc3] = test tc1 tc2 tc3
|
|
|
|
test_ _ = error "expected 3 chat clients"
|
|
|
|
|
|
|
|
testChat4 :: Profile -> Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChat4 p1 p2 p3 p4 test = testChatN [p1, p2, p3, p4] test_
|
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2, tc3, tc4] = test tc1 tc2 tc3 tc4
|
|
|
|
test_ _ = error "expected 4 chat clients"
|
|
|
|
|
|
|
|
concurrentlyN_ :: [IO a] -> IO ()
|
|
|
|
concurrentlyN_ = mapConcurrently_ id
|
2021-08-05 20:51:48 +01:00
|
|
|
|
|
|
|
serverCfg :: ServerConfig
|
|
|
|
serverCfg =
|
|
|
|
ServerConfig
|
2022-01-11 08:50:44 +00:00
|
|
|
{ transports = [(serverPort, transport @TLS)],
|
2021-08-05 20:51:48 +01:00
|
|
|
tbqSize = 1,
|
2022-01-06 16:03:45 +00:00
|
|
|
serverTbqSize = 1,
|
2021-09-04 07:32:56 +01:00
|
|
|
msgQueueQuota = 4,
|
2021-08-05 20:51:48 +01:00
|
|
|
queueIdBytes = 12,
|
|
|
|
msgIdBytes = 6,
|
|
|
|
storeLog = Nothing,
|
2022-01-11 08:50:44 +00:00
|
|
|
caCertificateFile = "tests/fixtures/tls/ca.crt",
|
|
|
|
privateKeyFile = "tests/fixtures/tls/server.key",
|
|
|
|
certificateFile = "tests/fixtures/tls/server.crt"
|
2021-08-05 20:51:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
withSmpServer :: IO a -> IO a
|
|
|
|
withSmpServer = serverBracket (`runSMPServerBlocking` serverCfg) (pure ()) . const
|
|
|
|
|
|
|
|
serverBracket :: (TMVar Bool -> IO ()) -> IO () -> (ThreadId -> IO a) -> IO a
|
|
|
|
serverBracket process afterProcess f = do
|
|
|
|
started <- newEmptyTMVarIO
|
|
|
|
bracket
|
|
|
|
(forkIOWithUnmask ($ process started))
|
|
|
|
(\t -> killThread t >> afterProcess >> waitFor started "stop")
|
|
|
|
(\t -> waitFor started "start" >> f t)
|
|
|
|
where
|
|
|
|
waitFor started s =
|
|
|
|
5000000 `timeout` atomically (takeTMVar started) >>= \case
|
|
|
|
Nothing -> error $ "server did not " <> s
|
|
|
|
_ -> pure ()
|