File size: 2,510 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package message

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/notification"
)

func TestBundleAlertMessages(t *testing.T) {
	t.Run("existing bundle", func(t *testing.T) {

		n := time.Date(2006, 1, 1, 0, 0, 0, 0, time.UTC)
		bundle := Message{
			ID: "e",
			// bundles for alerts should also be joined
			Type:      notification.MessageTypeAlertBundle,
			CreatedAt: n.Add(time.Hour),
		}
		msg := []Message{
			{
				ID:        "a",
				AlertID:   1,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n,
			},
			{
				ID:        "b",
				AlertID:   2,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(time.Minute),
			},
			{
				ID:        "c",
				AlertID:   3,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(-time.Hour),
			},
			{
				ID:        "d",
				AlertID:   4,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(time.Hour),
			},
			bundle,
		}

		out, err := bundleAlertMessages(msg, func(b Message) (string, error) {
			t.Helper()
			// should use existing bundle
			t.Fail()
			return "", nil
		}, func(parentID string, ids []string) error {
			t.Helper()
			assert.Equal(t, "e", parentID)
			assert.ElementsMatch(t, []string{"a", "b", "c", "d"}, ids)

			return nil
		})
		assert.NoError(t, err)
		assert.Len(t, out, 1)
		assert.EqualValues(t, bundle, out[0])
	})

	t.Run("new bundle", func(t *testing.T) {

		n := time.Date(2006, 1, 1, 0, 0, 0, 0, time.UTC)

		msg := []Message{
			{
				ID:        "a",
				AlertID:   1,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n,
			},
			{
				ID:        "b",
				AlertID:   2,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(time.Minute),
			},
			{
				ID:        "c",
				AlertID:   3,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(-time.Hour),
			},
			{
				ID:        "d",
				AlertID:   4,
				Type:      notification.MessageTypeAlert,
				CreatedAt: n.Add(time.Hour),
			},
		}

		out, err := bundleAlertMessages(msg, func(b Message) (string, error) {
			t.Helper()

			return "e", nil
		}, func(parentID string, ids []string) error {
			t.Helper()
			assert.Equal(t, "e", parentID)
			assert.ElementsMatch(t, []string{"a", "b", "c", "d"}, ids)

			return nil
		})
		assert.NoError(t, err)
		assert.Len(t, out, 1)
		assert.EqualValues(t, Message{
			ID:        "e",
			Type:      notification.MessageTypeAlertBundle,
			CreatedAt: n.Add(-time.Hour),
		}, out[0])
	})

}