From 35963f9c74d8767e21b3500937149d9e7839bef5 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Fri, 24 Oct 2025 11:24:05 +0000 Subject: [PATCH] core: don't show no connections diff on event (fix tests) (#6403) * different approach * fix tests --- src/Simplex/Chat/Options/SQLite.hs | 4 ++-- src/Simplex/Chat/View.hs | 23 ++++++++++++++++------- tests/JSONFixtures.hs | 6 ++++++ tests/JSONTests.hs | 1 + tests/MobileTests.hs | 22 +++++++++++++++++----- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Simplex/Chat/Options/SQLite.hs b/src/Simplex/Chat/Options/SQLite.hs index d2e79eea79..8591f0801c 100644 --- a/src/Simplex/Chat/Options/SQLite.hs +++ b/src/Simplex/Chat/Options/SQLite.hs @@ -76,8 +76,8 @@ toDBOpts ChatDbOpts {dbFilePrefix, dbKey, trackQueries, vacuumOnMigration} dbSuf { dbFilePath = dbFilePrefix <> dbSuffix, dbKey, keepKey, - track = trackQueries, - vacuum = vacuumOnMigration + vacuum = vacuumOnMigration, + track = trackQueries } chatSuffix :: String diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index bbccd514b4..c16f76148f 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -1422,21 +1422,30 @@ viewUserPrivacy User {userId} User {userId = userId', localDisplayName = n', sho ] viewConnDiffSync :: DatabaseDiff AgentUserId -> DatabaseDiff AgentConnId -> [StyledString] -viewConnDiffSync userDiff connDiff = - viewConnDiffSummary userDiff connDiff - <> ["removed extra users in agent" | not (null $ extraIds userDiff)] - <> ["removed extra connections in agent" | not (null $ extraIds connDiff)] +viewConnDiffSync userDiff connDiff + | noDiff userDiff && noDiff connDiff = [] + | otherwise = + viewConnDiffSummary' userDiff connDiff + <> ["removed extra users in agent" | not (null $ extraIds userDiff)] + <> ["removed extra connections in agent" | not (null $ extraIds connDiff)] + where + noDiff DatabaseDiff {missingIds, extraIds} = null missingIds && null extraIds viewConnDiffSummary :: DatabaseDiff AgentUserId -> DatabaseDiff AgentConnId -> [StyledString] viewConnDiffSummary userDiff connDiff | noDiff userDiff && noDiff connDiff = ["no difference between agent and chat connections"] | otherwise = - ["connections difference summary:"] - <> showDatabaseDiff "users" userDiff - <> showDatabaseDiff "connections" connDiff + viewConnDiffSummary' userDiff connDiff where noDiff DatabaseDiff {missingIds, extraIds} = null missingIds && null extraIds + +viewConnDiffSummary' :: DatabaseDiff AgentUserId -> DatabaseDiff AgentConnId -> [StyledString] +viewConnDiffSummary' userDiff connDiff = + ["connections difference summary:"] + <> showDatabaseDiff "users" userDiff + <> showDatabaseDiff "connections" connDiff + where showDatabaseDiff name DatabaseDiff {missingIds, extraIds} = ["number of missing " <> name <> " in agent: " <> sShow (length missingIds) | not (null missingIds)] <> ["number of extra " <> name <> " in agent: " <> sShow (length extraIds) | not (null extraIds)] diff --git a/tests/JSONFixtures.hs b/tests/JSONFixtures.hs index f02d04491e..f3364dac81 100644 --- a/tests/JSONFixtures.hs +++ b/tests/JSONFixtures.hs @@ -28,6 +28,12 @@ chatStartedSwift = "{\"result\":{\"_owsf\":true,\"chatStarted\":{}}}" chatStartedTagged :: LB.ByteString chatStartedTagged = "{\"result\":{\"type\":\"chatStarted\"}}" +connectionsDiffSwift :: LB.ByteString +connectionsDiffSwift = "{\"result\":{\"_owsf\":true,\"connectionsDiff\":{\"userIds\":{\"missingIds\":[],\"extraIds\":[]},\"connIds\":{\"missingIds\":[],\"extraIds\":[]}}}}" + +connectionsDiffTagged :: LB.ByteString +connectionsDiffTagged = "{\"result\":{\"type\":\"connectionsDiff\",\"userIds\":{\"missingIds\":[],\"extraIds\":[]},\"connIds\":{\"missingIds\":[],\"extraIds\":[]}}}" + userJSON :: LB.ByteString userJSON = "{\"userId\":1,\"agentUserId\":\"1\",\"userContactId\":1,\"localDisplayName\":\"alice\",\"profile\":{\"profileId\":1,\"displayName\":\"alice\",\"fullName\":\"\",\"shortDescr\":\"Alice\",\"localAlias\":\"\"},\"fullPreferences\":{\"timedMessages\":{\"allow\":\"yes\"},\"fullDelete\":{\"allow\":\"no\"},\"reactions\":{\"allow\":\"yes\"},\"voice\":{\"allow\":\"yes\"},\"files\":{\"allow\":\"always\"},\"calls\":{\"allow\":\"yes\"},\"sessions\":{\"allow\":\"no\"},\"commands\":[]},\"activeUser\":true,\"activeOrder\":1,\"showNtfs\":true,\"sendRcptsContacts\":true,\"sendRcptsSmallGroups\":true,\"autoAcceptMemberContacts\":false}" diff --git a/tests/JSONTests.hs b/tests/JSONTests.hs index ed4d591547..52d722d26a 100644 --- a/tests/JSONTests.hs +++ b/tests/JSONTests.hs @@ -25,6 +25,7 @@ owsf2TaggedJSONTest = do activeUserExistsSwift `to` activeUserExistsTagged activeUserSwift `to` activeUserTagged chatStartedSwift `to` chatStartedTagged + connectionsDiffSwift `to` connectionsDiffTagged parsedMarkdownSwift `to` parsedMarkdownTagged where to :: LB.ByteString -> LB.ByteString -> IO () diff --git a/tests/MobileTests.hs b/tests/MobileTests.hs index d4f65b3b1c..7213a0e91c 100644 --- a/tests/MobileTests.hs +++ b/tests/MobileTests.hs @@ -1,4 +1,6 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TemplateHaskell #-} @@ -7,6 +9,7 @@ module MobileTests (mobileTests) where +import ChatClient import ChatTests.DBUtils import ChatTests.Utils import Control.Concurrent.STM @@ -28,7 +31,8 @@ import Foreign.StablePtr import Foreign.Storable (peek) import GHC.IO.Encoding (setLocaleEncoding, setFileSystemEncoding, setForeignEncoding) import JSONFixtures -import Simplex.Chat.Controller (ChatController (..)) +import Simplex.Chat +import Simplex.Chat.Controller (ChatController (..), ChatDatabase (..)) import Simplex.Chat.Mobile hiding (error) import Simplex.Chat.Mobile.File import Simplex.Chat.Mobile.Shared @@ -37,7 +41,6 @@ import Simplex.Chat.Options.DB import Simplex.Chat.Store import Simplex.Chat.Store.Profiles import Simplex.Chat.Types (AgentUserId (..), Profile (..)) -import Simplex.Messaging.Agent.Store.Interface import Simplex.Messaging.Agent.Store.Shared (MigrationConfig (..), MigrationConfirmation (..)) import qualified Simplex.Messaging.Agent.Store.SQLite.DB as DB import qualified Simplex.Messaging.Crypto as C @@ -111,6 +114,14 @@ chatStarted = chatStartedTagged #endif +connectionsDiff :: LB.ByteString +connectionsDiff = +#if defined(darwin_HOST_OS) && defined(swiftJSON) + connectionsDiffSwift +#else + connectionsDiffTagged +#endif + parsedMarkdown :: LB.ByteString parsedMarkdown = #if defined(darwin_HOST_OS) && defined(swiftJSON) @@ -134,15 +145,16 @@ testChatApi :: TestParams -> IO () testChatApi ps = do let tmp = tmpPath ps dbPrefix = tmp "1" - f = dbPrefix <> chatSuffix - Right st <- createChatStore (DBOpts f "myKey" False True DB.TQOff) (MigrationConfig MCYesUp Nothing) - Right _ <- withTransaction st $ \db -> runExceptT $ createUserRecord db (AgentUserId 1) aliceProfile {preferences = Nothing} True + Right ChatDatabase {chatStore, agentStore} <- createChatDatabase (ChatDbOpts dbPrefix "myKey" DB.TQOff True) (MigrationConfig MCYesUp Nothing) + insertUser agentStore + Right _ <- withTransaction chatStore $ \db -> runExceptT $ createUserRecord db (AgentUserId 1) aliceProfile {preferences = Nothing} True Right cc <- chatMigrateInit dbPrefix "myKey" "yesUp" Left (DBMErrorNotADatabase _) <- chatMigrateInit dbPrefix "" "yesUp" Left (DBMErrorNotADatabase _) <- chatMigrateInit dbPrefix "anotherKey" "yesUp" chatSendCmd cc "/u" `shouldReturn` activeUser chatSendCmd cc "/create user alice Alice" `shouldReturn` activeUserExists chatSendCmd cc "/_start" `shouldReturn` chatStarted + chatRecvMsg cc `shouldReturn` connectionsDiff chatRecvMsgWait cc 10000 `shouldReturn` "" chatParseMarkdown "hello" `shouldBe` "{}" chatParseMarkdown "*hello*" `shouldBe` parsedMarkdown