|
1 | 1 | module Test.Integration (test) where |
2 | 2 |
|
3 | | -import Cardano.Api (AssetId (AdaAssetId), Quantity (Quantity), TxOut (TxOut), UTxO (unUTxO), txOutValueToValue, valueToList) |
| 3 | +import Cardano.Api (AssetId (AdaAssetId), Quantity (Quantity), TxOut (TxOut), UTxO (UTxO, unUTxO), txOutValueToValue, valueToList) |
4 | 4 | import Data.Map qualified as Map |
5 | 5 | import System.Environment (setEnv) |
6 | 6 | import Test.Tasty (TestTree) |
7 | | -import Test.Tasty.HUnit (assertFailure, testCase, (@?=)) |
| 7 | +import Test.Tasty.HUnit (assertBool, assertFailure, testCase, (@?=)) |
8 | 8 | import Tools.CardanoApi (utxosAtAddress) |
9 | 9 |
|
10 | 10 | import Control.Monad.IO.Class (liftIO) |
11 | 11 | import Control.Monad.Reader (ask) |
12 | 12 | import DSL ( |
| 13 | + RunResult (RunFailed, RunSuccess), |
13 | 14 | ada, |
14 | 15 | addSomeWallet, |
15 | 16 | cardanoMainnetAddress, |
| 17 | + runContract, |
16 | 18 | runUsingCluster, |
17 | 19 | waitSeconds, |
18 | 20 | ) |
19 | 21 |
|
| 22 | +import DebugContract.GetUtxos qualified as DebugContract |
| 23 | +import DebugContract.PayToWallet qualified as DebugContract |
| 24 | + |
| 25 | +import BotInterface.Wallet (ledgerPaymentPkh) |
| 26 | + |
20 | 27 | -- FIXME: something prints node configs polluting test outputs |
21 | 28 | test :: TestTree |
22 | 29 | test = do |
23 | | - testCase "Basic integration: launch and add wallet" $ do |
| 30 | + testCase "Basic integration: launch, add wallet, tx from wallet to wallet" $ do |
24 | 31 | withTestConf . runUsingCluster $ do |
25 | | - ws <- addSomeWallet (ada 101) |
26 | | - case ws of |
27 | | - Left e -> liftIO $ assertFailure $ "Error: " <> show e |
28 | | - Right wallet -> checkFunds wallet |
| 32 | + w1 <- addSomeWallet (ada 101) >>= either (error . show) pure |
| 33 | + checkFunds w1 (ada 101) |
| 34 | + w2 <- addSomeWallet (ada 102) >>= either (error . show) pure |
| 35 | + checkFunds w2 (ada 102) |
| 36 | + |
| 37 | + assertSucceeds |
| 38 | + "Get utxos" |
| 39 | + (runContract w1 DebugContract.getUtxos) |
| 40 | + assertFails |
| 41 | + "Get utxos throwing error" |
| 42 | + (runContract w1 DebugContract.getUtxosThrowsErr) |
| 43 | + assertFails |
| 44 | + "Get utxos throwing exception" |
| 45 | + (runContract w1 DebugContract.getUtxosThrowsEx) |
| 46 | + assertFails |
| 47 | + "Pay negative amount" |
| 48 | + (runContract w1 (DebugContract.payTo (ledgerPaymentPkh w2) (-10_000_000))) |
| 49 | + |
| 50 | + checkAdaTxFromTo w1 w2 |
29 | 51 | where |
30 | | - checkFunds wallet' = |
| 52 | + checkFunds wallet' expectedAmt = do |
| 53 | + let expectedAmt' = toInteger expectedAmt |
31 | 54 | ask >>= \cEnv -> liftIO $ do |
32 | 55 | waitSeconds 2 |
33 | 56 | res <- utxosAtAddress cEnv (cardanoMainnetAddress wallet') |
34 | 57 | let resultValue = toCombinedFlatValue <$> res |
35 | | - resultValue @?= Right [(AdaAssetId, Quantity 101000000)] |
| 58 | + resultValue @?= Right [(AdaAssetId, Quantity expectedAmt')] |
| 59 | + |
| 60 | + assertSucceeds tag act = do |
| 61 | + act >>= liftIO . assertBool (tag <> " did not succeed") . isSuccess |
| 62 | + |
| 63 | + assertFails tag act = do |
| 64 | + act >>= liftIO . assertBool (tag <> " did not fail") . not . isSuccess |
| 65 | + |
| 66 | + checkAdaTxFromTo w1 w2 = do |
| 67 | + res <- runContract w1 (DebugContract.payTo (ledgerPaymentPkh w2) 10_000_000) |
| 68 | + cEnv <- ask |
| 69 | + liftIO $ do |
| 70 | + assertBool ("Wallet to wallet tx failed: " <> show res) (isSuccess res) |
| 71 | + waitSeconds 1 -- todo: some "wait tx processed" could be handy |
| 72 | + utxosAtAddress cEnv (cardanoMainnetAddress w2) |
| 73 | + >>= \case |
| 74 | + Left e -> |
| 75 | + assertFailure $ "Failed ot get wallet UTxO: " <> show e |
| 76 | + Right (UTxO utxo) -> |
| 77 | + let utxoCnt = Map.size utxo |
| 78 | + in assertBool |
| 79 | + ("Should be 2 UTxO at destination wallet, but request returned " <> show utxoCnt) |
| 80 | + (utxoCnt == 2) |
| 81 | + |
| 82 | + isSuccess = \case |
| 83 | + RunSuccess _ _ -> True |
| 84 | + RunFailed _ -> False |
36 | 85 |
|
37 | 86 | withTestConf :: IO b -> IO b |
38 | 87 | withTestConf runTest = do |
|
0 commit comments