{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.State.PoolDistrSpec (spec) where

import Cardano.Ledger.BaseTypes (StrictMaybe (..))
import Cardano.Ledger.Binary (natVersion)
import Cardano.Ledger.State (BlsKey, IndividualPoolStake (..))
import Test.Cardano.Ledger.Binary.RoundTrip (
  cborTrip,
  embedTripExpectation,
  roundTripCborRangeExpectation,
 )
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Arbitrary ()

spec :: Spec
spec :: Spec
spec =
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"IndividualPoolStake" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    String -> (IndividualPoolStake -> BlsKey -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"BlsKey round-trips from Dijkstra onwards" ((IndividualPoolStake -> BlsKey -> Expectation) -> Spec)
-> (IndividualPoolStake -> BlsKey -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$
      \(IndividualPoolStake
ips :: IndividualPoolStake) (BlsKey
blsKey :: BlsKey) ->
        Version -> Version -> IndividualPoolStake -> Expectation
forall t.
(Show t, Eq t, EncCBOR t, DecCBOR t, HasCallStack) =>
Version -> Version -> t -> Expectation
roundTripCborRangeExpectation
          (forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @12)
          Version
forall a. Bounded a => a
maxBound
          IndividualPoolStake
ips {individualPoolStakeBls = SJust blsKey}
    String -> (IndividualPoolStake -> BlsKey -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"BlsKey is neither encoded nor decoded before Dijkstra" ((IndividualPoolStake -> BlsKey -> Expectation) -> Spec)
-> (IndividualPoolStake -> BlsKey -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$
      \(IndividualPoolStake
ips :: IndividualPoolStake) (BlsKey
blsKey :: BlsKey) ->
        [Version] -> (Version -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Version
forall a. Bounded a => a
minBound .. forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @11] ((Version -> Expectation) -> Expectation)
-> (Version -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Version
version ->
          Version
-> Version
-> Trip IndividualPoolStake IndividualPoolStake
-> (IndividualPoolStake -> IndividualPoolStake -> Expectation)
-> IndividualPoolStake
-> Expectation
forall a b.
(Typeable b, Eq b, HasCallStack) =>
Version
-> Version
-> Trip a b
-> (b -> a -> Expectation)
-> a
-> Expectation
embedTripExpectation
            Version
version
            Version
version
            Trip IndividualPoolStake IndividualPoolStake
forall a b. (EncCBOR a, DecCBOR b) => Trip a b
cborTrip
            (\IndividualPoolStake
decoded IndividualPoolStake
original -> IndividualPoolStake
decoded IndividualPoolStake -> IndividualPoolStake -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` IndividualPoolStake
original {individualPoolStakeBls = SNothing})
            IndividualPoolStake
ips {individualPoolStakeBls = SJust blsKey}