<commit_before>{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TupleSections #-}

module PactContinuationSpec (spec) where

import qualified Control.Exception as Exception
import Control.Lens hiding ((.=))
import Control.Monad.Reader
import Data.Aeson
import Data.Decimal
import Data.Default (def)
import qualified Data.HashMap.Strict as HM
import qualified Data.List.NonEmpty as NEL
import Data.Text (Text, unpack)
import qualified Data.Text as T
import NeatInterpolation (text)
import Network.HTTP.Client (Manager)
import qualified Network.HTTP.Client as HTTP
import Prelude hiding (concat)
import Servant.Client
import System.Environment (withArgs)
import System.Timeout

import Test.Hspec

import Pact.ApiReq
import Pact.Server.API
import Pact.Server.Test
import Pact.Types.API
import Pact.Types.Command
import Pact.Types.Crypto as Crypto
import Pact.Types.PactValue (PactValue(..))
import Pact.Types.Pretty
import Pact.Types.Runtime
import Pact.Types.SPV

#if ! MIN_VERSION_servant_client(0,16,0)
type ClientError = ServantError
#endif

---- TESTS -----

spec :: Spec
spec = describe "pacts in dev server" $ do
  mgr <- runIO $ HTTP.newManager HTTP.defaultManagerSettings
  describe "testPactContinuation" $ testPactContinuation mgr
  describe "testPactRollback" $ testPactRollback mgr
  describe "testPactYield" $ testPactYield mgr
  describe "testTwoPartyEscrow" $ testTwoPartyEscrow mgr
  describe "testNestedPacts" $ testNestedPacts mgr
  describe "testManagedCaps" $ testManagedCaps mgr

_runOne :: (HTTP.Manager -> Spec) -> Spec
_runOne test = do
  mgr <- runIO $ HTTP.newManager HTTP.defaultManagerSettings
  test mgr

testManagedCaps :: HTTP.Manager -> Spec
testManagedCaps mgr = before_ flushDb $ after_ flushDb $
  it "exercises managed PAY cap" $ do
    let setupPath = testDir ++ "cont-scripts/setup-"
        testPath = testDir ++ "cont-scripts/managed-"

    (_, sysModuleCmd)  <- mkApiReq (setupPath ++ "01-system.yaml")
    (_, acctModuleCmd) <- mkApiReq (setupPath ++ "02-accounts.yaml")
    (_, createAcctCmd) <- mkApiReq (setupPath ++ "04-create.yaml")
    (_, managedPay) <- mkApiReq (testPath ++ "01-pay.yaml")
    (_, managedPayFails) <- mkApiReq (testPath ++ "02-pay-fails.yaml")
    let allCmds = [sysModuleCmd,acctModuleCmd,createAcctCmd,managedPay,managedPayFails]
    allResults <- runAll mgr allCmds

    mhash <- either (fail . show) (return . ModuleHash . Hash) $
      parseB64UrlUnpaddedText' "HniQBJ-NUJan20k4t6MiqpzhqkSsKmIzN5ef76pcLCU"

    runResults allResults $ do
      sysModuleCmd `succeedsWith` textVal "system module loaded"
      acctModuleCmd `succeedsWith` textVal "TableCreated"
      createAcctCmd `succeedsWith`  Nothing -- Alice should be funded with $100
      managedPay `succeedsWith'`
        (Just $ PactSuccess (textVal' "Transfer succeeded")
         [PactEvent "PAY"
          [textVal' "Alice",textVal' "Bobb",decValue' 0.9]
          "accounts"
          mhash])
      managedPayFails `failsWith` Just "insufficient balance"


-- | allows passing e.g. "-m CrossChain" to match only `testCrossChainYield` in ghci
_runArgs :: String -> IO ()
_runArgs args = withArgs (words args) $ hspec spec

testNestedPacts :: HTTP.Manager -> Spec
testNestedPacts mgr = before_ flushDb $ after_ flushDb $
  it "throws error when multiple defpact executions occur in same transaction" $ do
    adminKeys <- genKeys
    let makeExecCmdWith = makeExecCmd adminKeys

    moduleCmd <- makeExecCmdWith (threeStepPactCode "nestedPact")
    nestedExecPactCmd <- makeExecCmdWith ("(nestedPact.tester)" <> " (nestedPact.tester)")
    allResults <- runAll mgr [moduleCmd, nestedExecPactCmd]

    runResults allResults $ do
      moduleCmd `succeedsWith`  Nothing
      nestedExecPactCmd `failsWith` (Just "Multiple or nested pact exec found")
<commit_msg>Fix broken test<commit_after>{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TupleSections #-}

module PactContinuationSpec (spec) where

import qualified Control.Exception as Exception
import Control.Lens hiding ((.=))
import Control.Monad.Reader
import Data.Aeson
import Data.Decimal
import Data.Default (def)
import qualified Data.HashMap.Strict as HM
import qualified Data.List.NonEmpty as NEL
import Data.Text (Text, unpack)
import qualified Data.Text as T
import NeatInterpolation (text)
import Network.HTTP.Client (Manager)
import qualified Network.HTTP.Client as HTTP
import Prelude hiding (concat)
import Servant.Client
import System.Environment (withArgs)
import System.Timeout

import Test.Hspec

import Pact.ApiReq
import Pact.Server.API
import Pact.Server.Test
import Pact.Types.API
import Pact.Types.Command
import Pact.Types.Crypto as Crypto
import Pact.Types.PactValue (PactValue(..))
import Pact.Types.Pretty
import Pact.Types.Runtime
import Pact.Types.SPV

#if ! MIN_VERSION_servant_client(0,16,0)
type ClientError = ServantError
#endif

---- TESTS -----

spec :: Spec
spec = describe "pacts in dev server" $ do
  mgr <- runIO $ HTTP.newManager HTTP.defaultManagerSettings
  describe "testPactContinuation" $ testPactContinuation mgr
  describe "testPactRollback" $ testPactRollback mgr
  describe "testPactYield" $ testPactYield mgr
  describe "testTwoPartyEscrow" $ testTwoPartyEscrow mgr
  describe "testNestedPacts" $ testNestedPacts mgr
  describe "testManagedCaps" $ testManagedCaps mgr

_runOne :: (HTTP.Manager -> Spec) -> Spec
_runOne test = do
  mgr <- runIO $ HTTP.newManager HTTP.defaultManagerSettings
  test mgr

testManagedCaps :: HTTP.Manager -> Spec
testManagedCaps mgr = before_ flushDb $ after_ flushDb $
  it "exercises managed PAY cap" $ do
    let setupPath = testDir ++ "cont-scripts/setup-"
        testPath = testDir ++ "cont-scripts/managed-"

    (_, sysModuleCmd)  <- mkApiReq (setupPath ++ "01-system.yaml")
    (_, acctModuleCmd) <- mkApiReq (setupPath ++ "02-accounts.yaml")
    (_, createAcctCmd) <- mkApiReq (setupPath ++ "04-create.yaml")
    (_, managedPay) <- mkApiReq (testPath ++ "01-pay.yaml")
    (_, managedPayFails) <- mkApiReq (testPath ++ "02-pay-fails.yaml")
    let allCmds = [sysModuleCmd,acctModuleCmd,createAcctCmd,managedPay,managedPayFails]
    allResults <- runAll mgr allCmds

    mhash <- either (fail . show) (return . ModuleHash . Hash) $
      parseB64UrlUnpaddedText' "HniQBJ-NUJan20k4t6MiqpzhqkSsKmIzN5ef76pcLCU"

    runResults allResults $ do
      sysModuleCmd `succeedsWith` textVal "system module loaded"
      acctModuleCmd `succeedsWith` textVal "TableCreated"
      createAcctCmd `succeedsWith`  Nothing -- Alice should be funded with $100
      managedPay `succeedsWith'`
        (Just $ PactSuccess (textVal' "Transfer succeeded")
         [PactEvent "PAY"
          [textVal' "Alice",textVal' "Bob",decValue' 0.9]
          "accounts"
          mhash])
      managedPayFails `failsWith` Just "insufficient balance"


-- | allows passing e.g. "-m CrossChain" to match only `testCrossChainYield` in ghci
_runArgs :: String -> IO ()
_runArgs args = withArgs (words args) $ hspec spec

testNestedPacts :: HTTP.Manager -> Spec
testNestedPacts mgr = before_ flushDb $ after_ flushDb $
  it "throws error when multiple defpact executions occur in same transaction" $ do
    adminKeys <- genKeys
    let makeExecCmdWith = makeExecCmd adminKeys

    moduleCmd <- makeExecCmdWith (threeStepPactCode "nestedPact")
    nestedExecPactCmd <- makeExecCmdWith ("(nestedPact.tester)" <> " (nestedPact.tester)")
    allResults <- runAll mgr [moduleCmd, nestedExecPactCmd]

    runResults allResults $ do
      moduleCmd `succeedsWith`  Nothing
      nestedExecPactCmd `failsWith` (Just "Multiple or nested pact exec found")

{'fraction_non_alphanumeric': 0.10163807558364504, 'fraction_numerical': 0.007223010447568683, 'mean_word_length': 4.193569993302076, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '98712', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2513, 'n_words': 664}