spark-server/internal/module/sync/logic/inbox_test.go
DESKTOP-L8OUL9F\gfl0034 e27ae8723a fix: 单聊系统消息带回当前用户视角的对方
SYNC_INBOX 补 conversation_type、peer_id、group_id 和 last_read_msg_id。历史消息在 sender 为 0 时用 peer_id 定位对方。已读清未读时保留会话 ZSET 分数,避免被毫秒时间戳冲到游标后面。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 15:04:03 +08:00

78 lines
2.5 KiB
Go

package logic
import (
"testing"
"spark-server/internal/infrastructure/id"
convlogic "spark-server/internal/module/conversation/logic"
convmodel "spark-server/internal/module/conversation/model"
inboxmodel "spark-server/internal/module/inbox/model"
"spark-server/internal/module/sync/entity"
syncmodel "spark-server/internal/module/sync/model"
)
func TestSyncInboxIncremental(t *testing.T) {
ids, err := id.New(1)
if err != nil {
t.Fatal(err)
}
inbox := inboxmodel.NewMemoryStore()
box := syncmodel.NewMemoryStore(ids)
svc := NewService(inbox, box)
ctx := t.Context()
if err := inbox.TouchUnread(ctx, 2, 9, 100); err != nil {
t.Fatal(err)
}
if err := inbox.IncrUnread(ctx, 2, 9, 3); err != nil {
t.Fatal(err)
}
if err := inbox.SetPreview(ctx, 9, []byte(`{"msg_id":100}`)); err != nil {
t.Fatal(err)
}
out, err := svc.SyncInbox(ctx, entity.InboxRequest{UserID: 2, DeviceID: 7})
if err != nil || len(out.Dialogs) != 1 || out.Dialogs[0].Unread != 3 || out.DialogScore != 100 {
t.Fatalf("inbox %+v %v", out, err)
}
again, err := svc.SyncInbox(ctx, entity.InboxRequest{UserID: 2, DeviceID: 7, DialogScore: out.DialogScore, MailboxSeq: out.MailboxSeq})
if err != nil || len(again.Dialogs) != 0 {
t.Fatalf("no dialog delta %+v %v", again, err)
}
if err := box.Put(ctx, 2, entity.EventRecall, 9, []byte(`{"msg_id":100}`)); err != nil {
t.Fatal(err)
}
rec, err := svc.SyncInbox(ctx, entity.InboxRequest{UserID: 2, DeviceID: 7, DialogScore: out.DialogScore, MailboxSeq: out.MailboxSeq})
if err != nil || len(rec.Mailbox) != 1 || rec.Mailbox[0].EventType != entity.EventRecall {
t.Fatalf("mailbox %+v %v", rec, err)
}
}
func TestSyncInboxPeerForSystemSender(t *testing.T) {
ids, err := id.New(1)
if err != nil {
t.Fatal(err)
}
ctx := t.Context()
chats := convmodel.NewMemoryP2P()
convID, err := convlogic.NewP2PService(chats, ids).EnsureP2P(ctx, 1001, 1002)
if err != nil {
t.Fatal(err)
}
if err := chats.UpdateLastRead(ctx, convID, 1001, 88001); err != nil {
t.Fatal(err)
}
inbox := inboxmodel.NewMemoryStore()
if err := inbox.TouchUnread(ctx, 1001, convID, 88001); err != nil {
t.Fatal(err)
}
svc := NewService(inbox, nil)
svc.BindFaces(chats)
out, err := svc.SyncInbox(ctx, entity.InboxRequest{UserID: 1001, DeviceID: 7})
if err != nil || len(out.Dialogs) != 1 {
t.Fatalf("inbox %+v %v", out, err)
}
d := out.Dialogs[0]
if d.ConversationID != convID || d.ConversationType != 1 || d.PeerID != 1002 || d.GroupID != 0 || d.LastReadMsgID != 88001 {
t.Fatalf("face %+v", d)
}
}