234 lines
8.3 KiB
Go
234 lines
8.3 KiB
Go
package admindashboard
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func dashboardTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func TestManualEntryTodayScopesByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
now := time.Date(2026, 11, 16, 12, 0, 0, 0, timeutil.ShanghaiLocation())
|
|
entries := []model.CSManualEntry{
|
|
{TenantID: 101, EntryNo: "MT-A", EntryGroup: "collect", AdminID: 1, AdminName: "A客服", CreatedAt: now},
|
|
{TenantID: 202, EntryNo: "MT-B", EntryGroup: "sell", AdminID: 2, AdminName: "B客服", CreatedAt: now},
|
|
}
|
|
if err := db.Create(&entries).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
repo := NewRepository(db)
|
|
itemsA, err := repo.manualEntryToday(dashboardTenantContext(101), now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
itemsB, err := repo.manualEntryToday(dashboardTenantContext(202), now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(itemsA) != 1 || itemsA[0].AdminName != "A客服" ||
|
|
len(itemsB) != 1 || itemsB[0].AdminName != "B客服" {
|
|
t.Fatalf("tenant manual entry stats A=%+v B=%+v", itemsA, itemsB)
|
|
}
|
|
}
|
|
|
|
func TestSummaryScopesBuyerAndSourceMetricsByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenants := []model.Tenant{
|
|
{ID: 101, Code: "tenant_a", Name: "A站", Status: "active"},
|
|
{ID: 202, Code: "tenant_b", Name: "B站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
users := []model.User{
|
|
{TenantID: 101, Phone: "13200000101", Status: "active", RealnameStatus: "verified"},
|
|
{TenantID: 101, Phone: "13200000102", Status: "active"},
|
|
{TenantID: 202, Phone: "13200000201", Status: "active", RealnameStatus: "verified"},
|
|
}
|
|
if err := db.Create(&users).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
accountA := model.GameAccount{
|
|
SourceTenantID: 101, OwnerID: users[0].ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "A站货源", Status: "published",
|
|
}
|
|
accountB := model.GameAccount{
|
|
SourceTenantID: 202, OwnerID: users[2].ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "B站货源", Status: "published",
|
|
}
|
|
if err := db.Create(&accountA).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&accountB).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listingA := model.RentalListing{
|
|
SourceTenantID: 101, ListingNo: "L-DASHBOARD-A", AccountID: accountA.ID,
|
|
OwnerID: users[0].ID, PriceCent: 1000, Status: "published", ReviewStatus: "approved",
|
|
}
|
|
listingB := model.RentalListing{
|
|
SourceTenantID: 202, ListingNo: "L-DASHBOARD-B", AccountID: accountB.ID,
|
|
OwnerID: users[2].ID, PriceCent: 2000, Status: "published", ReviewStatus: "approved",
|
|
}
|
|
if err := db.Create(&listingA).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&listingB).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
site := model.DistributionSite{
|
|
SiteCode: "dashboard_shared_dist", SiteName: "看板共享货源站",
|
|
Domain: "dashboard-shared.example.com", Status: "active",
|
|
}
|
|
if err := db.Create(&site).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
orders := []model.RentalOrder{
|
|
{
|
|
OrderNo: "RO-DASHBOARD-A-BUY", ListingID: listingB.ID, AccountID: accountB.ID,
|
|
OwnerID: users[2].ID, RenterID: users[0].ID, BuyerTenantID: 101,
|
|
SourceTenantID: 202, PaymentTenantID: 101, RentAmountCent: 2500,
|
|
OwnerRentAmountCent: 1600, SourceAmountCent: 1600, Status: "renting",
|
|
},
|
|
{
|
|
OrderNo: "RO-DASHBOARD-A-SOURCE", ListingID: listingA.ID, AccountID: accountA.ID,
|
|
OwnerID: users[0].ID, RenterID: users[2].ID, BuyerTenantID: 202,
|
|
SourceTenantID: 101, PaymentTenantID: 202, RentAmountCent: 3000,
|
|
OwnerRentAmountCent: 1800, SourceAmountCent: 1800, Status: "completed",
|
|
},
|
|
{
|
|
OrderNo: "RO-DASHBOARD-A-SHARED", ListingID: listingB.ID, AccountID: accountB.ID,
|
|
OwnerID: users[2].ID, RenterID: users[0].ID, BuyerTenantID: 101,
|
|
SourceTenantID: 202, PaymentTenantID: 101, RentAmountCent: 4000,
|
|
OwnerRentAmountCent: 2600, SourceAmountCent: 2600, Status: "completed",
|
|
DistributionSiteID: &site.ID, DistributionMode: "two_party_distributor_receive",
|
|
},
|
|
}
|
|
if err := db.Create(&orders).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.Dispute{
|
|
OrderID: orders[2].ID, InitiatorID: users[0].ID, InitiatorType: "user",
|
|
TargetUserID: users[2].ID, Type: "cannot_login", Status: "open",
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
summary, err := NewRepository(db).Summary(dashboardTenantContext(101))
|
|
if err != nil {
|
|
t.Fatalf("tenant summary: %v", err)
|
|
}
|
|
if summary.Metrics.TotalUsers != 2 || summary.Metrics.VerifiedUsers != 1 {
|
|
t.Fatalf("tenant user metrics = %+v", summary.Metrics)
|
|
}
|
|
if summary.Metrics.TotalListings != 1 || summary.Metrics.PublishedListings != 1 {
|
|
t.Fatalf("tenant listing metrics = %+v", summary.Metrics)
|
|
}
|
|
if summary.Metrics.TotalOrders != 3 {
|
|
t.Fatalf("tenant relevant order count = %d, want 3", summary.Metrics.TotalOrders)
|
|
}
|
|
if summary.Tenant == nil {
|
|
t.Fatal("tenant statistics missing")
|
|
}
|
|
if summary.Tenant.BuyerOrderCount != 2 || summary.Tenant.BuyerGMVCent != 6500 {
|
|
t.Fatalf("buyer tenant stats = %+v", summary.Tenant)
|
|
}
|
|
if summary.Tenant.SourceOrderCount != 1 || summary.Tenant.SourceAmountCent != 1800 {
|
|
t.Fatalf("source tenant stats = %+v", summary.Tenant)
|
|
}
|
|
if summary.Pending.Disputes != 1 || len(summary.RecentDisputes) != 1 {
|
|
t.Fatalf("distribution disputes missing from tenant dashboard: pending=%+v recent=%+v",
|
|
summary.Pending, summary.RecentDisputes)
|
|
}
|
|
}
|
|
|
|
func TestSummaryPendingReviewsIncludeTenantDistributionSites(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenants := []model.Tenant{
|
|
{ID: 101, Code: "dashboard_scope_a", Name: "A站", Status: "active"},
|
|
{ID: 202, Code: "dashboard_scope_b", Name: "B站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sites := []model.DistributionSite{
|
|
{SiteCode: "dashboard_scope_site_a", SiteName: "A分销站", Domain: "dash-scope-a.example.com", Status: "active", DisplayAccountEnabled: true},
|
|
{SiteCode: "dashboard_scope_site_b", SiteName: "B分销站", Domain: "dash-scope-b.example.com", Status: "active", DisplayAccountEnabled: true},
|
|
}
|
|
if err := db.Create(&sites).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&[]model.ChatSiteTenantLink{
|
|
{SiteID: sites[0].ID, TenantID: 101, Enabled: true},
|
|
{SiteID: sites[1].ID, TenantID: 202, Enabled: true},
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
users := []model.User{
|
|
{TenantID: 101, Phone: "13200000301", Status: "active"},
|
|
{TenantID: 202, Phone: "13200000302", Status: "active"},
|
|
{TenantID: 0, DistributionSiteID: sites[0].ID, Phone: "13200000303", Status: "active"},
|
|
{TenantID: 0, DistributionSiteID: sites[1].ID, Phone: "13200000304", Status: "active"},
|
|
}
|
|
if err := db.Create(&users).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
createAccount := func(tenantID, ownerID uint64, title string) model.GameAccount {
|
|
account := model.GameAccount{
|
|
SourceTenantID: tenantID,
|
|
OwnerID: ownerID,
|
|
GameName: "delta_force",
|
|
ServerRegion: "国服",
|
|
LoginPlatform: "steam",
|
|
Title: title,
|
|
Status: "draft",
|
|
}
|
|
if err := db.Create(&account).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return account
|
|
}
|
|
createPending := func(sourceTenantID, ownerID uint64, siteID *uint64, suffix, status string) model.RentalListing {
|
|
account := createAccount(sourceTenantID, ownerID, suffix)
|
|
listing := model.RentalListing{
|
|
SourceTenantID: sourceTenantID,
|
|
ListingNo: "DASH-" + suffix,
|
|
AccountID: account.ID,
|
|
OwnerID: ownerID,
|
|
PriceCent: 1000,
|
|
Status: status,
|
|
ReviewStatus: "pending",
|
|
DistributionSiteID: siteID,
|
|
}
|
|
if err := db.Create(&listing).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return listing
|
|
}
|
|
|
|
createPending(101, users[0].ID, nil, "MAIN-A", "draft")
|
|
createPending(0, users[2].ID, &sites[0].ID, "SITE-A", "draft")
|
|
createPending(202, users[1].ID, nil, "MAIN-B", "draft")
|
|
createPending(0, users[3].ID, &sites[1].ID, "SITE-B", "draft")
|
|
createPending(101, users[0].ID, nil, "OFFLINE-A", "offline")
|
|
|
|
summary, err := NewRepository(db).Summary(dashboardTenantContext(101))
|
|
if err != nil {
|
|
t.Fatalf("tenant summary: %v", err)
|
|
}
|
|
if summary.Pending.ListingReviews != 2 {
|
|
t.Fatalf("pending listing reviews = %d, want 2 (main A + site A)", summary.Pending.ListingReviews)
|
|
}
|
|
}
|