packages feed

plexus-synapse-4.2.0: test/WatchSpec.hs

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | @synapse --watch@: a call made again each time a file it reads changes.
--
-- In process, "Synapse.Watch"'s own parts: which files a command reads
-- (and the two commands that cannot be watched), the arguments of one
-- call, what is said after a call, and the wait for a change — a write, a
-- truncate-then-write, a save by rename, a file gone and back.
--
-- End to end, the built synapse against the mock service (a home, a cache
-- and a runtime directory of the test's own): the call at once and again on
-- a change with the file's new contents, the answers alone on stdout and
-- what the watch says on stderr, a failing call that ends nothing, @-o
-- FILE@ written whole by every call, the refusals, and Ctrl-C.
module Main (main) where

import Control.Concurrent (forkIO, threadDelay)
import Control.Exception (SomeException, try)
import Control.Monad (forM_, void)
import Data.Aeson
import qualified Data.Aeson.Key as Key
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
import Data.IORef
import Data.List (isInfixOf, isPrefixOf)
import Data.Text (Text)
import System.Directory (createDirectoryIfMissing, findExecutable, removeFile, renameFile)
import System.Environment (getEnvironment)
import System.Exit (ExitCode(..))
import System.FilePath ((</>))
import System.IO (Handle, hClose, hGetLine, hIsEOF)
import System.IO.Temp (withSystemTempDirectory)
import System.Posix.Signals (sigINT, signalProcess)
import System.Process
import System.Timeout (timeout)
import Test.Hspec

import MockService
import Synapse.CLI.Source (SourceEnv(..))
import Synapse.Watch

main :: IO ()
main = hspec $ do
  unitSpec
  synapse <- runIO (findExecutable "synapse")
  case synapse of
    Nothing -> it "(the synapse executable is not on PATH)" $ pendingWith "cabal test puts it there"
    Just exe -> endToEnd exe

-- ============================================================================
-- In process
-- ============================================================================

unitSpec :: Spec
unitSpec = describe "Synapse.Watch" $ do
  describe "the files a command reads" $ do
    let files params path = do
          cache <- newIORef Nothing
          let env = SourceEnv { seCwd = "/work", seHome = Just "/home/ada", seStdin = pure "", seCache = cache }
          pure (watchedFiles env params path)

    it "a flag's value that names a file, under the working directory" $
      files Nothing ["ui", "author", "--bundle", "live", "--sources.source", "@src/clock.svelte"]
        `shouldReturn` Right ["/work/src/clock.svelte"]

    it "--flag=@FILE, an absolute path and ~/" $
      files Nothing ["t", "amend", "--body=@/tmp/body.md", "--title", "@~/title.txt"]
        `shouldReturn` Right ["/tmp/body.md", "/home/ada/title.txt"]

    it "-p @FILE, first" $
      files (Just "@params.json") ["t", "amend", "--body", "@body.md"]
        `shouldReturn` Right ["/work/params.json", "/work/body.md"]

    it "a file named twice is watched once" $
      files Nothing ["t", "amend", "--body", "@body.md", "--title", "@body.md"]
        `shouldReturn` Right ["/work/body.md"]

    it "@@text is text, and a lone @ is itself: neither is a file" $
      files Nothing ["t", "amend", "--body", "@@handle", "--title", "@", "--id", "@id.txt"]
        `shouldReturn` Right ["/work/id.txt"]

    it "a command that reads no file is refused" $ do
      files Nothing ["t", "amend", "--body", "text"] `shouldReturn` Left NothingToWatch
      files (Just "{\"id\":\"x\"}") ["t", "amend"] `shouldReturn` Left NothingToWatch

    it "a command that reads stdin is refused, naming the flag" $ do
      files Nothing ["t", "amend", "--id", "@id.txt", "--body", "-"] `shouldReturn` Left (ReadsStdin "--body")
      files Nothing ["t", "amend", "--id", "@id.txt", "--body=-"] `shouldReturn` Left (ReadsStdin "--body")
      files (Just "-") ["t", "amend", "--id", "@id.txt"] `shouldReturn` Left (ReadsStdin "-p")

  describe "the arguments of one call" $ do
    it "are the command's without --watch" $
      childArgs ["-P", "4460", "--watch", "-j", "menger", "t", "amend", "--id", "@id.txt"]
        `shouldBe` ["-P", "4460", "-j", "menger", "t", "amend", "--id", "@id.txt"]

    it "only synapse's own: a method's --watch, after it, stays" $
      childArgs ["--watch", "menger", "t", "list", "--watch", "true"]
        `shouldBe` ["menger", "t", "list", "--watch", "true"]

  describe "what is said after a call" $ do
    it "the first call: that it is the call as it stands, how it ended and how long it took" $
      reportLine 1 [] ExitSuccess 0.0834 Nothing
        `shouldBe` "synapse: #1 the call as it stands: answered in 83 ms"

    it "a change: the file, and how long after the save the answer was whole" $
      reportLine 4 ["src/clock.svelte"] ExitSuccess 0.412 (Just 0.4551)
        `shouldBe` "synapse: #4 src/clock.svelte changed: answered in 412 ms, 455 ms after the save"

    it "a call that failed says its exit" $
      reportLine 2 ["a.md", "b.md"] (ExitFailure 1) 0.02 (Just 0.06)
        `shouldBe` "synapse: #2 a.md, b.md changed: exit 1 in 20 ms, 60 ms after the save"

  describe "the wait for a change" $ do
    let scratch act = withSystemTempDirectory "synapse-watch" $ \dir -> do
          let a = dir </> "a.txt"; b = dir </> "b.txt"
          writeFile a "one"; writeFile b "one"
          seen <- mapM (\f -> (,) f <$> stampOf f) [a, b]
          act dir a b seen
        waited = timeout 5000000 . awaitChange pollMicros settleMicros
        later act = void (forkIO (threadDelay 150000 >> act))

    it "does not end while nothing changes" $ scratch $ \_ _ _ seen ->
      timeout 400000 (awaitChange pollMicros settleMicros seen) `shouldReturn` Nothing

    it "ends on a write, naming the file that changed and no other" $ scratch $ \_ a _ seen -> do
      later (writeFile a "two, longer")
      Just (now, changed) <- waited seen
      changed `shouldBe` [a]
      now `shouldNotBe` seen

    it "a truncate and the write after it are one change, read when it is whole" $ scratch $ \_ a _ seen -> do
      later (writeFile a "" >> threadDelay 5000 >> writeFile a "the whole of it")
      Just (_, changed) <- waited seen
      changed `shouldBe` [a]
      readFile a `shouldReturn` "the whole of it"

    it "a save by rename is a change" $ scratch $ \dir a _ seen -> do
      later (writeFile (dir </> "a.tmp") "renamed into place" >> renameFile (dir </> "a.tmp") a)
      Just (_, changed) <- waited seen
      changed `shouldBe` [a]

    it "a file gone is waited for, and is a change once it is back" $ scratch $ \_ a _ seen -> do
      removeFile a
      timeout 300000 (awaitChange pollMicros settleMicros seen) `shouldReturn` Nothing
      later (writeFile a "back")
      Just (_, changed) <- waited seen
      changed `shouldBe` [a]

    it "two files changed together are one change naming both" $ scratch $ \_ a b seen -> do
      later (writeFile a "two!" >> writeFile b "two!")
      Just (_, changed) <- waited seen
      changed `shouldBe` [a, b]

-- ============================================================================
-- The built synapse
-- ============================================================================

endToEnd :: FilePath -> Spec
endToEnd exe = describe "synapse --watch, the built synapse against the mock service" $ do
  it "makes the call at once, and again with the file's new contents when it changes" $
    withRun $ \dir spawn -> do
      let idFile = dir </> "id.txt"
      writeFile idFile "MENGER-1"
      writeIORef callsMade []
      watching (spawn ["--watch", "-j"] ["tickets", "amend", "--id", "@" <> idFile, "--body", "x"]) $ \out err _ -> do
        saidBy err "#1 " `shouldReturn` True
        writeFile idFile "MENGER-2"
        saidBy err "#2 " `shouldReturn` True
        said <- readIORef err
        -- what the watch says: what it follows, then each call
        head said `shouldSatisfy` ("synapse: watching " `isPrefixOf`)
        head said `shouldSatisfy` ("id.txt" `isInfixOf`)
        line "#1 " said `shouldSatisfy` ("synapse: #1 the call as it stands: answered in " `isPrefixOf`)
        line "#2 " said `shouldSatisfy` (\l -> "id.txt changed: answered in " `isInfixOf` l && " after the save" `isInfixOf` l)
        -- stdout: the answers and nothing else, the second with the new contents
        answers <- readIORef out
        filter ("MENGER-1" `isInfixOf`) answers `shouldNotBe` []
        filter ("MENGER-2" `isInfixOf`) answers `shouldNotBe` []
        filter ("synapse:" `isInfixOf`) answers `shouldBe` []
        -- two calls, each sent once
        (length . filter (== "tickets.amend") <$> readIORef callsMade) `shouldReturn` 2

  it "a call that fails ends nothing: the next change is the next call" $
    withRun $ \dir spawn -> do
      let params = dir </> "params.json"
      writeFile params "{\"id\":\"MENGER-1\",\"body\":\"x\"}"
      watching (spawn ["--watch", "-j", "-p", "@" <> params] ["tickets", "amend"]) $ \out err _ -> do
        saidBy err "#1 " `shouldReturn` True
        writeFile params "{\"id\":"
        saidBy err "#2 " `shouldReturn` True
        writeFile params "{\"id\":\"MENGER-3\",\"body\":\"x\"}"
        saidBy err "#3 " `shouldReturn` True
        said <- readIORef err
        line "#2 " said `shouldSatisfy` ("params.json changed: exit " `isInfixOf`)
        line "#3 " said `shouldSatisfy` ("params.json changed: answered in " `isInfixOf`)
        (filter ("MENGER-3" `isInfixOf`) <$> readIORef out) >>= (`shouldNotBe` [])

  it "-o FILE is written whole by every call" $
    withRun $ \dir spawn -> do
      let idFile = dir </> "id.txt"; answer = dir </> "answer.out"
      writeFile idFile "MENGER-1"
      watching (spawn ["--watch", "-j", "-o", answer] ["tickets", "amend", "--id", "@" <> idFile, "--body", "x"]) $ \out err _ -> do
        saidBy err "#1 " `shouldReturn` True
        (BS8.unpack <$> BS.readFile answer) >>= (`shouldSatisfy` ("MENGER-1" `isInfixOf`))
        writeFile idFile "MENGER-2"
        saidBy err "#2 " `shouldReturn` True
        now <- BS8.unpack <$> BS.readFile answer
        now `shouldSatisfy` ("MENGER-2" `isInfixOf`)
        now `shouldSatisfy` (not . ("MENGER-1" `isInfixOf`))
        readIORef out `shouldReturn` []

  it "Ctrl-C ends it, 130" $
    withRun $ \dir spawn -> do
      let idFile = dir </> "id.txt"
      writeFile idFile "MENGER-1"
      watching (spawn ["--watch"] ["tickets", "amend", "--id", "@" <> idFile, "--body", "x"]) $ \_ err ph -> do
        saidBy err "#1 " `shouldReturn` True
        Just pid <- getPid ph
        signalProcess sigINT pid
        timeout 5000000 (waitForProcess ph) `shouldReturn` Just (ExitFailure 130)

  forM_ [ ("a call that reads no file", ["tickets", "amend", "--id", "MENGER-1"], "reads none")
        , ("a call that reads stdin", ["tickets", "amend", "--id", "@id.txt", "--body", "-"], "cannot follow stdin (--body -)") ]
    $ \(what, call, why) ->
   it (what <> " is refused, 2, and nothing is called") $
    withRun $ \_ spawn -> do
      writeIORef callsMade []
      (code, out, err) <- readCreateProcessWithExitCode (spawn ["--watch"] call) { std_in = NoStream } ""
      (code, out) `shouldBe` (ExitFailure 2, "")
      err `shouldSatisfy` (why `isInfixOf`)
      readIORef callsMade `shouldReturn` []

  it "--watch is for a call to a service: a meta-command is refused, 2" $
    withRun $ \_ _ -> do
      (code, _, err) <- readProcessWithExitCode exe ["--watch", "_key", "list"] ""
      code `shouldBe` ExitFailure 2
      err `shouldSatisfy` ("--watch is for a call to a service, not _key" `isInfixOf`)
  where
    -- a run of the built synapse against a fresh mock service; @spawn
    -- globals args@
    withRun act = withSystemTempDirectory "synapse-watch" $ \dir -> withMockService todaysService $ \port -> do
      forM_ ["home", "cache", "run"] $ \d -> createDirectoryIfMissing True (dir </> d)
      env0 <- getEnvironment
      let env' = [ (k, v) | (k, v) <- env0, k `notElem` ["HOME", "XDG_CACHE_HOME", "XDG_RUNTIME_DIR", "SYNAPSE_AS", "PLEXUS_SESSION", "SYNAPSE_RECONNECT_FOR"] ]
              ++ [ ("HOME", dir </> "home"), ("XDG_CACHE_HOME", dir </> "cache"), ("XDG_RUNTIME_DIR", dir </> "run")
                 , ("PLEXUS_KEYCHAIN", "off"), ("SYNAPSE_HOLD_FOR", "3s") ]
          spawn globals args = (proc exe (globals ++ ["--url", "ws://127.0.0.1:" <> show port <> "/ws", "menger"] ++ args))
            { env = Just env', std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe }
      writeIORef schemaOverride (Just [("tickets", ticketsWithWrites)])
      r <- try (act dir spawn)
      writeIORef schemaOverride Nothing
      either (\(e :: SomeException) -> ioError (userError (show e))) pure r

    -- a watch running: its stdout and stderr gathered a line at a time
    -- (oldest first), ended when the body is over
    watching :: CreateProcess -> (IORef [String] -> IORef [String] -> ProcessHandle -> IO a) -> IO a
    watching cp body = do
      (Just inp, Just out, Just err, ph) <- createProcess cp
      hClose inp
      outLines <- gather out
      errLines <- gather err
      r <- try (body outLines errLines ph)
      terminateProcess ph
      _ <- timeout 5000000 (waitForProcess ph)
      either (\(e :: SomeException) -> ioError (userError (show e))) pure r

    gather :: Handle -> IO (IORef [String])
    gather h = do
      ref <- newIORef []
      let loop = do
            eof <- hIsEOF h
            if eof then pure () else do
              l <- hGetLine h
              atomicModifyIORef' ref (\ls -> (ls ++ [l], ()))
              loop
      _ <- forkIO (void (try loop :: IO (Either SomeException ())))
      pure ref

    -- whether a line saying @what@ has come, within 20 s
    saidBy :: IORef [String] -> String -> IO Bool
    saidBy ref what = go (400 :: Int)
      where
        go 0 = pure False
        go n = do
          ls <- readIORef ref
          if any (what `isInfixOf`) ls then pure True else threadDelay 50000 >> go (n - 1)

    line what ls = case filter (what `isInfixOf`) ls of
      (l : _) -> l
      [] -> ""

-- | menger's tickets in miniature: a read and a write.
ticketsWithWrites :: Value
ticketsWithWrites = object
  [ "namespace" .= ("tickets" :: Text), "version" .= ("0.5.0" :: Text), "hash" .= ("t0" :: Text)
  , "description" .= ("Plans as data" :: Text)
  , "methods" .=
      [ m "get" "One ticket by id" (Just "read") [("id", True)]
      , m "amend" "Replace a ticket's body" (Just "write") [("id", True), ("body", False)] ] ]
  where
    m :: Text -> Text -> Maybe Text -> [(Text, Bool)] -> Value
    m n d g ps = object $
      [ "name" .= n, "description" .= d, "hash" .= (n <> "0"), "streaming" .= False
      , "params" .= object
          [ "type" .= ("object" :: Text)
          , "required" .= [ p | (p, True) <- ps ]
          , "properties" .= object [ Key.fromText p .= object ["type" .= ("string" :: Text)] | (p, _) <- ps ] ] ]
      <> maybe [] (\g' -> ["group" .= g']) g