339 lines
13 KiB
Go
339 lines
13 KiB
Go
package chat
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/modules/chathub"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestAdminConversationListIsTenantScopedAndPlatformCanSeeAll(t *testing.T) {
|
|
db := setupTenantChatTestDB(t)
|
|
tenantA := model.Tenant{Code: "tenant-chat-a", Name: "大锤测试", Status: "active"}
|
|
tenantB := model.Tenant{Code: "tenant-chat-b", Name: "解忧测试", Status: "active"}
|
|
if err := db.Create(&tenantA).Error; err != nil {
|
|
t.Fatalf("创建租户 A: %v", err)
|
|
}
|
|
if err := db.Create(&tenantB).Error; err != nil {
|
|
t.Fatalf("创建租户 B: %v", err)
|
|
}
|
|
csRole, superRole := tenantChatRoles(t, db)
|
|
adminA := createTenantAdmin(t, db, "cs-a", csRole.ID, tenantA.ID)
|
|
adminB := createTenantAdmin(t, db, "cs-b", csRole.ID, tenantB.ID)
|
|
superAdmin := createTenantAdmin(t, db, "platform", superRole.ID, 0)
|
|
|
|
convA := createSupportConversation(t, db, tenantA.ID, adminA.ID, "大锤咨询")
|
|
createSupportConversation(t, db, tenantB.ID, adminB.ID, "解忧咨询")
|
|
repo := NewRepository(db, nil, nil)
|
|
|
|
ctxA := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantA.ID})
|
|
ctxB := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantB.ID})
|
|
listA, err := repo.ListConversationsWithFilter(ctxA, Principal{Type: "admin", ID: adminA.ID}, 1, 20, adminChatFilterAll, adminChatStageAll, "")
|
|
if err != nil {
|
|
t.Fatalf("查询租户 A 会话: %v", err)
|
|
}
|
|
if listA.Total != 1 || listA.Items.([]ConversationDTO)[0].ID != convA.ID {
|
|
t.Fatalf("租户 A 会话 = %+v, want only %d", listA.Items, convA.ID)
|
|
}
|
|
listB, err := repo.ListConversationsWithFilter(ctxB, Principal{Type: "admin", ID: adminB.ID}, 1, 20, adminChatFilterAll, adminChatStageAll, "")
|
|
if err != nil {
|
|
t.Fatalf("查询租户 B 会话: %v", err)
|
|
}
|
|
if listB.Total != 1 {
|
|
t.Fatalf("租户 B 会话数 = %d, want 1", listB.Total)
|
|
}
|
|
listPlatform, err := repo.ListConversationsWithFilter(ctxA, Principal{Type: "admin", ID: superAdmin.ID}, 1, 20, adminChatFilterAll, adminChatStageAll, "")
|
|
if err != nil {
|
|
t.Fatalf("查询平台会话: %v", err)
|
|
}
|
|
if listPlatform.Total != 2 {
|
|
t.Fatalf("平台会话数 = %d, want 2", listPlatform.Total)
|
|
}
|
|
|
|
if _, err := repo.FindConversation(ctxB, Principal{Type: "admin", ID: adminB.ID}, convA.ID); err != ErrConversationNotFound {
|
|
t.Fatalf("租户 B 读取租户 A 会话 error = %v, want not found", err)
|
|
}
|
|
if _, err := repo.FindConversation(ctxA, Principal{Type: "admin", ID: superAdmin.ID}, convA.ID); err != nil {
|
|
t.Fatalf("平台超管读取会话: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCrossTenantOrderConversationIsVisibleToBothSides(t *testing.T) {
|
|
db := setupTenantChatTestDB(t)
|
|
buyerTenant := model.Tenant{Code: "tenant-buyer", Name: "买方", Status: "active"}
|
|
sourceTenant := model.Tenant{Code: "tenant-source", Name: "货源方", Status: "active"}
|
|
if err := db.Create(&buyerTenant).Error; err != nil {
|
|
t.Fatalf("创建买方租户: %v", err)
|
|
}
|
|
if err := db.Create(&sourceTenant).Error; err != nil {
|
|
t.Fatalf("创建货源租户: %v", err)
|
|
}
|
|
owner := model.User{TenantID: sourceTenant.ID, Phone: "13900000001", Nickname: "号主"}
|
|
renter := model.User{TenantID: buyerTenant.ID, Phone: "13900000002", Nickname: "租客"}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatalf("创建号主: %v", err)
|
|
}
|
|
if err := db.Create(&renter).Error; err != nil {
|
|
t.Fatalf("创建租客: %v", err)
|
|
}
|
|
order := model.RentalOrder{
|
|
OrderNo: "CROSS-TENANT-CHAT",
|
|
ListingID: 88,
|
|
AccountID: 88,
|
|
OwnerID: owner.ID,
|
|
RenterID: renter.ID,
|
|
BuyerTenantID: buyerTenant.ID,
|
|
SourceTenantID: sourceTenant.ID,
|
|
Status: "renting",
|
|
}
|
|
if err := db.Create(&order).Error; err != nil {
|
|
t.Fatalf("创建跨站订单: %v", err)
|
|
}
|
|
conversation := model.ChatConversation{
|
|
TenantID: buyerTenant.ID,
|
|
OrderID: &order.ID,
|
|
Type: ConversationTypeOrderGroup,
|
|
Title: "跨站订单群",
|
|
Status: "active",
|
|
}
|
|
if err := db.Create(&conversation).Error; err != nil {
|
|
t.Fatalf("创建跨站会话: %v", err)
|
|
}
|
|
now := time.Now()
|
|
if err := db.Create(&[]model.ChatParticipant{
|
|
{ConversationID: conversation.ID, ParticipantType: "user", ParticipantID: renter.ID, Role: "renter", JoinedAt: now},
|
|
{ConversationID: conversation.ID, ParticipantType: "user", ParticipantID: owner.ID, Role: "owner", JoinedAt: now},
|
|
}).Error; err != nil {
|
|
t.Fatalf("创建跨站会话成员: %v", err)
|
|
}
|
|
repo := NewRepository(db, nil, nil)
|
|
for name, tc := range map[string]struct {
|
|
user uint64
|
|
scope tenancy.Scope
|
|
}{
|
|
"buyer": {user: renter.ID, scope: tenancy.Scope{TenantID: buyerTenant.ID}},
|
|
"source": {user: owner.ID, scope: tenancy.Scope{TenantID: sourceTenant.ID}},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
ctx := tenancy.WithScope(t.Context(), tc.scope)
|
|
got, err := repo.FindOrderConversation(ctx, tc.user, order.ID)
|
|
if err != nil {
|
|
t.Fatalf("读取跨站会话: %v", err)
|
|
}
|
|
if got.ID != conversation.ID {
|
|
t.Fatalf("conversation = %d, want %d", got.ID, conversation.ID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func setupTenantChatTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db,
|
|
&model.Tenant{},
|
|
&model.User{},
|
|
&model.Role{},
|
|
&model.AdminUser{},
|
|
&model.AdminUserRole{},
|
|
&model.RentalListing{},
|
|
&model.RentalOrder{},
|
|
&model.ChatConversation{},
|
|
&model.ChatParticipant{},
|
|
&model.ChatAdminConversationState{},
|
|
&model.ChatMessage{},
|
|
&model.ChatSiteTenantLink{},
|
|
&model.DistributionSite{},
|
|
); err != nil {
|
|
t.Fatalf("迁移租户聊天测试库: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func tenantChatRoles(t *testing.T, db *gorm.DB) (model.Role, model.Role) {
|
|
t.Helper()
|
|
csRole := model.Role{Code: "cs", Name: "客服"}
|
|
superRole := model.Role{Code: "super_admin", Name: "超级管理员"}
|
|
if err := db.Create(&csRole).Error; err != nil {
|
|
t.Fatalf("创建客服角色: %v", err)
|
|
}
|
|
if err := db.Create(&superRole).Error; err != nil {
|
|
t.Fatalf("创建超管角色: %v", err)
|
|
}
|
|
return csRole, superRole
|
|
}
|
|
|
|
func createTenantAdmin(t *testing.T, db *gorm.DB, username string, roleID, tenantID uint64) model.AdminUser {
|
|
t.Helper()
|
|
admin := model.AdminUser{Username: username, Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatalf("创建管理员 %s: %v", username, err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{AdminID: admin.ID, RoleID: roleID, TenantID: tenantID}).Error; err != nil {
|
|
t.Fatalf("绑定管理员角色 %s: %v", username, err)
|
|
}
|
|
return admin
|
|
}
|
|
|
|
func createSupportConversation(t *testing.T, db *gorm.DB, tenantID, adminID uint64, title string) model.ChatConversation {
|
|
t.Helper()
|
|
conversation := model.ChatConversation{TenantID: tenantID, Type: ConversationTypeGeneralSupport, Title: title, Status: "active"}
|
|
if err := db.Create(&conversation).Error; err != nil {
|
|
t.Fatalf("创建会话 %s: %v", title, err)
|
|
}
|
|
if err := db.Create(&model.ChatParticipant{
|
|
ConversationID: conversation.ID,
|
|
ParticipantType: "admin",
|
|
ParticipantID: adminID,
|
|
Role: "support",
|
|
JoinedAt: time.Now(),
|
|
}).Error; err != nil {
|
|
t.Fatalf("创建会话成员 %s: %v", title, err)
|
|
}
|
|
return conversation
|
|
}
|
|
|
|
func TestLegacyDistributionSupportConversationVisibleToMappedTenant(t *testing.T) {
|
|
db := setupTenantChatTestDB(t)
|
|
tenantA := model.Tenant{Code: "tenant-dachui-map", Name: "大锤映射", Status: "active"}
|
|
tenantB := model.Tenant{Code: "tenant-jieyou-map", Name: "解忧映射", Status: "active"}
|
|
if err := db.Create(&tenantA).Error; err != nil {
|
|
t.Fatalf("创建租户 A: %v", err)
|
|
}
|
|
if err := db.Create(&tenantB).Error; err != nil {
|
|
t.Fatalf("创建租户 B: %v", err)
|
|
}
|
|
csRole, _ := tenantChatRoles(t, db)
|
|
adminA := createTenantAdmin(t, db, "cs-map-a", csRole.ID, tenantA.ID)
|
|
adminB := createTenantAdmin(t, db, "cs-map-b", csRole.ID, tenantB.ID)
|
|
site := model.DistributionSite{SiteCode: "map_site", SiteName: "映射分销", Domain: "map.example.com", Status: "active"}
|
|
if err := db.Create(&site).Error; err != nil {
|
|
t.Fatalf("创建分销站点: %v", err)
|
|
}
|
|
if err := db.Create(&model.ChatSiteTenantLink{SiteID: site.ID, TenantID: tenantA.ID, Enabled: true}).Error; err != nil {
|
|
t.Fatalf("创建站点租户映射: %v", err)
|
|
}
|
|
conversation := createSupportConversation(t, db, 0, adminA.ID, "【映射分销】平台客服")
|
|
if err := db.Model(&conversation).Updates(map[string]any{
|
|
"distribution_site_id": site.ID,
|
|
"type": ConversationTypeGeneralSupport,
|
|
}).Error; err != nil {
|
|
t.Fatalf("补充会话站点: %v", err)
|
|
}
|
|
listingConversation := createSupportConversation(t, db, 0, adminA.ID, "映射分销账号群")
|
|
if err := db.Model(&listingConversation).Updates(map[string]any{
|
|
"distribution_site_id": site.ID,
|
|
"type": ConversationTypeListingGroup,
|
|
}).Error; err != nil {
|
|
t.Fatalf("补充账号群站点: %v", err)
|
|
}
|
|
siteB := model.DistributionSite{SiteCode: "map_site_b", SiteName: "解忧映射分销", Domain: "map-b.example.com", Status: "active"}
|
|
if err := db.Create(&siteB).Error; err != nil {
|
|
t.Fatalf("创建解忧分销站点: %v", err)
|
|
}
|
|
if err := db.Create(&model.ChatSiteTenantLink{SiteID: siteB.ID, TenantID: tenantB.ID, Enabled: true}).Error; err != nil {
|
|
t.Fatalf("创建解忧站点映射: %v", err)
|
|
}
|
|
conversationB := createSupportConversation(t, db, 0, adminB.ID, "【解忧映射分销】平台客服")
|
|
if err := db.Model(&conversationB).Updates(map[string]any{
|
|
"distribution_site_id": siteB.ID,
|
|
"type": ConversationTypeGeneralSupport,
|
|
}).Error; err != nil {
|
|
t.Fatalf("补充解忧会话站点: %v", err)
|
|
}
|
|
|
|
repo := NewRepository(db, nil, nil)
|
|
ctxA := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantA.ID})
|
|
listA, err := repo.ListConversationsWithFilter(ctxA, Principal{Type: "admin", ID: adminA.ID}, 1, 20, adminChatFilterMine, adminChatStageAll, "")
|
|
if err != nil {
|
|
t.Fatalf("查询映射租户客服会话: %v", err)
|
|
}
|
|
if listA.Total != 2 {
|
|
t.Fatalf("映射租户 mine 会话数 = %d, want 2", listA.Total)
|
|
}
|
|
seen := map[uint64]bool{}
|
|
for _, item := range listA.Items.([]ConversationDTO) {
|
|
seen[item.ID] = true
|
|
}
|
|
if !seen[conversation.ID] || !seen[listingConversation.ID] {
|
|
t.Fatalf("映射租户 mine 会话 = %+v, want 包含 %d 和 %d", listA.Items, conversation.ID, listingConversation.ID)
|
|
}
|
|
if _, err := repo.FindConversation(ctxA, Principal{Type: "admin", ID: adminA.ID}, conversation.ID); err != nil {
|
|
t.Fatalf("映射租户读取会话详情: %v", err)
|
|
}
|
|
|
|
ctxB := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantB.ID})
|
|
listB, err := repo.ListConversationsWithFilter(ctxB, Principal{Type: "admin", ID: adminB.ID}, 1, 20, adminChatFilterAll, adminChatStageAll, "")
|
|
if err != nil {
|
|
t.Fatalf("查询其他租户会话: %v", err)
|
|
}
|
|
if listB.Total != 1 || listB.Items.([]ConversationDTO)[0].ID != conversationB.ID {
|
|
t.Fatalf("解忧映射租户会话 = %+v, want only %d", listB.Items, conversationB.ID)
|
|
}
|
|
if _, err := repo.FindConversation(ctxA, Principal{Type: "admin", ID: adminA.ID}, conversationB.ID); err != ErrConversationNotFound {
|
|
t.Fatalf("大锤客服读取解忧映射会话 error = %v, want not found", err)
|
|
}
|
|
|
|
event := tenantScopedEvent(db, &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversation.ID})
|
|
if !containsTenantID(event.TenantIDs, tenantA.ID) {
|
|
t.Fatalf("事件租户 = %v, want 包含 %d", event.TenantIDs, tenantA.ID)
|
|
}
|
|
}
|
|
|
|
func TestEnsureSupportConversationUsesMappedTenant(t *testing.T) {
|
|
db := setupTenantChatTestDB(t)
|
|
tenant := model.Tenant{Code: "tenant-support-map", Name: "大锤客服", Status: "active"}
|
|
if err := db.Create(&tenant).Error; err != nil {
|
|
t.Fatalf("创建租户: %v", err)
|
|
}
|
|
csRole, _ := tenantChatRoles(t, db)
|
|
support := createTenantAdmin(t, db, "cs-mapped-support", csRole.ID, tenant.ID)
|
|
site := model.DistributionSite{SiteCode: "mapped_site", SiteName: "映射站点", Domain: "mapped.example.com", Status: "active"}
|
|
if err := db.Create(&site).Error; err != nil {
|
|
t.Fatalf("创建分销站点: %v", err)
|
|
}
|
|
if err := db.Create(&model.ChatSiteTenantLink{SiteID: site.ID, TenantID: tenant.ID, Enabled: true}).Error; err != nil {
|
|
t.Fatalf("创建映射: %v", err)
|
|
}
|
|
user := model.User{TenantID: 0, DistributionSiteID: site.ID, Phone: "13900009999", Nickname: "分销咨询用户", Status: "active"}
|
|
if err := db.Create(&user).Error; err != nil {
|
|
t.Fatalf("创建用户: %v", err)
|
|
}
|
|
|
|
repo := NewRepository(db, nil, nil)
|
|
item, err := repo.EnsureSupportConversation(t.Context(), user.ID, "general", site.SiteCode)
|
|
if err != nil {
|
|
t.Fatalf("创建映射客服会话: %v", err)
|
|
}
|
|
var conversation model.ChatConversation
|
|
if err := db.First(&conversation, item.ID).Error; err != nil {
|
|
t.Fatalf("读取会话: %v", err)
|
|
}
|
|
if conversation.TenantID != tenant.ID {
|
|
t.Fatalf("会话 tenant_id = %d, want %d", conversation.TenantID, tenant.ID)
|
|
}
|
|
var supportCount int64
|
|
if err := db.Model(&model.ChatParticipant{}).
|
|
Where("conversation_id = ? AND participant_type = ? AND role = ? AND participant_id = ?", conversation.ID, "admin", "support", support.ID).
|
|
Count(&supportCount).Error; err != nil {
|
|
t.Fatalf("查询客服参与者: %v", err)
|
|
}
|
|
if supportCount != 1 {
|
|
t.Fatalf("映射租户客服参与者数量 = %d, want 1", supportCount)
|
|
}
|
|
}
|
|
|
|
func containsTenantID(ids []uint64, target uint64) bool {
|
|
for _, id := range ids {
|
|
if id == target {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|