From 109686cbb5fded08c8cf13a4b6f740c2b02d1ac8 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:01:20 +0400 Subject: [PATCH] tests --- src/Simplex/Chat.hs | 6 + src/Simplex/Chat/Controller.hs | 1 + src/Simplex/Chat/Terminal/Input.hs | 1 + tests/ChatTests/Forward.hs | 197 +++++++++++++++++++++++++++-- 4 files changed, 195 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 314ff3487a..c4646dcb2a 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -1585,6 +1585,11 @@ processChatCommand' vr = \case forwardedItemId <- withStore $ \db -> getDirectChatItemIdByText db userId contactId msgDir forwardedMsg toChatRef <- getChatRef user toChatName processChatCommand $ APIForwardChatItem (ChatRef CTDirect contactId) toChatRef forwardedItemId + ForwardLocalMessage toChatName forwardedMsg -> withUser $ \user -> do + folderId <- withStore (`getUserNoteFolderId` user) + forwardedItemId <- withStore $ \db -> getLocalChatItemIdByText' db user folderId forwardedMsg + toChatRef <- getChatRef user toChatName + processChatCommand $ APIForwardChatItem (ChatRef CTLocal folderId) toChatRef forwardedItemId DeleteMessage chatName deletedMsg -> withUser $ \user -> do chatRef <- getChatRef user chatName deletedItemId <- getSentChatItemIdByText user chatRef deletedMsg @@ -7058,6 +7063,7 @@ chatCommandP = "/live " *> (SendLiveMessage <$> chatNameP <*> (A.space *> msgTextP <|> pure "")), (">@" <|> "> @") *> forwardMsgP (AMsgDirection SMDRcv), (">>@" <|> ">> @") *> forwardMsgP (AMsgDirection SMDSnd), + (">* -> " <|> "> * -> ") *> (ForwardLocalMessage <$> chatNameP <* A.space <*> msgTextP), (">@" <|> "> @") *> sendMsgQuote (AMsgDirection SMDRcv), (">>@" <|> ">> @") *> sendMsgQuote (AMsgDirection SMDSnd), ("\\ " <|> "\\") *> (DeleteMessage <$> chatNameP <* A.space <*> textP), diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 46e6925a05..cc63265fe9 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -412,6 +412,7 @@ data ChatCommand | SendLiveMessage ChatName Text | SendMessageQuote {contactName :: ContactName, msgDir :: AMsgDirection, quotedMsg :: Text, message :: Text} | ForwardMessage {contactName :: ContactName, msgDir :: AMsgDirection, toChatName :: ChatName, forwardedMsg :: Text} + | ForwardLocalMessage {toChatName :: ChatName, forwardedMsg :: Text} | SendMessageBroadcast Text -- UserId (not used in UI) | DeleteMessage ChatName Text | DeleteMemberMessage GroupName ContactName Text diff --git a/src/Simplex/Chat/Terminal/Input.hs b/src/Simplex/Chat/Terminal/Input.hs index 54582d7c8c..5c36994190 100644 --- a/src/Simplex/Chat/Terminal/Input.hs +++ b/src/Simplex/Chat/Terminal/Input.hs @@ -87,6 +87,7 @@ runInputLoop ct@ChatTerminal {termState, liveMessageState} cc = forever $ do Right SendFile {} -> True Right SendMessageQuote {} -> True Right ForwardMessage {} -> True + Right ForwardLocalMessage {} -> True Right SendGroupMessageQuote {} -> True Right ForwardGroupMessage {} -> True Right SendMessageBroadcast {} -> True diff --git a/tests/ChatTests/Forward.hs b/tests/ChatTests/Forward.hs index d73ce4182c..bc8cc81e5f 100644 --- a/tests/ChatTests/Forward.hs +++ b/tests/ChatTests/Forward.hs @@ -10,10 +10,18 @@ import Test.Hspec hiding (it) chatForwardTests :: SpecWith FilePath chatForwardTests = do describe "forward messages" $ do - it "from contact to contact" testForwardContactContact + it "from contact to contact" testForwardContactToContact + it "from contact to group" testForwardContactToGroup + it "from contact to notes" testForwardContactToNotes + it "from group to contact" testForwardGroupToContact + it "from group to group" testForwardGroupToGroup + it "from group to notes" testForwardGroupToNotes + it "from notes to contact" testForwardNotesToContact + it "from notes to group" testForwardNotesToGroup + it "from notes to notes" testForwardNotesToNotes -testForwardContactContact :: HasCallStack => FilePath -> IO () -testForwardContactContact = +testForwardContactToContact :: HasCallStack => FilePath -> IO () +testForwardContactToContact = testChat3 aliceProfile bobProfile cathProfile $ \alice bob cath -> do connectUsers alice bob @@ -23,9 +31,8 @@ testForwardContactContact = alice #> "@bob hi" bob <# "alice> hi" msgId <- lastItemId alice - - alice #> "@bob hey" - bob <# "alice> hey" + bob #> "@alice hey" + alice <# "bob> hey" alice ##> ("/_forward @2 @3 " <> msgId) alice <# "@cath -> forwarded" @@ -33,8 +40,178 @@ testForwardContactContact = cath <# "alice> -> forwarded" cath <## " hi" - bob `send` "> @alice -> @cath hey" - bob <# "@cath -> forwarded" - bob <## " hey" - cath <# "bob> -> forwarded" + alice `send` "> @bob -> @cath hey" + alice <# "@cath -> forwarded" + alice <## " hey" + cath <# "alice> -> forwarded" cath <## " hey" + + alice ##> "/tail @cath 2" + alice <# "@cath -> forwarded" + alice <## " hi" + alice <# "@cath -> forwarded" + alice <## " hey" + + cath ##> "/tail @alice 2" + cath <# "alice> -> forwarded" + cath <## " hi" + cath <# "alice> -> forwarded" + cath <## " hey" + +testForwardContactToGroup :: HasCallStack => FilePath -> IO () +testForwardContactToGroup = + testChat3 aliceProfile bobProfile cathProfile $ + \alice bob cath -> do + connectUsers alice bob + createGroup2 "team" alice cath + + alice #> "@bob hi" + bob <# "alice> hi" + bob #> "@alice hey" + alice <# "bob> hey" + + alice `send` ">> @bob -> #team hi" + alice <# "#team -> forwarded" + alice <## " hi" + cath <# "#team alice> -> forwarded" + cath <## " hi" + + alice `send` "> @bob -> #team hey" + alice <# "#team -> forwarded" + alice <## " hey" + cath <# "#team alice> -> forwarded" + cath <## " hey" + +testForwardContactToNotes :: HasCallStack => FilePath -> IO () +testForwardContactToNotes = + testChat2 aliceProfile bobProfile $ + \alice bob -> do + createCCNoteFolder alice + connectUsers alice bob + + alice #> "@bob hi" + bob <# "alice> hi" + bob #> "@alice hey" + alice <# "bob> hey" + + alice `send` ">> @bob -> * hi" + alice <# "* -> forwarded" + alice <## " hi" + + alice `send` "> @bob -> * hey" + alice <# "* -> forwarded" + alice <## " hey" + +testForwardGroupToContact :: HasCallStack => FilePath -> IO () +testForwardGroupToContact = + testChat3 aliceProfile bobProfile cathProfile $ + \alice bob cath -> do + createGroup2 "team" alice bob + connectUsers alice cath + + alice #> "#team hi" + bob <# "#team alice> hi" + bob #> "#team hey" + alice <# "#team bob> hey" + + alice `send` "> #team -> @cath hi" + alice <# "@cath -> forwarded" + alice <## " hi" + cath <# "alice> -> forwarded" + cath <## " hi" + + alice `send` "> #team -> @cath hey" + alice <# "@cath -> forwarded" + alice <## " hey" + cath <# "alice> -> forwarded" + cath <## " hey" + +testForwardGroupToGroup :: HasCallStack => FilePath -> IO () +testForwardGroupToGroup = + testChat3 aliceProfile bobProfile cathProfile $ + \alice bob cath -> do + createGroup2 "team" alice bob + createGroup2 "club" alice cath + + alice #> "#team hi" + bob <# "#team alice> hi" + bob #> "#team hey" + alice <# "#team bob> hey" + + alice `send` "> #team -> #club hi" + alice <# "#club -> forwarded" + alice <## " hi" + cath <# "#club alice> -> forwarded" + cath <## " hi" + + alice `send` "> #team -> #club hey" + alice <# "#club -> forwarded" + alice <## " hey" + cath <# "#club alice> -> forwarded" + cath <## " hey" + +testForwardGroupToNotes :: HasCallStack => FilePath -> IO () +testForwardGroupToNotes = + testChat2 aliceProfile bobProfile $ + \alice bob -> do + createCCNoteFolder alice + createGroup2 "team" alice bob + + alice #> "#team hi" + bob <# "#team alice> hi" + bob #> "#team hey" + alice <# "#team bob> hey" + + alice `send` "> #team -> * hi" + alice <# "* -> forwarded" + alice <## " hi" + + alice `send` "> #team -> * hey" + alice <# "* -> forwarded" + alice <## " hey" + +testForwardNotesToContact :: HasCallStack => FilePath -> IO () +testForwardNotesToContact = + testChat2 aliceProfile cathProfile $ + \alice cath -> do + createCCNoteFolder alice + connectUsers alice cath + + alice /* "hi" + + alice `send` "> * -> @cath hi" + alice <# "@cath -> forwarded" + alice <## " hi" + cath <# "alice> -> forwarded" + cath <## " hi" + +testForwardNotesToGroup :: HasCallStack => FilePath -> IO () +testForwardNotesToGroup = + testChat2 aliceProfile cathProfile $ + \alice cath -> do + createCCNoteFolder alice + createGroup2 "team" alice cath + + alice /* "hi" + + alice `send` "> * -> #team hi" + alice <# "#team -> forwarded" + alice <## " hi" + cath <# "#team alice> -> forwarded" + cath <## " hi" + +testForwardNotesToNotes :: HasCallStack => FilePath -> IO () +testForwardNotesToNotes tmp = + withNewTestChat tmp "alice" aliceProfile $ \alice -> do + createCCNoteFolder alice + + alice /* "hi" + + alice `send` "> * -> * hi" + alice <# "* -> forwarded" + alice <## " hi" + + alice ##> "/tail * 2" + alice <# "* hi" + alice <# "* -> forwarded" + alice <## " hi"