From dce19860998e83d7c9cf860c91916227ea100ddc Mon Sep 17 00:00:00 2001
From: Alfonso Acosta <alfonso@stellar.org>
Date: Fri, 8 Sep 2023 18:11:25 +0200
Subject: [PATCH] Add a few more basic CLI integration tests (#940)

---
 cmd/soroban-rpc/internal/test/cli_test.go     | 42 +++++++++++++++----
 cmd/soroban-rpc/internal/test/integration.go  |  7 ++--
 .../test/simulate_transaction_test.go         |  2 +-
 3 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/cmd/soroban-rpc/internal/test/cli_test.go b/cmd/soroban-rpc/internal/test/cli_test.go
index 9d059bad6..307b2a674 100644
--- a/cmd/soroban-rpc/internal/test/cli_test.go
+++ b/cmd/soroban-rpc/internal/test/cli_test.go
@@ -13,20 +13,45 @@ import (
 	"github.com/stellar/go/keypair"
 	"github.com/stellar/go/txnbuild"
 	"github.com/stellar/go/xdr"
+	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 )
 
-func TestInstallContractWithCLI(t *testing.T) {
+func TestCLIContractInstall(t *testing.T) {
 	NewCLITest(t)
-	output, err := runCLICommand(t, "contract install --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm")
-	require.NoError(t, err)
+	output, err := runCLICommand("contract install --wasm " + helloWorldContractPath)
+	assert.NoError(t, err)
 	wasm := getHelloWorldContract(t)
 	contractHash := xdr.Hash(sha256.Sum256(wasm))
-	require.Contains(t, string(output), contractHash.HexString())
+	require.Contains(t, output, contractHash.HexString())
+}
+
+func TestCLIContractDeploy(t *testing.T) {
+	NewCLITest(t)
+	output, err := runCLICommand("contract deploy --id 1 --wasm " + helloWorldContractPath)
+	assert.NoError(t, err)
+	require.Contains(t, output, "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM")
+}
+
+func TestCLIContractInvokeWithWasm(t *testing.T) {
+	NewCLITest(t)
+	output, err := runCLICommand(fmt.Sprintf("contract invoke --id 1 --wasm %s -- hello --world=world", helloWorldContractPath))
+	assert.NoError(t, err)
+	require.Contains(t, output, `["Hello","world"]`)
+}
+
+func TestCLIContractDeployAndInvoke(t *testing.T) {
+	NewCLITest(t)
+	output, err := runCLICommand("contract deploy --id 1 --wasm " + helloWorldContractPath)
+	assert.NoError(t, err)
+	contractID := strings.TrimSpace(output)
+	output, err = runCLICommand(fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
+	assert.NoError(t, err)
+	require.Contains(t, output, `["Hello","world"]`)
 }
 
-func runCLICommand(t *testing.T, cmd string) ([]byte, error) {
-	baseCmdArgs := []string{"run", "--", "--vv"}
+func runCLICommand(cmd string) (string, error) {
+	baseCmdArgs := []string{"run", "-q", "--", "--vv"}
 	args := strings.Split(cmd, " ")
 	args = append(baseCmdArgs, args...)
 	c := exec.Command("cargo", args...)
@@ -34,7 +59,8 @@ func runCLICommand(t *testing.T, cmd string) ([]byte, error) {
 		fmt.Sprintf("RPC_URL=http://localhost:%d/", sorobanRPCPort),
 		fmt.Sprintf("NETWORK_PASPRHASE=%s", StandaloneNetworkPassphrase),
 	)
-	return c.Output()
+	bin, err := c.CombinedOutput()
+	return string(bin), err
 }
 
 func NewCLITest(t *testing.T) *Test {
@@ -44,7 +70,7 @@ func NewCLITest(t *testing.T) *Test {
 
 	sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
 
-	// Create default account used byt the CLI
+	// Create default account used by the CLI
 	tx, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
 		SourceAccount: &txnbuild.SimpleAccount{
 			AccountID: keypair.Root(StandaloneNetworkPassphrase).Address(),
diff --git a/cmd/soroban-rpc/internal/test/integration.go b/cmd/soroban-rpc/internal/test/integration.go
index f733a14d7..bdd3e4474 100644
--- a/cmd/soroban-rpc/internal/test/integration.go
+++ b/cmd/soroban-rpc/internal/test/integration.go
@@ -32,9 +32,10 @@ const (
 	goMonorepoGithubPath        = "github.com/stellar/go"
 	friendbotURL                = "http://localhost:8000/friendbot"
 	// Needed when Core is run with ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING=true
-	checkpointFrequency = 8
-	sorobanRPCPort      = 8000
-	adminPort           = 8080
+	checkpointFrequency    = 8
+	sorobanRPCPort         = 8000
+	adminPort              = 8080
+	helloWorldContractPath = "../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm"
 )
 
 type Test struct {
diff --git a/cmd/soroban-rpc/internal/test/simulate_transaction_test.go b/cmd/soroban-rpc/internal/test/simulate_transaction_test.go
index cc6d4ed77..b9af738bb 100644
--- a/cmd/soroban-rpc/internal/test/simulate_transaction_test.go
+++ b/cmd/soroban-rpc/internal/test/simulate_transaction_test.go
@@ -36,7 +36,7 @@ var (
 func getHelloWorldContract(t *testing.T) []byte {
 	_, filename, _, _ := runtime.Caller(0)
 	testDirName := path.Dir(filename)
-	contractFile := path.Join(testDirName, "../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm")
+	contractFile := path.Join(testDirName, helloWorldContractPath)
 	ret, err := os.ReadFile(contractFile)
 	if err != nil {
 		t.Fatalf("unable to read test_hello_world.wasm (%v) please run `make build-test-wasms` at the project root directory", err)