From af07ef3584d95bc3bf2785dd740503a7a47c6188 Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 16 Jul 2024 09:31:45 +0300 Subject: [PATCH] chore: add populate multichain wallet tests --- .../populate_multichain_wallet_test.go | 232 ++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 pkg/data_fetcher/populate_multichain_wallet_test.go diff --git a/pkg/data_fetcher/populate_multichain_wallet_test.go b/pkg/data_fetcher/populate_multichain_wallet_test.go new file mode 100644 index 0000000..5ab6690 --- /dev/null +++ b/pkg/data_fetcher/populate_multichain_wallet_test.go @@ -0,0 +1,232 @@ +package data_fetcher + +import ( + aliasManagerPkg "main/pkg/alias_manager" + configPkg "main/pkg/config" + "main/pkg/config/types" + "main/pkg/fs" + loggerPkg "main/pkg/logger" + "main/pkg/metrics" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDataFetcherPopulateWalletExplorerNotPresent(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + {Name: "chain"}, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + wallet := &types.Link{Value: "address"} + + dataFetcher.PopulateWallet(config.Chains[0], wallet, "subscription") + require.Empty(t, wallet.Href) + require.Empty(t, wallet.Title) +} + +func TestDataFetcherPopulateWalletPresent(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateWallet(config.Chains[0], wallet, "subscription") + + require.Equal(t, "link address", wallet.Href) + require.Equal(t, "alias", wallet.Title) +} + +func TestDataFetcherPopulateMultichainWalletNoChannelOrPort(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateMultichainWallet(config.Chains[0], "", "", wallet, "subscription") + + require.Equal(t, "link address", wallet.Href) + require.Equal(t, "alias", wallet.Title) +} +func TestDataFetcherPopulateMultichainWalletNoRemoteChainFetched(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + dataFetcher.Cache.Set("chain_channel_channel_port_port", nil) + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateMultichainWallet(config.Chains[0], "channel", "port", wallet, "subscription") + + require.Empty(t, wallet.Href) + require.Empty(t, wallet.Title) +} + +func TestDataFetcherPopulateMultichainWalletNoLocalChainFetched(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + dataFetcher.Cache.Set("chain_channel_channel_port_port", "remote-chain") + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateMultichainWallet(config.Chains[0], "channel", "port", wallet, "subscription") + + require.Empty(t, wallet.Href) + require.Empty(t, wallet.Title) +} + +func TestDataFetcherPopulateMultichainWalletNoLocalChainExplorer(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + { + Name: "chain2", + ChainID: "remote-chain", + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + dataFetcher.Cache.Set("chain_channel_channel_port_port", "remote-chain") + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateMultichainWallet(config.Chains[0], "channel", "port", wallet, "subscription") + + require.Empty(t, wallet.Href) + require.Empty(t, wallet.Title) +} + +func TestDataFetcherPopulateMultichainWalletRemoteOk(t *testing.T) { + t.Parallel() + + config := &configPkg.AppConfig{ + Chains: types.Chains{ + { + Name: "chain", + Explorer: &types.Explorer{WalletLinkPattern: "link %s"}, + }, + { + Name: "chain2", + ChainID: "remote-chain", + Explorer: &types.Explorer{WalletLinkPattern: "another link %s"}, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager) + + dataFetcher.Cache.Set("chain_channel_channel_port_port", "remote-chain") + + err := aliasManager.Set("subscription", "chain", "address", "alias") + require.NoError(t, err) + + wallet := &types.Link{Value: "address"} + dataFetcher.PopulateMultichainWallet(config.Chains[0], "channel", "port", wallet, "subscription") + + require.Equal(t, "another link address", wallet.Href) + require.Equal(t, "alias", wallet.Title) +}