package types_test

import (
	"errors"

	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	moduletestutil "git.cw.tr/mukan-network/mukan-sdk/types/module/testutil"

	ibc "git.cw.tr/mukan-network/mukan-ibc/modules/core"
	"git.cw.tr/mukan-network/mukan-ibc/modules/core/23-commitment/types"
	"git.cw.tr/mukan-network/mukan-ibc/modules/core/23-commitment/types/v2"
)

func (suite *MerkleTestSuite) TestCodecTypeRegistration() {
	testCases := []struct {
		name    string
		typeURL string
		expErr  error
	}{
		{
			"success: MerkleRoot",
			sdk.MsgTypeURL(&types.MerkleRoot{}),
			nil,
		},
		{
			"success: MerklePrefix",
			sdk.MsgTypeURL(&types.MerklePrefix{}),
			nil,
		},
		{
			"success: MerklePath",
			sdk.MsgTypeURL(&v2.MerklePath{}),
			nil,
		},
		{
			"type not registered on codec",
			"ibc.invalid.MsgTypeURL",
			errors.New("unable to resolve type URL ibc.invalid.MsgTypeURL"),
		},
	}

	for _, tc := range testCases {
		suite.Run(tc.name, func() {
			encodingCfg := moduletestutil.MakeTestEncodingConfig(ibc.AppModuleBasic{})
			msg, err := encodingCfg.Codec.InterfaceRegistry().Resolve(tc.typeURL)

			if tc.expErr == nil {
				suite.NotNil(msg)
				suite.Require().NoError(err)
			} else {
				suite.Nil(msg)
				suite.Require().ErrorContains(err, tc.expErr.Error())
			}
		})
	}
}
