module Hydra.Ledger.SimpleSpec where

import Hydra.Prelude
import Test.Hydra.Prelude

import Hydra.Ledger (ChainSlot (ChainSlot), applyTransactions)
import Hydra.Ledger.Simple
import Test.QuickCheck (Property, forAllShrink, shrinkList)

spec :: Spec
spec :: Spec
spec =
  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"validates only correctly built transactions" Property
prop_validateCorrectTransactions

prop_validateCorrectTransactions :: Property
prop_validateCorrectTransactions :: Property
prop_validateCorrectTransactions =
  Gen [SimpleTx]
-> ([SimpleTx] -> [[SimpleTx]]) -> ([SimpleTx] -> Bool) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink (UTxOType SimpleTx -> Gen [SimpleTx]
genSequenceOfValidTransactions Set SimpleTxIn
UTxOType SimpleTx
forall a. Monoid a => a
mempty) [SimpleTx] -> [[SimpleTx]]
shrinkSequence (([SimpleTx] -> Bool) -> Property)
-> ([SimpleTx] -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \[SimpleTx]
txs ->
    Either (SimpleTx, ValidationError) (Set SimpleTxIn) -> Bool
forall a b. Either a b -> Bool
isRight (Ledger SimpleTx
-> ChainSlot
-> UTxOType SimpleTx
-> [SimpleTx]
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx)
forall tx.
Ledger tx
-> ChainSlot
-> UTxOType tx
-> [tx]
-> Either (tx, ValidationError) (UTxOType tx)
applyTransactions Ledger SimpleTx
simpleLedger (Natural -> ChainSlot
ChainSlot Natural
0) Set SimpleTxIn
UTxOType SimpleTx
forall a. Monoid a => a
mempty [SimpleTx]
txs)

shrinkSequence :: [SimpleTx] -> [[SimpleTx]]
shrinkSequence :: [SimpleTx] -> [[SimpleTx]]
shrinkSequence = (SimpleTx -> [SimpleTx]) -> [SimpleTx] -> [[SimpleTx]]
forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList ([SimpleTx] -> SimpleTx -> [SimpleTx]
forall a b. a -> b -> a
const [])