From b6b227382d046729b6dd0b5e767d97a6b80a3007 Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Fri, 3 Nov 2023 22:41:18 +0300 Subject: [PATCH] feat: add version to /help (#50) --- cmd/cosmos-proposals-checker.go | 10 ++++---- pkg/app.go | 4 ++-- pkg/config/config_test.go | 38 ++++++++++++++++-------------- pkg/mutes/mutes_test.go | 28 +++++++++++----------- pkg/report/generator_test.go | 20 ++++++++-------- pkg/reporters/telegram/help.go | 2 +- pkg/reporters/telegram/telegram.go | 4 ++++ pkg/state/state_test.go | 36 ++++++++++++++-------------- templates/telegram/help.html | 2 +- 9 files changed, 75 insertions(+), 69 deletions(-) diff --git a/cmd/cosmos-proposals-checker.go b/cmd/cosmos-proposals-checker.go index 6832487..82f34c2 100644 --- a/cmd/cosmos-proposals-checker.go +++ b/cmd/cosmos-proposals-checker.go @@ -7,15 +7,15 @@ import ( "github.com/spf13/cobra" ) -func Execute(configPath string) { - app := pkg.NewApp(configPath) - app.Start() -} - var ( version = "unknown" ) +func Execute(configPath string) { + app := pkg.NewApp(configPath, version) + app.Start() +} + func main() { var ConfigPath string diff --git a/pkg/app.go b/pkg/app.go index de152b5..b04b7c8 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -24,7 +24,7 @@ type App struct { Reporters []reportersPkg.Reporter } -func NewApp(configPath string) *App { +func NewApp(configPath string, version string) *App { config, err := configPkg.GetConfig(configPath) if err != nil { logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config") @@ -50,7 +50,7 @@ func NewApp(configPath string) *App { StateGenerator: stateGenerator, Reporters: []reportersPkg.Reporter{ pagerduty.NewPagerDutyReporter(config.PagerDutyConfig, log), - telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, log), + telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, log, version), }, } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index adab1aa..67fa2af 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -3,6 +3,8 @@ package config import ( "testing" + "github.com/stretchr/testify/require" + configTypes "main/pkg/config/types" "github.com/stretchr/testify/assert" @@ -16,7 +18,7 @@ func TestValidateChainWithEmptyName(t *testing.T) { } err := chain.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateChainWithoutEndpoints(t *testing.T) { @@ -28,7 +30,7 @@ func TestValidateChainWithoutEndpoints(t *testing.T) { } err := chain.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateChainWithoutWallets(t *testing.T) { @@ -41,7 +43,7 @@ func TestValidateChainWithoutWallets(t *testing.T) { } err := chain.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateChainWithValidConfig(t *testing.T) { @@ -55,7 +57,7 @@ func TestValidateChainWithValidConfig(t *testing.T) { } err := chain.Validate() - assert.Equal(t, err, nil, "Error should not be presented!") + require.NoError(t, err, "Error should not be presented!") } func TestChainGetNameWithoutPrettyName(t *testing.T) { @@ -66,8 +68,8 @@ func TestChainGetNameWithoutPrettyName(t *testing.T) { PrettyName: "", } - err := chain.GetName() - assert.Equal(t, err, "chain", "Chain name should match!") + name := chain.GetName() + assert.Equal(t, "chain", name, "Chain name should match!") } func TestChainGetNameWithPrettyName(t *testing.T) { @@ -79,7 +81,7 @@ func TestChainGetNameWithPrettyName(t *testing.T) { } err := chain.GetName() - assert.Equal(t, err, "chain-pretty", "Chain name should match!") + assert.Equal(t, "chain-pretty", err, "Chain name should match!") } func TestValidateConfigNoChains(t *testing.T) { @@ -89,7 +91,7 @@ func TestValidateConfigNoChains(t *testing.T) { Chains: []*configTypes.Chain{}, } err := config.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateConfigInvalidChain(t *testing.T) { @@ -103,7 +105,7 @@ func TestValidateConfigInvalidChain(t *testing.T) { }, } err := config.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateConfigWrongProposalType(t *testing.T) { @@ -120,7 +122,7 @@ func TestValidateConfigWrongProposalType(t *testing.T) { }, } err := config.Validate() - assert.NotEqual(t, err, nil, "Error should be presented!") + require.Error(t, err, nil, "Error should be presented!") } func TestValidateConfigValidChain(t *testing.T) { @@ -137,7 +139,7 @@ func TestValidateConfigValidChain(t *testing.T) { }, } err := config.Validate() - assert.Equal(t, err, nil, "Error should not be presented!") + require.NoError(t, err, "Error should not be presented!") } func TestFindChainByNameIfPresent(t *testing.T) { @@ -149,7 +151,7 @@ func TestFindChainByNameIfPresent(t *testing.T) { } chain := chains.FindByName("chain2") - assert.NotEqual(t, chain, nil, "Chain should be presented!") + assert.NotNil(t, chain, "Chain should be presented!") } func TestFindChainByNameIfNotPresent(t *testing.T) { @@ -170,7 +172,7 @@ func TestGetLinksEmpty(t *testing.T) { chain := configTypes.Chain{} links := chain.GetExplorerProposalsLinks("test") - assert.Equal(t, len(links), 0, "Expected 0 links") + assert.Empty(t, links, "Expected 0 links") } func TestGetLinksPresent(t *testing.T) { @@ -184,9 +186,9 @@ func TestGetLinksPresent(t *testing.T) { } links := chain.GetExplorerProposalsLinks("test") - assert.Equal(t, len(links), 2, "Expected 2 links") - assert.Equal(t, links[0].Name, "Keplr", "Expected Keplr link") - assert.Equal(t, links[0].Href, "https://wallet.keplr.app/#/chain/governance?detailId=test", "Wrong Keplr link") - assert.Equal(t, links[1].Name, "Explorer", "Expected Explorer link") - assert.Equal(t, links[1].Href, "example.com/proposal/test", "Wrong explorer link") + assert.Len(t, links, 2, "Expected 2 links") + assert.Equal(t, "Keplr", links[0].Name, "Expected Keplr link") + assert.Equal(t, "https://wallet.keplr.app/#/chain/governance?detailId=test", links[0].Href, "Wrong Keplr link") + assert.Equal(t, "Explorer", links[1].Name, "Expected Explorer link") + assert.Equal(t, "example.com/proposal/test", links[1].Href, "Wrong explorer link") } diff --git a/pkg/mutes/mutes_test.go b/pkg/mutes/mutes_test.go index ce955fa..71ab1fc 100644 --- a/pkg/mutes/mutes_test.go +++ b/pkg/mutes/mutes_test.go @@ -12,7 +12,7 @@ func TestMuteMatchesNoParams(t *testing.T) { mute := &Mute{} muted := mute.Matches("chain", "proposal") - assert.Equal(t, muted, true, "Mute should match!") + assert.True(t, muted, "Mute should match!") } func TestMuteMatchesWithChainSpecified(t *testing.T) { @@ -20,9 +20,9 @@ func TestMuteMatchesWithChainSpecified(t *testing.T) { mute := &Mute{Chain: "chain"} muted := mute.Matches("chain", "proposal") - assert.Equal(t, muted, true, "Mute should match!") + assert.True(t, muted, "Mute should match!") muted2 := mute.Matches("chain2", "proposal") - assert.Equal(t, muted2, false, "Mute should not match!") + assert.False(t, muted2, "Mute should not match!") } func TestMuteMatchesWithProposalSpecified(t *testing.T) { @@ -30,9 +30,9 @@ func TestMuteMatchesWithProposalSpecified(t *testing.T) { mute := &Mute{ProposalID: "proposal"} muted := mute.Matches("chain", "proposal") - assert.Equal(t, muted, true, "Mute should match!") + assert.True(t, muted, "Mute should match!") muted2 := mute.Matches("chain", "proposal2") - assert.Equal(t, muted2, false, "Mute should not match!") + assert.False(t, muted2, "Mute should not match!") } func TestMuteMatchesWithAllSpecified(t *testing.T) { @@ -40,11 +40,11 @@ func TestMuteMatchesWithAllSpecified(t *testing.T) { mute := &Mute{Chain: "chain", ProposalID: "proposal"} muted := mute.Matches("chain", "proposal") - assert.Equal(t, muted, true, "Mute should match!") + assert.True(t, muted, "Mute should match!") muted2 := mute.Matches("chain", "proposal2") - assert.Equal(t, muted2, false, "Mute should not match!") + assert.False(t, muted2, "Mute should not match!") muted3 := mute.Matches("chain2", "proposal") - assert.Equal(t, muted3, false, "Mute should not match!") + assert.False(t, muted3, "Mute should not match!") } func TestMutesMatchesIgnoreExpired(t *testing.T) { @@ -56,7 +56,7 @@ func TestMutesMatchesIgnoreExpired(t *testing.T) { }, } muted := mutes.IsMuted("chain", "proposal") - assert.Equal(t, muted, false, "Mute should not be muted!") + assert.False(t, muted, "Mute should not be muted!") } func TestMutesMatchesNotIgnoreActual(t *testing.T) { @@ -68,7 +68,7 @@ func TestMutesMatchesNotIgnoreActual(t *testing.T) { }, } muted := mutes.IsMuted("chain", "proposal") - assert.Equal(t, muted, true, "Mute should be muted!") + assert.True(t, muted, "Mute should be muted!") } func TestMutesAddsMute(t *testing.T) { @@ -81,8 +81,8 @@ func TestMutesAddsMute(t *testing.T) { } mutes.AddMute(&Mute{Chain: "chain2", Expires: time.Now().Add(time.Hour)}) - assert.Equal(t, len(mutes.Mutes), 1, "There should be 1 mute!") - assert.Equal(t, mutes.Mutes[0].Chain, "chain2", "Chain name should match!") + assert.Len(t, mutes.Mutes, 1, "There should be 1 mute!") + assert.Equal(t, "chain2", mutes.Mutes[0].Chain, "Chain name should match!") } func TestMutesDeletesMute(t *testing.T) { @@ -95,6 +95,6 @@ func TestMutesDeletesMute(t *testing.T) { } mutes.AddMute(&Mute{Chain: "chain2", Expires: time.Now().Add(time.Hour)}) - assert.Equal(t, len(mutes.Mutes), 1, "There should be 1 mute!") - assert.Equal(t, mutes.Mutes[0].Chain, "chain2", "Chain name should match!") + assert.Len(t, mutes.Mutes, 1, "There should be 1 mute!") + assert.Equal(t, "chain2", mutes.Mutes[0].Chain, "Chain name should match!") } diff --git a/pkg/report/generator_test.go b/pkg/report/generator_test.go index bb79634..e486a22 100644 --- a/pkg/report/generator_test.go +++ b/pkg/report/generator_test.go @@ -31,11 +31,11 @@ func TestReportGeneratorWithProposalError(t *testing.T) { }) report := generator.GenerateReport(oldState, newState) - assert.Equal(t, len(report.Entries), 1, "Expected to have 1 entry!") + assert.Len(t, report.Entries, 1, "Expected to have 1 entry!") entry, ok := report.Entries[0].(events.ProposalsQueryErrorEvent) assert.True(t, ok, "Expected to have a proposal query error!") - assert.Equal(t, entry.Error.Error(), "test error", "Error text mismatch!") + assert.Equal(t, "test error", entry.Error.Error(), "Error text mismatch!") } func TestReportGeneratorWithVoteError(t *testing.T) { @@ -68,11 +68,11 @@ func TestReportGeneratorWithVoteError(t *testing.T) { }) report := generator.GenerateReport(oldState, newState) - assert.Equal(t, len(report.Entries), 1, "Expected to have 1 entry!") + assert.Len(t, report.Entries, 1, "Expected to have 1 entry!") entry, ok := report.Entries[0].(events.VoteQueryError) assert.True(t, ok, "Expected to have a vote query error!") - assert.Equal(t, entry.Proposal.ID, "proposal", "Proposal ID mismatch!") + assert.Equal(t, "proposal", entry.Proposal.ID, "Proposal ID mismatch!") } func TestReportGeneratorWithNotVoted(t *testing.T) { @@ -103,11 +103,11 @@ func TestReportGeneratorWithNotVoted(t *testing.T) { }) report := generator.GenerateReport(oldState, newState) - assert.Equal(t, len(report.Entries), 1, "Expected to have 1 entry!") + assert.Len(t, report.Entries, 1, "Expected to have 1 entry!") entry, ok := report.Entries[0].(events.NotVotedEvent) assert.True(t, ok, "Expected to have not voted type!") - assert.Equal(t, entry.Proposal.ID, "proposal", "Proposal ID mismatch!") + assert.Equal(t, "proposal", entry.Proposal.ID, "Proposal ID mismatch!") } func TestReportGeneratorWithVoted(t *testing.T) { @@ -157,11 +157,11 @@ func TestReportGeneratorWithVoted(t *testing.T) { }) report := generator.GenerateReport(oldState, newState) - assert.Equal(t, len(report.Entries), 1, "Expected to have 1 entry!") + assert.Len(t, report.Entries, 1, "Expected to have 1 entry!") entry, ok := report.Entries[0].(events.VotedEvent) assert.True(t, ok, "Expected to have voted type!") - assert.Equal(t, entry.Proposal.ID, "proposal", "Proposal ID mismatch!") + assert.Equal(t, "proposal", entry.Proposal.ID, "Proposal ID mismatch!") } func TestReportGeneratorWithRevoted(t *testing.T) { @@ -215,9 +215,9 @@ func TestReportGeneratorWithRevoted(t *testing.T) { }) report := generator.GenerateReport(oldState, newState) - assert.Equal(t, len(report.Entries), 1, "Expected to have 1 entry!") + assert.Len(t, report.Entries, 1, "Expected to have 1 entry!") entry, ok := report.Entries[0].(events.RevotedEvent) assert.True(t, ok, "Expected to have revoted type!") - assert.Equal(t, entry.Proposal.ID, "proposal", "Proposal ID mismatch!") + assert.Equal(t, "proposal", entry.Proposal.ID, "Proposal ID mismatch!") } diff --git a/pkg/reporters/telegram/help.go b/pkg/reporters/telegram/help.go index a280f13..394f520 100644 --- a/pkg/reporters/telegram/help.go +++ b/pkg/reporters/telegram/help.go @@ -14,7 +14,7 @@ func (reporter *Reporter) HandleHelp(c tele.Context) error { template, _ := reporter.GetTemplate("help") var buffer bytes.Buffer - if err := template.Execute(&buffer, nil); err != nil { + if err := template.Execute(&buffer, reporter.Version); err != nil { reporter.Logger.Error().Err(err).Msg("Error rendering help template") return err } diff --git a/pkg/reporters/telegram/telegram.go b/pkg/reporters/telegram/telegram.go index 2efe63e..415312b 100644 --- a/pkg/reporters/telegram/telegram.go +++ b/pkg/reporters/telegram/telegram.go @@ -28,6 +28,8 @@ type Reporter struct { TelegramBot *tele.Bot Logger zerolog.Logger Templates map[string]*template.Template + + Version string } const ( @@ -39,6 +41,7 @@ func NewTelegramReporter( mutesManager *mutes.Manager, stateGenerator *state.Generator, logger *zerolog.Logger, + version string, ) *Reporter { return &Reporter{ TelegramToken: config.TelegramToken, @@ -47,6 +50,7 @@ func NewTelegramReporter( StateGenerator: stateGenerator, Logger: logger.With().Str("component", "telegram_reporter").Logger(), Templates: make(map[string]*template.Template, 0), + Version: version, } } diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index f88fedc..0ccc028 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -15,7 +15,7 @@ func TestSetVoteWithoutChainInfo(t *testing.T) { state := NewState() _, _, found := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found, false, "Vote should not be presented!") + assert.False(t, found, "Vote should not be presented!") state.SetVote( &configTypes.Chain{Name: "chain"}, @@ -29,9 +29,9 @@ func TestSetVoteWithoutChainInfo(t *testing.T) { ) vote, _, found2 := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found2, true, "Vote should be presented!") - assert.Equal(t, vote.HasVoted(), true, "Vote should be presented!") - assert.Equal(t, vote.IsError(), false, "There should be no error!") + assert.True(t, found2, "Vote should be presented!") + assert.True(t, vote.HasVoted(), "Vote should be presented!") + assert.False(t, vote.IsError(), "There should be no error!") } func TestSetProposalErrorWithoutChainInfo(t *testing.T) { @@ -41,10 +41,10 @@ func TestSetProposalErrorWithoutChainInfo(t *testing.T) { state.SetChainProposalsError(&configTypes.Chain{Name: "test"}, errors.New("test error")) hasError2 := state.ChainInfos["test"].HasProposalsError() - assert.Equal(t, hasError2, true, "Chain info should have a proposal error!") + assert.True(t, hasError2, "Chain info should have a proposal error!") err := state.ChainInfos["test"].ProposalsError - assert.Equal(t, err.Error(), "test error", "Errors text should match!") + assert.Equal(t, "test error", err.Error(), "Errors text should match!") } func TestSetVotes(t *testing.T) { @@ -82,15 +82,15 @@ func TestSetProposalErrorWithChainInfo(t *testing.T) { } hasError := state.ChainInfos["test"].HasProposalsError() - assert.Equal(t, hasError, false, "Chain info should not have a proposal error!") + assert.False(t, hasError, "Chain info should not have a proposal error!") state.SetChainProposalsError(&configTypes.Chain{Name: "test"}, errors.New("test error")) hasError2 := state.ChainInfos["test"].HasProposalsError() - assert.Equal(t, hasError2, true, "Chain info should have a proposal error!") + assert.True(t, hasError2, "Chain info should have a proposal error!") err := state.ChainInfos["test"].ProposalsError - assert.Equal(t, err.Error(), "test error", "Errors text should match!") + assert.Equal(t, "test error", err.Error(), "Errors text should match!") } func TestGetVoteWithoutChainInfo(t *testing.T) { @@ -99,7 +99,7 @@ func TestGetVoteWithoutChainInfo(t *testing.T) { state := State{} _, _, found := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found, false, "There should be no vote!") + assert.False(t, found, "There should be no vote!") } func TestGetVoteWithoutProposalVotes(t *testing.T) { @@ -114,7 +114,7 @@ func TestGetVoteWithoutProposalVotes(t *testing.T) { } _, _, found := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found, false, "There should be no vote!") + assert.False(t, found, "There should be no vote!") } func TestGetVoteWithWalletVoteNotPresent(t *testing.T) { @@ -131,7 +131,7 @@ func TestGetVoteWithWalletVoteNotPresent(t *testing.T) { } _, _, found := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found, false, "There should be no vote!") + assert.False(t, found, "There should be no vote!") } func TestGetVoteWithWalletVotePresent(t *testing.T) { @@ -152,7 +152,7 @@ func TestGetVoteWithWalletVotePresent(t *testing.T) { } _, _, found := state.GetVoteAndProposal("chain", "proposal", "wallet") - assert.Equal(t, found, true, "There should be a vote!") + assert.True(t, found, "There should be a vote!") } func TestHasVotedWithoutChainInfo(t *testing.T) { @@ -161,7 +161,7 @@ func TestHasVotedWithoutChainInfo(t *testing.T) { state := State{} voted := state.HasVoted("chain", "proposal", "wallet") - assert.Equal(t, voted, false, "There should be no vote!") + assert.False(t, voted, "There should be no vote!") } func TestHasVotedWithChainInfo(t *testing.T) { @@ -174,7 +174,7 @@ func TestHasVotedWithChainInfo(t *testing.T) { } voted := state.HasVoted("chain", "proposal", "wallet") - assert.Equal(t, voted, false, "There should be no vote!") + assert.False(t, voted, "There should be no vote!") } func TestHasVotedWithWalletVoteIntoNotPresent(t *testing.T) { @@ -191,7 +191,7 @@ func TestHasVotedWithWalletVoteIntoNotPresent(t *testing.T) { } voted := state.HasVoted("chain", "proposal", "wallet") - assert.Equal(t, voted, false, "There should be no vote!") + assert.False(t, voted, "There should be no vote!") } func TestHasVotedWithWalletVoteInfoPresent(t *testing.T) { @@ -212,7 +212,7 @@ func TestHasVotedWithWalletVoteInfoPresent(t *testing.T) { } voted := state.HasVoted("chain", "proposal", "wallet") - assert.Equal(t, voted, false, "There should be no vote!") + assert.False(t, voted, "There should be no vote!") } func TestHasVotedWithWalletVotePresent(t *testing.T) { @@ -237,5 +237,5 @@ func TestHasVotedWithWalletVotePresent(t *testing.T) { } voted := state.HasVoted("chain", "proposal", "wallet") - assert.Equal(t, voted, true, "There should be a vote!") + assert.True(t, voted, "There should be a vote!") } diff --git a/templates/telegram/help.html b/templates/telegram/help.html index 3e8240a..041d3c1 100644 --- a/templates/telegram/help.html +++ b/templates/telegram/help.html @@ -1,4 +1,4 @@ -cosmos-proposals-checker +cosmos-proposals-checker v{{ . }} Notifies you about the proposals your wallets hasn't voted upon. Can understand the following commands: - /proposals - displays active proposals and your wallets' votes on them