File size: 4,504 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package twilio

import (
	"strconv"
	"strings"
	"testing"

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

func resultCheck(t *testing.T, expected string, res string, err error) {
	t.Helper()
	t.Log("Length", len(res))
	assert.NoError(t, err)
	assert.Equal(t, expected, res)
}

func TestMapGSM(t *testing.T) {
	check := func(input, exp string) {
		t.Run("", func(t *testing.T) {
			res := strings.Map(mapGSM, input)
			if res != exp {
				t.Errorf("got %s; want %s", strconv.Quote(res), strconv.Quote(exp))
			}
		})
	}

	check("foo\\bar", "foo?bar")
	check("foo\bar", "fooar")
	check("foo\nbar", "foo bar")
	check("foo\t bar@/ok:asdf", "foo  bar@/ok:asdf")
	check("[Testing] {alert_message: `okay`}", "(Testing) (alert-message: 'okay')")
}

func TestSMS_RenderAlert(t *testing.T) {
	check := func(name string, a notification.Alert, link string, code int, exp string) {
		t.Run(name, func(t *testing.T) {
			res, err := renderAlertMessage("TestApp", a, link, code)
			resultCheck(t, exp, res, err)
		})
	}

	check("normal",
		notification.Alert{
			AlertID: 123,
			Summary: "Testing",
		},
		"https://example.com/alerts/123",
		1,
		`TestApp: Alert #123: Testing

https://example.com/alerts/123

Reply '1a' to ack, '1e' to escalate, '1c' to close.`,
	)

	check("no-reply-code",
		notification.Alert{
			AlertID: 123,
			Summary: "Testing",
		},
		"https://example.com/alerts/123",
		0,
		`TestApp: Alert #123: Testing

https://example.com/alerts/123`,
	)

	check("no-link",
		notification.Alert{
			AlertID: 123,
			Summary: "Testing",
		},
		"",
		1,
		`TestApp: Alert #123: Testing

Reply '1a' to ack, '1e' to escalate, '1c' to close.`,
	)

	check("no-link-or-reply-code",
		notification.Alert{
			AlertID: 123,
			Summary: "Testing",
		},
		"",
		0,
		`TestApp: Alert #123: Testing`,
	)

	check("truncate",
		notification.Alert{
			AlertID: 123,
			Summary: "Testing with a really really obnoxiously long message that will be need to be truncated at some point.",
		},
		"https://example.com/alerts/123",
		1,
		`TestApp: Alert #123: Testing with a really really obnoxiously long message

https://example.com/alerts/123

Reply '1a' to ack, '1e' to escalate, '1c' to close.`,
	)

	check("truncate-long-id",
		notification.Alert{
			AlertID: 123456789,
			Summary: "Testing with a really really obnoxiously long message that will be need to be truncated at some point.",
		},
		"https://example.com/alerts/123",
		1,
		`TestApp: Alert #123456789: Testing with a really really obnoxiously long me

https://example.com/alerts/123

Reply '1a' to ack, '1e' to escalate, '1c' to close.`,
	)

	check("message-too-long",
		// can't fit summary
		notification.Alert{
			AlertID: 123456789,
			Summary: "Testing with a really really obnoxiously long message that will be need to be truncated at some point.",
		},
		"https://example.com/alerts/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff",
		123456789,
		`TestApp: Alert #123456789: 

https://example.com/alerts/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff/123ff

Reply '123456789a' to ack, '123456789e' to escalate, '123456789c' to close.`,
	)
}

func TestSMS_RenderAlertBundle(t *testing.T) {
	check := func(name string, a notification.AlertBundle, link string, code int, exp string) {
		t.Run(name, func(t *testing.T) {
			res, err := renderAlertBundleMessage("TestApp", a, link, code)
			resultCheck(t, exp, res, err)
		})
	}

	check("alert-bundle-one",
		notification.AlertBundle{
			Count:       1,
			ServiceName: "My Service",
		},
		"https://example.com/services/321-654/alerts",
		100,
		`TestApp: Svc 'My Service': 1 unacked alert

	https://example.com/services/321-654/alerts

	Reply '100aa' to ack all, '100cc' to close all.`,
	)

	check("alert-bundle",
		notification.AlertBundle{
			Count:       5,
			ServiceName: "My Service",
		},
		"https://example.com/services/321-654/alerts",
		100,
		`TestApp: Svc 'My Service': 5 unacked alerts

	https://example.com/services/321-654/alerts

	Reply '100aa' to ack all, '100cc' to close all.`,
	)
}

func TestSMS_RenderAlertStatus(t *testing.T) {
	check := func(name string, a notification.AlertStatus, exp string) {
		t.Run(name, func(t *testing.T) {
			res, err := renderAlertStatusMessage("TestApp", a)
			resultCheck(t, exp, res, err)
		})
	}

	check("alert-status",
		notification.AlertStatus{
			AlertID:  123,
			Summary:  "Testing",
			LogEntry: "Some log entry",
		},
		`TestApp: Alert #123: Testing

	Some log entry`,
	)
}