module Ribosome.Test.TmuxCommon where

import Chiasma.Data.CodecError (CodecError)
import Chiasma.Data.RenderError (RenderError)
import Chiasma.Data.TmuxError (TmuxError)
import Chiasma.Effect.TmuxClient (NativeTmux)
import qualified Chiasma.Test.Data.TmuxTestConfig as Chiasma
import Chiasma.Test.Tmux (TestTmuxEffects, withSystemTempDir, withTestTmux)
import Polysemy.Test (UnitTest)

import Ribosome.Data.PluginName (PluginName)
import Ribosome.Host.Data.BootError (BootError (BootError))
import Ribosome.Host.Test.Run (TestStack)
import Ribosome.Test.Data.TestConfig (TmuxTestConfig (TmuxTestConfig))
import Ribosome.Test.Embed (runTestConf)

type TmuxErrors =
  [
    Stop CodecError,
    Error CodecError,
    Stop RenderError,
    Error RenderError,
    Stop TmuxError,
    Error TmuxError,
    Error Text
  ]

type TmuxBaseStack =
  TestTmuxEffects ++ TmuxErrors

type TmuxStack =
  NativeTmux : TmuxBaseStack ++ Reader PluginName : TestStack

interpretTmuxErrors ::
  Member (Error BootError) r =>
  InterpretersFor TmuxErrors r
interpretTmuxErrors :: forall (r :: EffectRow).
Member (Error BootError) r =>
InterpretersFor TmuxErrors r
interpretTmuxErrors =
  (Text -> BootError) -> Sem (Error Text : r) a -> Sem r a
forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError Text -> BootError
BootError (Sem (Error Text : r) a -> Sem r a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem (Error Text : r) a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError @TmuxError (Text -> BootError
BootError (Text -> BootError)
-> (TmuxError -> Text) -> TmuxError -> BootError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TmuxError -> Text
forall b a. (Show a, IsString b) => a -> b
show) (Sem (Error TmuxError : Error Text : r) a
 -> Sem (Error Text : r) a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem (Error TmuxError : Error Text : r) a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem (Error Text : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Stop TmuxError : Error TmuxError : Error Text : r) a
-> Sem (Error TmuxError : Error Text : r) a
forall err (r :: EffectRow) a.
Member (Error err) r =>
Sem (Stop err : r) a -> Sem r a
stopToError (Sem (Stop TmuxError : Error TmuxError : Error Text : r) a
 -> Sem (Error TmuxError : Error Text : r) a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem (Stop TmuxError : Error TmuxError : Error Text : r) a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem (Error TmuxError : Error Text : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError @RenderError (Text -> BootError
BootError (Text -> BootError)
-> (RenderError -> Text) -> RenderError -> BootError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenderError -> Text
forall b a. (Show a, IsString b) => a -> b
show) (Sem
   (Error RenderError
      : Stop TmuxError : Error TmuxError : Error Text : r)
   a
 -> Sem (Stop TmuxError : Error TmuxError : Error Text : r) a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem
         (Error RenderError
            : Stop TmuxError : Error TmuxError : Error Text : r)
         a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem (Stop TmuxError : Error TmuxError : Error Text : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Stop RenderError
     : Error RenderError : Stop TmuxError : Error TmuxError : Error Text
     : r)
  a
-> Sem
     (Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
forall err (r :: EffectRow) a.
Member (Error err) r =>
Sem (Stop err : r) a -> Sem r a
stopToError (Sem
   (Stop RenderError
      : Error RenderError : Stop TmuxError : Error TmuxError : Error Text
      : r)
   a
 -> Sem
      (Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem
         (Stop RenderError
            : Error RenderError : Stop TmuxError : Error TmuxError : Error Text
            : r)
         a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem
     (Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall e1 e2 (r :: EffectRow) a.
Member (Error e2) r =>
(e1 -> e2) -> Sem (Error e1 : r) a -> Sem r a
mapError @CodecError (Text -> BootError
BootError (Text -> BootError)
-> (CodecError -> Text) -> CodecError -> BootError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CodecError -> Text
forall b a. (Show a, IsString b) => a -> b
show) (Sem
   (Error CodecError
      : Stop RenderError : Error RenderError : Stop TmuxError
      : Error TmuxError : Error Text : r)
   a
 -> Sem
      (Stop RenderError
         : Error RenderError : Stop TmuxError : Error TmuxError : Error Text
         : r)
      a)
-> (Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a
    -> Sem
         (Error CodecError
            : Stop RenderError : Error RenderError : Stop TmuxError
            : Error TmuxError : Error Text : r)
         a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
-> Sem
     (Stop RenderError
        : Error RenderError : Stop TmuxError : Error TmuxError : Error Text
        : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Stop CodecError
     : Error CodecError : Stop RenderError : Error RenderError
     : Stop TmuxError : Error TmuxError : Error Text : r)
  a
-> Sem
     (Error CodecError
        : Stop RenderError : Error RenderError : Stop TmuxError
        : Error TmuxError : Error Text : r)
     a
forall err (r :: EffectRow) a.
Member (Error err) r =>
Sem (Stop err : r) a -> Sem r a
stopToError

withTmuxTest ::
  Members TestStack r =>
  Chiasma.TmuxTestConfig ->
  InterpretersFor TmuxBaseStack r
withTmuxTest :: forall (r :: EffectRow).
Members TestStack r =>
TmuxTestConfig -> InterpretersFor TmuxBaseStack r
withTmuxTest TmuxTestConfig
conf =
  Sem
  (Stop CodecError
     : Error CodecError : Stop RenderError : Error RenderError
     : Stop TmuxError : Error TmuxError : Error Text : r)
  a
-> Sem r a
forall (r :: EffectRow).
Member (Error BootError) r =>
InterpretersFor TmuxErrors r
interpretTmuxErrors (Sem
   (Stop CodecError
      : Error CodecError : Stop RenderError : Error RenderError
      : Stop TmuxError : Error TmuxError : Error Text : r)
   a
 -> Sem r a)
-> (Sem
      (PScoped () () (TmuxClient TmuxRequest TmuxResponse)
         : Resumable
             TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse))
         : Resumable CodecError (NativeCodec TmuxCommand)
         : Resumable CodecError (NativeCodec (Panes Pane))
         : Reader TmuxNative : Stop CodecError : Error CodecError
         : Stop RenderError : Error RenderError : Stop TmuxError
         : Error TmuxError : Error Text : r)
      a
    -> Sem
         (Stop CodecError
            : Error CodecError : Stop RenderError : Error RenderError
            : Stop TmuxError : Error TmuxError : Error Text : r)
         a)
-> Sem
     (PScoped () () (TmuxClient TmuxRequest TmuxResponse)
        : Resumable
            TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse))
        : Resumable CodecError (NativeCodec TmuxCommand)
        : Resumable CodecError (NativeCodec (Panes Pane))
        : Reader TmuxNative : Stop CodecError : Error CodecError
        : Stop RenderError : Error RenderError : Stop TmuxError
        : Error TmuxError : Error Text : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (Path Abs Dir
 -> Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a)
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
forall (r :: EffectRow) a.
Members '[Resource, Embed IO] r =>
(Path Abs Dir -> Sem r a) -> Sem r a
withSystemTempDir ((Path Abs Dir
  -> Sem
       (Stop CodecError
          : Error CodecError : Stop RenderError : Error RenderError
          : Stop TmuxError : Error TmuxError : Error Text : r)
       a)
 -> Sem
      (Stop CodecError
         : Error CodecError : Stop RenderError : Error RenderError
         : Stop TmuxError : Error TmuxError : Error Text : r)
      a)
-> (Sem
      (PScoped () () (TmuxClient TmuxRequest TmuxResponse)
         : Resumable
             TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse))
         : Resumable CodecError (NativeCodec TmuxCommand)
         : Resumable CodecError (NativeCodec (Panes Pane))
         : Reader TmuxNative : Stop CodecError : Error CodecError
         : Stop RenderError : Error RenderError : Stop TmuxError
         : Error TmuxError : Error Text : r)
      a
    -> Path Abs Dir
    -> Sem
         (Stop CodecError
            : Error CodecError : Stop RenderError : Error RenderError
            : Stop TmuxError : Error TmuxError : Error Text : r)
         a)
-> Sem
     (PScoped () () (TmuxClient TmuxRequest TmuxResponse)
        : Resumable
            TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse))
        : Resumable CodecError (NativeCodec TmuxCommand)
        : Resumable CodecError (NativeCodec (Panes Pane))
        : Reader TmuxNative : Stop CodecError : Error CodecError
        : Stop RenderError : Error RenderError : Stop TmuxError
        : Error TmuxError : Error Text : r)
     a
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  TmuxTestConfig
-> Sem
     (TestTmuxEffects
      ++ (Stop CodecError
            : Error CodecError : Stop RenderError : Error RenderError
            : Stop TmuxError : Error TmuxError : Error Text : r))
     a
-> Path Abs Dir
-> Sem
     (Stop CodecError
        : Error CodecError : Stop RenderError : Error RenderError
        : Stop TmuxError : Error TmuxError : Error Text : r)
     a
forall t d (r :: EffectRow) a.
Members
  '[Test, Time t d, Log, Resource, Stop TmuxError, Error Text, Race,
    Async, Embed IO]
  r =>
TmuxTestConfig
-> Sem (TestTmuxEffects ++ r) a -> Path Abs Dir -> Sem r a
withTestTmux TmuxTestConfig
conf

runTmuxNvim ::
  HasCallStack =>
  TmuxTestConfig ->
  Sem TmuxStack () ->
  UnitTest
runTmuxNvim :: HasCallStack => TmuxTestConfig -> Sem TmuxStack () -> UnitTest
runTmuxNvim (TmuxTestConfig TestConfig
conf TmuxTestConfig
tmuxConf) =
  HasCallStack =>
TestConfig -> Sem (Reader PluginName : TestStack) () -> UnitTest
TestConfig -> Sem (Reader PluginName : TestStack) () -> UnitTest
runTestConf TestConfig
conf (Sem
   '[Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
     Reader LogConfig, Reader HostConfig, Time Time Date, Log,
     PScoped () Restoration RestoreMask,
     PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
     Race, Async, Error BootError, Test, Fail, Error TestError,
     Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
   ()
 -> UnitTest)
-> (Sem
      '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
        PScoped () () (TmuxClient TmuxRequest TmuxResponse),
        Resumable
          TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
        Resumable CodecError (NativeCodec TmuxCommand),
        Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
        Stop CodecError, Error CodecError, Stop RenderError,
        Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
        Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
        Reader LogConfig, Reader HostConfig, Time Time Date, Log,
        PScoped () Restoration RestoreMask,
        PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
        Race, Async, Error BootError, Test, Fail, Error TestError,
        Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
      ()
    -> Sem
         '[Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
           Reader LogConfig, Reader HostConfig, Time Time Date, Log,
           PScoped () Restoration RestoreMask,
           PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
           Race, Async, Error BootError, Test, Fail, Error TestError,
           Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
         ())
-> Sem
     '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
       PScoped () () (TmuxClient TmuxRequest TmuxResponse),
       Resumable
         TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
       Resumable CodecError (NativeCodec TmuxCommand),
       Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
       Stop CodecError, Error CodecError, Stop RenderError,
       Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
       Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
       Reader LogConfig, Reader HostConfig, Time Time Date, Log,
       PScoped () Restoration RestoreMask,
       PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
       Race, Async, Error BootError, Test, Fail, Error TestError,
       Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
     ()
-> UnitTest
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  TmuxTestConfig
-> InterpretersFor
     TmuxBaseStack
     '[Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
       Reader LogConfig, Reader HostConfig, Time Time Date, Log,
       PScoped () Restoration RestoreMask,
       PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
       Race, Async, Error BootError, Test, Fail, Error TestError,
       Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
forall (r :: EffectRow).
Members TestStack r =>
TmuxTestConfig -> InterpretersFor TmuxBaseStack r
withTmuxTest TmuxTestConfig
tmuxConf (Sem
   '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
     Resumable
       TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
     Resumable CodecError (NativeCodec TmuxCommand),
     Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
     Stop CodecError, Error CodecError, Stop RenderError,
     Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
     Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
     Reader LogConfig, Reader HostConfig, Time Time Date, Log,
     PScoped () Restoration RestoreMask,
     PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
     Race, Async, Error BootError, Test, Fail, Error TestError,
     Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
   ()
 -> Sem
      '[Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
        Reader LogConfig, Reader HostConfig, Time Time Date, Log,
        PScoped () Restoration RestoreMask,
        PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
        Race, Async, Error BootError, Test, Fail, Error TestError,
        Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
      ())
-> (Sem
      '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
        PScoped () () (TmuxClient TmuxRequest TmuxResponse),
        Resumable
          TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
        Resumable CodecError (NativeCodec TmuxCommand),
        Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
        Stop CodecError, Error CodecError, Stop RenderError,
        Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
        Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
        Reader LogConfig, Reader HostConfig, Time Time Date, Log,
        PScoped () Restoration RestoreMask,
        PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
        Race, Async, Error BootError, Test, Fail, Error TestError,
        Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
      ()
    -> Sem
         '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
           Resumable
             TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
           Resumable CodecError (NativeCodec TmuxCommand),
           Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
           Stop CodecError, Error CodecError, Stop RenderError,
           Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
           Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
           Reader LogConfig, Reader HostConfig, Time Time Date, Log,
           PScoped () Restoration RestoreMask,
           PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
           Race, Async, Error BootError, Test, Fail, Error TestError,
           Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
         ())
-> Sem
     '[PScoped () () (TmuxClient TmuxRequest TmuxResponse),
       PScoped () () (TmuxClient TmuxRequest TmuxResponse),
       Resumable
         TmuxError (PScoped () () (TmuxClient TmuxRequest TmuxResponse)),
       Resumable CodecError (NativeCodec TmuxCommand),
       Resumable CodecError (NativeCodec (Panes Pane)), Reader TmuxNative,
       Stop CodecError, Error CodecError, Stop RenderError,
       Error RenderError, Stop TmuxError, Error TmuxError, Error Text,
       Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
       Reader LogConfig, Reader HostConfig, Time Time Date, Log,
       PScoped () Restoration RestoreMask,
       PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
       Race, Async, Error BootError, Test, Fail, Error TestError,
       Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
     ()
-> Sem
     '[Reader PluginName, Log, Tagged "stderr" Log, Tagged "file" Log,
       Reader LogConfig, Reader HostConfig, Time Time Date, Log,
       PScoped () Restoration RestoreMask,
       PScoped () Restoration RestoreMask, PScoped () (MVar ()) Gate,
       Race, Async, Error BootError, Test, Fail, Error TestError,
       Hedgehog IO, Error Failure, Embed IO, Resource, Final IO]
     ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall err (eff :: (* -> *) -> * -> *) (r :: EffectRow).
Members '[Resumable err eff, Stop err] r =>
InterpreterFor eff r
restop @TmuxError @NativeTmux