feat: enrich receive messages with display names

This commit is contained in:
zhaoyilun
2026-07-10 08:01:15 +08:00
parent 10e7a616c6
commit 298482c25d
6 changed files with 232 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/modelcontextprotocol/go-sdk/mcp"
"isphere-ai-bridge/internal/isphere"
"isphere-ai-bridge/internal/msglib"
)
func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
@@ -142,6 +143,76 @@ func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
}
}
func TestISphereReceiveMessagesToolUsesInjectedDisplayEntities(t *testing.T) {
fake := &fakeReceiveMessagesSource{
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{
ID: "msg-display-1",
ConversationID: "project-room@conference|sender@example",
ConversationType: "groupchat",
SenderID: "sender@example",
ReceiverID: "project-room@conference",
Text: "keep this body unchanged",
Timestamp: "1783423807000",
}}},
}
fakeDisplay := &fakeDisplayEntitySource{entities: []msglib.DisplayEntity{
{EntityType: msglib.EntityTypeContacts, SourceTable: "TD_CustomEffigy", JID: "sender@example", DisplayName: "Sender Name", Confidence: 0.9},
{EntityType: msglib.EntityTypeGroups, SourceTable: "TD_WorkGroupAuth", JID: "project-room@conference", DisplayName: "Project Room", Confidence: 0.9},
}}
session, cleanup := connectToolsTestSession(t, func(server *mcp.Server) {
RegisterISphereReadToolsWithDisplayEntities(server, fake, fakeDisplay)
})
defer cleanup()
callResult, err := session.CallTool(context.Background(), &mcp.CallToolParams{
Name: ToolNameReceiveMessages,
Arguments: map[string]any{"query": "body", "limit": 5},
})
if err != nil {
t.Fatalf("call %s: %v", ToolNameReceiveMessages, err)
}
if callResult.IsError {
t.Fatalf("call result is error: %+v", callResult)
}
var decoded struct {
Conversation *struct {
ConversationID string `json:"conversation_id"`
ConversationType string `json:"conversation_type"`
DisplayName string `json:"display_name"`
} `json:"conversation"`
Messages []struct {
SenderID string `json:"sender_id"`
SenderName string `json:"sender_name"`
Text string `json:"text"`
ContentText string `json:"content_text"`
} `json:"messages"`
}
payload, _ := json.Marshal(callResult.StructuredContent)
if err := json.Unmarshal(payload, &decoded); err != nil {
t.Fatalf("decode structured content %s: %v", payload, err)
}
if decoded.Conversation == nil || decoded.Conversation.DisplayName != "Project Room" || decoded.Conversation.ConversationType != "group" {
t.Fatalf("unexpected conversation: %+v", decoded.Conversation)
}
if len(decoded.Messages) != 1 {
t.Fatalf("messages = %+v, want one", decoded.Messages)
}
msg := decoded.Messages[0]
if msg.SenderID != "sender@example" || msg.SenderName != "Sender Name" {
t.Fatalf("unexpected sender fields: %+v", msg)
}
if msg.Text != "keep this body unchanged" || msg.ContentText != "keep this body unchanged" {
t.Fatalf("message text changed: %+v", msg)
}
if len(fakeDisplay.queries) != 2 {
t.Fatalf("display queries = %+v, want contacts and groups lookup", fakeDisplay.queries)
}
if fakeDisplay.queries[0].EntityType != msglib.EntityTypeContacts || fakeDisplay.queries[1].EntityType != msglib.EntityTypeGroups {
t.Fatalf("display query entity types = %+v", fakeDisplay.queries)
}
}
func TestISphereReceiveMessagesToolValidatesRemainingContractArgs(t *testing.T) {
fake := &fakeReceiveMessagesSource{
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{