Files
Ydisks-Xianyu-Helper/internal/account/manager_test.go
T
ChristandClaude Opus 4.8 57f9090dd2 refactor: automation 中心落地 + 清理 delivery_rules/SystemMessage 死代码
把卡密自动发货从 engine.DeliveryService 收敛到统一的 automation.Center:
WS 事件/计划任务/手动触发统一为 Task,trigger_key 持久化防重,付款发货、
评价赠品、超时求评价共用一套规则+动作编排。新增 internal/automation 包、
db.AutomationRules repository、00007 迁移。

同步删除被取代的并行死系统:
- engine.DeliveryService + delivery_test.go(已无引用)
- db.DeliveryRules/DeliveryRule + 3 个 Match 方法(生产零调用)
- card_handlers 的 /delivery-rules CRUD(前端早已只走 automation-rules)
- engine.SystemMessage + extractSystemMessage(dispatch 不再单独路由 message["3"])
- store.DeliveryRules 字段、Cards.IncrementDeliveryTimes
- 00008 迁移 DROP delivery_rules/delivery_rule_variants + email_verifications/
  captcha_codes/ai_item_cache/risk_control_logs + publish 批次 3 个死列
- isAPIPath 清理 ~15 个未挂载的前缀,stub_handlers.go 内联删除
- vite.config 删 /delivery-rules proxy

其它:orders.Upsert 注释改为诚实描述(version 列不参与并发控制);
dispatch.cleanupDedupLocked 用 sort.Slice 取代 O(n²) 选择排序;
删自定义 min 用内置;deliveryRuleItemOwned 改名 itemOwnedByUser。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 21:40:15 +08:00

115 lines
3.6 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 }
// 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", "a@e.com", "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", "a@e.com", "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()
}