143 lines
4.7 KiB
Go
143 lines
4.7 KiB
Go
package cprelationbroadcast
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/model"
|
|
"chatapp3-golang/internal/service/regionimgroup"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestProcessEventBuildsCPRelationBroadcastPayload(t *testing.T) {
|
|
service, broadcaster := newCPRelationBroadcastTestService(t)
|
|
|
|
_, err := service.ProcessEvent(context.Background(), EventRequest{
|
|
SysOrigin: "LIKEI",
|
|
EventID: "CP_RELATION:9001",
|
|
ApplyID: flexibleInt64(9001),
|
|
RelationType: RelationTypeCP,
|
|
UserID: flexibleInt64(1001),
|
|
CpUserID: flexibleInt64(1002),
|
|
OccurredAt: "2026-05-22T12:00:00Z",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ProcessEvent() error = %v", err)
|
|
}
|
|
if len(broadcaster.requests) != 1 {
|
|
t.Fatalf("requests = %d, want 1", len(broadcaster.requests))
|
|
}
|
|
req := broadcaster.requests[0]
|
|
if req.Type != MessageTypeCPRelationBroadcast {
|
|
t.Fatalf("type = %q", req.Type)
|
|
}
|
|
data := req.Data
|
|
if data["msg"] != "Alice与Bob get cp relation" ||
|
|
data["relationType"] != RelationTypeCP ||
|
|
data["userId"] != "1001" ||
|
|
data["cpUserId"] != "1002" {
|
|
t.Fatalf("payload = %+v", data)
|
|
}
|
|
user, ok := data["user"].(map[string]any)
|
|
if !ok || user["userNickname"] != "Alice" || user["account"] != "alice" {
|
|
t.Fatalf("user payload = %+v", data["user"])
|
|
}
|
|
cpUser, ok := data["cpUser"].(map[string]any)
|
|
if !ok || cpUser["userNickname"] != "Bob" || cpUser["account"] != "bob" {
|
|
t.Fatalf("cpUser payload = %+v", data["cpUser"])
|
|
}
|
|
}
|
|
|
|
func TestProcessEventBuildsBrotherAndSistersMessages(t *testing.T) {
|
|
service, broadcaster := newCPRelationBroadcastTestService(t)
|
|
cases := []struct {
|
|
relationType string
|
|
wantMsg string
|
|
}{
|
|
{RelationTypeBrother, "Alice与Bob结为兄弟"},
|
|
{RelationTypeSisters, "Alice与Bob结为姐妹"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
broadcaster.requests = nil
|
|
_, err := service.ProcessEvent(context.Background(), EventRequest{
|
|
RelationType: tc.relationType,
|
|
UserID: flexibleInt64(1001),
|
|
CpUserID: flexibleInt64(1002),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ProcessEvent(%s) error = %v", tc.relationType, err)
|
|
}
|
|
if len(broadcaster.requests) != 1 {
|
|
t.Fatalf("requests = %d, want 1", len(broadcaster.requests))
|
|
}
|
|
if got := broadcaster.requests[0].Data["msg"]; got != tc.wantMsg {
|
|
t.Fatalf("msg = %v, want %s", got, tc.wantMsg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessPayloadDecodesMessageEventEnvelopeAndAliases(t *testing.T) {
|
|
service, broadcaster := newCPRelationBroadcastTestService(t)
|
|
service.cfg.CPRelationBroadcast.MQ.Tag = "cp_relation_broadcast"
|
|
payload := `{"tag":"cp_relation_broadcast","body":"{\"sys_origin\":\"likei\",\"event_id\":\"CP_RELATION:9002\",\"apply_id\":\"9002\",\"relation_type\":\"BROTHERS\",\"send_user_id\":\"1001\",\"accept_user_id\":\"1002\"}"}`
|
|
|
|
_, err := service.ProcessPayload(context.Background(), payload)
|
|
if err != nil {
|
|
t.Fatalf("ProcessPayload() error = %v", err)
|
|
}
|
|
if len(broadcaster.requests) != 1 {
|
|
t.Fatalf("requests = %d, want 1", len(broadcaster.requests))
|
|
}
|
|
req := broadcaster.requests[0]
|
|
if req.SysOrigin != "LIKEI" {
|
|
t.Fatalf("sysOrigin = %q", req.SysOrigin)
|
|
}
|
|
if req.Data["eventId"] != "CP_RELATION:9002" ||
|
|
req.Data["applyId"] != "9002" ||
|
|
req.Data["relationType"] != RelationTypeBrother ||
|
|
req.Data["msg"] != "Alice与Bob结为兄弟" {
|
|
t.Fatalf("payload = %+v", req.Data)
|
|
}
|
|
}
|
|
|
|
func newCPRelationBroadcastTestService(t *testing.T) (*Service, *fakeCPRegionBroadcaster) {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.UserBaseInfo{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
now := time.Now()
|
|
if err := db.Create(&[]model.UserBaseInfo{
|
|
{ID: 1001, Account: "alice", UserNickname: "Alice", UserAvatar: "alice.png", OriginSys: "LIKEI", CountryCode: "SA", CountryName: "Saudi Arabia", CreateTime: now},
|
|
{ID: 1002, Account: "bob", UserNickname: "Bob", UserAvatar: "bob.png", OriginSys: "LIKEI", CountryCode: "SA", CountryName: "Saudi Arabia", CreateTime: now},
|
|
}).Error; err != nil {
|
|
t.Fatalf("seed users: %v", err)
|
|
}
|
|
broadcaster := &fakeCPRegionBroadcaster{}
|
|
return NewService(config.Config{
|
|
CPRelationBroadcast: config.CPRelationBroadcastConfig{DefaultSysOrigin: "LIKEI"},
|
|
}, db, broadcaster), broadcaster
|
|
}
|
|
|
|
type fakeCPRegionBroadcaster struct {
|
|
requests []regionimgroup.RegionBroadcastRequest
|
|
}
|
|
|
|
func (f *fakeCPRegionBroadcaster) SendRegionBroadcast(_ context.Context, req regionimgroup.RegionBroadcastRequest) (*regionimgroup.RegionBroadcastResponse, error) {
|
|
f.requests = append(f.requests, req)
|
|
return ®ionimgroup.RegionBroadcastResponse{
|
|
SysOrigin: req.SysOrigin,
|
|
RegionCode: "AR",
|
|
GroupID: "@region-ar",
|
|
Type: req.Type,
|
|
}, nil
|
|
}
|