fix: fan out unread presence to online sessions

This commit is contained in:
richjohnrambo
2026-09-17 10:24:39 +08:00
parent 7df3313c78
commit 816be46e03
6 changed files with 161 additions and 5 deletions
+56
View File
@@ -52,6 +52,62 @@ func TestRealtimeEnvelopeDeliversToUserSessions(t *testing.T) {
}
}
func TestRealtimeUserPresenceSkipsSessionsAttachedToTopic(t *testing.T) {
oldStore := globals.sessionStore
defer func() { globals.sessionStore = oldStore }()
tenantID := types.TenantID(1)
sender := types.Uid(1001)
recipient := types.Uid(1002)
topic := sender.P2PName(recipient)
if topic == "" {
t.Fatal("failed to create p2p topic")
}
visibleTopic, err := types.P2PNameForUser(recipient, topic)
if err != nil || visibleTopic == "" {
t.Fatalf("failed to create visible topic: %v", err)
}
globals.sessionStore = realtimeTestSessionStore(
realtimeTestSession("sid-attached", tenantID, recipient, visibleTopic),
realtimeTestSession("sid-me-only", tenantID, recipient, ""),
)
msg := &ServerComMessage{
Pres: &MsgServerPres{
Topic: "me",
Src: topic,
What: "msg",
SeqId: 35,
SkipTopic: topic,
},
}
raw, err := json.Marshal(msg)
if err != nil {
t.Fatal(err)
}
deliverRealtimeFanoutEnvelope(&store.RealtimeFanoutEnvelope{
TenantID: tenantID,
Kind: realtimeKindMessage,
TargetType: realtimeTargetUser,
Target: recipient.UserId(),
SkipTopic: topic,
Message: raw,
})
if len(globals.sessionStore.Get("sid-attached").send) != 0 {
t.Fatal("attached p2p session must not receive duplicate me presence")
}
select {
case got := <-globals.sessionStore.Get("sid-me-only").send:
pres := got.(*ServerComMessage).Pres
if pres == nil || pres.Topic != "me" || pres.What != "msg" || pres.Src != topic || pres.SeqId != 35 {
t.Fatalf("unexpected presence: %#v", pres)
}
default:
t.Fatal("me-only session must receive message presence")
}
}
func TestRealtimeP2PTopicFallbackRewritesVisibleTopic(t *testing.T) {
oldStore := globals.sessionStore
oldHub := globals.hub