Skip to content

Commit

Permalink
Add a test for 'observableStateChange' stream
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed May 4, 2021
1 parent b30fe5d commit 83e2fa5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions plutus-pab/src/Plutus/PAB/Webserver/WebSocket.hs
Expand Up @@ -29,6 +29,7 @@ module Plutus.PAB.Webserver.WebSocket
, walletFundsChange
, openEndpoints
, slotChange
, observableStateChange
) where

import qualified Cardano.Wallet.Mock as Mock
Expand Down
17 changes: 17 additions & 0 deletions plutus-pab/test/Plutus/PAB/CoreSpec.hs
Expand Up @@ -12,6 +12,7 @@ module Plutus.PAB.CoreSpec
( tests
, stopContractInstanceTest
, walletFundsChangeTest
, observableStateChangeTest
) where

import Control.Lens ((&), (+~))
Expand All @@ -29,6 +30,7 @@ import Data.Foldable (fold, traverse_)
import qualified Data.Aeson.Types as JSON
import Data.Either (isRight)
import qualified Data.Map as Map
import Data.Maybe (isJust)
import qualified Data.Monoid as M
import Data.Proxy (Proxy (..))
import Data.Semigroup (Last (..))
Expand Down Expand Up @@ -111,6 +113,7 @@ executionTests =
, testCase "stop contract instance" stopContractInstanceTest
, testCase "can subscribe to slot updates" slotChangeTest
, testCase "can subscribe to wallet funds changes" walletFundsChangeTest
, testCase "can subscribe to observable state changes" observableStateChangeTest
]

waitForUpdateTest :: IO ()
Expand Down Expand Up @@ -169,6 +172,20 @@ walletFundsChangeTest = runScenario $ do
vl2 <- liftIO (WS.readN 1 stream2) >>= \case { [newVal] -> pure newVal; _ -> throwError (OtherError "newVal not found")}
assertEqual "generated wallet should receive a payment" payment vl2

observableStateChangeTest :: IO ()
observableStateChangeTest = runScenario $ do
let getCurrency :: JSON.Value -> Maybe OneShotCurrency
getCurrency vl = do
case JSON.parseEither JSON.parseJSON vl of
Right (Just (Last cur)) -> Just cur
_ -> Nothing
env <- Core.askInstancesState @(Builtin TestContracts) @(Simulator.SimulatorState (Builtin TestContracts))
instanceId <- Simulator.activateContract defaultWallet Currency
createCurrency instanceId SimpleMPS{tokenName="my token", amount = 10000}
let stream = WS.observableStateChange instanceId env
vl2 <- liftIO (WS.readN 2 stream) >>= \case { [_, newVal] -> pure newVal; _ -> throwError (OtherError "newVal not found")}
assertBool "observable state should change" (isJust $ getCurrency vl2)

currencyTest :: TestTree
currencyTest =
let mps = SimpleMPS{tokenName="my token", amount = 10000}
Expand Down
4 changes: 2 additions & 2 deletions plutus-tx/src/PlutusTx/AssocMap.hs
Expand Up @@ -39,13 +39,13 @@ import PlutusTx.Lift (makeLift)
import PlutusTx.Prelude hiding (all, lookup, null, toList)
import qualified PlutusTx.Prelude as P
import PlutusTx.These
import qualified Prelude

{-# ANN module ("HLint: ignore Use newtype instead of data"::String) #-}

-- | A 'Map' of key-value pairs.
newtype Map k v = Map { unMap :: [(k, v)] }
deriving (Show)
deriving stock (Generic)
deriving stock (Generic, Prelude.Eq, Show)
deriving newtype (Eq, Ord, IsData, NFData)

instance Functor (Map k) where
Expand Down
2 changes: 1 addition & 1 deletion plutus-use-cases/src/Plutus/Contracts/Currency.hs
Expand Up @@ -69,7 +69,7 @@ data OneShotCurrency = OneShotCurrency
-- ^ How many units of each 'TokenName' are to
-- be forged.
}
deriving stock (Generic, Prelude.Show)
deriving stock (Generic, Prelude.Show, Prelude.Eq)
deriving anyclass (ToJSON, FromJSON)

PlutusTx.makeLift ''OneShotCurrency
Expand Down

0 comments on commit 83e2fa5

Please sign in to comment.