-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates_test.go
More file actions
310 lines (285 loc) · 7.37 KB
/
templates_test.go
File metadata and controls
310 lines (285 loc) · 7.37 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package flashduty
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
)
func TestChannelEnumValues(t *testing.T) {
t.Parallel()
channels := ChannelEnumValues()
if len(channels) != 13 {
t.Fatalf("expected 13 channels, got %d", len(channels))
}
want := map[string]bool{
"dingtalk": true, "slack": true, "telegram": true,
"email": true, "sms": true, "zoom": true,
}
found := make(map[string]bool)
for _, ch := range channels {
found[ch] = true
}
if !slices.IsSorted(channels) {
t.Fatal("expected channel enum values to be sorted")
}
for k := range want {
if !found[k] {
t.Errorf("missing expected channel %q", k)
}
}
}
func TestTemplateVariables(t *testing.T) {
t.Parallel()
vars := TemplateVariables()
if len(vars) != 40 {
t.Fatalf("expected 40 template variables, got %d", len(vars))
}
// Verify it returns a copy
vars[0].Name = "MUTATED"
original := TemplateVariables()
if original[0].Name == "MUTATED" {
t.Fatal("TemplateVariables() should return a copy, not the original slice")
}
}
func TestTemplateCustomFunctions(t *testing.T) {
t.Parallel()
fns := TemplateCustomFunctions()
if len(fns) != 19 {
t.Fatalf("expected 19 custom functions, got %d", len(fns))
}
// Verify it returns a copy
fns[0].Name = "MUTATED"
original := TemplateCustomFunctions()
if original[0].Name == "MUTATED" {
t.Fatal("TemplateCustomFunctions() should return a copy, not the original slice")
}
}
func TestTemplateSprigFunctions(t *testing.T) {
t.Parallel()
fns := TemplateSprigFunctions()
if len(fns) != 19 {
t.Fatalf("expected 19 sprig functions, got %d", len(fns))
}
// Verify it returns a copy
fns[0].Name = "MUTATED"
original := TemplateSprigFunctions()
if original[0].Name == "MUTATED" {
t.Fatal("TemplateSprigFunctions() should return a copy, not the original slice")
}
}
func TestGetPresetTemplate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
channel string
serverResp map[string]any
wantErr string
wantCode string
}{
{
name: "invalid channel",
channel: "unknown",
wantErr: "unknown channel: unknown",
},
{
name: "valid channel returns template",
channel: "slack",
serverResp: map[string]any{
"data": map[string]any{
"slack": "Hello {{.Title}}",
},
},
wantCode: "Hello {{.Title}}",
},
{
name: "empty template code",
channel: "slack",
serverResp: map[string]any{
"data": map[string]any{
"slack": "",
},
},
wantErr: "no preset template found for channel: slack",
},
{
name: "API error",
channel: "slack",
serverResp: map[string]any{
"error": map[string]any{
"code": "Forbidden",
"message": "access denied",
},
},
wantErr: "Forbidden: access denied",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var ts *httptest.Server
if tt.serverResp != nil {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(tt.serverResp)
}))
defer ts.Close()
}
opts := []Option{}
if ts != nil {
opts = append(opts, WithBaseURL(ts.URL))
}
client, err := NewClient("test-key", opts...)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
out, err := client.GetPresetTemplate(context.Background(), &GetPresetTemplateInput{
Channel: tt.channel,
})
if tt.wantErr != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %q", tt.wantErr, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.TemplateCode != tt.wantCode {
t.Errorf("template code = %q, want %q", out.TemplateCode, tt.wantCode)
}
if out.Channel != tt.channel {
t.Errorf("channel = %q, want %q", out.Channel, tt.channel)
}
})
}
}
func TestValidateTemplate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input *ValidateTemplateInput
serverResp map[string]any
wantErr string
wantSuccess bool
wantErrors int
wantWarnings int
}{
{
name: "invalid channel",
input: &ValidateTemplateInput{Channel: "unknown", TemplateCode: "test"},
wantErr: "unknown channel: unknown",
},
{
name: "successful validation",
input: &ValidateTemplateInput{Channel: "email", TemplateCode: "Hello {{.Title}}"},
serverResp: map[string]any{
"data": map[string]any{
"success": true,
"content": "Hello Test Incident",
"message": "",
},
},
wantSuccess: true,
},
{
name: "template parse error",
input: &ValidateTemplateInput{Channel: "email", TemplateCode: "{{.Bad"},
serverResp: map[string]any{
"data": map[string]any{
"success": false,
"content": "",
"message": "template parse error",
},
},
wantSuccess: false,
wantErrors: 1,
},
{
name: "size limit exceeded - telegram",
input: &ValidateTemplateInput{Channel: "telegram", TemplateCode: "big"},
serverResp: map[string]any{
"data": map[string]any{
"success": true,
"content": strings.Repeat("x", 5000),
"message": "",
},
},
wantSuccess: false,
wantErrors: 1,
},
{
name: "size warning at 80% threshold",
input: &ValidateTemplateInput{Channel: "dingtalk", TemplateCode: "medium"},
serverResp: map[string]any{
"data": map[string]any{
"success": true,
"content": strings.Repeat("x", 3500), // 87.5% of 4000
"message": "",
},
},
wantSuccess: true,
wantWarnings: 1,
},
{
name: "no limit channel - no warning",
input: &ValidateTemplateInput{Channel: "email", TemplateCode: "big"},
serverResp: map[string]any{
"data": map[string]any{
"success": true,
"content": strings.Repeat("x", 100000),
"message": "",
},
},
wantSuccess: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var ts *httptest.Server
if tt.serverResp != nil {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(tt.serverResp)
}))
defer ts.Close()
}
opts := []Option{}
if ts != nil {
opts = append(opts, WithBaseURL(ts.URL))
}
client, err := NewClient("test-key", opts...)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
out, err := client.ValidateTemplate(context.Background(), tt.input)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %q", tt.wantErr, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.Success != tt.wantSuccess {
t.Errorf("success = %v, want %v (errors: %v, warnings: %v)", out.Success, tt.wantSuccess, out.Errors, out.Warnings)
}
if len(out.Errors) != tt.wantErrors {
t.Errorf("errors count = %d, want %d: %v", len(out.Errors), tt.wantErrors, out.Errors)
}
if len(out.Warnings) != tt.wantWarnings {
t.Errorf("warnings count = %d, want %d: %v", len(out.Warnings), tt.wantWarnings, out.Warnings)
}
})
}
}