阶段十:完成 Go Engine 注释基线清零

This commit is contained in:
Christ committed 2026-08-15 05:12:37 +08:00
1 parent 9255c233aa
commit 64e7868444
22 files changed
+1355 -8557

No files matched your search

+70 -8
View File
@@ -20,40 +20,54 @@ type recordingHandler struct {
refresh int
}
// HandleChatMessage 处理聊天消息。
func (h *recordingHandler) HandleChatMessage(_ context.Context, m ChatMessage) error {
h.mu.Lock()
defer h.mu.Unlock()
h.chats = append(h.chats, m)
return nil
}
// HandleSystemEvent 处理系统Event。
func (h *recordingHandler) HandleSystemEvent(_ context.Context, task automation.Task) error {
h.mu.Lock()
defer h.mu.Unlock()
_ = task
return nil
}
// OnPasswordLoginRefresh 负责On密码登录Refresh相关处理。
func (h *recordingHandler) OnPasswordLoginRefresh(_ context.Context, _ string) bool {
h.mu.Lock()
defer h.mu.Unlock()
h.refresh++
return true
}
// OnAccountAlert 负责On账号Alert相关处理。
func (h *recordingHandler) OnAccountAlert(_ context.Context, _, _, _, _ string) {}
// newAccountForTest 负责new账号ForTest相关处理。
func newAccountForTest(t *testing.T) (*Account, *recordingHandler, *db.Store, func()) {
t.Helper()
// dbPath 保存db路径,供当前处理流程使用
dbPath := filepath.Join(t.TempDir(), "test.db")
// d、err 保存d、err,供当前处理流程使用
d, _, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
// store 保存store,供当前处理流程使用
store := db.NewStore(d, db.DialectSQLite)
store.Users.Create(context.Background(), "admin", "[email protected]", "pw")
// admin 保存admin,供当前处理流程使用
admin, _ := store.Users.GetByUsername(context.Background(), "admin")
store.Cookies.Save(context.Background(), "cid", "unb=123; _m_h5_tk=tk_1;", admin.ID)
store.Cookies.SetStatus(context.Background(), "cid", true)
// h 保存h,供当前处理流程使用
h := &recordingHandler{}
// acc 保存acc,供当前处理流程使用
acc := New(Config{
CookieID: "cid",
CookieStr: "unb=123; _m_h5_tk=tk_1;",
@@ -68,7 +82,9 @@ func newAccountForTest(t *testing.T) (*Account, *recordingHandler, *db.Store, fu
// TestExtractChatMessage_RealSample 用真实抓包样本验证消息字段提取。
func TestExtractChatMessage_RealSample(t *testing.T) {
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustDecryptGoldenSample(t)
// chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie")
if chat == nil {
t.Fatal("应为聊天消息")
@@ -84,36 +100,49 @@ func TestExtractChatMessage_RealSample(t *testing.T) {
}
}
// TestExtractChatMessage_FiltersContentType14Notice 负责TestExtract聊天消息Filters内容Type14Notice相关处理。
func TestExtractChatMessage_FiltersContentType14Notice(t *testing.T) {
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustContentType14Notice(t)
if chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
if // chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
t.Fatalf("contentType=14 系统提示不应进入聊天回复: %+v", chat)
}
}
// TestExtractChatMessageUsesReminderTitleAsNickname 负责TestExtract聊天消息UsesReminder标题AsNickname相关处理。
func TestExtractChatMessageUsesReminderTitleAsNickname(t *testing.T) {
// decrypted 保存decrypted,供当前处理流程使用
decrypted := map[string]any{"1": map[string]any{
"2": "chat-1@goofish",
"10": map[string]any{"reminderContent": "你好", "reminderTitle": "真实昵称", "senderUserId": "buyer-1", "sessionType": "1"},
}}
// chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "account-1", "cookie")
if chat == nil || chat.SenderName != "真实昵称" {
t.Fatalf("chat=%+v", chat)
}
}
// TestExtractChatMessage_FiltersRefundTradeCard 负责TestExtract聊天消息FiltersRefundTrade卡密相关处理。
func TestExtractChatMessage_FiltersRefundTradeCard(t *testing.T) {
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustRefundTradeCard(t)
if chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
if // chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
t.Fatalf("退款交易卡片不应进入聊天回复: %+v", chat)
}
}
// TestExtractChatMessage_FiltersPaidDeliveryCardFromChat 负责TestExtract聊天消息FiltersPaid发货卡密From聊天相关处理。
func TestExtractChatMessage_FiltersPaidDeliveryCardFromChat(t *testing.T) {
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustPaidDeliveryCard(t)
if chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
if // chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
t.Fatalf("付款待发货系统卡片不应进入聊天回复链: %+v", chat)
}
// task 保存任务,供当前处理流程使用
task := automation.ExtractTaskFromWS("cid", "cookie", decrypted)
if task == nil || task.TriggerType != automation.TriggerOrderPaid {
t.Fatalf("付款待发货卡片应进入自动化中心: %+v", task)
@@ -122,11 +151,14 @@ func TestExtractChatMessage_FiltersPaidDeliveryCardFromChat(t *testing.T) {
// TestDedup_SkipsDuplicateWithinExpiry 同一消息 ID 1 小时内只处理一次。
func TestDedup_SkipsDuplicateWithinExpiry(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustDecryptGoldenSample(t)
// chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie")
// 首次:标记并应继续。
@@ -146,17 +178,22 @@ func TestDedup_SkipsDuplicateWithinExpiry(t *testing.T) {
// TestDebounce_CoalescesRapidMessages 连续消息只投递最后一条。
func TestDebounce_CoalescesRapidMessages(t *testing.T) {
// acc、h、cleanup 保存acc、h、cleanup,供当前处理流程使用
acc, h, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
// decrypted 保存decrypted,供当前处理流程使用
decrypted := mustDecryptGoldenSample(t)
// chat 保存聊天,供当前处理流程使用
chat := extractChatMessage(decrypted, "cid", "cookie")
// 修改文本模拟连续多条。
chat1 := *chat
chat1.Text = "第一条"
// chat2 保存chat2,供当前处理流程使用
chat2 := *chat
chat2.Text = "第二条"
// chat3 保存chat3,供当前处理流程使用
chat3 := *chat
chat3.Text = "第三条"
@@ -180,13 +217,16 @@ func TestDebounce_CoalescesRapidMessages(t *testing.T) {
// TestExtractItemID reminderUrl 中提取 itemId。
func TestExtractItemID(t *testing.T) {
// cases 保存cases,供当前处理流程使用
cases := map[string]string{
"fleamarket://message_chat?itemId=900052644277&peerUserId=3149637063": "900052644277",
"noitemid": "",
"fleamarket://x?itemId=ABC123": "ABC123",
}
// in、want 表示当前遍历过程中的in、want
for in, want := range cases {
if got := extractItemID(in); got != want {
if // got 保存got,供当前处理流程使用
got := extractItemID(in); got != want {
t.Errorf("extractItemID(%q)=%q want %q", in, got, want)
}
}
@@ -194,11 +234,13 @@ func TestExtractItemID(t *testing.T) {
// mustDecryptGoldenSample 复用 protocol 包的真实样本:直接硬编码一条最小解密结构,
// 避免循环依赖。这里构造一个等价的最小消息用于字段提取测试。
// mustDecryptGoldenSample 负责mustDecryptGoldenSample相关处理。
func mustDecryptGoldenSample(t *testing.T) map[string]any {
t.Helper()
// 真实样本关键字段(来自 protocol golden test 解密输出):
// message["1"]["2"]="47983389009@goofish", ["1"]["10"]["reminderContent"]="[我已拍下,待付款]",
// ["1"]["10"]["reminderUrl"] 含 itemId=900052644277
// s 保存s,供当前处理流程使用
s := `{
"1": {
"2": "47983389009@goofish",
@@ -214,15 +256,19 @@ func mustDecryptGoldenSample(t *testing.T) map[string]any {
"userId": "3149637063"
}
}`
// m 保存m,供当前处理流程使用
var m map[string]any
if err := json.Unmarshal([]byte(s), &m); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return m
}
// mustContentType14Notice 负责must内容Type14Notice相关处理。
func mustContentType14Notice(t *testing.T) map[string]any {
t.Helper()
// s 保存s,供当前处理流程使用
s := "{" +
"\"1\":{" +
"\"2\":\"63107041124@goofish\"," +
@@ -235,15 +281,19 @@ func mustContentType14Notice(t *testing.T) map[string]any {
"\"6\":{\"3\":{\"4\":14,\"5\":\"{\\\"contentType\\\":14,\\\"tip\\\":{\\\"argInfo\\\":{\\\"arg1\\\":\\\"NoBargainGuide\\\"}}}\"}}" +
"}" +
"}"
// m 保存m,供当前处理流程使用
var m map[string]any
if err := json.Unmarshal([]byte(s), &m); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return m
}
// mustRefundTradeCard 负责mustRefundTrade卡密相关处理。
func mustRefundTradeCard(t *testing.T) map[string]any {
t.Helper()
// s 保存s,供当前处理流程使用
s := "{" +
"\"1\":{" +
"\"2\":\"63107041124@goofish\"," +
@@ -258,15 +308,19 @@ func mustRefundTradeCard(t *testing.T) map[string]any {
"\"6\":{\"3\":{\"4\":26,\"5\":\"{\\\"contentType\\\":26}\"}}" +
"}" +
"}"
// m 保存m,供当前处理流程使用
var m map[string]any
if err := json.Unmarshal([]byte(s), &m); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return m
}
// mustPaidDeliveryCard 负责mustPaid发货卡密相关处理。
func mustPaidDeliveryCard(t *testing.T) map[string]any {
t.Helper()
// s 保存s,供当前处理流程使用
s := "{" +
"\"1\":{" +
"\"2\":\"63107041124@goofish\"," +
@@ -281,8 +335,10 @@ func mustPaidDeliveryCard(t *testing.T) map[string]any {
"\"6\":{\"3\":{\"4\":26,\"5\":\"{\\\"contentType\\\":26}\"}}" +
"}" +
"}"
// m 保存m,供当前处理流程使用
var m map[string]any
if err := json.Unmarshal([]byte(s), &m); err != nil {
if // err 保存err,供当前处理流程使用
err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return m
@@ -290,6 +346,7 @@ func mustPaidDeliveryCard(t *testing.T) map[string]any {
// TestRetryDelay 复刻 _calculate_retry_delay 的分段逻辑。
func TestRetryDelay(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
defer acc.Stop()
@@ -306,19 +363,24 @@ func TestRetryDelay(t *testing.T) {
expectDelayRange(t, acc.retryDelay("some other error"), 2*time.Second)
}
// expectDelayRange 负责expect延迟Range相关处理。
func expectDelayRange(t *testing.T, got, base time.Duration) {
t.Helper()
// max 保存max,供当前处理流程使用
max := base + base*3/10
if got < base || got > max {
t.Fatalf("retryDelay=%v want in [%v,%v]", got, base, max)
}
}
// TestRuntimeStatusClassifiesAuthenticationFailures 负责TestRuntime状态ClassifiesAuthenticationFailures相关处理。
func TestRuntimeStatusClassifiesAuthenticationFailures(t *testing.T) {
// acc、cleanup 保存acc、cleanup,供当前处理流程使用
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
acc.setRuntimeError(context.Background(), fmt.Errorf("token API 登录凭证已失效: FAIL_SYS_TOKEN_EXOIRED"))
// status 保存状态,供当前处理流程使用
status := acc.RuntimeStatus()
if status.State != RuntimeAuthExpired || status.Connected {
t.Fatalf("status=%+v", status)