Files
knowledge-governance/internal/qawemail/store_test.go
T
姚章浩 10568943af
build / build (push) Successful in 1m48s
test(qawemail): 用绝对域名断言不可解析主机
构建机 resolv.conf 带 search 域(Tailscale tailf49802.ts.net)时,
smtp.kg-test.invalid 会被拼成 ...tailf49802.ts.net 并由 Fake-IP DNS
解析成功,断言实际测的是构建机搜索域。改成带末尾点的绝对名后与
搜索域无关,仍保持“不可解析主机必须判非法”的原意。
2026-11-16 20:27:25 +08:00

170 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 保留域,永远不解析。
// 末尾的点是必须的:构建机的 resolv.conf 可能带 search 域(本环境 Tailscale
// 的 tailf49802.ts.net),不带点就会被拼成 smtp.kg-test.invalid.<search> 并
// 由 Fake-IP DNS 解析出来,于是这条断言测的是构建机的搜索域而不是被测逻辑。
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")
}
}