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" "cosmossdk.io/x/auth/vesting" "cosmossdk.io/x/auth/vesting/client/cli" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" addresscodec "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" ) type CLITestSuite struct { suite.Suite kr keyring.Keyring encCfg testutilmod.TestEncodingConfig baseCtx client.Context } func TestMigrateTestSuite(t *testing.T) { suite.Run(t, new(CLITestSuite)) } func (s *CLITestSuite) SetupSuite() { s.encCfg = testutilmod.MakeTestEncodingConfig(vesting.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). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) } func (s *CLITestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() { accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) cmd := cli.NewMsgCreatePeriodicVestingAccountCmd() cmd.SetOutput(io.Discard) extraArgs := []string{ fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()), fmt.Sprintf("--%s=test-chain", flags.FlagChainID), fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address), } testCases := []struct { name string to sdk.AccAddress extraArgs []string expectErrMsg string }{ { "valid transaction", accounts[0].Address, extraArgs, "", }, { "invalid to address", sdk.AccAddress{}, extraArgs, "empty address string is not allowed", }, } for _, tc := range testCases { tc := tc s.Run(tc.name, func() { ctx := svrcmd.CreateExecuteContext(context.Background()) cmd.SetContext(ctx) cmd.SetArgs(append([]string{tc.to.String(), "./periods.json"}, tc.extraArgs...)) s.Require().NoError(client.SetCmdClientContextHandler(s.baseCtx, cmd)) err := cmd.Execute() if tc.expectErrMsg != "" { s.Require().ErrorContains(err, "empty address string is not allowed") } else { s.Require().NoError(err) } }) } }