From f0e8ffa2bb6e88334b59a0b6a0a697eef01e9d3f Mon Sep 17 00:00:00 2001 From: AskaEth Date: Tue, 23 Jun 2026 12:26:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20extractProactiveMessage=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=20+=20=E4=BF=AE=E5=A4=8D=20log.Prin?= =?UTF-8?q?tf=20=E6=A0=BC=E5=BC=8F=E5=8C=96=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 7 个测试覆盖:无标记/Web推送/OBv11群聊/私聊/@提及/否定语境/继续思考 - 修复 thinker.go:454 log.Printf %%v → %v 格式化错误 Co-Authored-By: Claude --- .../ai-core/internal/background/thinker.go | 2 +- .../internal/background/thinker_test.go | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 backend/ai-core/internal/background/thinker_test.go diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index d0fb6ae..4ec72e6 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -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") } diff --git a/backend/ai-core/internal/background/thinker_test.go b/backend/ai-core/internal/background/thinker_test.go new file mode 100644 index 0000000..95154e0 --- /dev/null +++ b/backend/ai-core/internal/background/thinker_test.go @@ -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) + } +}