package admindashboard import ( "testing" "time" "hfb_sys/backend/internal/database" "hfb_sys/backend/internal/listingstatus" "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/timeutil" ) func TestListingDailyOverviewGroupsBySourceChannel(t *testing.T) { db := database.NewTestDB() if err := db.AutoMigrate(&model.ListingStatusEvent{}, &model.ListingUpload{}); err != nil { t.Fatalf("AutoMigrate() error = %v", err) } loc := timeutil.ShanghaiLocation() now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc) listingID1 := uint64(1) listingID2 := uint64(2) listingID4 := uint64(4) uploads := []model.ListingUpload{ {ListingID: &listingID1, UploaderName: "客服1", SourceChannel: "淘宝"}, {ListingID: &listingID2, UploaderName: "客服2", SourceChannel: "微信"}, {ListingID: &listingID4, UploaderName: "客服3"}, } if err := db.Create(&uploads).Error; err != nil { t.Fatalf("create uploads error = %v", err) } events := []model.ListingStatusEvent{ {ListingID: listingID1, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now}, {ListingID: listingID1, EventType: listingstatus.EventOffline, Source: listingstatus.SourceAdmin, CreatedAt: now.Add(time.Minute)}, {ListingID: listingID2, EventType: listingstatus.EventRented, Source: listingstatus.SourceOrder, CreatedAt: now.Add(2 * time.Minute)}, {ListingID: 3, EventType: listingstatus.EventPublished, Source: listingstatus.SourceSeller, CreatedAt: now.Add(3 * time.Minute)}, {ListingID: listingID4, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now.Add(4 * time.Minute)}, } if err := db.Create(&events).Error; err != nil { t.Fatalf("create events error = %v", err) } overview, err := NewRepository(db).listingDailyOverview(t.Context(), now, 1) if err != nil { t.Fatalf("listingDailyOverview() error = %v", err) } if overview.Today.PublishedCount != 3 || overview.Today.ActiveOfflineCount != 1 || overview.Today.TradeLeaveCount != 1 { t.Fatalf("today stats = %#v", overview.Today) } statsByChannel := make(map[string]ListingChannelDayStatsDTO) for _, item := range overview.TodayChannels { statsByChannel[item.SourceChannel] = item } assertChannelStats(t, statsByChannel["淘宝"], 1, 1, 0) assertChannelStats(t, statsByChannel["微信"], 0, 0, 1) assertChannelStats(t, statsByChannel[listingChannelWebsite], 1, 0, 0) assertChannelStats(t, statsByChannel[listingChannelExternalUnknown], 1, 0, 0) } func assertChannelStats(t *testing.T, item ListingChannelDayStatsDTO, published, offline, tradeLeave int64) { t.Helper() if item.PublishedCount != published || item.ActiveOfflineCount != offline || item.TradeLeaveCount != tradeLeave { t.Fatalf("channel %q stats = %#v, want published=%d offline=%d tradeLeave=%d", item.SourceChannel, item, published, offline, tradeLeave) } }