209 lines
6.5 KiB
Go
209 lines
6.5 KiB
Go
package pickup
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/auditlog"
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func pickupTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func seedPickupTenantListing(t *testing.T, db *gorm.DB, tenantID uint64, suffix string) model.RentalListing {
|
|
t.Helper()
|
|
owner := model.User{
|
|
TenantID: tenantID,
|
|
Phone: "1880000" + suffix,
|
|
Nickname: "号主" + suffix,
|
|
Status: "active",
|
|
}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
account := model.GameAccount{
|
|
SourceTenantID: tenantID,
|
|
OwnerID: owner.ID,
|
|
ServerRegion: "测试区",
|
|
LoginPlatform: "微信",
|
|
Title: "测试账号" + suffix,
|
|
Status: "published",
|
|
}
|
|
if err := db.Create(&account).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listing := model.RentalListing{
|
|
SourceTenantID: tenantID,
|
|
ListingNo: "LST-PICKUP-TENANT-" + suffix,
|
|
AccountID: account.ID,
|
|
OwnerID: owner.ID,
|
|
PriceCent: 10000,
|
|
Status: "published",
|
|
ReviewStatus: "approved",
|
|
HandoffMode: "owner",
|
|
SettlementMode: "owner_wallet",
|
|
}
|
|
if err := db.Create(&listing).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return listing
|
|
}
|
|
|
|
func TestPickupTenantScopeSupportsCrossTenantCreationAndSeparateLists(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenants := []model.Tenant{
|
|
{Code: "pickup_tenant_a", Name: "提号A站", Status: "active"},
|
|
{Code: "pickup_tenant_b", Name: "提号B站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tenantA, tenantB := tenants[0], tenants[1]
|
|
listingFromA := seedPickupTenantListing(t, db, tenantA.ID, "01")
|
|
listingFromB := seedPickupTenantListing(t, db, tenantB.ID, "02")
|
|
listingFromBForB := seedPickupTenantListing(t, db, tenantB.ID, "03")
|
|
|
|
repo := NewRepository(db)
|
|
ctxA := pickupTenantContext(tenantA.ID)
|
|
ctxB := pickupTenantContext(tenantB.ID)
|
|
|
|
available, err := repo.ListAvailableListings(ctxA, AvailableListingQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
crossSeen := false
|
|
for _, item := range available.Items {
|
|
if item.ID == listingFromB.ID {
|
|
crossSeen = item.IsCrossTenant && item.SourceTenantID == tenantB.ID
|
|
}
|
|
}
|
|
if !crossSeen {
|
|
t.Fatalf("available listings missing cross tenant source: %+v", available.Items)
|
|
}
|
|
|
|
crossPickup, err := repo.Create(ctxA, CreateRequest{ListingID: listingFromB.ID}, 1, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatalf("cross tenant pickup: %v", err)
|
|
}
|
|
if crossPickup.TenantID != tenantA.ID || crossPickup.SourceTenantID != tenantB.ID || !crossPickup.IsCrossTenant {
|
|
t.Fatalf("cross pickup scope = %+v", crossPickup)
|
|
}
|
|
completedCross, err := repo.Complete(ctxA, crossPickup.ID, CompleteRequest{
|
|
SettleAmountCent: 8000,
|
|
CompleteRemark: "跨站提号完成",
|
|
}, 1, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatalf("complete cross tenant pickup: %v", err)
|
|
}
|
|
if completedCross.Status != StatusCompleted {
|
|
t.Fatalf("cross pickup status = %s", completedCross.Status)
|
|
}
|
|
var ledger model.WalletLedger
|
|
if err := db.Where("biz_type = ? AND biz_no = ?", "admin_pickup_settle", crossPickup.PickupNo).
|
|
First(&ledger).Error; err != nil {
|
|
t.Fatalf("source tenant wallet ledger: %v", err)
|
|
}
|
|
if ledger.TenantID != tenantB.ID || ledger.UserID != crossPickup.OwnerID {
|
|
t.Fatalf("cross pickup wallet ledger = %+v", ledger)
|
|
}
|
|
ownPickup, err := repo.Create(ctxA, CreateRequest{ListingID: listingFromA.ID}, 1, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tenantBPickup, err := repo.Create(ctxB, CreateRequest{ListingID: listingFromBForB.ID}, 2, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
listA, err := repo.ListAdmin(ctxA, AdminPickupQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if listA.Total != 2 || len(listA.Items) != 2 {
|
|
t.Fatalf("tenant A pickups = %+v", listA)
|
|
}
|
|
for _, item := range listA.Items {
|
|
if item.TenantID != tenantA.ID {
|
|
t.Fatalf("tenant A list leaked pickup: %+v", item)
|
|
}
|
|
}
|
|
listB, err := repo.ListAdmin(ctxB, AdminPickupQuery{Page: 1, PageSize: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if listB.Total != 1 || listB.Items[0].ID != tenantBPickup.ID {
|
|
t.Fatalf("tenant B pickups = %+v", listB)
|
|
}
|
|
|
|
if _, err := repo.FindByID(ctxB, ownPickup.ID); !errors.Is(err, ErrPickupNotFound) {
|
|
t.Fatalf("cross-tenant detail error = %v, want ErrPickupNotFound", err)
|
|
}
|
|
if _, err := repo.UpdateProfit(ctxB, ownPickup.ID, UpdateProfitRequest{ProfitAmountCent: 2000}, 2, auditlog.Meta{}); !errors.Is(err, ErrPickupNotFound) {
|
|
t.Fatalf("cross-tenant update error = %v, want ErrPickupNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestPickupShopsAreSeparatedByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenants := []model.Tenant{
|
|
{Code: "pickup_shop_a", Name: "A站", Status: "active"},
|
|
{Code: "pickup_shop_b", Name: "B站", Status: "active"},
|
|
}
|
|
if err := db.Create(&tenants).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewRepository(db)
|
|
ctxA := pickupTenantContext(tenants[0].ID)
|
|
ctxB := pickupTenantContext(tenants[1].ID)
|
|
|
|
shopA, err := repo.CreateShop(ctxA, CreatePickupShopRequest{Name: "同名店铺"}, 1, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
shopB, err := repo.CreateShop(ctxB, CreatePickupShopRequest{Name: "同名店铺"}, 2, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatalf("same shop name in another tenant: %v", err)
|
|
}
|
|
if shopA.ID == shopB.ID {
|
|
t.Fatal("tenant shops must be separate rows")
|
|
}
|
|
itemsA, err := repo.ListShops(ctxA, PickupShopQuery{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
itemsB, err := repo.ListShops(ctxB, PickupShopQuery{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(itemsA) != 1 || len(itemsB) != 1 || itemsA[0].ID != shopA.ID || itemsB[0].ID != shopB.ID {
|
|
t.Fatalf("tenant shops A=%+v B=%+v", itemsA, itemsB)
|
|
}
|
|
}
|
|
|
|
func TestPickupTenantScopeSourceTenantFallback(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
tenant := model.Tenant{Code: "pickup_same", Name: "同站", Status: "active"}
|
|
if err := db.Create(&tenant).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listing := seedPickupTenantListing(t, db, tenant.ID, "09")
|
|
repo := NewRepository(db)
|
|
pickup, err := repo.Create(pickupTenantContext(tenant.ID), CreateRequest{ListingID: listing.ID}, 1, auditlog.Meta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pickup.IsCrossTenant || pickup.SourceTenantID != pickup.TenantID {
|
|
t.Fatalf("same-site pickup scope = %+v", pickup)
|
|
}
|
|
if pickup.PickupNo == "" {
|
|
t.Fatal("pickup number must not be empty")
|
|
}
|
|
}
|