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 项全绿。
131 lines
5.2 KiB
Go
131 lines
5.2 KiB
Go
package qawemail
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
// TestScheduledSendRequiresConfirmationAndSendsExactlyOnce pins the whole gate against a real
|
|
// database: only an enabled slot, a closed week and an unchanged confirmation produce one message.
|
|
func TestScheduledSendRequiresConfirmationAndSendsExactlyOnce(t *testing.T) {
|
|
dsn := os.Getenv("KG_WEEKLY_MAIL_TEST_DSN")
|
|
if dsn == "" {
|
|
t.Skip("explicit disposable weekly-mail database required")
|
|
}
|
|
if !strings.Contains(dsn, "/kg_weekly_mail_test_") {
|
|
t.Fatal("refusing a non-disposable database")
|
|
}
|
|
if strings.Contains(dsn, "@postgres:") || strings.Contains(dsn, "/governance?") {
|
|
t.Fatal("refusing the shared governance database")
|
|
}
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer pool.Close()
|
|
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mailer := &fakeMailer{}
|
|
store := NewStore(pool, "fixture-weekly-mail", os.Getenv("KG_WEEKLY_MAIL_TEST_KEY"), mailer)
|
|
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_email_sends WHERE tenant_key=$1`, store.tenant); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_confirmations WHERE tenant_key=$1`, store.tenant); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := pool.Exec(ctx, `DELETE FROM qa_weekly_email_settings WHERE tenant_key=$1`, store.tenant); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := pool.Exec(ctx, `DELETE FROM qa_ops_weekly_drafts WHERE tenant_key=$1`, store.tenant); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
markdown := "# 知识库周报 2026 WK43\n\n## 二、核心指标\n\n正确率:90.66%\n"
|
|
if _, err := pool.Exec(ctx, `INSERT INTO qa_ops_weekly_drafts(tenant_key,week,revision,markdown,seed,updated_by,updated_at) VALUES($1,'2026-W43',2,$2,'{}'::jsonb,'fixture-admin',now())`, store.tenant, markdown); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Slot: Monday 09:00 Beijing of the week after 2026-W43 closes.
|
|
slot := time.Date(2026, 10, 26, 1, 5, 0, 0, time.UTC)
|
|
if _, err := store.SaveSettings(ctx, "fixture-admin", SettingsInput{
|
|
BaseRevision: 0, Host: "localhost", Port: 465, From: "kb@example.com",
|
|
Recipients: []string{"lead@example.com"}, Weekday: 1, Hour: 9, Enabled: true,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Confirm(ctx, "2026-W43", "fixture-admin"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unconfirmed := []string{"2026-W44"}
|
|
_ = unconfirmed
|
|
|
|
if result, err := store.SendDue(ctx, slot); err != nil || result.State != "blocked" {
|
|
// No sealed SMTP password was configured, so the send must be blocked rather than faked.
|
|
if err != nil || result.State != "blocked" {
|
|
t.Fatalf("missing password must block sending, got %+v %v", result, err)
|
|
}
|
|
}
|
|
if len(mailer.sent) != 0 {
|
|
t.Fatal("a blocked send must not reach the mailer")
|
|
}
|
|
|
|
// Seed a password through the real encryption path, then send inside the slot.
|
|
if _, err := store.SaveSettings(ctx, "fixture-admin", SettingsInput{
|
|
BaseRevision: 1, Host: "localhost", Port: 465, From: "kb@example.com", Password: "app-password",
|
|
Recipients: []string{"lead@example.com"}, Weekday: 1, Hour: 9, Enabled: true,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
offSlot := slot.Add(2 * time.Hour)
|
|
if result, err := store.SendDue(ctx, offSlot); err != nil || result.State != "not_due" {
|
|
t.Fatalf("outside the configured hour nothing may be sent, got %+v %v", result, err)
|
|
}
|
|
sent, err := store.SendDue(ctx, slot)
|
|
if err != nil || sent.State != "sent" || sent.Week != "2026-W43" {
|
|
t.Fatalf("expected one sent report, got %+v %v", sent, err)
|
|
}
|
|
if len(mailer.sent) != 1 {
|
|
t.Fatalf("mailer calls = %d, want 1", len(mailer.sent))
|
|
}
|
|
message := mailer.sent[0]
|
|
if len(message.To) != 1 || message.To[0] != "lead@example.com" || !strings.Contains(message.Subject, "2026-W43") {
|
|
t.Fatalf("message envelope wrong: %+v", message)
|
|
}
|
|
if !strings.Contains(message.HTML, "90.66%") || !strings.Contains(message.Text, "90.66%") {
|
|
t.Fatal("both HTML and text must carry the confirmed report body")
|
|
}
|
|
if mailer.configs[0].Password != "app-password" {
|
|
t.Fatal("the sealed password must be unsealed for the SMTP call")
|
|
}
|
|
again, err := store.SendDue(ctx, slot)
|
|
// 同一时段第二次触发必须幂等:已 sent 就不再投递,也不谎报成“没有可发的周报”。
|
|
if err != nil || again.State != "already_sent" {
|
|
t.Fatalf("a second attempt in the same slot must not resend, got %+v %v", again, err)
|
|
}
|
|
if again.MessageID == "" {
|
|
t.Fatal("already_sent 必须带回第一次真实发送的 message_id")
|
|
}
|
|
if len(mailer.sent) != 1 {
|
|
t.Fatal("duplicate send detected")
|
|
}
|
|
|
|
// Editing the draft invalidates the confirmation: the changed body must never be mailed.
|
|
if _, err := pool.Exec(ctx, `UPDATE qa_ops_weekly_drafts SET revision=3,markdown=$2 WHERE tenant_key=$1 AND week='2026-W43'`, store.tenant, markdown+"\n改过的正文\n"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stale, err := store.SendDue(ctx, time.Date(2026, 11, 2, 1, 5, 0, 0, time.UTC))
|
|
if err != nil || stale.State != "nothing_confirmed" {
|
|
t.Fatalf("a changed body must invalidate the confirmation, got %+v %v", stale, err)
|
|
}
|
|
if len(mailer.sent) != 1 {
|
|
t.Fatal("changed body was mailed despite confirmation invalidation")
|
|
}
|
|
}
|