Files
Ydisks-Xianyu-Helper/internal/account/manager_test.go
T
ChristandClaude Opus 4.8 3f7954af84 feat: token 保活 + 通知系统 + 批量铺货 + 卡密批量 + 前端路由与样式对齐
后端
- token 刷新:session 过期立即触发密码登录兜底(不再空跑5次冷却);
  FAIL_SYS_SESSION_EXPIRED 识别为凭证失效;token 刷新成功日志;
  在线定时刷新 token 间隔从 1h 调整为 15min,持续续期 cookie
- QR 登录:修复自动验证成功状态被 EXPIRED 覆盖的竞态(前端拿不到 cookie)
- 通知系统:Notifier 加 NotifyAccountAlert/SendToChannel;engine Handler
  加 OnAccountAlert(warn/critical/风控三触发点);handlerAdapter 接 notifier;
  通知渠道测试发送端点 POST /notification-channels/{id}/test;
  NotificationChannelRow 加 json tag 修复前端读不到字段
- 卡密批量:POST /cards/batch 批量创建卡密组;POST /cards/{id}/append-data
  往 data 组充卡密;DB AppendBatchData
- 通知渠道启用切换修复(全量更新避免 type CHECK 约束报错)

前端
- history 路由(/app/ 前缀避开 API 冲突,刷新不丢位置,popstate 同步)
- 通知设置页:渠道管理 + 账号绑定 + 配置指南 + SMTP(从系统设置移入)
- 卡密批量导入弹窗(批量建组 + 单组充卡密)
- 商品卡片缩小约 2/3;Rules/Notifications 头部样式对齐标准页
- 初始化提示按 OS 区分命令(Windows/macOS)
- updateChannel 全量更新修复启用切换失败

文档
- docs/批量铺货使用文档.md(给铺货 agent 的资料包准备规格)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-06 12:27:37 +08:00

116 lines
3.7 KiB
Go

package account
import (
"context"
"path/filepath"
"testing"
"time"
"xianyu-go/internal/db"
"xianyu-go/internal/automation"
"xianyu-go/internal/engine"
)
type noopHandler struct{}
func (noopHandler) HandleChatMessage(context.Context, engine.ChatMessage) error { return nil }
func (noopHandler) HandleSystemEvent(context.Context, automation.Task) error { return nil }
func (noopHandler) OnPasswordLoginRefresh(context.Context, string) bool { return false }
func (noopHandler) OnAccountAlert(context.Context, string, string, string, string) {}
// TestManagerStartStop 验证从 DB 加载账号、启停和 GetInstance。
// 用无效 cookie 让账号快速进入重连等待(不会真正连上),验证管理逻辑而非网络。
func TestManagerStartStop(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "test.db")
d, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
defer d.Close()
store := db.NewStore(d)
store.Users.Create(context.Background(), "admin", "[email protected]", "pw")
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
// 两个启用 + 一个禁用的账号。
store.Cookies.Save(context.Background(), "acc1", "unb=1; _m_h5_tk=t1_1;", admin.ID)
store.Cookies.SetStatus(context.Background(), "acc1", true)
store.Cookies.Save(context.Background(), "acc2", "unb=2; _m_h5_tk=t2_1;", admin.ID)
store.Cookies.SetStatus(context.Background(), "acc2", true)
store.Cookies.Save(context.Background(), "acc3", "unb=3; _m_h5_tk=t3_1;", admin.ID)
store.Cookies.SetStatus(context.Background(), "acc3", false)
mgr := NewManager(store, noopHandler{}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := mgr.StartAll(ctx); err != nil {
t.Fatalf("StartAll: %v", err)
}
// acc1/acc2 应有运行实例,acc3 不应。
for _, id := range []string{"acc1", "acc2"} {
if acc, ok := mgr.GetInstance(id); !ok || acc == nil {
t.Fatalf("GetInstance(%s) 失败", id)
}
}
// GetInstance 可取到。
if acc, ok := mgr.GetInstance("acc1"); !ok || acc == nil {
t.Fatal("GetInstance(acc1) 失败")
}
if _, ok := mgr.GetInstance("acc3"); ok {
t.Fatal("acc3 不应有实例")
}
// Stop 应能干净停止。
mgr.Stop("acc1")
mgr.Stop("acc2")
if _, ok := mgr.GetInstance("acc1"); ok {
t.Fatal("Stop 后 acc1 仍存在")
}
if _, ok := mgr.GetInstance("acc2"); ok {
t.Fatal("Stop 后 acc2 仍存在")
}
}
// TestManagerStopAll 验证 StopAll 停止所有运行中的账号,用于进程优雅退出。
func TestManagerStopAll(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "test.db")
d, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
defer d.Close()
store := db.NewStore(d)
store.Users.Create(context.Background(), "admin", "[email protected]", "pw")
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
// 三个启用账号。
for _, id := range []string{"a1", "a2", "a3"} {
store.Cookies.Save(context.Background(), id, "unb=1; _m_h5_tk=t_1;", admin.ID)
store.Cookies.SetStatus(context.Background(), id, true)
}
mgr := NewManager(store, noopHandler{}, nil)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := mgr.StartAll(ctx); err != nil {
t.Fatalf("StartAll: %v", err)
}
for _, id := range []string{"a1", "a2", "a3"} {
if _, ok := mgr.GetInstance(id); !ok {
t.Fatalf("GetInstance(%s) 失败", id)
}
}
// StopAll 应清空全部实例。
mgr.StopAll()
for _, id := range []string{"a1", "a2", "a3"} {
if _, ok := mgr.GetInstance(id); ok {
t.Fatalf("StopAll 后 %s 仍存在", id)
}
}
// StopAll 在空状态下不应 panic / 死锁。
mgr.StopAll()
}