1) 发送记录读回(确定性回归):Sends() 的 SELECT 只有 6 列,Scan 却收 7 个字段(多出 message_id)。只要历史里存在任意一条发送记录,整个列表就返回 ErrUnavailable —— 发送 成功后页面刷新、以及“完成读真实发送记录”都会失败。改为查询带 message_id,并检查 rows.Err(),不把截断的列表当成完整历史。 2) 未知投递不再当成可重试失败:DATA 已提交但没收到最终 250(连接中断/超时)时,邮件可能 已被服务器接受;旧实现记 failed 并允许下一分钟重发,会重复投递。现在由 SMTPMailer 在 writer.Close() 失败处抛出窄类型 unknownDeliveryError,deliver 保留 sending 并记录原因, 自动/手动路径都不再重发(手动返回 ERRDeliveryUnknown,HTTP 409 说明原因)。连接建立前 失败、STARTTLS/AUTH/MAIL/RCPT/DATA 命令被拒、正文写入失败仍是确定失败,可如实 failed 并 重试一次。 3) SMTP 交互加有限期限:整体 90 秒(随请求 ctx 收紧),并用 context.AfterFunc 在取消时打断 阻塞读写,避免服务器接受 DATA 后不响应时挂死 worker 与发送行。 验证:真实隔离 PG 先 RED 后 GREEN(非空 sent+failed 历史读回成功并带真实 message_id; HTTP GET /weekly-mail/sends 非空历史 200);本机 TLS SMTP 桩 + 真实 SMTPMailer 覆盖成功、 RCPT 550、DATA 后丢最终 250、DATA 阶段卡死(ctx 800ms 内返回)四类;真实 deliver 在未知 结果时留 sending 且不重发,在明确拒绝时 failed 并可重试一次成功。make verify 17 项全绿。
This commit is contained in:
@@ -39,6 +39,8 @@ func weeklyMailSendError(w http.ResponseWriter, err error) {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "未配置邮件服务器、发件人或收件人,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrSendInFlight):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "该周该版本有一次发送结果未知(记录停在发送中),为避免重复投递未重发;请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrDeliveryUnknown):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 已提交但未收到最终确认,投递结果未知;为避免重复已停止重发,请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrNoPassword):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 授权码缺失或无法解密,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrInvalid):
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
)
|
||||
|
||||
// 页面 reload / “完成读真实发送记录”走的就是这个 handler。用真实 PG 的非空历史证明
|
||||
// 产品读回路径返回 200 且带 message_id,而不是 503——“历史行存在就整个列表不可用”
|
||||
// 正是上一版 Sends() 的确定性回归。
|
||||
func TestWeeklyMailSendsHandlerReadsRealHistoryWithMessageID(t *testing.T) {
|
||||
dsn := os.Getenv("KG_WEEKLY_MAIL_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("显式一次性周报邮件数据库未提供(KG_WEEKLY_MAIL_TEST_DSN):跳过不等于通过")
|
||||
}
|
||||
if !strings.Contains(dsn, "/kg_weekly_mail_test_") || strings.Contains(dsn, "/governance?") {
|
||||
t.Fatal("拒绝非一次性周报测试库")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal("测试库连接参数无效(详情省略)")
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := qawemail.NewStore(pool, "fixture-http-history", os.Getenv("KG_WEEKLY_MAIL_TEST_KEY"), nil)
|
||||
for _, statement := range []string{
|
||||
`DELETE FROM qa_weekly_email_sends WHERE tenant_key=$1`,
|
||||
} {
|
||||
if _, err := pool.Exec(ctx, statement, "fixture-http-history"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO qa_weekly_email_sends(tenant_key,week,draft_revision,body_sha256,status,attempts,message_id,last_error,created_at,updated_at)
|
||||
VALUES($1,'2026-W45',16,$2,'sent',1,'<real@example.com>','',now(),now()),
|
||||
($1,'2026-W44',7,$2,'failed',2,'','smtp RCPT 被拒: 550',now(),now() - interval '1 hour')`,
|
||||
"fixture-http-history", strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends?week=2026-W45", nil)
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(recorder, request, Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("非空历史必须 200,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data struct {
|
||||
Sends []qawemail.Send `json:"sends"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 1 || payload.Data.Sends[0].MessageID != "<real@example.com>" || payload.Data.Sends[0].Status != "sent" {
|
||||
t.Fatalf("发送记录必须带回真实 message_id:%+v", payload.Data.Sends)
|
||||
}
|
||||
|
||||
// 全量历史(不带 week)同样必须可读,且 failed 行不伪造 message_id。
|
||||
all := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends", nil)
|
||||
all = all.WithContext(context.WithValue(all.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
allRecorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(allRecorder, all, Config{QAWeeklyMail: store})
|
||||
if allRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("全量历史必须 200,status=%d body=%s", allRecorder.Code, allRecorder.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(allRecorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 2 {
|
||||
t.Fatalf("应返回 2 条历史,got %+v", payload.Data.Sends)
|
||||
}
|
||||
for _, item := range payload.Data.Sends {
|
||||
if item.Status == "failed" && item.MessageID != "" {
|
||||
t.Fatalf("failed 行不得有 message_id:%+v", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user