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, "