feat: extractProactiveMessage 单元测试 + 修复 log.Printf 格式化 bug

- 7 个测试覆盖:无标记/Web推送/OBv11群聊/私聊/@提及/否定语境/继续思考
- 修复 thinker.go:454 log.Printf %%v → %v 格式化错误

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-06-23 12:26:39 +08:00
parent b3ce0b4f52
commit f0e8ffa2bb
2 changed files with 120 additions and 1 deletions
@@ -451,7 +451,7 @@ func NewThinker(
}
loc, err := time.LoadLocation(tzName)
if err != nil {
log.Printf("[后台思考] 无效时区 '%s',回退到 Asia/Shanghai: %%v", tzName, err)
log.Printf("[后台思考] 无效时区 '%s',回退到 Asia/Shanghai: %v", tzName, err)
loc, _ = time.LoadLocation("Asia/Shanghai")
}
@@ -0,0 +1,119 @@
package background
import (
"testing"
)
func TestExtractProactiveMessage_NoMarker(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
msg, target := thinker.extractProactiveMessage("今天天气真好,开拓者应该出门走走。")
if msg != "" {
t.Errorf("expected empty, got %q", msg)
}
if target != nil {
t.Errorf("expected nil target, got %+v", target)
}
}
func TestExtractProactiveMessage_WebPush(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
content := `今天在想开拓者的事…
【主动消息】今天天气真好,要出去走走吗♪`
msg, target := thinker.extractProactiveMessage(content)
if msg == "" {
t.Fatal("expected message, got empty")
}
if target != nil {
t.Errorf("expected nil target (web push), got %+v", target)
}
if msg != "今天天气真好,要出去走走吗♪" {
t.Errorf("unexpected message: %q", msg)
}
}
func TestExtractProactiveMessage_OBv11Group(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
content := `反思中…
【主动消息】【OBv11群聊:123456】大家早上好呀♪`
msg, target := thinker.extractProactiveMessage(content)
if msg == "" {
t.Fatal("expected message, got empty")
}
if target == nil {
t.Fatal("expected target, got nil")
}
if target.Platform != "qq" {
t.Errorf("expected platform 'qq', got %q", target.Platform)
}
if target.ChatType != "group" {
t.Errorf("expected chatType 'group', got %q", target.ChatType)
}
if target.GroupID != "123456" {
t.Errorf("expected groupID '123456', got %q", target.GroupID)
}
if msg != "大家早上好呀♪" {
t.Errorf("unexpected message: %q", msg)
}
}
func TestExtractProactiveMessage_OBv11Private(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
content := `【主动消息】【OBv11私聊:789012】好久不见,最近怎么样?`
msg, target := thinker.extractProactiveMessage(content)
if msg == "" {
t.Fatal("expected message, got empty")
}
if target == nil {
t.Fatal("expected target, got nil")
}
if target.ChatType != "private" {
t.Errorf("expected chatType 'private', got %q", target.ChatType)
}
if target.UserID != "789012" {
t.Errorf("expected userID '789012', got %q", target.UserID)
}
if target.GroupID != "" {
t.Errorf("expected empty groupID for private chat, got %q", target.GroupID)
}
if msg != "好久不见,最近怎么样?" {
t.Errorf("unexpected message: %q", msg)
}
}
func TestExtractProactiveMessage_OBv11GroupAt(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
content := `【主动消息】【OBv11群聊:123456@999888】你说得对呢`
msg, target := thinker.extractProactiveMessage(content)
if msg == "" {
t.Fatal("expected message, got empty")
}
if target == nil {
t.Fatal("expected target, got nil")
}
if target.AtUserID != "999888" {
t.Errorf("expected atUserID '999888', got %q", target.AtUserID)
}
}
func TestExtractProactiveMessage_Negation(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
// "不需要" negates the marker.
content := "开拓者在休息,不需要写【主动消息】打扰他。"
msg, _ := thinker.extractProactiveMessage(content)
if msg != "" {
t.Errorf("expected empty (negated), got %q", msg)
}
}
func TestExtractProactiveMessage_ContinueMarker(t *testing.T) {
thinker := &Thinker{platformFormats: defaultPlatformFormats()}
// 【继续思考】should NOT be extracted as proactive message.
content := "反思完成。【继续思考】"
msg, target := thinker.extractProactiveMessage(content)
if msg != "" {
t.Errorf("expected empty, got %q", msg)
}
if target != nil {
t.Errorf("expected nil target, got %+v", target)
}
}