mukan-sdk/x/bank/client/cli/tx_test.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

251 lines
6.2 KiB
Go

package cli_test
import (
"context"
"fmt"
"io"
"testing"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
)
type CLITestSuite struct {
suite.Suite
kr keyring.Keyring
encCfg testutilmod.TestEncodingConfig
baseCtx client.Context
}
func TestCLITestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}
func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(bank.AppModuleBasic{})
s.kr = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = client.Context{}.
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard)
}
func (s *CLITestSuite) TestSendTxCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewSendTxCmd(address.NewBech32Codec("cosmos"))
cmd.SetOut(io.Discard)
extraArgs := []string{
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=test-chain", flags.FlagChainID),
}
testCases := []struct {
name string
ctxGen func() client.Context
from, to sdk.AccAddress
amount sdk.Coins
extraArgs []string
expectErrMsg string
}{
{
"valid transaction",
func() client.Context {
return s.baseCtx
},
accounts[0].Address,
accounts[0].Address,
sdk.NewCoins(
sdk.NewCoin("stake", sdkmath.NewInt(10)),
sdk.NewCoin("photon", sdkmath.NewInt(40)),
),
extraArgs,
"",
},
{
"invalid to Address",
func() client.Context {
return s.baseCtx
},
accounts[0].Address,
sdk.AccAddress{},
sdk.NewCoins(
sdk.NewCoin("stake", sdkmath.NewInt(10)),
sdk.NewCoin("photon", sdkmath.NewInt(40)),
),
extraArgs,
"empty address string is not allowed",
},
{
"invalid coins",
func() client.Context {
return s.baseCtx
},
accounts[0].Address,
accounts[0].Address,
nil,
extraArgs,
"invalid coins",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
args := append([]string{tc.from.String(), tc.to.String(), tc.amount.String()}, tc.extraArgs...)
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetContext(ctx)
cmd.SetArgs(args)
s.Require().NoError(client.SetCmdClientContextHandler(tc.ctxGen(), cmd))
out, err := clitestutil.ExecTestCLICmd(tc.ctxGen(), cmd, args)
if tc.expectErrMsg != "" {
s.Require().Error(err)
s.Require().Contains(out.String(), tc.expectErrMsg)
} else {
s.Require().NoError(err)
msg := &sdk.TxResponse{}
s.Require().NoError(tc.ctxGen().Codec.UnmarshalJSON(out.Bytes(), msg), out.String())
}
})
}
}
func (s *CLITestSuite) TestMultiSendTxCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 3)
cmd := cli.NewMultiSendTxCmd(address.NewBech32Codec("cosmos"))
cmd.SetOut(io.Discard)
extraArgs := []string{
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=test-chain", flags.FlagChainID),
}
testCases := []struct {
name string
ctxGen func() client.Context
from string
to []string
amount sdk.Coins
extraArgs []string
expectErrMsg string
}{
{
"valid transaction",
func() client.Context {
return s.baseCtx
},
accounts[0].Address.String(),
[]string{
accounts[1].Address.String(),
accounts[2].Address.String(),
},
sdk.NewCoins(
sdk.NewCoin("stake", sdkmath.NewInt(10)),
sdk.NewCoin("photon", sdkmath.NewInt(40)),
),
extraArgs,
"",
},
{
"invalid from Address",
func() client.Context {
return s.baseCtx
},
"foo",
[]string{
accounts[1].Address.String(),
accounts[2].Address.String(),
},
sdk.NewCoins(
sdk.NewCoin("stake", sdkmath.NewInt(10)),
sdk.NewCoin("photon", sdkmath.NewInt(40)),
),
extraArgs,
"key not found",
},
{
"invalid recipients",
func() client.Context {
return s.baseCtx
},
accounts[0].Address.String(),
[]string{
accounts[1].Address.String(),
"bar",
},
sdk.NewCoins(
sdk.NewCoin("stake", sdkmath.NewInt(10)),
sdk.NewCoin("photon", sdkmath.NewInt(40)),
),
extraArgs,
"invalid bech32 string",
},
{
"invalid amount",
func() client.Context {
return s.baseCtx
},
accounts[0].Address.String(),
[]string{
accounts[1].Address.String(),
accounts[2].Address.String(),
},
nil,
extraArgs,
"must send positive amount",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
ctx := svrcmd.CreateExecuteContext(context.Background())
var args []string
args = append(args, tc.from)
args = append(args, tc.to...)
args = append(args, tc.amount.String())
args = append(args, tc.extraArgs...)
cmd.SetContext(ctx)
cmd.SetArgs(args)
s.Require().NoError(client.SetCmdClientContextHandler(tc.ctxGen(), cmd))
out, err := clitestutil.ExecTestCLICmd(tc.ctxGen(), cmd, args)
if tc.expectErrMsg != "" {
s.Require().Error(err)
s.Require().Contains(out.String(), tc.expectErrMsg)
} else {
s.Require().NoError(err)
msg := &sdk.TxResponse{}
s.Require().NoError(tc.ctxGen().Codec.UnmarshalJSON(out.Bytes(), msg), out.String())
}
})
}
}