Files
Ydisks-Xianyu-Helper/internal/notify/notifier_dispatch_test.go
Christ 16d63dc7c9 完善系统可靠性、登录风控与跨数据库回归
可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。

登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。

安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。

自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。

前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。

测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。

验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
2026-07-14 21:20:22 +08:00

355 lines
11 KiB
Go
Raw Permalink 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 notify
import (
"context"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"xianyu-go/internal/db"
)
// nilLogger 返回一个丢弃所有输出的 logger,用于不需要日志噪声的测试。
func nilLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(io.Discard, nil))
}
// newNotifyStoreBare 提供一个独立 store,方便各测试用例自由构造数据。
// 预置一个 admin 用户和一个 cookie_id="cid" 的 cookie 记录以满足外键约束。
func newNotifyStoreBare(t *testing.T) (*db.Store, func()) {
t.Helper()
d, _, err := db.Open(context.Background(), filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatalf("Open: %v", err)
}
s := db.NewStore(d, db.DialectSQLite)
s.Users.Create(context.Background(), "admin", "a@e.com", "pw")
admin, _ := s.Users.GetByUsername(context.Background(), "admin")
s.Cookies.Save(context.Background(), "cid", "unb=123; _m_h5_tk=tk_1;", admin.ID)
return s, func() { _ = d.Close() }
}
// addWebhookChannel 插入一个 webhook 渠道并绑定到 cookieID,返回渠道 ID。
func addWebhookChannel(t *testing.T, s *db.Store, cookieID, name, webhookURL string) int64 {
t.Helper()
res, err := s.DB.ExecContext(context.Background(),
`INSERT INTO notification_channels (name,type,config,enabled,user_id) VALUES (?,?,?,1,1)`,
name, "webhook", `{"webhook_url":"`+webhookURL+`"}`)
if err != nil {
t.Fatalf("insert channel: %v", err)
}
id, _ := res.LastInsertId()
if _, err := s.DB.ExecContext(context.Background(),
`INSERT INTO message_notifications (cookie_id,channel_id,enabled) VALUES (?,?,1)`,
cookieID, id); err != nil {
t.Fatalf("insert binding: %v", err)
}
return id
}
// TestNotifyAccountAlert_NoStore store 为 nil 时安全返回。
func TestNotifyAccountAlert_NoStore(t *testing.T) {
n := &Notifier{logger: nilLogger()}
// 不应 panic。
n.NotifyAccountAlert("cid", "warn", "标题", "正文")
}
// TestNotifyAccountAlert_NoChannels 无绑定渠道时不报错。
func TestNotifyAccountAlert_NoChannels(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
n := New("cid", s, nil)
n.NotifyAccountAlert("cid", "warn", "标题", "正文")
}
// TestNotifyAccountAlert_WithChannel 告警通知发送并包含标题与正文。
func TestNotifyAccountAlert_WithChannel(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.WriteHeader(200)
}))
defer srv.Close()
addWebhookChannel(t, s, "cid", "告警渠道", srv.URL)
n := New("cid", s, nil)
n.NotifyAccountAlert("cid", "critical", "Token失效", "请重新登录")
if gotBody == "" {
t.Fatal("应发送告警通知")
}
if !strings.Contains(gotBody, "严重") || !strings.Contains(gotBody, "Token失效") || !strings.Contains(gotBody, "请重新登录") {
t.Errorf("告警正文缺少关键字: %s", gotBody)
}
}
func TestNotifyEventUsesPersistentOutboxWhenStarted(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
addWebhookChannel(t, s, "cid", "outbox", srv.URL)
n := New("cid", s, nilLogger())
// 标记异步模式但不启动循环,精确验证调用返回时尚未发生外部网络请求。
n.started.Store(true)
n.NotifyAccountAlert("cid", "warn", "持久化通知", "正文")
if got := atomic.LoadInt32(&calls); got != 0 {
t.Fatalf("business call performed synchronous network I/O: %d", got)
}
var queued int
if err := s.DB.QueryRow(`SELECT COUNT(*) FROM notification_outbox WHERE status='pending'`).Scan(&queued); err != nil || queued != 1 {
t.Fatalf("queued=%d err=%v", queued, err)
}
n.drainOutbox(context.Background())
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("outbox delivery calls=%d", got)
}
if err := s.DB.QueryRow(`SELECT COUNT(*) FROM notification_outbox`).Scan(&queued); err != nil || queued != 0 {
t.Fatalf("remaining=%d err=%v", queued, err)
}
}
// TestNotifyAccountAlert_LevelLabels 覆盖 levelLabel 各分支。
func TestNotifyAccountAlert_LevelLabels(t *testing.T) {
cases := map[string]string{
"critical": "严重",
"warn": "警告",
"info": "提示",
"unknown": "unknown",
}
for level, want := range cases {
if got := levelLabel(level); got != want {
t.Errorf("levelLabel(%q)=%q want %q", level, got, want)
}
}
}
// TestSendToChannel 直接发送到指定渠道。
func TestSendToChannel(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.WriteHeader(200)
}))
defer srv.Close()
chID := addWebhookChannel(t, s, "cid", "直发渠道", srv.URL)
n := New("cid", s, nil)
if err := n.SendToChannel(chID, "直发测试"); err != nil {
t.Fatalf("SendToChannel: %v", err)
}
if !strings.Contains(gotBody, "直发测试") {
t.Errorf("正文缺失: %s", gotBody)
}
}
// TestSendToChannel_Errors store 未初始化 / 渠道不存在。
func TestSendToChannel_Errors(t *testing.T) {
// store 为 nil。
n := &Notifier{logger: nilLogger()}
if err := n.SendToChannel(1, "x"); err == nil {
t.Fatal("store 为 nil 应报错")
}
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
n2 := New("cid", s, nil)
// 不存在的渠道 ID。
if err := n2.SendToChannel(99999, "x"); err == nil {
t.Fatal("渠道不存在应报错")
}
}
// TestNotifyDelivery_MultiChannel 某渠道失败不影响其他渠道。
func TestNotifyDelivery_MultiChannel(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var okCount int32
// 成功渠道。
okSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
atomic.AddInt32(&okCount, 1)
w.WriteHeader(200)
}))
defer okSrv.Close()
// 失败渠道(始终 500)。
failSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
w.WriteHeader(500)
}))
defer failSrv.Close()
addWebhookChannel(t, s, "cid", "成功渠道", okSrv.URL)
addWebhookChannel(t, s, "cid", "失败渠道", failSrv.URL)
n := New("cid", s, nil)
n.NotifyDelivery("cid", "买家", "b1", "item1", "成功", "chat1")
if got := atomic.LoadInt32(&okCount); got != 1 {
t.Errorf("成功渠道应收到 1 次,实际 %d", got)
}
}
// TestNotifyDelivery_TemplateVars 通知正文应包含买家名、商品ID、结果等变量。
func TestNotifyDelivery_TemplateVars(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.WriteHeader(200)
}))
defer srv.Close()
addWebhookChannel(t, s, "cid", "模板渠道", srv.URL)
n := New("cid", s, nil)
n.NotifyDelivery("cid", "张三", "BID123", "ITEM456", "发货成功", "CHAT789")
for _, want := range []string{"张三", "BID123", "ITEM456", "发货成功", "CHAT789"} {
if !strings.Contains(gotBody, want) {
t.Errorf("正文缺少 %q: %s", want, gotBody)
}
}
}
// TestNotifyDelivery_EmptyChatID chatID 为空时回退为“未知”。
func TestNotifyDelivery_EmptyChatID(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
var gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.WriteHeader(200)
}))
defer srv.Close()
addWebhookChannel(t, s, "cid", "渠道", srv.URL)
n := New("cid", s, nil)
n.NotifyDelivery("cid", "买家", "b1", "item1", "结果", "")
if !strings.Contains(gotBody, "未知") {
t.Errorf("空 chatID 应回退为“未知”: %s", gotBody)
}
}
// TestParseConfig_InvalidJSON 非法 JSON 走旧格式兼容分支。
func TestParseConfig_InvalidJSON(t *testing.T) {
m := parseConfig("{not json")
if m["config"] != "{not json" {
t.Errorf("非法 JSON 应放入 config: %v", m)
}
}
// TestStrOr 覆盖 string / 非 string / 缺失三个分支。
func TestStrOr(t *testing.T) {
m := map[string]any{
"s": "abc",
"n": 42,
"b": true,
}
if got := strOr(m, "s", "x"); got != "abc" {
t.Errorf("strOr(s)=%q", got)
}
if got := strOr(m, "n", "x"); got != "42" {
t.Errorf("strOr(n)=%q", got)
}
if got := strOr(m, "b", "x"); got != "true" {
t.Errorf("strOr(b)=%q", got)
}
if got := strOr(m, "missing", "def"); got != "def" {
t.Errorf("strOr(missing)=%q", got)
}
}
// TestFallback fallback 空串与非空串。
func TestFallback(t *testing.T) {
if got := fallback("", "默认"); got != "默认" {
t.Errorf("fallback('')=%q", got)
}
if got := fallback("值", "默认"); got != "值" {
t.Errorf("fallback('值')=%q", got)
}
}
// TestNotifyAccountAlert_DBError 查询出错时不 panic(err != nil 分支)。
func TestNotifyAccountAlert_DBError(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
d := s.DB
cleanup() // 提前关闭 DB,使查询返回错误
_ = d
n := New("cid", s, nil)
// store 非 nil 但 DB 已关闭,查询会报错;不应 panic。
n.NotifyAccountAlert("cid", "warn", "标题", "正文")
}
// TestNotifyDelivery_DBError 查询出错时不 panic(err != nil 分支)。
func TestNotifyDelivery_DBError(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
cleanup() // 提前关闭 DB
n := New("cid", s, nil)
n.NotifyDelivery("cid", "买家", "b1", "item1", "结果", "chat1")
}
// TestSendToChannel_DBError 查询渠道出错时返回包装错误。
func TestSendToChannel_DBError(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
cleanup() // 提前关闭 DB
n := New("cid", s, nil)
if err := n.SendToChannel(1, "x"); err == nil {
t.Fatal("DB 查询出错应返回 error")
}
}
// TestNotifyAccountAlert_SendError 某渠道发送失败时记录错误但不中断。
func TestNotifyAccountAlert_SendError(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer srv.Close()
addWebhookChannel(t, s, "cid", "失败渠道", srv.URL)
n := New("cid", s, nil)
// 渠道返回 5xx,send 报错 → 走 logger.Error 分支,但不应 panic。
n.NotifyAccountAlert("cid", "warn", "标题", "正文")
}
// TestNew_DefaultLogger logger 为 nil 时使用默认 logger。
func TestNew_DefaultLogger(t *testing.T) {
s, cleanup := newNotifyStoreBare(t)
defer cleanup()
n := New("cid", s, nil)
if n == nil || n.logger == nil || n.httpc == nil {
t.Fatal("New 未正确初始化字段")
}
if n.cookieID != "cid" {
t.Errorf("cookieID=%q", n.cookieID)
}
if n.httpc.Timeout != 10*time.Second {
t.Errorf("httpc timeout=%v", n.httpc.Timeout)
}
}