-- |Assertions for Neovim UI elements
module Ribosome.Test.Ui where

import Polysemy.Test (Hedgehog, assertEq, (===))

import Ribosome.Api.Window (currentCursor, cursor)
import Ribosome.Host.Api.Data (Window)
import Ribosome.Host.Api.Effect (nvimListWins)
import Ribosome.Host.Effect.Rpc (Rpc)

-- |Assert the number of windows.
windowCountIs ::
  Monad m =>
  Members [Rpc, Hedgehog m] r =>
  Int ->
  Sem r ()
windowCountIs :: forall (m :: * -> *) (r :: EffectRow).
(Monad m, Members '[Rpc, Hedgehog m] r) =>
Int -> Sem r ()
windowCountIs Int
count = do
  [Window]
wins <- Sem r [Window]
forall (r :: EffectRow). Member Rpc r => Sem r [Window]
nvimListWins
  Int
count Int -> Int -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Monad m, Eq a, Show a, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> Sem r ()
=== [Window] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Window]
wins

-- |Assert the cursor position in a window.
cursorIs ::
  Monad m =>
  Members [Rpc, Hedgehog m] r =>
  Int ->
  Int ->
  Window ->
  Sem r ()
cursorIs :: forall (m :: * -> *) (r :: EffectRow).
(Monad m, Members '[Rpc, Hedgehog m] r) =>
Int -> Int -> Window -> Sem r ()
cursorIs Int
line Int
col =
  (Int, Int) -> (Int, Int) -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Monad m, Eq a, Show a, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> Sem r ()
assertEq (Int
line, Int
col) ((Int, Int) -> Sem r ())
-> (Window -> Sem r (Int, Int)) -> Window -> Sem r ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Window -> Sem r (Int, Int)
forall (r :: EffectRow). Member Rpc r => Window -> Sem r (Int, Int)
cursor

-- |Assert the cursor position in the current window.
currentCursorIs ::
  Monad m =>
  Members [Rpc, Hedgehog m] r =>
  Int ->
  Int ->
  Sem r ()
currentCursorIs :: forall (m :: * -> *) (r :: EffectRow).
(Monad m, Members '[Rpc, Hedgehog m] r) =>
Int -> Int -> Sem r ()
currentCursorIs Int
line Int
col =
  (Int, Int) -> (Int, Int) -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Monad m, Eq a, Show a, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> Sem r ()
assertEq (Int
line, Int
col) ((Int, Int) -> Sem r ()) -> Sem r (Int, Int) -> Sem r ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Sem r (Int, Int)
forall (r :: EffectRow). Member Rpc r => Sem r (Int, Int)
currentCursor