-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailingo_test.go
More file actions
819 lines (687 loc) · 17.6 KB
/
mailingo_test.go
File metadata and controls
819 lines (687 loc) · 17.6 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
package mailingo
import (
"embed"
"os"
"strings"
"testing"
"github.com/lib-x/mailingo/options"
)
//go:embed testdata/*.json
var testFS embed.FS
func TestNew(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
Logo: "https://example.com/logo.png",
Copyright: "© 2025 Test Product. All rights reserved.",
}
mailer := New(product, DefaultTheme)
if mailer == nil {
t.Fatal("Expected mailer to be non-nil")
}
if mailer.product.Name != product.Name {
t.Errorf("Expected product name %s, got %s", product.Name, mailer.product.Name)
}
if mailer.theme.PrimaryColor != DefaultTheme.PrimaryColor {
t.Errorf("Expected primary color %s, got %s", DefaultTheme.PrimaryColor, mailer.theme.PrimaryColor)
}
}
func TestLoadMessageFile(t *testing.T) {
// Create temporary test translation file
content := `{
"greeting": "Hello",
"signature": "Best regards",
"product.copyright": "© 2025 Test Product. All rights reserved."
}`
tmpFile, err := os.CreateTemp("", "test-*.en.json")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.WriteString(content); err != nil {
t.Fatalf("Failed to write temp file: %v", err)
}
tmpFile.Close()
product := Product{
Name: "Test Product",
Link: "https://example.com",
Copyright: "product.copyright",
}
mailer := New(product, DefaultTheme)
err = mailer.LoadMessageFile(tmpFile.Name())
if err != nil {
t.Errorf("LoadMessageFile failed: %v", err)
}
}
func TestLoadMessageFileFS(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
Copyright: "product.copyright",
}
mailer := New(product, DefaultTheme)
err := mailer.LoadMessageFileFS(testFS, "testdata/en.json")
if err != nil {
t.Errorf("LoadMessageFileFS failed: %v", err)
}
}
func TestGenerateHTML(t *testing.T) {
product := Product{
Name: "Acme Corporation",
Link: "https://acme.com",
Logo: "https://acme.com/logo.png",
Copyright: "product.copyright",
}
mailer := New(product, DefaultTheme)
// Load test translations
err := mailer.LoadMessageFileFS(testFS, "testdata/en.json")
if err != nil {
t.Fatalf("Failed to load translations: %v", err)
}
email := Email{
Body: Body{
Name: "John Doe",
Greeting: "greeting",
Title: "email.welcome.title",
Intros: []string{
"email.welcome.intro",
},
Dictionary: []Entry{
{Key: "email.username", Value: "johndoe"},
{Key: "email.email", Value: "john@example.com"},
},
Actions: []Action{
{
Instructions: "email.action.instructions",
Button: Button{
Text: "email.action.button",
Link: "https://acme.com/confirm?token=abc123",
},
},
},
Outros: []string{
"email.welcome.outro",
},
Signature: "signature",
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Verify HTML contains expected elements
if !strings.Contains(html, "<!DOCTYPE html>") {
t.Error("HTML should contain DOCTYPE")
}
if !strings.Contains(html, "John Doe") {
t.Error("HTML should contain recipient name")
}
if !strings.Contains(html, "Acme Corporation") {
t.Error("HTML should contain product name")
}
if !strings.Contains(html, DefaultTheme.PrimaryColor) {
t.Error("HTML should contain theme primary color")
}
}
func TestGeneratePlainText(t *testing.T) {
product := Product{
Name: "Acme Corporation",
Link: "https://acme.com",
Copyright: "product.copyright",
}
mailer := New(product, DefaultTheme)
// Load test translations
err := mailer.LoadMessageFileFS(testFS, "testdata/en.json")
if err != nil {
t.Fatalf("Failed to load translations: %v", err)
}
email := Email{
Body: Body{
Name: "Jane Smith",
Greeting: "greeting",
Title: "email.welcome.title",
Intros: []string{
"email.welcome.intro",
},
Dictionary: []Entry{
{Key: "email.username", Value: "janesmith"},
{Key: "email.email", Value: "jane@example.com"},
},
Actions: []Action{
{
Instructions: "email.action.instructions",
Button: Button{
Text: "email.action.button",
Link: "https://acme.com/verify",
},
},
},
Outros: []string{
"email.welcome.outro",
},
Signature: "signature",
},
}
text, err := mailer.GeneratePlainText(email, "en")
if err != nil {
t.Fatalf("GeneratePlainText failed: %v", err)
}
// Verify plain text contains expected elements
if !strings.Contains(text, "Jane Smith") {
t.Error("Plain text should contain recipient name")
}
if !strings.Contains(text, "Acme Corporation") {
t.Error("Plain text should contain product name")
}
if !strings.Contains(text, "janesmith") {
t.Error("Plain text should contain username from dictionary")
}
}
func TestMultipleLanguages(t *testing.T) {
product := Product{
Name: "Global Corp",
Link: "https://globalcorp.com",
Copyright: "product.copyright",
}
mailer := New(product, FlatTheme)
// Load English translations
err := mailer.LoadMessageFileFS(testFS, "testdata/en.json")
if err != nil {
t.Fatalf("Failed to load English translations: %v", err)
}
// Load Chinese translations
err = mailer.LoadMessageFileFS(testFS, "testdata/zh.json")
if err != nil {
t.Fatalf("Failed to load Chinese translations: %v", err)
}
email := Email{
Body: Body{
Name: "Zhang San",
Greeting: "greeting",
Intros: []string{
"email.welcome.intro",
},
Signature: "signature",
},
}
// Generate English version
htmlEN, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("Failed to generate English HTML: %v", err)
}
if !strings.Contains(htmlEN, "Hello") {
t.Error("English HTML should contain 'Hello'")
}
// Generate Chinese version
htmlZH, err := mailer.GenerateHTML(email, "zh")
if err != nil {
t.Fatalf("Failed to generate Chinese HTML: %v", err)
}
if !strings.Contains(htmlZH, "您好") {
t.Error("Chinese HTML should contain '您好'")
}
}
func TestThemes(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
tests := []struct {
name string
theme Theme
color string
}{
{"DefaultTheme", DefaultTheme, "#3869D4"},
{"FlatTheme", FlatTheme, "#2F3133"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mailer := New(product, tt.theme)
email := Email{
Body: Body{
Name: "Test User",
Intros: []string{"Test message"},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, tt.color) {
t.Errorf("HTML should contain theme color %s", tt.color)
}
})
}
}
func TestCustomButtonColor(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
customColor := "#FF5733"
email := Email{
Body: Body{
Name: "Test User",
Actions: []Action{
{
Instructions: "Click the button",
Button: Button{
Text: "Custom Button",
Link: "https://example.com/action",
Color: customColor,
},
},
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, customColor) {
t.Errorf("HTML should contain custom button color %s", customColor)
}
}
func TestInvertedButton(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Actions: []Action{
{
Instructions: "Click the inverted button",
InvertedButton: true,
Button: Button{
Text: "Inverted Button",
Link: "https://example.com/action",
},
},
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, "email-button-inverted") {
t.Error("HTML should contain inverted button class")
}
}
func TestTableRendering(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Table: Table{
Data: [][]Entry{
// Header row
{
{Key: "Product"},
{Key: "Quantity"},
{Key: "Price"},
},
// Data rows
{
{Value: "Widget A"},
{Value: "2"},
{Value: "$19.99"},
},
{
{Value: "Widget B"},
{Value: "1"},
{Value: "$29.99"},
},
},
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Verify table elements
if !strings.Contains(html, "email-table") {
t.Error("HTML should contain table class")
}
if !strings.Contains(html, "Widget A") {
t.Error("HTML should contain table data")
}
if !strings.Contains(html, "Product") {
t.Error("HTML should contain table header")
}
}
func TestEmptyEmail(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML should not fail with empty email: %v", err)
}
if !strings.Contains(html, "Test User") {
t.Error("HTML should still contain recipient name")
}
text, err := mailer.GeneratePlainText(email, "en")
if err != nil {
t.Fatalf("GeneratePlainText should not fail with empty email: %v", err)
}
if !strings.Contains(text, "Test User") {
t.Error("Plain text should still contain recipient name")
}
}
func TestTranslateFallback(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
// Don't load any translations
email := Email{
Body: Body{
Name: "Test User",
Greeting: "nonexistent.key",
Intros: []string{
"This is a literal string, not a translation key",
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Should fall back to the original key/text
if !strings.Contains(html, "nonexistent.key") {
t.Error("HTML should contain fallback key when translation is missing")
}
if !strings.Contains(html, "This is a literal string") {
t.Error("HTML should contain literal string")
}
}
func TestAttachments(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Intros: []string{
"Your documents are ready.",
},
Attachments: []Attachment{
{
Name: "document.pdf",
URL: "https://example.com/download/doc.pdf",
Size: "2.5 MB",
Type: "PDF Document",
},
{
Name: "spreadsheet.xlsx",
URL: "https://example.com/download/sheet.xlsx",
Size: "1.2 MB",
Type: "Excel Spreadsheet",
},
},
},
}
// Test HTML generation with attachments
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, "email-attachments") {
t.Error("HTML should contain attachments section")
}
if !strings.Contains(html, "document.pdf") {
t.Error("HTML should contain first attachment name")
}
if !strings.Contains(html, "spreadsheet.xlsx") {
t.Error("HTML should contain second attachment name")
}
if !strings.Contains(html, "2.5 MB") {
t.Error("HTML should contain attachment size")
}
// Test plain text generation with attachments
text, err := mailer.GeneratePlainText(email, "en")
if err != nil {
t.Fatalf("GeneratePlainText failed: %v", err)
}
if !strings.Contains(text, "Attachments:") {
t.Error("Plain text should contain attachments header")
}
if !strings.Contains(text, "document.pdf") {
t.Error("Plain text should contain attachment name")
}
if !strings.Contains(text, "https://example.com/download/doc.pdf") {
t.Error("Plain text should contain attachment URL")
}
}
func TestSMTPAttachments(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Intros: []string{
"Please see attached file.",
},
},
SMTPAttachments: []SMTPAttachment{
{
Filename: "report.pdf",
Content: []byte("PDF content"),
ContentType: "application/pdf",
},
},
}
// SMTP attachments should not appear in HTML body
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if strings.Contains(html, "report.pdf") {
t.Error("HTML should not contain SMTP attachment filename")
}
// But the Email struct should have them
if len(email.SMTPAttachments) != 1 {
t.Error("Email should have SMTP attachments")
}
if email.SMTPAttachments[0].Filename != "report.pdf" {
t.Error("SMTP attachment filename should be preserved")
}
}
func TestCombinedAttachments(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Intros: []string{
"Files included both ways.",
},
Attachments: []Attachment{
{
Name: "large_file.zip",
URL: "https://example.com/download/large.zip",
Size: "50 MB",
Type: "ZIP Archive",
},
},
},
SMTPAttachments: []SMTPAttachment{
{
Filename: "small_summary.pdf",
Content: []byte("PDF content"),
ContentType: "application/pdf",
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Body attachments should appear in HTML
if !strings.Contains(html, "large_file.zip") {
t.Error("HTML should contain body attachment")
}
// SMTP attachments should not appear in HTML
if strings.Contains(html, "small_summary.pdf") {
t.Error("HTML should not contain SMTP attachment")
}
// Both should be present in the Email struct
if len(email.Body.Attachments) != 1 {
t.Error("Should have body attachment")
}
if len(email.SMTPAttachments) != 1 {
t.Error("Should have SMTP attachment")
}
}
func TestCustomCSS(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
customCSS := `
.email-title { font-size: 32px; }
.custom-class { color: red; }
`
mailer := New(product, DefaultTheme, options.WithCustomCSS(customCSS))
email := Email{
Body: Body{
Name: "Test User",
Title: "Custom CSS Test",
Intros: []string{
"Testing custom CSS",
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Debug: find and print the style section
startIdx := strings.Index(html, "<style>")
endIdx := strings.Index(html, "</style>")
if startIdx != -1 && endIdx != -1 {
styleSection := html[startIdx : endIdx+8]
t.Logf("Style section (last 300 chars): %s", styleSection[len(styleSection)-300:])
}
// The customCSS should be in the style section
if !strings.Contains(html, ".custom-class") {
t.Error("HTML should contain custom class")
}
if !strings.Contains(html, "font-size: 32px") {
t.Error("HTML should contain custom CSS font-size")
}
}
func TestCustomTemplateString(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
customTemplate := `
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<h1>{{.Body.Title}}</h1>
<p>Hello {{.Body.Name}}</p>
<p>{{.Product.Name}}</p>
</body>
</html>
`
mailer := New(product, DefaultTheme, options.WithCustomTemplateString(customTemplate))
email := Email{
Body: Body{
Name: "Alice",
Title: "Test Title",
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, "<h1>Test Title</h1>") {
t.Error("HTML should use custom template")
}
if !strings.Contains(html, "<p>Hello Alice</p>") {
t.Error("HTML should contain recipient name from custom template")
}
if !strings.Contains(html, "<p>Test Product</p>") {
t.Error("HTML should contain product name from custom template")
}
}
func TestCustomTemplateWithEmbedFS(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
// Use the default template from embedded FS as a test
mailer := New(product, DefaultTheme, options.WithCustomTemplateFile(templatesFS, "templates/default.html"))
email := Email{
Body: Body{
Name: "Bob",
Intros: []string{
"Test message",
},
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
if !strings.Contains(html, "Bob") {
t.Error("HTML should contain recipient name")
}
if !strings.Contains(html, "Test message") {
t.Error("HTML should contain intro text")
}
}
func TestDefaultTemplateWithoutOptions(t *testing.T) {
product := Product{
Name: "Test Product",
Link: "https://example.com",
}
// Create mailer without any options - should use default template
mailer := New(product, DefaultTheme)
email := Email{
Body: Body{
Name: "Test User",
Title: "Default Template Test",
},
}
html, err := mailer.GenerateHTML(email, "en")
if err != nil {
t.Fatalf("GenerateHTML failed: %v", err)
}
// Should use default template
if !strings.Contains(html, "email-container") {
t.Error("Should use default template with standard classes")
}
if !strings.Contains(html, "Test User") {
t.Error("HTML should contain recipient name")
}
}