fix(weixin_ilink): drop user file name from media failure log; fix TestLogin_LogRedaction data race

This commit is contained in:
AskaEth committed 2026-10-26 22:00:09 +08:00
1 parent d47185feb8
commit e39cc5d096
3 files changed
+85 -14

No files matched your search

@@ -1,7 +1,6 @@
package weixin_ilink
import (
"bytes"
"context"
"fmt"
"log/slog"
@@ -102,7 +101,7 @@ func TestStartLogin_WaitingPastWindowReMints(t *testing.T) {
case "/ilink/bot/get_bot_qrcode":
mintCalls++
return handlerResult{body: jsonBody(map[string]any{
"qrcode": fmt.Sprintf("qr-w%d", mintCalls),
"qrcode": fmt.Sprintf("qr-w%d", mintCalls),
"qrcode_img_content": fmt.Sprintf("https://img/w%d", mintCalls),
})}
case "/ilink/bot/get_qrcode_status":
@@ -148,19 +147,30 @@ func TestStartLogin_WaitingPastWindowReMints(t *testing.T) {
// TestLogin_LogRedaction 验证扫码登录路径只记录已脱敏的标识:可渲染的二维码 URL 与
// bot id 会出现,但原始 qrcode 标识符、bot_token 以及主人的 user_id 绝不写入。
//
// ⚠️ 日志缓冲必须是**并发安全**的 syncBuffer(M56 修掉既有 DATA RACE):写侧是
// StartLogin 起的登录 goroutine(completeLogin 里的 `login confirmed`),读侧是测试
// goroutine 的 buf.String(),两侧之间没有 happens-before 边 —— 裸 bytes.Buffer 在
// -race 下必报。等待条件也必须等**那一行日志**落盘,而不只是 ad.IsConnected():
// completeLogin 先在 a.mu 下置 connected、**之后**才打日志,只等 IsConnected 会
// 偶发地读到还没有 `login confirmed` 的缓冲(同一处竞态的另一种表现:抖动)。
func TestLogin_LogRedaction(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
statusCalls := 0
srv := newJSONServer(t, nil, func(req recordedReq) handlerResult {
var buf syncBuffer
rec := &reqRecorder{}
// 用 reqRecorder 数状态轮询次数(它自带互斥),而不是在处理器里 += 一个裸 int:
// 处理器跑在 httptest 的服务端 goroutine 上,任何无保护的共享计数器都是同一类
// 竞态的下一个落点。
srv := newJSONServer(t, rec, func(req recordedReq) handlerResult {
switch req.path {
case "/ilink/bot/get_bot_qrcode":
return handlerResult{body: jsonBody(map[string]any{
"qrcode": "qr-redact", "qrcode_img_content": "https://img/redact",
})}
case "/ilink/bot/get_qrcode_status":
statusCalls++
if statusCalls == 1 {
// rec.add 在处理器被调用之前完成,所以这里数到的已包含本次请求。
calls := len(rec.find(func(q recordedReq) bool { return q.path == "/ilink/bot/get_qrcode_status" }))
if calls == 1 {
return handlerResult{body: jsonBody(map[string]any{"status": "scaned"})}
}
return handlerResult{body: jsonBody(map[string]any{
@@ -180,7 +190,9 @@ func TestLogin_LogRedaction(t *testing.T) {
if _, err := ad.StartLogin(context.Background()); err != nil {
t.Fatalf("StartLogin: %v", err)
}
waitFor(t, func() bool { return ad.IsConnected() })
waitFor(t, func() bool {
return ad.IsConnected() && strings.Contains(buf.String(), "login confirmed")
})
logs := buf.String()
for _, want := range []string{