Files
Ydisks-Xianyu-Helper/internal/db/extras_test.go
T
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

464 lines
16 KiB
Go

package db
import (
"context"
"errors"
"testing"
"time"
)
// --- extras.go ---
// TestKeywords_AllWithType AllWithType 路径 + Add 默认 type。
func TestKeywords_AllWithType(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
// Add 不指定 type → 默认 text。
id1, err := s.Keywords.Add(ctx, cid, "你好", "在的", "", "", "")
if err != nil || id1 == 0 {
t.Fatalf("Add: %d %v", id1, err)
}
// 指定 type=image + imageURL + itemID。
if _, err := s.Keywords.Add(ctx, cid, "图", "[img]", "item1", "image", "http://img"); err != nil {
t.Fatalf("Add image: %v", err)
}
if _, err := s.Keywords.Add(ctx, cid, "你好呀", "更精确", "", "text", ""); err != nil {
t.Fatal(err)
}
kws, err := s.Keywords.AllWithType(ctx, cid)
if err != nil {
t.Fatalf("AllWithType: %v", err)
}
if len(kws) != 3 {
t.Fatalf("len=%d want 3", len(kws))
}
if kws[0].Keyword != "你好呀" {
t.Fatalf("longest keyword must win deterministically: %#v", kws)
}
// 验证默认 type=text 兜底(第一条)。
var foundText, foundImage bool
for _, k := range kws {
if k.Keyword == "你好" && k.Reply == "在的" && k.Type == "text" {
foundText = true
}
if k.Keyword == "图" && k.Reply == "[img]" && k.Type == "image" && k.ImageURL == "http://img" && k.ItemID == "item1" {
foundImage = true
}
}
if !foundText || !foundImage {
t.Fatalf("关键字字段不符: %#v", kws)
}
}
// TestKeywords_AllRowsEmptyCookie AllRows 对无关键字的账号返回 nil 不报错。
func TestKeywords_AllRowsEmptyCookie(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
seedAccount(t, s)
rows, err := s.Keywords.AllRows(ctx, "no-such-cookie")
if err != nil || len(rows) != 0 {
t.Fatalf("AllRows 空: %#v err=%v", rows, err)
}
}
// TestItemReplies_SetDelete ItemReplies.Set/Delete/AllForUser + Get。
func TestItemReplies_SetDelete(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
// Get 不存在 → ErrNotFound。
if _, err := s.ItemReps.Get(ctx, cid, "i1"); !errors.Is(err, ErrNotFound) {
t.Fatalf("want ErrNotFound, got %v", err)
}
// Set 写入。
if err := s.ItemReps.Set(ctx, cid, "i1", "回复A"); err != nil {
t.Fatalf("Set: %v", err)
}
got, err := s.ItemReps.Get(ctx, cid, "i1")
if err != nil || got.ReplyContent != "回复A" {
t.Fatalf("Get: %#v err=%v", got, err)
}
// 二次 Set 同 item → 覆盖(先删后插)。
if err := s.ItemReps.Set(ctx, cid, "i1", "回复B"); err != nil {
t.Fatalf("Set overwrite: %v", err)
}
got, _ = s.ItemReps.Get(ctx, cid, "i1")
if got.ReplyContent != "回复B" {
t.Fatalf("覆盖后=%q want 回复B", got.ReplyContent)
}
// AllForUser。
all, err := s.ItemReps.AllForUser(ctx, cid)
if err != nil || len(all) != 1 {
t.Fatalf("AllForUser: %#v err=%v", all, err)
}
// Delete。
if err := s.ItemReps.Delete(ctx, cid, "i1"); err != nil {
t.Fatalf("Delete: %v", err)
}
if _, err := s.ItemReps.Get(ctx, cid, "i1"); !errors.Is(err, ErrNotFound) {
t.Fatalf("Delete 后应 ErrNotFound, got %v", err)
}
}
// TestDefaultReplies_GetAndRecord DefaultReplies.Get/HasRecord/AddRecord。
func TestDefaultReplies_GetAndRecord(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
// Get 不存在 → ErrNotFound。
if _, err := s.DefaultReps.Get(ctx, cid); !errors.Is(err, ErrNotFound) {
t.Fatalf("want ErrNotFound, got %v", err)
}
// 直接写入一条。
_, _ = s.DB.ExecContext(ctx,
`INSERT INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once) VALUES (?, 1, '你好', 'http://img', 1)`,
cid)
dr, err := s.DefaultReps.Get(ctx, cid)
if err != nil {
t.Fatalf("Get: %v", err)
}
if !dr.Enabled || dr.ReplyContent != "你好" || dr.ReplyImageURL != "http://img" || !dr.ReplyOnce {
t.Fatalf("default reply: %#v", dr)
}
// HasRecord 未记录 → false。
if s.DefaultReps.HasRecord(ctx, cid, "chat1") {
t.Fatal("未记录应 false")
}
// AddRecord → 已记录。
if err := s.DefaultReps.AddRecord(ctx, cid, "chat1"); err != nil {
t.Fatalf("AddRecord: %v", err)
}
if !s.DefaultReps.HasRecord(ctx, cid, "chat1") {
t.Fatal("记录后应 true")
}
// 重复 AddRecord 应不报错(INSERT IGNORE)。
if err := s.DefaultReps.AddRecord(ctx, cid, "chat1"); err != nil {
t.Fatalf("重复 AddRecord: %v", err)
}
}
// TestNotifications_GetChannelAndAccountChannels GetChannel + AccountChannels。
func TestNotifications_GetChannelAndAccountChannels(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
uid, cid := seedAccount(t, s)
// GetChannel 不存在 → nil, nil。
got, err := s.Notifications.GetChannel(ctx, 99999)
if err != nil || got != nil {
t.Fatalf("GetChannel 不存在应 nil, got %#v err=%v", got, err)
}
chID, _ := s.Notifications.CreateChannel(ctx, &NotificationChannelRow{
Name: "wh", Type: "webhook", Config: `{"url":"x"}`, Enabled: true, UserID: uid,
})
got, err = s.Notifications.GetChannel(ctx, chID)
if err != nil || got == nil || got.Name != "wh" || got.Config != `{"url":"x"}` {
t.Fatalf("GetChannel: %#v err=%v", got, err)
}
// AccountChannels:绑定渠道后查询。
s.Notifications.SetBindings(ctx, cid, []int64{chID})
channels, err := s.Notifications.AccountChannels(ctx, cid)
if err != nil {
t.Fatalf("AccountChannels: %v", err)
}
if len(channels) != 1 || channels[0].ID != chID || channels[0].Name != "wh" {
t.Fatalf("AccountChannels: %#v", channels)
}
// 禁用渠道后不应再返回。
s.Notifications.UpdateChannel(ctx, &NotificationChannelRow{
ID: chID, Name: "wh", Type: "webhook", Config: `{"url":"x"}`, Enabled: false, UserID: uid,
})
channels, _ = s.Notifications.AccountChannels(ctx, cid)
if len(channels) != 0 {
t.Fatalf("禁用渠道后 AccountChannels len=%d want 0", len(channels))
}
}
func TestNotificationsRejectCrossUserBindings(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
uid, cid := seedAccount(t, s)
if ok, err := s.Users.Create(ctx, "u2", "u2@example.com", "pw"); err != nil || !ok {
t.Fatalf("create second user: ok=%v err=%v", ok, err)
}
u2, _ := s.Users.GetByUsername(ctx, "u2")
ownCh, _ := s.Notifications.CreateChannel(ctx, &NotificationChannelRow{
Name: "own", Type: "webhook", Config: `{}`, Enabled: true, UserID: uid,
})
otherCh, _ := s.Notifications.CreateChannel(ctx, &NotificationChannelRow{
Name: "other", Type: "webhook", Config: `{}`, Enabled: true, UserID: u2.ID,
})
if err := s.Notifications.SetBindings(ctx, cid, []int64{otherCh}); !errors.Is(err, ErrForbidden) {
t.Fatalf("cross-user SetBindings should be forbidden, got %v", err)
}
if err := s.Notifications.SetBindings(ctx, cid, []int64{ownCh}); err != nil {
t.Fatalf("own SetBindings: %v", err)
}
// 模拟历史脏数据:当前账号绑定到了其他用户渠道,读取侧也必须过滤掉。
_, _ = s.DB.ExecContext(ctx, `INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES (?, ?, 1)`, cid, otherCh)
bindings, err := s.Notifications.AccountBindings(ctx, cid)
if err != nil {
t.Fatalf("AccountBindings: %v", err)
}
if len(bindings) != 1 || bindings[0] != ownCh {
t.Fatalf("bindings should only include own channel: %#v", bindings)
}
channels, err := s.Notifications.AccountChannels(ctx, cid)
if err != nil {
t.Fatalf("AccountChannels: %v", err)
}
if len(channels) != 1 || channels[0].ID != ownCh {
t.Fatalf("channels should only include own channel: %#v", channels)
}
}
// TestSettings_GetAndAll Get 不存在返回空串 + All 全量。
func TestSettings_GetAndAll(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
// Get 不存在 → 空串不报错。
v, err := s.Settings.Get(ctx, "no-such-key")
if err != nil || v != "" {
t.Fatalf("Get 不存在: v=%q err=%v", v, err)
}
// 写入两条。
s.Settings.Set(ctx, "k1", "v1")
s.Settings.Set(ctx, "k2", "v2")
// 二次 Set 同 key → 覆盖(UPSERT)。
s.Settings.Set(ctx, "k1", "v1-updated")
all, err := s.Settings.All(ctx)
if err != nil {
t.Fatalf("All: %v", err)
}
if all["k1"] != "v1-updated" || all["k2"] != "v2" {
t.Fatalf("All = %#v", all)
}
got, _ := s.Settings.Get(ctx, "k1")
if got != "v1-updated" {
t.Fatalf("Get k1=%q want v1-updated", got)
}
}
// TestSettings_Public Public 过滤:白名单 key 返回值,私有 key 不外露。
func TestSettings_Public(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
// 写入一个公开 key + 几个遗留/私有 key。
s.Settings.Set(ctx, "theme_color", "blue")
s.Settings.Set(ctx, "registration_enabled", "true")
s.Settings.Set(ctx, "show_default_login_info", "true")
s.Settings.Set(ctx, "private_secret", "topsecret")
s.Settings.Set(ctx, "qq_reply_secret_key", "should-not-leak")
pub, err := s.Settings.Public(ctx)
if err != nil {
t.Fatalf("Public: %v", err)
}
if pub["theme_color"] != "blue" {
t.Fatalf("公开 key 缺失: %#v", pub)
}
// 私有 key 不应出现。
if _, ok := pub["private_secret"]; ok {
t.Fatal("private_secret 不应外露")
}
if _, ok := pub["qq_reply_secret_key"]; ok {
t.Fatal("qq_reply_secret_key 不应外露")
}
if _, ok := pub["login_captcha_enabled"]; ok {
t.Fatal("未实现的登录验证码开关不应公开")
}
if _, ok := pub["registration_enabled"]; ok {
t.Fatal("未实现的注册开关不应公开")
}
if _, ok := pub["show_default_login_info"]; ok {
t.Fatal("未使用的默认登录提示开关不应公开")
}
}
// --- cookies.go ---
// TestCookies_DeleteAndStatuses Delete/GetValue/GetDetails/SetStatus/GetStatus/UpdateProfile。
func TestCookies_DeleteAndStatuses(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
// GetValue。
v, err := s.Cookies.GetValue(ctx, cid)
if err != nil || v != "cv=admin" {
t.Fatalf("GetValue: v=%q err=%v", v, err)
}
// GetValue 不存在。
if _, err := s.Cookies.GetValue(ctx, "nope"); !errors.Is(err, ErrNotFound) {
t.Fatalf("want ErrNotFound, got %v", err)
}
// GetDetails 不存在。
if _, err := s.Cookies.GetDetails(ctx, "nope"); !errors.Is(err, ErrNotFound) {
t.Fatalf("want ErrNotFound, got %v", err)
}
if _, err := s.DB.ExecContext(ctx, `UPDATE cookies SET remark=NULL, username=NULL, password=NULL WHERE id=?`, cid); err != nil {
t.Fatalf("set nullable text fields: %v", err)
}
details, err := s.Cookies.GetDetails(ctx, cid)
if err != nil {
t.Fatalf("GetDetails should tolerate NULL text fields: %v", err)
}
if details.Remark != "" || details.Username != "" || details.Password != "" {
t.Fatalf("NULL text fields should decode as empty strings: %+v", details)
}
// GetAutoConfirm 不存在 → ErrNotFound。
if _, err := s.Cookies.GetAutoConfirm(ctx, "nope"); !errors.Is(err, ErrNotFound) {
t.Fatalf("GetAutoConfirm 不存在应 ErrNotFound, got %v", err)
}
// GetAutoConfirm 存在(默认 enabled=true)。
if enabled, err := s.Cookies.GetAutoConfirm(ctx, cid); err != nil || !enabled {
t.Fatalf("GetAutoConfirm 存在: enabled=%v err=%v", enabled, err)
}
// SetStatus false → GetStatus false。
if err := s.Cookies.SetStatus(ctx, cid, false); err != nil {
t.Fatalf("SetStatus false: %v", err)
}
if s.Cookies.GetStatus(ctx, cid) {
t.Fatal("GetStatus 应 false")
}
// SetStatus true。
s.Cookies.SetStatus(ctx, cid, true)
if !s.Cookies.GetStatus(ctx, cid) {
t.Fatal("GetStatus 应 true")
}
// 不存在的账号和数据库错误都必须安全地按停用处理。
if s.Cookies.GetStatus(ctx, "no-such-cookie") {
t.Fatal("不存在账号 GetStatus 应返回 false")
}
// UpdateProfile。
if err := s.Cookies.UpdateProfile(ctx, cid, "昵称", "http://avatar"); err != nil {
t.Fatalf("UpdateProfile: %v", err)
}
d, _ := s.Cookies.GetDetails(ctx, cid)
if d.Nickname != "昵称" || d.AvatarURL != "http://avatar" {
t.Fatalf("UpdateProfile 后: %#v", d)
}
// AllForUser userID=0 取全部(管理员视图)。
all, err := s.Cookies.AllForUser(ctx, 0)
if err != nil || all[cid] != "cv=admin" {
t.Fatalf("AllForUser(0): %#v err=%v", all, err)
}
if _, err := s.DB.ExecContext(ctx, `INSERT INTO item_replay (item_id,cookie_id,reply_content) VALUES ('item-stale',?,'不应残留')`, cid); err != nil {
t.Fatalf("seed item_replay: %v", err)
}
// Delete cookie → 无外键的 item_replay 也必须显式清理。
if err := s.Cookies.Delete(ctx, cid); err != nil {
t.Fatalf("Delete: %v", err)
}
if _, err := s.Cookies.GetValue(ctx, cid); !errors.Is(err, ErrNotFound) {
t.Fatalf("Delete 后 GetValue 应 ErrNotFound, got %v", err)
}
var stale int
if err := s.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM item_replay WHERE cookie_id=?`, cid).Scan(&stale); err != nil || stale != 0 {
t.Fatalf("Delete 后 item_replay count=%d err=%v", stale, err)
}
}
// TestCookies_SaveReuseUserID Save 在 userID=0 且 cookie 已存在时复用 user_id。
func TestCookies_SaveReuseUserID(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
uid, cid := seedAccount(t, s)
// cookie 已存在;用 userID=0 调 Save 应复用 existing user_id 并更新 value。
if err := s.Cookies.Save(ctx, cid, "new-value", 0); err != nil {
t.Fatalf("Save reuse: %v", err)
}
v, _ := s.Cookies.GetValue(ctx, cid)
if v != "new-value" {
t.Fatalf("value=%q want new-value", v)
}
// 验证 user_id 仍指向 admin。
d, _ := s.Cookies.GetDetails(ctx, cid)
if d.UserID != uid {
t.Fatalf("UserID=%d want %d", d.UserID, uid)
}
// Save 一个不存在的 cookie 且 userID=0 → 报错。
if err := s.Cookies.Save(ctx, "ghost-cookie", "v", 0); err == nil {
t.Fatal("不存在的 cookie + userID=0 应报错")
}
}
// TestCookies_GetPauseDurationExplicit 显式 pause_duration=0 应返回 0(有效值)。
func TestCookies_GetPauseDurationExplicit(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
// 显式置 0。
_, _ = s.DB.ExecContext(ctx, `UPDATE cookies SET pause_duration=0 WHERE id=?`, cid)
if pd := s.Cookies.GetPauseDuration(ctx, cid); pd != 0 {
t.Fatalf("GetPauseDuration=%d want 0", pd)
}
// 不存在的 cookie → 默认 10。
if pd := s.Cookies.GetPauseDuration(ctx, "nope"); pd != 10 {
t.Fatalf("GetPauseDuration 不存在=%d want 10", pd)
}
}
func TestCookies_SetPauseAndAutomaticExpiry(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
ctx := context.Background()
_, cid := seedAccount(t, s)
pausedUntil, err := s.Cookies.SetPause(ctx, cid, 15)
if err != nil {
t.Fatal(err)
}
paused, storedUntil, err := s.Cookies.IsPaused(ctx, cid)
if err != nil || !paused || storedUntil != pausedUntil || pausedUntil <= time.Now().UTC().Unix() {
t.Fatalf("pause state: paused=%v until=%d stored=%d err=%v", paused, pausedUntil, storedUntil, err)
}
if _, err := s.DB.ExecContext(ctx, `UPDATE cookies SET paused_until=? WHERE id=?`, time.Now().UTC().Add(-time.Second).Unix(), cid); err != nil {
t.Fatal(err)
}
if paused, _, err = s.Cookies.IsPaused(ctx, cid); err != nil || paused {
t.Fatalf("expired pause must be inactive: paused=%v err=%v", paused, err)
}
if until, err := s.Cookies.SetPause(ctx, cid, 0); err != nil || until != 0 {
t.Fatalf("cancel pause: until=%d err=%v", until, err)
}
}
func TestCookiesGetStatusFailsClosedOnDatabaseError(t *testing.T) {
s, cleanup := newTestDB(t)
defer cleanup()
if err := s.DB.Close(); err != nil {
t.Fatal(err)
}
if s.Cookies.GetStatus(context.Background(), "cid") {
t.Fatal("database error must not silently enable an account")
}
}