+import Language.Haskell.LSP.Types.Lens as LSP hiding
+ (capabilities, message, rename, applyEdit)
+import Language.Haskell.LSP.Types.Capabilities as LSP
+import System.Directory
+import System.FilePath
+import System.Timeout
+
+{-# 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 :: Session ApplyWorkspaceEditRequest
+ -- 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 :: Session ApplyWorkspaceEditRequest
+ 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 :: Session ApplyWorkspaceEditRequest
+ 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" (NotLogMessage _)) = 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 "ResponseMessage WorkspaceEdit" (RspDocumentSymbols _)) = True
+ selector _ = False
+ sesh = do
+ doc <- openDoc "Desktop/simple.hs" "haskell"
+ sendRequest TextDocumentDocumentSymbol (DocumentSymbolParams doc Nothing)
+ skipMany anyNotification
+ message :: Session RenameResponse -- the wrong type
+ in runSession serverExe fullCaps "test/data/renamePass" sesh
+ `shouldThrow` selector
+
+ -- This is too fickle at the moment
+ -- describe "replaySession" $
+ -- it "passes a test" $
+ -- replaySession serverExe "test/data/renamePass"
+ -- it "fails a test" $
+ -- let selector (ReplayOutOfOrder _ _) = True
+ -- selector _ = False
+ -- in replaySession serverExe "test/data/renameFail" `shouldThrow` selector
+
+ -- describe "manual javascript session" $
+ -- it "passes a test" $
+ -- runSession "javascript-typescript-stdio" fullCaps "test/data/javascriptPass" $ do
+ -- doc <- openDoc "test.js" "javascript"
+
+ -- noDiagnostics
+
+ -- Right (fooSymbol:_) <- getDocumentSymbols doc
+
+ -- liftIO $ do
+ -- fooSymbol ^. name `shouldBe` "foo"
+ -- fooSymbol ^. kind `shouldBe` SkFunction
+
+ 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 "doAnEdit" (Just (List [args])) Nothing
+ request_ WorkspaceExecuteCommand reqParams
+
+ editReq <- message :: Session ApplyWorkspaceEditRequest
+ 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 "doAnEdit" (Just (List [args])) Nothing
+ request_ WorkspaceExecuteCommand 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
+ [CACodeAction action] <- getCodeActions doc (Range (Position 1 14) (Position 1 18))
+ liftIO $ action ^. title `shouldBe` "Delete this"