2022-02-16 23:24:48 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2022-02-06 16:18:01 +00:00
|
|
|
|
|
|
|
module MobileTests where
|
|
|
|
|
|
|
|
import ChatTests
|
|
|
|
import Control.Monad.Except
|
|
|
|
import Simplex.Chat.Mobile
|
|
|
|
import Simplex.Chat.Store
|
2023-01-13 13:54:07 +04:00
|
|
|
import Simplex.Chat.Types (AgentUserId (..), Profile (..))
|
2023-01-31 11:07:48 +00:00
|
|
|
import System.FilePath ((</>))
|
2022-02-06 16:18:01 +00:00
|
|
|
import Test.Hspec
|
|
|
|
|
2023-01-31 11:07:48 +00:00
|
|
|
mobileTests :: SpecWith FilePath
|
2022-02-06 16:18:01 +00:00
|
|
|
mobileTests = do
|
|
|
|
describe "mobile API" $ do
|
|
|
|
it "start new chat without user" testChatApiNoUser
|
|
|
|
it "start new chat with existing user" testChatApi
|
|
|
|
|
|
|
|
noActiveUser :: String
|
2022-04-12 12:24:34 +01:00
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2022-02-06 16:18:01 +00:00
|
|
|
noActiveUser = "{\"resp\":{\"chatCmdError\":{\"chatError\":{\"error\":{\"errorType\":{\"noActiveUser\":{}}}}}}}"
|
2022-02-16 23:24:48 +00:00
|
|
|
#else
|
|
|
|
noActiveUser = "{\"resp\":{\"type\":\"chatCmdError\",\"chatError\":{\"type\":\"error\",\"errorType\":{\"type\":\"noActiveUser\"}}}}"
|
|
|
|
#endif
|
2022-02-06 16:18:01 +00:00
|
|
|
|
|
|
|
activeUserExists :: String
|
2022-04-12 12:24:34 +01:00
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-31 12:24:18 +00:00
|
|
|
activeUserExists = "{\"resp\":{\"chatCmdError\":{\"user_\":{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true},\"chatError\":{\"error\":{\"errorType\":{\"userExists\":{\"contactName\":\"alice\"}}}}}}}"
|
2022-02-16 23:24:48 +00:00
|
|
|
#else
|
2023-01-31 12:24:18 +00:00
|
|
|
activeUserExists = "{\"resp\":{\"type\":\"chatCmdError\",\"user_\":{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true},\"chatError\":{\"type\":\"error\",\"errorType\":{\"type\":\"userExists\",\"contactName\":\"alice\"}}}}"
|
2022-02-16 23:24:48 +00:00
|
|
|
#endif
|
2022-02-06 16:18:01 +00:00
|
|
|
|
|
|
|
activeUser :: String
|
2022-04-12 12:24:34 +01:00
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-16 16:10:43 +04:00
|
|
|
activeUser = "{\"resp\":{\"activeUser\":{\"user\":{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}}"
|
2022-02-16 23:24:48 +00:00
|
|
|
#else
|
2023-01-13 13:54:07 +04:00
|
|
|
activeUser = "{\"resp\":{\"type\":\"activeUser\",\"user\":{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}}}"
|
2022-02-16 23:24:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
chatStarted :: String
|
2022-04-12 12:24:34 +01:00
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2022-02-16 23:24:48 +00:00
|
|
|
chatStarted = "{\"resp\":{\"chatStarted\":{}}}"
|
|
|
|
#else
|
|
|
|
chatStarted = "{\"resp\":{\"type\":\"chatStarted\"}}"
|
|
|
|
#endif
|
2022-02-06 16:18:01 +00:00
|
|
|
|
2022-06-11 11:52:55 +01:00
|
|
|
contactSubSummary :: String
|
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-14 15:45:13 +04:00
|
|
|
contactSubSummary = "{\"resp\":{\"contactSubSummary\":{" <> userJSON <> ",\"contactSubscriptions\":[]}}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#else
|
2023-01-14 15:45:13 +04:00
|
|
|
contactSubSummary = "{\"resp\":{\"type\":\"contactSubSummary\"," <> userJSON <> ",\"contactSubscriptions\":[]}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#endif
|
|
|
|
|
2022-07-17 15:51:17 +01:00
|
|
|
memberSubSummary :: String
|
2022-06-11 11:52:55 +01:00
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-14 15:45:13 +04:00
|
|
|
memberSubSummary = "{\"resp\":{\"memberSubSummary\":{" <> userJSON <> ",\"memberSubscriptions\":[]}}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#else
|
2023-01-14 15:45:13 +04:00
|
|
|
memberSubSummary = "{\"resp\":{\"type\":\"memberSubSummary\"," <> userJSON <> ",\"memberSubscriptions\":[]}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#endif
|
|
|
|
|
2022-10-13 17:12:22 +04:00
|
|
|
userContactSubSummary :: String
|
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-14 15:45:13 +04:00
|
|
|
userContactSubSummary = "{\"resp\":{\"userContactSubSummary\":{" <> userJSON <> ",\"userContactSubscriptions\":[]}}}"
|
2022-10-13 17:12:22 +04:00
|
|
|
#else
|
2023-01-14 15:45:13 +04:00
|
|
|
userContactSubSummary = "{\"resp\":{\"type\":\"userContactSubSummary\"," <> userJSON <> ",\"userContactSubscriptions\":[]}}"
|
2022-10-13 17:12:22 +04:00
|
|
|
#endif
|
|
|
|
|
2022-06-11 11:52:55 +01:00
|
|
|
pendingSubSummary :: String
|
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
2023-01-14 15:45:13 +04:00
|
|
|
pendingSubSummary = "{\"resp\":{\"pendingSubSummary\":{" <> userJSON <> ",\"pendingSubscriptions\":[]}}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#else
|
2023-01-14 15:45:13 +04:00
|
|
|
pendingSubSummary = "{\"resp\":{\"type\":\"pendingSubSummary\"," <> userJSON <> ",\"pendingSubscriptions\":[]}}"
|
2022-06-11 11:52:55 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-14 15:45:13 +04:00
|
|
|
userJSON :: String
|
2023-01-31 12:24:18 +00:00
|
|
|
userJSON = "\"user\":{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"no\"},\"fullDelete\":{\"allow\":\"no\"},\"voice\":{\"allow\":\"yes\"}},\"activeUser\":true}"
|
2023-01-14 15:45:13 +04:00
|
|
|
|
2022-06-11 11:52:55 +01:00
|
|
|
parsedMarkdown :: String
|
|
|
|
#if defined(darwin_HOST_OS) && defined(swiftJSON)
|
|
|
|
parsedMarkdown = "{\"formattedText\":[{\"format\":{\"bold\":{}},\"text\":\"hello\"}]}"
|
|
|
|
#else
|
|
|
|
parsedMarkdown = "{\"formattedText\":[{\"format\":{\"type\":\"bold\"},\"text\":\"hello\"}]}"
|
|
|
|
#endif
|
|
|
|
|
2023-01-31 11:07:48 +00:00
|
|
|
testChatApiNoUser :: FilePath -> IO ()
|
|
|
|
testChatApiNoUser tmp = do
|
|
|
|
let dbPrefix = tmp </> "1"
|
|
|
|
Right cc <- chatMigrateInit dbPrefix ""
|
|
|
|
Left (DBMErrorNotADatabase _) <- chatMigrateInit dbPrefix "myKey"
|
2022-02-06 16:18:01 +00:00
|
|
|
chatSendCmd cc "/u" `shouldReturn` noActiveUser
|
|
|
|
chatSendCmd cc "/_start" `shouldReturn` noActiveUser
|
2023-01-04 21:06:28 +04:00
|
|
|
chatSendCmd cc "/create user alice Alice" `shouldReturn` activeUser
|
2022-02-16 23:24:48 +00:00
|
|
|
chatSendCmd cc "/_start" `shouldReturn` chatStarted
|
2022-02-06 16:18:01 +00:00
|
|
|
|
2023-01-31 11:07:48 +00:00
|
|
|
testChatApi :: FilePath -> IO ()
|
|
|
|
testChatApi tmp = do
|
|
|
|
let dbPrefix = tmp </> "1"
|
2022-09-02 16:38:41 +01:00
|
|
|
f = chatStoreFile dbPrefix
|
|
|
|
st <- createChatStore f "myKey" True
|
2023-01-13 13:54:07 +04:00
|
|
|
Right _ <- withTransaction st $ \db -> runExceptT $ createUserRecord db (AgentUserId 1) aliceProfile {preferences = Nothing} True
|
2022-09-23 19:22:56 +01:00
|
|
|
Right cc <- chatMigrateInit dbPrefix "myKey"
|
|
|
|
Left (DBMErrorNotADatabase _) <- chatMigrateInit dbPrefix ""
|
|
|
|
Left (DBMErrorNotADatabase _) <- chatMigrateInit dbPrefix "anotherKey"
|
2022-02-06 16:18:01 +00:00
|
|
|
chatSendCmd cc "/u" `shouldReturn` activeUser
|
2023-01-04 21:06:28 +04:00
|
|
|
chatSendCmd cc "/create user alice Alice" `shouldReturn` activeUserExists
|
2022-02-16 23:24:48 +00:00
|
|
|
chatSendCmd cc "/_start" `shouldReturn` chatStarted
|
2022-06-11 11:52:55 +01:00
|
|
|
chatRecvMsg cc `shouldReturn` contactSubSummary
|
2022-10-13 17:12:22 +04:00
|
|
|
chatRecvMsg cc `shouldReturn` userContactSubSummary
|
2022-07-17 15:51:17 +01:00
|
|
|
chatRecvMsg cc `shouldReturn` memberSubSummary
|
2022-06-11 11:52:55 +01:00
|
|
|
chatRecvMsgWait cc 10000 `shouldReturn` pendingSubSummary
|
|
|
|
chatRecvMsgWait cc 10000 `shouldReturn` ""
|
|
|
|
chatParseMarkdown "hello" `shouldBe` "{}"
|
|
|
|
chatParseMarkdown "*hello*" `shouldBe` parsedMarkdown
|