github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/cmd/state-svc/internal/messages/messages_test.go (about) 1 package messages 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/ActiveState/cli/internal/errs" 9 "github.com/ActiveState/cli/internal/graph" 10 "github.com/blang/semver" 11 ) 12 13 func Test_check(t *testing.T) { 14 type args struct { 15 params *ConditionParams 16 messages []*graph.MessageInfo 17 lastReportMap map[string]interface{} 18 baseTime time.Time 19 } 20 tests := []struct { 21 name string 22 args args 23 wantIDs []string 24 wantErr bool 25 }{ 26 { 27 "No special conditions", 28 args{ 29 params: &ConditionParams{}, 30 messages: []*graph.MessageInfo{ 31 {ID: "A"}, {ID: "B"}, {ID: "C"}, 32 }, 33 lastReportMap: map[string]interface{}{}, 34 baseTime: time.Now(), 35 }, 36 []string{"A", "B", "C"}, 37 false, 38 }, 39 { 40 "Simple Command Condition", 41 args{ 42 params: &ConditionParams{ 43 Command: "foo", 44 }, 45 messages: []*graph.MessageInfo{ 46 {ID: "A", Condition: `eq .Command "bar"`}, 47 {ID: "B", Condition: `eq .Command "foo"`}, 48 {ID: "C", Condition: `eq .Command "foobar"`}, 49 }, 50 lastReportMap: map[string]interface{}{}, 51 baseTime: time.Now(), 52 }, 53 []string{"B"}, 54 false, 55 }, 56 { 57 "'contains' Condition", 58 args{ 59 params: &ConditionParams{ 60 UserEmail: "john@doe.org", 61 }, 62 messages: []*graph.MessageInfo{ 63 {ID: "A", Condition: `contains .UserEmail "john"`}, 64 {ID: "B", Condition: `contains .UserEmail "fred"`}, 65 }, 66 lastReportMap: map[string]interface{}{}, 67 baseTime: time.Now(), 68 }, 69 []string{"A"}, 70 false, 71 }, 72 { 73 "String 'hasPrefix' Condition", 74 args{ 75 params: &ConditionParams{ 76 UserEmail: "john@doe.org", 77 }, 78 messages: []*graph.MessageInfo{ 79 {ID: "A", Condition: `hasPrefix .UserEmail "john"`}, 80 {ID: "B", Condition: `hasPrefix .UserEmail "org"`}, 81 }, 82 lastReportMap: map[string]interface{}{}, 83 baseTime: time.Now(), 84 }, 85 []string{"A"}, 86 false, 87 }, 88 { 89 "String 'hasSuffix' Condition", 90 args{ 91 params: &ConditionParams{ 92 UserEmail: "john@doe.org", 93 }, 94 messages: []*graph.MessageInfo{ 95 {ID: "A", Condition: `hasSuffix .UserEmail "john"`}, 96 {ID: "B", Condition: `hasSuffix .UserEmail "org"`}, 97 }, 98 lastReportMap: map[string]interface{}{}, 99 baseTime: time.Now(), 100 }, 101 []string{"B"}, 102 false, 103 }, 104 { 105 "`regexMatch` Condition", 106 args{ 107 params: &ConditionParams{ 108 UserEmail: "john@doe.org", 109 }, 110 messages: []*graph.MessageInfo{ 111 {ID: "A", Condition: `regexMatch .UserEmail ".*@doe.org$"`}, 112 {ID: "B", Condition: `regexMatch .UserEmail "^doe.org$"`}, 113 }, 114 lastReportMap: map[string]interface{}{}, 115 baseTime: time.Now(), 116 }, 117 []string{"A"}, 118 false, 119 }, 120 { 121 "`regexMatch` Compilation Error", 122 args{ 123 params: &ConditionParams{ 124 UserEmail: "john@doe.org", 125 }, 126 messages: []*graph.MessageInfo{ 127 {ID: "A", Condition: `regexMatch .UserEmail ".*@doe.org$"`}, 128 {ID: "B", Condition: `regexMatch .UserEmail ".*("`}, 129 }, 130 lastReportMap: map[string]interface{}{}, 131 baseTime: time.Now(), 132 }, 133 []string{"A"}, 134 false, 135 }, 136 { 137 "Version Condition", 138 args{ 139 params: &ConditionParams{ 140 StateVersion: NewVersionFromSemver(semver.MustParse("7.8.9-SHA123456a7b")), 141 }, 142 messages: []*graph.MessageInfo{ 143 {ID: "A", Condition: `eq .StateVersion.Major 7`}, 144 {ID: "B", Condition: `eq .StateVersion.Minor 8`}, 145 {ID: "C", Condition: `eq .StateVersion.Patch 9`}, 146 {ID: "D", Condition: `hasSuffix .StateVersion.Raw "SHA123456a7b"`}, 147 {ID: "E", Condition: `eq .StateVersion.Major 1`}, 148 {ID: "F", Condition: `eq .StateVersion.Minor 2`}, 149 {ID: "G", Condition: `eq .StateVersion.Patch 3`}, 150 {ID: "H", Condition: `eq .StateVersion.Build "foo"`}, 151 }, 152 lastReportMap: map[string]interface{}{}, 153 baseTime: time.Now(), 154 }, 155 []string{"A", "B", "C", "D"}, 156 false, 157 }, 158 { 159 "Repeat Disabled", 160 args{ 161 params: &ConditionParams{}, 162 messages: []*graph.MessageInfo{ 163 {ID: "A", Repeat: graph.MessageRepeatTypeDisabled}, 164 {ID: "B", Repeat: graph.MessageRepeatTypeDisabled}, 165 {ID: "C", Repeat: graph.MessageRepeatTypeDisabled}, 166 }, 167 lastReportMap: map[string]interface{}{ 168 "A": time.Now(), 169 "C": time.Now(), 170 }, 171 baseTime: time.Now(), 172 }, 173 []string{"B"}, 174 false, 175 }, 176 { 177 "Repeat Constantly", 178 args{ 179 params: &ConditionParams{}, 180 messages: []*graph.MessageInfo{ 181 {ID: "A", Repeat: graph.MessageRepeatTypeConstantly}, 182 {ID: "B", Repeat: graph.MessageRepeatTypeConstantly}, 183 {ID: "C", Repeat: graph.MessageRepeatTypeConstantly}, 184 }, 185 lastReportMap: map[string]interface{}{ 186 "A": time.Now(), 187 "C": time.Now().Add(-time.Hour * 24 * 30), 188 }, 189 baseTime: time.Now(), 190 }, 191 []string{"A", "B", "C"}, 192 false, 193 }, 194 { 195 "Repeat Hourly", 196 args{ 197 params: &ConditionParams{}, 198 messages: []*graph.MessageInfo{ 199 {ID: "A", Repeat: graph.MessageRepeatTypeHourly}, 200 {ID: "B", Repeat: graph.MessageRepeatTypeHourly}, 201 {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, 202 }, 203 lastReportMap: map[string]interface{}{ 204 "A": time.Now(), 205 "B": time.Now().Add(-time.Hour), 206 "C": time.Now(), 207 }, 208 baseTime: time.Now(), 209 }, 210 []string{"B"}, 211 false, 212 }, 213 { 214 "Repeat Daily", 215 args{ 216 params: &ConditionParams{}, 217 messages: []*graph.MessageInfo{ 218 {ID: "A", Repeat: graph.MessageRepeatTypeHourly}, 219 {ID: "B", Repeat: graph.MessageRepeatTypeHourly}, 220 {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, 221 }, 222 lastReportMap: map[string]interface{}{ 223 "A": time.Now(), 224 "B": time.Now().Add(-time.Hour * 24), 225 "C": time.Now(), 226 }, 227 baseTime: time.Now(), 228 }, 229 []string{"B"}, 230 false, 231 }, 232 { 233 "Repeat Weekly", 234 args{ 235 params: &ConditionParams{}, 236 messages: []*graph.MessageInfo{ 237 {ID: "A", Repeat: graph.MessageRepeatTypeHourly}, 238 {ID: "B", Repeat: graph.MessageRepeatTypeHourly}, 239 {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, 240 }, 241 lastReportMap: map[string]interface{}{ 242 "A": time.Now(), 243 "B": time.Now().Add(-time.Hour * 24 * 7), 244 "C": time.Now(), 245 }, 246 baseTime: time.Now(), 247 }, 248 []string{"B"}, 249 false, 250 }, 251 { 252 "Repeat Monthly", 253 args{ 254 params: &ConditionParams{}, 255 messages: []*graph.MessageInfo{ 256 {ID: "A", Repeat: graph.MessageRepeatTypeHourly}, 257 {ID: "B", Repeat: graph.MessageRepeatTypeHourly}, 258 {ID: "C", Repeat: graph.MessageRepeatTypeHourly}, 259 }, 260 lastReportMap: map[string]interface{}{ 261 "A": time.Now(), 262 "B": time.Now().Add(-time.Hour * 24 * 7 * 30), 263 "C": time.Now(), 264 }, 265 baseTime: time.Now(), 266 }, 267 []string{"B"}, 268 false, 269 }, 270 } 271 for _, tt := range tests { 272 t.Run(tt.name, func(t *testing.T) { 273 got, err := check(tt.args.params, tt.args.messages, tt.args.lastReportMap, tt.args.baseTime) 274 if (err != nil) != tt.wantErr { 275 t.Errorf("check() error = %v, wantErr %v", errs.JoinMessage(err), tt.wantErr) 276 return 277 } 278 gotIDs := []string{} 279 for _, msg := range got { 280 gotIDs = append(gotIDs, msg.ID) 281 } 282 if !reflect.DeepEqual(gotIDs, tt.wantIDs) { 283 t.Errorf("check() got = %v, want %v", gotIDs, tt.wantIDs) 284 } 285 }) 286 } 287 }