Files
knowledge-governance/internal/qawemail/scheduling_integration_test.go
T
hermes 43b1e423fe
build / build (push) Successful in 1m11s
fix(qa): 规范化邮件调度测试并声明其外部依赖
gofmt 修正;该集成测试需要一次性库与测试密钥,按仓库约定在 test-scope.json 声明为可选未运行。
2026-10-26 10:01:14 +08:00

127 lines
5.0 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)
if err != nil || again.State != "nothing_confirmed" {
t.Fatalf("a second attempt in the same slot must not resend, got %+v %v", again, err)
}
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")
}
}