132 lines
6.2 KiB
Go
132 lines
6.2 KiB
Go
package qawemail
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSlotMatchesUsesBeijingWeekdayAndHour(t *testing.T) {
|
|
settings := Settings{Weekday: 1, Hour: 9}
|
|
// Monday 09:30 Beijing == Monday 01:30 UTC.
|
|
if !slotMatches(settings, time.Date(2026, 10, 26, 1, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Monday 09:30 Beijing must match a Monday-09 slot")
|
|
}
|
|
if slotMatches(settings, time.Date(2026, 10, 26, 2, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Monday 10:30 Beijing must not match a Monday-09 slot")
|
|
}
|
|
// Sunday 23:30 UTC is Monday 07:30 Beijing: still Monday, wrong hour.
|
|
if slotMatches(settings, time.Date(2026, 10, 25, 23, 30, 0, 0, time.UTC)) {
|
|
t.Fatal("Sunday 23:30 UTC is not the configured hour")
|
|
}
|
|
}
|
|
|
|
func TestReportClosedOnlyAfterTheISOWeekEnds(t *testing.T) {
|
|
if reportClosed("2026-W43", time.Date(2026, 10, 25, 10, 0, 0, 0, time.UTC)) {
|
|
t.Fatal("a week is not closed before its Monday boundary")
|
|
}
|
|
if !reportClosed("2026-W43", time.Date(2026, 10, 26, 1, 0, 0, 0, time.UTC)) {
|
|
t.Fatal("week 43 must be closed after Monday 00:00 Beijing")
|
|
}
|
|
if reportClosed("2026-W99", time.Now()) || reportClosed("2026-W00", time.Now()) {
|
|
t.Fatal("impossible ISO weeks must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRefusesEnabledSettingsWithoutTransportAndBadRecipients(t *testing.T) {
|
|
if _, err := normalize(SettingsInput{Host: "", From: "", Recipients: nil, Enabled: true}); err != ErrInvalid {
|
|
t.Fatalf("enabled without host/from/recipients must be invalid, got %v", err)
|
|
}
|
|
if _, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"not-an-address"}, Enabled: true}); err == nil {
|
|
t.Fatal("a malformed recipient must be rejected")
|
|
}
|
|
good, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com", "b@example.com"}, Enabled: true})
|
|
if err != nil || good.Port != 465 || good.Weekday != 1 || len(good.Recipients) != 2 {
|
|
t.Fatalf("defaults not applied: %+v %v", good, err)
|
|
}
|
|
duplicated := SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com", "A@example.com"}}
|
|
if _, err := normalize(duplicated); err != ErrInvalid {
|
|
t.Fatal("case-insensitive duplicate recipients must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestBuildMIMEProvidesReadableTextAndHTMLWithEncodedSubject(t *testing.T) {
|
|
html, text := renderReport("# 知识库周报 2026-W43\n\n## 二、核心指标\n\n正确率:90.66%\n")
|
|
if !strings.Contains(html, "<h1>") || !strings.Contains(html, "90.66%") {
|
|
t.Fatalf("rendered weekly HTML missing structure or metrics: %s", html[:120])
|
|
}
|
|
if !strings.Contains(text, "正确率:90.66%") {
|
|
t.Fatal("plain text body must keep the readable report")
|
|
}
|
|
raw := buildMIME(Message{From: "kb@example.com", To: []string{"lead@example.com"}, Subject: "知识库周报 2026-W43", HTML: html, Text: text}, "<id@example.com>", "BOUND")
|
|
for _, want := range []string{"Content-Type: multipart/alternative", "text/plain", "text/html", "Message-ID: <id@example.com>", "Subject: =?UTF-8?B?"} {
|
|
if !strings.Contains(raw, want) {
|
|
t.Fatalf("MIME payload missing %q", want)
|
|
}
|
|
}
|
|
encoded := base64.StdEncoding.EncodeToString([]byte("知识库周报 2026-W43"))
|
|
if !strings.Contains(raw, encoded) {
|
|
t.Fatal("subject must be RFC2047-encoded so Chinese survives clients")
|
|
}
|
|
}
|
|
|
|
type fakeMailer struct {
|
|
sent []Message
|
|
configs []SMTPConfig
|
|
err error
|
|
}
|
|
|
|
func (f *fakeMailer) Send(_ context.Context, cfg SMTPConfig, msg Message) (string, error) {
|
|
f.configs = append(f.configs, cfg)
|
|
if f.err != nil {
|
|
return "", f.err
|
|
}
|
|
f.sent = append(f.sent, msg)
|
|
return "<fake@example.com>", nil
|
|
}
|
|
|
|
func TestMailerRefusesPlaintextServersWithoutSTARTTLS(t *testing.T) {
|
|
if _, err := (SMTPMailer{}).Send(context.Background(), SMTPConfig{Host: "127.0.0.1", Port: 25}, Message{From: "kb@example.com", To: []string{"a@example.com"}, Subject: "x"}); err == nil {
|
|
t.Fatal("a server that cannot encrypt must be refused rather than sent in clear")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRejectsACopyThatIsAlsoAPrimaryRecipient(t *testing.T) {
|
|
_, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Cc: []string{"A@example.com"}, Enabled: true})
|
|
if err != ErrInvalid {
|
|
t.Fatal("a person in both To and Cc would receive the report twice")
|
|
}
|
|
good, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Cc: []string{"b@example.com", "c@example.com"}, Enabled: true})
|
|
if err != nil || len(good.Cc) != 2 {
|
|
t.Fatalf("valid cc list rejected: %+v %v", good.Cc, err)
|
|
}
|
|
}
|
|
|
|
func TestBuildMIMEPlacesTheChartAfterTheBodyAsAnInlineImage(t *testing.T) {
|
|
html, text := renderReport("# 周报\n\n正确率:90.66%\n")
|
|
png := []byte("\x89PNG\r\n\x1a\nfixture-bytes")
|
|
html = strings.Replace(html, "</body>", `<p><img src="cid:weekly-dashboard@knowledge-governance"></p></body>`, 1)
|
|
raw := buildMIME(Message{From: "kb@example.com", To: []string{"a@example.com"}, Cc: []string{"b@example.com"}, Subject: "周报", HTML: html, Text: text,
|
|
Images: []InlineImage{{Name: "dashboard.png", ContentType: "image/png", CID: "weekly-dashboard@knowledge-governance", Bytes: png}}}, "<id@example.com>", "BOUND")
|
|
for _, want := range []string{"Cc: b@example.com", "multipart/related", "multipart/alternative", "Content-ID: <weekly-dashboard@knowledge-governance>", "Content-Disposition: inline; filename=\"dashboard.png\"", "image/png"} {
|
|
if !strings.Contains(raw, want) {
|
|
t.Fatalf("MIME payload missing %q", want)
|
|
}
|
|
}
|
|
if !strings.Contains(html, "cid:weekly-dashboard@knowledge-governance") {
|
|
t.Fatal("html body must reference the inline cid")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeAcceptsAnOptionalReviewAddressAndRejectsMalformedOnes(t *testing.T) {
|
|
good, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Review: "owner@example.com", Enabled: true})
|
|
if err != nil || good.Review != "owner@example.com" {
|
|
t.Fatalf("review address rejected: %q %v", good.Review, err)
|
|
}
|
|
if _, err := normalize(SettingsInput{Host: "smtp.example.com", From: "kb@example.com", Recipients: []string{"a@example.com"}, Review: "not-an-address"}); err != ErrInvalid {
|
|
t.Fatal("a malformed review address must be rejected")
|
|
}
|
|
}
|