test(channels): update inbound context assertions

This commit is contained in:
Hoshina 2026-04-01 21:34:24 +08:00
parent 53482a17bc
commit 3a9d1fc6fd
4 changed files with 32 additions and 45 deletions

View file

@ -65,8 +65,8 @@ func TestOnChatBotMessageReceived_GroupMentionOnlyUsesIsInAtListAndStripsMention
if inbound.ChatID != "group-abc" { if inbound.ChatID != "group-abc" {
t.Fatalf("chat_id=%q", inbound.ChatID) t.Fatalf("chat_id=%q", inbound.ChatID)
} }
if inbound.Peer.Kind != "group" || inbound.Peer.ID != "group-abc" { if inbound.Context.ChatType != "group" {
t.Fatalf("peer=%+v", inbound.Peer) t.Fatalf("chat_type=%q", inbound.Context.ChatType)
} }
if inbound.Content != "/help" { if inbound.Content != "/help" {
t.Fatalf("content=%q", inbound.Content) t.Fatalf("content=%q", inbound.Content)
@ -93,12 +93,15 @@ func TestOnChatBotMessageReceived_DirectFallbackSenderIDUsesConversationID(t *te
if inbound.ChatID != "conv-direct-42" { if inbound.ChatID != "conv-direct-42" {
t.Fatalf("chat_id=%q", inbound.ChatID) t.Fatalf("chat_id=%q", inbound.ChatID)
} }
if inbound.Peer.Kind != "direct" || inbound.Peer.ID != "openid-user-42" { if inbound.Context.ChatType != "direct" {
t.Fatalf("peer=%+v", inbound.Peer) t.Fatalf("chat_type=%q", inbound.Context.ChatType)
} }
if inbound.SenderID != "dingtalk:openid-user-42" { if inbound.SenderID != "openid-user-42" {
t.Fatalf("sender_id=%q", inbound.SenderID) t.Fatalf("sender_id=%q", inbound.SenderID)
} }
if inbound.Sender.CanonicalID != "dingtalk:openid-user-42" {
t.Fatalf("sender canonical_id=%q", inbound.Sender.CanonicalID)
}
if _, ok := ch.sessionWebhooks.Load("conv-direct-42"); !ok { if _, ok := ch.sessionWebhooks.Load("conv-direct-42"); !ok {
t.Fatal("expected session webhook keyed by conversation_id") t.Fatal("expected session webhook keyed by conversation_id")

View file

@ -50,15 +50,15 @@ func TestHandleC2CMessage_IncludesAccountIDMetadata(t *testing.T) {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("timeout waiting for inbound message") t.Fatal("timeout waiting for inbound message")
return return
case inbound, ok := <-messageBus.InboundChan(): case inbound, ok := <-messageBus.InboundChan():
if !ok { if !ok {
t.Fatal("expected inbound message") t.Fatal("expected inbound message")
}
if inbound.Context.Raw["account_id"] != "7750283E123456" {
t.Fatalf("account_id raw = %q, want %q", inbound.Context.Raw["account_id"], "7750283E123456")
}
return
} }
if inbound.Metadata["account_id"] != "7750283E123456" {
t.Fatalf("account_id metadata = %q, want %q", inbound.Metadata["account_id"], "7750283E123456")
}
return
}
} }
} }
@ -165,8 +165,8 @@ func TestHandleGroupATMessage_AttachmentOnlyPublishesMedia(t *testing.T) {
if !strings.HasPrefix(inbound.Media[0], "media://") { if !strings.HasPrefix(inbound.Media[0], "media://") {
t.Fatalf("inbound.Media[0] = %q, want media:// ref", inbound.Media[0]) t.Fatalf("inbound.Media[0] = %q, want media:// ref", inbound.Media[0])
} }
if inbound.Peer.Kind != "group" || inbound.Peer.ID != "group-1" { if inbound.Context.ChatType != "group" {
t.Fatalf("inbound.Peer = %+v, want group/group-1", inbound.Peer) t.Fatalf("inbound.Context.ChatType = %q, want group", inbound.Context.ChatType)
} }
} }

View file

@ -556,16 +556,10 @@ func TestHandleMessage_ForumTopic_SetsMetadata(t *testing.T) {
inbound, ok := <-messageBus.InboundChan() inbound, ok := <-messageBus.InboundChan()
require.True(t, ok, "expected inbound message") require.True(t, ok, "expected inbound message")
// Composite chatID should include thread ID // ChatID remains the parent chat; TopicID isolates the sub-conversation.
assert.Equal(t, "-1001234567890/42", inbound.ChatID) assert.Equal(t, "-1001234567890", inbound.ChatID)
assert.Equal(t, "group", inbound.Context.ChatType)
// Peer ID should include thread ID for session key isolation assert.Equal(t, "42", inbound.Context.TopicID)
assert.Equal(t, "group", inbound.Peer.Kind)
assert.Equal(t, "-1001234567890/42", inbound.Peer.ID)
// Parent peer metadata should be set for agent binding
assert.Equal(t, "topic", inbound.Metadata["parent_peer_kind"])
assert.Equal(t, "42", inbound.Metadata["parent_peer_id"])
} }
func TestHandleMessage_NoForum_NoThreadMetadata(t *testing.T) { func TestHandleMessage_NoForum_NoThreadMetadata(t *testing.T) {
@ -598,13 +592,8 @@ func TestHandleMessage_NoForum_NoThreadMetadata(t *testing.T) {
// Plain chatID without thread suffix // Plain chatID without thread suffix
assert.Equal(t, "-100999", inbound.ChatID) assert.Equal(t, "-100999", inbound.ChatID)
// Peer ID should be raw chat ID (no thread suffix) assert.Equal(t, "group", inbound.Context.ChatType)
assert.Equal(t, "group", inbound.Peer.Kind) assert.Empty(t, inbound.Context.TopicID)
assert.Equal(t, "-100999", inbound.Peer.ID)
// No parent peer metadata
assert.Empty(t, inbound.Metadata["parent_peer_kind"])
assert.Empty(t, inbound.Metadata["parent_peer_id"])
} }
func TestHandleMessage_ReplyThread_NonForum_NoIsolation(t *testing.T) { func TestHandleMessage_ReplyThread_NonForum_NoIsolation(t *testing.T) {
@ -641,13 +630,8 @@ func TestHandleMessage_ReplyThread_NonForum_NoIsolation(t *testing.T) {
// chatID should NOT include thread suffix for non-forum groups // chatID should NOT include thread suffix for non-forum groups
assert.Equal(t, "-100999", inbound.ChatID) assert.Equal(t, "-100999", inbound.ChatID)
// Peer ID should be raw chat ID (shared session for whole group) assert.Equal(t, "group", inbound.Context.ChatType)
assert.Equal(t, "group", inbound.Peer.Kind) assert.Empty(t, inbound.Context.TopicID)
assert.Equal(t, "-100999", inbound.Peer.ID)
// No parent peer metadata
assert.Empty(t, inbound.Metadata["parent_peer_kind"])
assert.Empty(t, inbound.Metadata["parent_peer_id"])
} }
func assertHandleMessageQuotedUserReply( func assertHandleMessageQuotedUserReply(
@ -700,7 +684,7 @@ func assertHandleMessageQuotedUserReply(
inbound, ok := <-messageBus.InboundChan() inbound, ok := <-messageBus.InboundChan()
require.True(t, ok) require.True(t, ok)
assert.Equal(t, strconv.Itoa(replyMessageID), inbound.Metadata["reply_to_message_id"]) assert.Equal(t, strconv.Itoa(replyMessageID), inbound.Context.ReplyToMessageID)
assert.Equal(t, expectedContent, inbound.Content) assert.Equal(t, expectedContent, inbound.Content)
} }
@ -786,7 +770,7 @@ func TestHandleMessage_ReplyToOwnBotMessage_UsesAssistantRole(t *testing.T) {
inbound, ok := <-messageBus.InboundChan() inbound, ok := <-messageBus.InboundChan()
require.True(t, ok) require.True(t, ok)
assert.Equal(t, "101", inbound.Metadata["reply_to_message_id"]) assert.Equal(t, "101", inbound.Context.ReplyToMessageID)
assert.Equal( assert.Equal(
t, t,
"[quoted assistant message from afjcjsbx_picoclaw_bot]: Fatto! Ho creato il file notizie_2026_03_28.md\n\nti ricordi questo file?", "[quoted assistant message from afjcjsbx_picoclaw_bot]: Fatto! Ho creato il file notizie_2026_03_28.md\n\nti ricordi questo file?",

View file

@ -50,11 +50,11 @@ func TestDispatchIncoming_UsesActualChatIDAndStoresReqIDRoute(t *testing.T) {
if inbound.MessageID != "msg-1" { if inbound.MessageID != "msg-1" {
t.Fatalf("inbound MessageID = %q, want msg-1", inbound.MessageID) t.Fatalf("inbound MessageID = %q, want msg-1", inbound.MessageID)
} }
if inbound.Peer.ID != "chat-1" { if inbound.Context.ChatType != "direct" {
t.Fatalf("inbound Peer.ID = %q, want chat-1", inbound.Peer.ID) t.Fatalf("inbound Context.ChatType = %q, want direct", inbound.Context.ChatType)
} }
if inbound.Metadata["req_id"] != "req-1" { if inbound.Context.ReplyHandles["req_id"] != "req-1" {
t.Fatalf("inbound req_id = %q, want req-1", inbound.Metadata["req_id"]) t.Fatalf("inbound req_id = %q, want req-1", inbound.Context.ReplyHandles["req_id"])
} }
default: default:
t.Fatal("expected inbound message to be published") t.Fatal("expected inbound message to be published")