From ec67e957a4ed4a4bb470f99fa850f6907eaf14dc Mon Sep 17 00:00:00 2001 From: mdrakos Date: Fri, 15 Nov 2024 10:45:40 -0800 Subject: [PATCH] Update messages with time functionality --- cmd/state-svc/internal/messages/condition.go | 17 +++++ cmd/state-svc/internal/messages/messages.go | 2 +- .../internal/messages/messages_test.go | 74 +++++++++++++++---- test/integration/msg_int_test.go | 68 ++++++++++++++++- 4 files changed, 143 insertions(+), 18 deletions(-) diff --git a/cmd/state-svc/internal/messages/condition.go b/cmd/state-svc/internal/messages/condition.go index 5e39e8326e..d6b0a4903e 100644 --- a/cmd/state-svc/internal/messages/condition.go +++ b/cmd/state-svc/internal/messages/condition.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" "text/template" + "time" "github.com/ActiveState/cli/internal/multilog" "github.com/ActiveState/cli/pkg/sysinfo" @@ -54,6 +55,22 @@ func conditionFuncMap() template.FuncMap { "contains": funk.Contains, "hasSuffix": strings.HasSuffix, "hasPrefix": strings.HasPrefix, + "dateBefore": func(date string) bool { + parsedDate, err := time.Parse(time.RFC3339, date) + if err != nil { + multilog.Error("Messages: Could not parse date: %s", err) + return false + } + return time.Now().Before(parsedDate) + }, + "dateAfter": func(date string) bool { + parsedDate, err := time.Parse(time.RFC3339, date) + if err != nil { + multilog.Error("Messages: Could not parse date: %s", err) + return false + } + return time.Now().After(parsedDate) + }, "regexMatch": func(str, pattern string) bool { rx, err := regexp.Compile(pattern) if err != nil { diff --git a/cmd/state-svc/internal/messages/messages.go b/cmd/state-svc/internal/messages/messages.go index 1495a8a1b2..feb5283666 100644 --- a/cmd/state-svc/internal/messages/messages.go +++ b/cmd/state-svc/internal/messages/messages.go @@ -44,7 +44,7 @@ func New(cfg *config.Instance, auth *auth.Auth) (*Messages, error) { return nil, errs.Wrap(err, "Could not parse state version") } - poll := poller.New(1*time.Hour, func() (interface{}, error) { + poll := poller.New(10*time.Minute, func() (interface{}, error) { defer func() { panics.LogAndPanic(recover(), debug.Stack()) }() diff --git a/cmd/state-svc/internal/messages/messages_test.go b/cmd/state-svc/internal/messages/messages_test.go index ffaf24f92a..2041bbf315 100644 --- a/cmd/state-svc/internal/messages/messages_test.go +++ b/cmd/state-svc/internal/messages/messages_test.go @@ -155,6 +155,48 @@ func Test_check(t *testing.T) { []string{"A", "B", "C", "D"}, false, }, + { + "Date Condition before", + args{ + params: &ConditionParams{}, + messages: []*graph.MessageInfo{ + {ID: "A", Condition: `dateBefore "2025-11-15T14:45:28Z"`}, + {ID: "B", Condition: `dateBefore "2015-11-15T14:45:28Z"`}, + }, + lastReportMap: map[string]interface{}{}, + baseTime: time.Now(), + }, + []string{"A"}, + false, + }, + { + "Date Condition after", + args{ + params: &ConditionParams{}, + messages: []*graph.MessageInfo{ + {ID: "A", Condition: `dateAfter "2015-11-15T14:45:28Z"`}, + {ID: "B", Condition: `dateAfter "2025-11-15T14:45:28Z"`}, + }, + lastReportMap: map[string]interface{}{}, + baseTime: time.Now(), + }, + []string{"A"}, + false, + }, + { + "Condition Date Range", + args{ + params: &ConditionParams{}, + messages: []*graph.MessageInfo{ + {ID: "A", Condition: `and (dateAfter "2015-11-15T14:45:28Z") (dateBefore "2025-11-15T14:45:28Z")`}, + {ID: "B", Condition: `and (dateAfter "2025-11-15T14:45:28Z") (dateBefore "2035-11-15T14:45:28Z")`}, + }, + lastReportMap: map[string]interface{}{}, + baseTime: time.Now(), + }, + []string{"A"}, + false, + }, { "Repeat Disabled", args{ @@ -165,8 +207,8 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeDisabled}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "C": time.Now(), + "A": time.Now().Format(time.RFC3339), + "C": time.Now().Format(time.RFC3339), }, baseTime: time.Now(), }, @@ -183,8 +225,8 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeConstantly}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "C": time.Now().Add(-time.Hour * 24 * 30), + "A": time.Now().Format(time.RFC3339), + "C": time.Now().Add(-time.Hour * 24 * 30).Format(time.RFC3339), }, baseTime: time.Now(), }, @@ -201,9 +243,9 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "B": time.Now().Add(-time.Hour), - "C": time.Now(), + "A": time.Now().Format(time.RFC3339), + "B": time.Now().Add(-time.Hour).Format(time.RFC3339), + "C": time.Now().Format(time.RFC3339), }, baseTime: time.Now(), }, @@ -220,9 +262,9 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "B": time.Now().Add(-time.Hour * 24), - "C": time.Now(), + "A": time.Now().Format(time.RFC3339), + "B": time.Now().Add(-time.Hour * 24).Format(time.RFC3339), + "C": time.Now().Format(time.RFC3339), }, baseTime: time.Now(), }, @@ -239,9 +281,9 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "B": time.Now().Add(-time.Hour * 24 * 7), - "C": time.Now(), + "A": time.Now().Format(time.RFC3339), + "B": time.Now().Add(-time.Hour * 24 * 7).Format(time.RFC3339), + "C": time.Now().Format(time.RFC3339), }, baseTime: time.Now(), }, @@ -258,9 +300,9 @@ func Test_check(t *testing.T) { {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, }, lastReportMap: map[string]interface{}{ - "A": time.Now(), - "B": time.Now().Add(-time.Hour * 24 * 7 * 30), - "C": time.Now(), + "A": time.Now().Format(time.RFC3339), + "B": time.Now().Add(-time.Hour * 24 * 7 * 30).Format(time.RFC3339), + "C": time.Now().Format(time.RFC3339), }, baseTime: time.Now(), }, diff --git a/test/integration/msg_int_test.go b/test/integration/msg_int_test.go index 0224d3ad3d..75db378b6a 100644 --- a/test/integration/msg_int_test.go +++ b/test/integration/msg_int_test.go @@ -36,6 +36,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() { Name string MessageJson string ExpectRepeat bool + ExpectShown bool }{ { "Defaults", @@ -44,6 +45,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() { "Message": "This is a [NOTICE]simple[/RESET] message" }]`, false, + true, }, { "Repeat Hourly", @@ -53,6 +55,7 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() { "Repeat": "Hourly" }]`, false, + true, }, { "Repeat Constantly", @@ -62,6 +65,67 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() { "Repeat": "Constantly" }]`, true, + true, + }, + { + "Condition DateAfter Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "dateAfter \"2015-11-15T14:45:28Z\"" + }]`, + false, + true, + }, + { + "Condition DateAfter Not Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "dateAfter \"2025-11-15T14:45:28Z\"" + }]`, + false, + false, + }, + { + "Condition DateBefore Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "dateBefore \"2025-11-15T14:45:28Z\"" + }]`, + false, + false, + }, + { + "Condition DateBefore Not Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "dateBefore \"2015-11-15T14:45:28Z\"" + }]`, + false, + false, + }, + { + "Condition Date Range Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "and (dateAfter \"2015-11-15T14:45:28Z\") (dateBefore \"2025-11-15T14:45:28Z\")" + }]`, + false, + true, + }, + { + "Condition Date Range Not Shown", + `[{ + "ID": "simple", + "Message": "This is a [NOTICE]simple[/RESET] message", + "Condition": "and (dateAfter \"2025-11-15T14:45:28Z\") (dateBefore \"2035-11-15T14:45:28Z\")" + }]`, + false, + false, }, } for _, tt := range tests { @@ -73,7 +137,9 @@ func (suite *MsgIntegrationTestSuite) TestMessage_Basic() { suite.Require().NoError(err) cp := ts.SpawnWithOpts(e2e.OptArgs("--version"), e2e.OptAppendEnv(constants.MessagesOverrideEnvVarName+"="+msgFile)) - cp.Expect(`This is a simple message`) + if tt.ExpectShown { + cp.Expect(`This is a simple message`) + } cp.Expect("ActiveState CLI by ActiveState Software Inc.") cp.ExpectExitCode(0)