package infra import ( "testing" ) func TestEmailBuildHTML(t *testing.T) { t.Run("construye mensaje html con campos basicos", func(t *testing.T) { msg := EmailBuildHTML("alice@example.com", []string{"bob@example.com"}, "Hola", "Hola") if msg.From != "alice@example.com" { t.Errorf("From: got %q, want %q", msg.From, "alice@example.com") } if len(msg.To) != 1 || msg.To[0] != "bob@example.com" { t.Errorf("To: got %v, want [bob@example.com]", msg.To) } if msg.Subject != "Hola" { t.Errorf("Subject: got %q, want %q", msg.Subject, "Hola") } if msg.BodyHTML != "Hola" { t.Errorf("BodyHTML: got %q, want %q", msg.BodyHTML, "Hola") } }) t.Run("copia el slice to para evitar aliasing", func(t *testing.T) { to := []string{"a@example.com", "b@example.com"} msg := EmailBuildHTML("x@x.com", to, "s", "h") to[0] = "mutated@example.com" if msg.To[0] == "mutated@example.com" { t.Error("To slice was aliased — mutation affected the message") } }) t.Run("campos no usados quedan vacios", func(t *testing.T) { msg := EmailBuildHTML("f@example.com", []string{"t@example.com"}, "s", "h") if msg.BodyText != "" { t.Errorf("BodyText should be empty, got %q", msg.BodyText) } if len(msg.CC) != 0 { t.Errorf("CC should be empty, got %v", msg.CC) } if len(msg.BCC) != 0 { t.Errorf("BCC should be empty, got %v", msg.BCC) } if len(msg.Attachments) != 0 { t.Errorf("Attachments should be empty, got %v", msg.Attachments) } }) } func TestEmailBuildText(t *testing.T) { t.Run("construye mensaje text con campos basicos", func(t *testing.T) { msg := EmailBuildText("alice@example.com", []string{"bob@example.com"}, "Alerta", "Servidor caido") if msg.BodyText != "Servidor caido" { t.Errorf("BodyText: got %q, want %q", msg.BodyText, "Servidor caido") } if msg.From != "alice@example.com" { t.Errorf("From: got %q, want %q", msg.From, "alice@example.com") } if msg.Subject != "Alerta" { t.Errorf("Subject: got %q, want %q", msg.Subject, "Alerta") } }) t.Run("body html queda vacio", func(t *testing.T) { msg := EmailBuildText("f@x.com", []string{"t@x.com"}, "s", "body") if msg.BodyHTML != "" { t.Errorf("BodyHTML should be empty, got %q", msg.BodyHTML) } }) } func TestEmailWithAttachment(t *testing.T) { t.Run("agrega adjunto sin mutar el original", func(t *testing.T) { orig := EmailBuildHTML("a@example.com", []string{"b@example.com"}, "s", "h") att := EmailAttachment{Filename: "f.pdf", ContentType: "application/pdf", Data: []byte("data")} msg2 := EmailWithAttachment(orig, att) if len(orig.Attachments) != 0 { t.Errorf("original was mutated: got %d attachments, want 0", len(orig.Attachments)) } if len(msg2.Attachments) != 1 { t.Errorf("got %d attachments, want 1", len(msg2.Attachments)) } if msg2.Attachments[0].Filename != "f.pdf" { t.Errorf("Filename: got %q, want %q", msg2.Attachments[0].Filename, "f.pdf") } }) t.Run("multiples adjuntos se acumulan", func(t *testing.T) { msg := EmailBuildHTML("a@x.com", []string{"b@x.com"}, "s", "h") msg = EmailWithAttachment(msg, EmailAttachment{Filename: "a1.txt", ContentType: "text/plain", Data: []byte("1")}) msg = EmailWithAttachment(msg, EmailAttachment{Filename: "a2.txt", ContentType: "text/plain", Data: []byte("2")}) if len(msg.Attachments) != 2 { t.Errorf("got %d attachments, want 2", len(msg.Attachments)) } }) t.Run("copia todos los campos del mensaje", func(t *testing.T) { orig := EmailMessage{ From: "a@x.com", To: []string{"b@x.com"}, CC: []string{"c@x.com"}, BCC: []string{"d@x.com"}, Subject: "Test", BodyHTML: "
Hi
", BodyText: "Hi", Headers: map[string]string{"X-Test": "value"}, } att := EmailAttachment{Filename: "f.png", ContentType: "image/png", Data: []byte("img")} msg2 := EmailWithAttachment(orig, att) if msg2.From != orig.From { t.Errorf("From mismatch: %q vs %q", msg2.From, orig.From) } if msg2.Subject != orig.Subject { t.Errorf("Subject mismatch: %q vs %q", msg2.Subject, orig.Subject) } if len(msg2.CC) != 1 || msg2.CC[0] != "c@x.com" { t.Errorf("CC not copied: %v", msg2.CC) } if msg2.Headers["X-Test"] != "value" { t.Errorf("Headers not copied: %v", msg2.Headers) } }) }