module Hydra.UtilsSpec where

import Hydra.Cardano.Api (FileError)
import Hydra.Options (GenerateKeyPair (GenerateKeyPair))
import Hydra.Prelude
import Hydra.Utils (genHydraKeys)
import System.FilePath ((</>))
import Test.Hydra.Prelude

spec :: Spec
spec :: Spec
spec = do
  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Should throw if it can't write on disk" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    Either (FileError ()) ()
result <- GenerateKeyPair -> IO (Either (FileError ()) ())
genHydraKeys (String -> GenerateKeyPair
GenerateKeyPair String
"/unexisting_directory")
    case Either (FileError ()) ()
result of
      Left (FileError ()
_ :: FileError e) -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Right ()
_ -> HasCallStack => String -> IO ()
String -> IO ()
expectationFailure String
"getHydraKeys should have failed with FileError"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Should throw if the file already exists" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
MonadIO m =>
String -> (String -> m r) -> m r
withTempDir String
"gen-hydra-keys" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
tmp -> do
      String -> String -> IO ()
forall (m :: * -> *). MonadIO m => String -> String -> m ()
writeFile (String
tmp String -> String -> String
</> String
"hydra") String
"hydra key"
      Either (FileError ()) ()
result <- GenerateKeyPair -> IO (Either (FileError ()) ())
genHydraKeys (String -> GenerateKeyPair
GenerateKeyPair (String -> GenerateKeyPair) -> String -> GenerateKeyPair
forall a b. (a -> b) -> a -> b
$ String
tmp String -> String -> String
</> String
"hydra")
      case Either (FileError ()) ()
result of
        Left (FileError ()
_ :: FileError e) -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        Right ()
_ -> HasCallStack => String -> IO ()
String -> IO ()
expectationFailure String
"getHydraKeys should have failed with FileError"