@@ -71,18 +71,20 @@ import Data.List.NonEmpty (NonEmpty)
7171import qualified Data.Map as M
7272import Data.Maybe (isJust , isNothing )
7373import qualified Data.Set as S
74+ import qualified Data.Text as T
7475import Data.Text.Encoding (decodeLatin1 )
76+ import qualified Data.Text.IO as T
7577import Data.Time.Clock (diffUTCTime , getCurrentTime )
7678import Data.Time.Clock.System (SystemTime (.. ), getSystemTime )
7779import Data.Type.Equality (testEquality , (:~:) (Refl ))
7880import Data.Word (Word16 )
7981import GHC.Stack (withFrozenCallStack )
8082import SMPAgentClient
81- import SMPClient ( cfgJ2QS , cfgMS , prevRange , prevVersion , proxyCfgJ2QS , proxyCfgMS , testPort , testPort2 , testStoreLogFile , withSmpServer , withSmpServers2 , withSmpServerConfigOn , withSmpServerProxy , withSmpServersProxy2 , withSmpServerStoreLogOn , withSmpServerStoreMsgLogOn )
83+ import SMPClient
8284import Simplex.Messaging.Agent hiding (createConnection , joinConnection , sendMessage )
8385import qualified Simplex.Messaging.Agent as A
8486import Simplex.Messaging.Agent.Client (ProtocolTestFailure (.. ), ProtocolTestStep (.. ), ServerQueueInfo (.. ), UserNetworkInfo (.. ), UserNetworkType (.. ), waitForUserNetwork )
85- import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (.. ), InitialAgentServers (.. ), createAgentStore )
87+ import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (.. ), Env ( .. ), InitialAgentServers (.. ), createAgentStore )
8688import Simplex.Messaging.Agent.Protocol hiding (CON , CONF , INFO , REQ , SENT )
8789import qualified Simplex.Messaging.Agent.Protocol as A
8890import Simplex.Messaging.Agent.Store.Common (DBStore (.. ), withTransaction )
@@ -115,6 +117,15 @@ import XFTPClient (testXFTPServer)
115117#if defined(dbPostgres)
116118import Fixtures
117119#endif
120+ #if defined(dbServerPostgres)
121+ import qualified Database.PostgreSQL.Simple as PSQL
122+ import Simplex.Messaging.Agent.Store (Connection (.. ), StoredRcvQueue (.. ), SomeConn (.. ))
123+ import Simplex.Messaging.Agent.Store.AgentStore (getConn )
124+ import Simplex.Messaging.Server.MsgStore.Journal (JournalQueue )
125+ import Simplex.Messaging.Server.MsgStore.Types (QSType (.. ))
126+ import Simplex.Messaging.Server.QueueStore.Postgres
127+ import Simplex.Messaging.Server.QueueStore.Types (QueueStoreClass (.. ))
128+ #endif
118129
119130type AEntityTransmission e = (ACorrId , ConnId , AEvent e )
120131
@@ -320,6 +331,7 @@ functionalAPITests ps = do
320331 it " should get 1-time link data after restart" $ testInviationShortLinkRestart ps
321332 it " should connect via contact short link after restart" $ testContactShortLinkRestart ps
322333 it " should connect via added contact short link after restart" $ testAddContactShortLinkRestart ps
334+ it " should create and get short links with the old contact queues" $ testOldContactQueueShortLink ps
323335 describe " Message delivery" $ do
324336 describe " update connection agent version on received messages" $ do
325337 it " should increase if compatible, shouldn'ps decrease" $
@@ -1307,6 +1319,49 @@ testAddContactShortLinkRestart ps = withAgentClients2 $ \a b -> do
13071319 connReq4 `shouldBe` connReq
13081320 linkUserData updatedConnData' `shouldBe` updatedData
13091321
1322+ testOldContactQueueShortLink :: HasCallStack => (ASrvTransport , AStoreType ) -> IO ()
1323+ testOldContactQueueShortLink ps@ (_, msType) = withAgentClients2 $ \ a b -> do
1324+ (contactId, CCLink connReq Nothing ) <- withSmpServer ps $ runRight $
1325+ A. createConnection a 1 True SCMContact Nothing Nothing CR. IKPQOn SMOnlyCreate
1326+ -- make it an "old" queue
1327+ let updateStoreLog f = replaceSubstringInFile f " queue_mode=C" " "
1328+ () <- case testServerStoreConfig msType of
1329+ ASSCfg _ _ (SSCMemory (Just StorePaths {storeLogFile})) -> updateStoreLog storeLogFile
1330+ ASSCfg _ _ (SSCMemoryJournal {storeLogFile}) -> updateStoreLog storeLogFile
1331+ ASSCfg _ _ (SSCDatabaseJournal {storeCfg}) -> do
1332+ #if defined(dbServerPostgres)
1333+ let AgentClient {agentEnv = Env {store}} = a
1334+ Right (SomeConn _ (ContactConnection _ RcvQueue {rcvId})) <- withTransaction store (`getConn` contactId)
1335+ st :: PostgresQueueStore (JournalQueue 'QSPostgres) <- newQueueStore @ (JournalQueue 'QSPostgres) storeCfg
1336+ Right 1 <- runExceptT $ withDB' " test" st $ \ db -> PSQL. execute db " UPDATE msg_queues SET queue_mode = ? WHERE recipient_id = ?" (Nothing :: Maybe QueueMode , rcvId )
1337+ closeQueueStore @ (JournalQueue 'QSPostgres) st
1338+ #else
1339+ error " no dbServerPostgres flag"
1340+ #endif
1341+ _ -> pure ()
1342+
1343+ withSmpServer ps $ do
1344+ let userData = " some user data"
1345+ shortLink <- runRight $ setContactShortLink a contactId userData Nothing
1346+ (connReq', connData') <- runRight $ getConnShortLink b 1 shortLink
1347+ strDecode (strEncode shortLink) `shouldBe` Right shortLink
1348+ connReq' `shouldBe` connReq
1349+ linkUserData connData' `shouldBe` userData
1350+ -- update user data
1351+ let updatedData = " updated user data"
1352+ shortLink' <- runRight $ setContactShortLink a contactId updatedData Nothing
1353+ shortLink' `shouldBe` shortLink
1354+ -- check updated
1355+ (connReq'', updatedConnData') <- runRight $ getConnShortLink b 1 shortLink
1356+ connReq'' `shouldBe` connReq
1357+ linkUserData updatedConnData' `shouldBe` updatedData
1358+
1359+ replaceSubstringInFile :: FilePath -> T. Text -> T. Text -> IO ()
1360+ replaceSubstringInFile filePath oldText newText = do
1361+ content <- T. readFile filePath
1362+ let newContent = T. replace oldText newText content
1363+ T. writeFile filePath newContent
1364+
13101365testIncreaseConnAgentVersion :: HasCallStack => (ASrvTransport , AStoreType ) -> IO ()
13111366testIncreaseConnAgentVersion ps = do
13121367 alice <- getSMPAgentClient' 1 agentCfg {smpAgentVRange = mkVersionRange 1 2 } initAgentServers testDB
0 commit comments