fix im business event delivery

This commit is contained in:
richjohnrambo
2026-08-28 16:24:57 +08:00
parent 9a46fdf43c
commit 78e6308174
4 changed files with 81 additions and 8 deletions
+43
View File
@@ -75,3 +75,46 @@ func TestDispatchRegistrationInviteUsageEvent(t *testing.T) {
t.Fatal("invite usage event was routed as a regular client event")
}
}
func TestDispatchUserBusinessEventToOnlineSession(t *testing.T) {
oldInfrastructure := store.HTTPInfrastructure
oldHub, oldSessions := globals.hub, globals.sessionStore
defer func() {
store.HTTPInfrastructure = oldInfrastructure
globals.hub, globals.sessionStore = oldHub, oldSessions
}()
uid := types.ParseUserId("usrv97xq86jw3c")
infrastructure := &passwordResetEventStore{}
infrastructure.events = []types.BusinessEvent{{
EventID: "friend-request-1", EventType: "friend.request.created",
TargetType: "user", Target: uid.UserId(), Payload: []byte(`{"request_id":"frq_1","user_id":"usrSrc"}`),
CreatedAt: time.Now().UTC(),
}}
store.HTTPInfrastructure = infrastructure
globals.hub = &Hub{routeSrv: make(chan *ServerComMessage, 1)}
globals.sessionStore = NewSessionStore(time.Minute)
session := &Session{sid: "s1", proto: WEBSOCK, uid: uid, tenantID: types.TenantID(1), send: make(chan any, 1)}
globals.sessionStore.sessCache[session.sid] = session
dispatchIMBusinessEvents(types.TenantID(1))
if len(infrastructure.delivered) != 1 || infrastructure.delivered[0] != "friend-request-1" {
t.Fatalf("business event was not marked delivered: %v", infrastructure.delivered)
}
if len(globals.hub.routeSrv) != 0 {
t.Fatal("online user event should be delivered directly to session without topic routing")
}
select {
case raw := <-session.send:
msg, ok := raw.(*ServerComMessage)
if !ok || msg.Info == nil {
t.Fatalf("unexpected outbound message: %#v", raw)
}
if msg.Info.What != "im" || msg.Info.Event != "friend.request.created" || msg.Info.Topic != uid.UserId() {
t.Fatalf("unexpected info event: %#v", msg.Info)
}
default:
t.Fatal("online user session did not receive business info event")
}
}