+import Language.LSP.Test
+import Language.LSP.Types
+import Language.LSP.Types.Lens hiding
+ (capabilities, message, rename, applyEdit)
+import qualified Language.LSP.Types.Lens as LSP
+import Language.LSP.Types.Capabilities as LSP
+import System.Directory
+import System.FilePath
+import System.Timeout
+import Data.Type.Equality
+
+{-# ANN module ("HLint: ignore Reduce duplication" :: String) #-}
+{-# ANN module ("HLint: ignore Unnecessary hiding" :: String) #-}
+
+
+main = findServer >>= \serverExe -> hspec $ do
+ describe "Session" $ do
+ it "fails a test" $ do
+ let session = runSession serverExe fullCaps "test/data/renamePass" $ do
+ openDoc "Desktop/simple.hs" "haskell"
+ anyRequest
+ in session `shouldThrow` anySessionException
+ it "initializeResponse" $ runSession serverExe fullCaps "test/data/renamePass" $ do
+ rsp <- initializeResponse
+ liftIO $ rsp ^. result `shouldSatisfy` isRight
+
+ it "runSessionWithConfig" $
+ runSession serverExe didChangeCaps "test/data/renamePass" $ return ()
+
+ describe "withTimeout" $ do
+ it "times out" $
+ let sesh = runSession serverExe fullCaps "test/data/renamePass" $ do
+ openDoc "Desktop/simple.hs" "haskell"
+ -- won't receive a request - will timeout
+ -- incoming logging requests shouldn't increase the
+ -- timeout
+ withTimeout 5 $ skipManyTill anyMessage (message SWorkspaceApplyEdit)
+ -- wait just a bit longer than 5 seconds so we have time
+ -- to open the document
+ in timeout 6000000 sesh `shouldThrow` anySessionException
+
+ it "doesn't time out" $
+ let sesh = runSession serverExe fullCaps "test/data/renamePass" $ do
+ openDoc "Desktop/simple.hs" "haskell"
+ withTimeout 5 $ skipManyTill anyMessage publishDiagnosticsNotification
+ in void $ timeout 6000000 sesh
+
+ it "further timeout messages are ignored" $ runSession serverExe fullCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ -- shouldn't timeout
+ withTimeout 3 $ getDocumentSymbols doc
+ -- longer than the original timeout
+ liftIO $ threadDelay (5 * 10^6)
+ -- shouldn't throw an exception
+ getDocumentSymbols doc
+ return ()
+
+ it "overrides global message timeout" $
+ let sesh =
+ runSessionWithConfig (def { messageTimeout = 5 }) serverExe fullCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ -- shouldn't time out in here since we are overriding it
+ withTimeout 10 $ liftIO $ threadDelay 7000000
+ getDocumentSymbols doc
+ return True
+ in sesh `shouldReturn` True
+
+ it "unoverrides global message timeout" $
+ let sesh =
+ runSessionWithConfig (def { messageTimeout = 5 }) serverExe fullCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ -- shouldn't time out in here since we are overriding it
+ withTimeout 10 $ liftIO $ threadDelay 7000000
+ getDocumentSymbols doc
+ -- should now timeout
+ skipManyTill anyMessage (message SWorkspaceApplyEdit)
+ isTimeout (Timeout _) = True
+ isTimeout _ = False
+ in sesh `shouldThrow` isTimeout
+
+
+ describe "SessionException" $ do
+ it "throw on time out" $
+ let sesh = runSessionWithConfig (def {messageTimeout = 10}) serverExe fullCaps "test/data/renamePass" $ do
+ skipMany loggingNotification
+ _ <- message SWorkspaceApplyEdit
+ return ()
+ in sesh `shouldThrow` anySessionException
+
+ it "don't throw when no time out" $ runSessionWithConfig (def {messageTimeout = 5}) serverExe fullCaps "test/data/renamePass" $ do
+ loggingNotification
+ liftIO $ threadDelay $ 6 * 1000000
+ _ <- openDoc "Desktop/simple.hs" "haskell"
+ return ()
+
+ describe "UnexpectedMessageException" $ do
+ it "throws when there's an unexpected message" $
+ let selector (UnexpectedMessage "Publish diagnostics notification" (FromServerMess SWindowLogMessage _)) = True
+ selector _ = False
+ in runSession serverExe fullCaps "test/data/renamePass" publishDiagnosticsNotification `shouldThrow` selector
+ it "provides the correct types that were expected and received" $
+ let selector (UnexpectedMessage "Response for: STextDocumentRename" (FromServerRsp STextDocumentDocumentSymbol _)) = True
+ selector _ = False
+ sesh = do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ sendRequest STextDocumentDocumentSymbol (DocumentSymbolParams Nothing Nothing doc)
+ skipMany anyNotification
+ response STextDocumentRename -- the wrong type
+ in runSession serverExe fullCaps "test/data/renamePass" sesh
+ `shouldThrow` selector
+
+ describe "text document VFS" $
+ it "sends back didChange notifications" $
+ runSession serverExe def "test/data/refactor" $ do
+ doc <- openDoc "Main.hs" "haskell"
+
+ let args = toJSON (doc ^. uri)
+ reqParams = ExecuteCommandParams Nothing "doAnEdit" (Just (List [args]))
+ request_ SWorkspaceExecuteCommand reqParams
+
+ editReq <- message SWorkspaceApplyEdit
+ liftIO $ do
+ let (Just cs) = editReq ^. params . edit . changes
+ [(u, List es)] = HM.toList cs
+ u `shouldBe` doc ^. uri
+ es `shouldBe` [TextEdit (Range (Position 0 0) (Position 0 5)) "howdy"]
+ contents <- documentContents doc
+ liftIO $ contents `shouldBe` "howdy:: IO Int\nmain = return (42)\n"
+
+ describe "getDocumentEdit" $
+ it "automatically consumes applyedit requests" $
+ runSession serverExe fullCaps "test/data/refactor" $ do
+ doc <- openDoc "Main.hs" "haskell"
+
+ let args = toJSON (doc ^. uri)
+ reqParams = ExecuteCommandParams Nothing "doAnEdit" (Just (List [args]))
+ request_ SWorkspaceExecuteCommand reqParams
+ contents <- getDocumentEdit doc
+ liftIO $ contents `shouldBe` "howdy:: IO Int\nmain = return (42)\n"
+
+ describe "getCodeActions" $
+ it "works" $ runSession serverExe fullCaps "test/data/refactor" $ do
+ doc <- openDoc "Main.hs" "haskell"
+ waitForDiagnostics
+ [InR action] <- getCodeActions doc (Range (Position 0 0) (Position 0 2))
+ actions <- getCodeActions doc (Range (Position 1 14) (Position 1 18))
+ liftIO $ action ^. title `shouldBe` "Delete this"
+ liftIO $ actions `shouldSatisfy` null
+
+ describe "getAllCodeActions" $
+ it "works" $ runSession serverExe fullCaps "test/data/refactor" $ do
+ doc <- openDoc "Main.hs" "haskell"
+ _ <- waitForDiagnostics
+ actions <- getAllCodeActions doc
+ liftIO $ do
+ let [InR action] = actions
+ action ^. title `shouldBe` "Delete this"
+ action ^. command . _Just . command `shouldBe` "deleteThis"
+
+ describe "getDocumentSymbols" $
+ it "works" $ runSession serverExe fullCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+
+ skipMany loggingNotification
+
+ Left (mainSymbol:_) <- getDocumentSymbols doc
+
+ liftIO $ do
+ mainSymbol ^. name `shouldBe` "foo"
+ mainSymbol ^. kind `shouldBe` SkObject
+ mainSymbol ^. range `shouldBe` mkRange 0 0 3 6
+
+ describe "applyEdit" $ do
+ it "increments the version" $ runSession serverExe docChangesCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ VersionedTextDocumentIdentifier _ (Just oldVersion) <- getVersionedDoc doc
+ let edit = TextEdit (Range (Position 1 1) (Position 1 3)) "foo"
+ VersionedTextDocumentIdentifier _ (Just newVersion) <- applyEdit doc edit
+ liftIO $ newVersion `shouldBe` oldVersion + 1
+ it "changes the document contents" $ runSession serverExe fullCaps "test/data/renamePass" $ do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ let edit = TextEdit (Range (Position 0 0) (Position 0 2)) "foo"
+ applyEdit doc edit
+ contents <- documentContents doc
+ liftIO $ contents `shouldSatisfy` T.isPrefixOf "foodule"