719876d039
build / build (push) Successful in 3m5s
根因:SendDue 同时要求“已到配置时段”与“该周已结束(下周一00:00)”。周日23点的时段
只在周内存在,当周周报永远不满足“已结束”,于是 11/08 23:00 只报 nothing_confirmed、
11/09 00:00 又已成 not_due —— 邮件从未尝试发送,与 SMTP 凭据/网络无关。
改法(最小、不碰历史发布模块):
1. 由发送窗口反推要发的周:周日窗口发当天结束的那一周,其它工作日仍发上一完整周
(周一09:00 语义不变)。自动发送只处理这一周,不再按“所有已结束的周”回填。
2. 认领改为只重认 failed(或首次)记录:sent 幂等返回真实 message_id;sending 视为
结果未知,拒绝盲重发并提示核对邮箱。自动与手工共用同一条渲染/认领/投递路径,
并发或重复点击只投递一次;失败如实写 failed,不假写 sent。
3. 新增管理员正式补发(页面按钮 + GET 预览 + POST /weekly-mail/send/{week}):精确指定
周、已确认版本与正文 hash,必须同源操作意图;未来周拒绝,当前周须到设定时点,
已结束周可显式补发。正文/看板图片/主送/抄送与自动发送一致,结果写入正式发送记录。
这不是“发送测试邮件”(只发 review 地址、不登记)。
验证:真实隔离 PostgreSQL(kg-pgtest-writeset 上的独立一次性库)先 RED(旧实现真实
复现 nothing_confirmed)后 GREEN;覆盖到点前/未来周/未确认/正文变化、周一语义保持、
手工补发一次并读回 message_id、并发只投递一次、结果未知不重发、失败可重试一次。
HTTP 与前端解析用例通过;make verify 17 项全绿。
167 lines
8.0 KiB
Go
167 lines
8.0 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")
|
||
}
|
||
}
|
||
|
||
// 时段自身决定要发哪一周:周日时段发当天结束的那一周,其它工作日发已经结束的上一周。
|
||
// 旧实现只在“周已结束(下周一00:00)”时发,而周日23点这个时段在周内 ⇒ 当周永远发不出去。
|
||
func TestSlotWeekDerivesTheReportWeekFromTheSlot(t *testing.T) {
|
||
cst := time.FixedZone("CST", 8*3600)
|
||
sunday := Settings{Weekday: 7, Hour: 23}
|
||
week, ok := slotWeek(sunday, time.Date(2026, 11, 8, 23, 5, 0, 0, cst))
|
||
if !ok || week != "2026-W45" {
|
||
t.Fatalf("周日23点必须对应当周 2026-W45,got %q %v", week, ok)
|
||
}
|
||
if _, ok := slotWeek(sunday, time.Date(2026, 11, 8, 22, 5, 0, 0, cst)); ok {
|
||
t.Fatal("到点前不得认为时段已到")
|
||
}
|
||
if _, ok := slotWeek(sunday, time.Date(2026, 11, 7, 23, 5, 0, 0, cst)); ok {
|
||
t.Fatal("周六23点不是配置的周日时段")
|
||
}
|
||
// 周一09点保持旧语义:发上一完整周。
|
||
monday := Settings{Weekday: 1, Hour: 9}
|
||
week, ok = slotWeek(monday, time.Date(2026, 11, 9, 9, 5, 0, 0, cst))
|
||
if !ok || week != "2026-W45" {
|
||
t.Fatalf("周一09点必须对应上一完整周 2026-W45,got %q %v", week, ok)
|
||
}
|
||
// 周三09点同理:该周尚未结束,不能提前发。
|
||
wednesday := Settings{Weekday: 3, Hour: 9}
|
||
week, ok = slotWeek(wednesday, time.Date(2026, 11, 11, 9, 5, 0, 0, cst))
|
||
if !ok || week != "2026-W45" {
|
||
t.Fatalf("周三09点必须仍指向 2026-W45,got %q %v", week, ok)
|
||
}
|
||
}
|
||
|
||
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: "localhost", From: "kb@example.com", Recipients: []string{"not-an-address"}, Enabled: true}); err == nil {
|
||
t.Fatal("a malformed recipient must be rejected")
|
||
}
|
||
// 校验会解析主机名;用 localhost 避免把构建机的 DNS 当成被测行为。
|
||
good, err := normalize(SettingsInput{Host: "localhost", 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)
|
||
}
|
||
// 解析不了的主机名必须仍被判为非法:.invalid 是 RFC 6761 保留域,永远不解析。
|
||
if _, err := normalize(SettingsInput{Host: "smtp.kg-test.invalid", From: "kb@example.com",
|
||
Recipients: []string{"a@example.com"}}); err != ErrInvalid {
|
||
t.Fatalf("unresolvable host must stay invalid, got %v", err)
|
||
}
|
||
duplicated := SettingsInput{Host: "localhost", 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: "localhost", 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: "localhost", 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: "localhost", 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: "localhost", From: "kb@example.com", Recipients: []string{"a@example.com"}, Review: "not-an-address"}); err != ErrInvalid {
|
||
t.Fatal("a malformed review address must be rejected")
|
||
}
|
||
}
|