package business import ( "context" "reflect" "testing" "kra/internal/biz/business" ) func TestChannelMetricSortingUsesFullScopeAndPreferencesBeforePagination(t *testing.T) { _, repo := prepareChannels(t) scope := business.UserScope{SelfID: 9, OnlySelf: true} for _, sample := range []struct { field string include, dedup bool want []uint64 }{ {"totalPayAmount", true, false, []uint64{101, 100, 103, 102}}, {"totalPayAmount", false, true, []uint64{103, 102, 101, 100}}, {"playerCount", true, false, []uint64{101, 100, 103, 102}}, {"playerCount", true, true, []uint64{101, 103, 100, 102}}, {"playerCount", false, true, []uint64{103, 101, 100, 102}}, } { t.Run(sample.field+"/"+map[bool]string{true: "sub", false: "self"}[sample.include]+"/"+map[bool]string{true: "dedup", false: "accounts"}[sample.dedup], func(t *testing.T) { filter := business.ChannelFilter{SortField: sample.field, IncludeSub: sample.include, IDCardDedup: sample.dedup} var got []uint64 for offset := range sample.want { page, err := repo.List(context.Background(), scope, filter, business.ListOffset(offset), business.ListLimit(1)) if err != nil || page.Total != 4 || len(page.Rows) != 1 || page.StatusCounts.Normal != 4 { t.Fatalf("unexpected page at %d: %+v %v", offset, page, err) } if page.Rows[0].SourceDB != "lx" { t.Fatal("metric sorting leaked another business or platform") } got = append(got, page.Rows[0].ID) } if !reflect.DeepEqual(got, sample.want) { t.Fatalf("want %v, got %v", sample.want, got) } filter.Ascending = true filter.Keyword = "grandchild" page, err := repo.List(context.Background(), scope, filter, business.ListLimit(1)) if err != nil || page.Total != 1 || len(page.Rows) != 1 || page.Rows[0].ID != 102 { t.Fatalf("metric sorting lost the name filter: %+v %v", page, err) } }) } } func TestChannelMetricSortRetainsZeroRowsAndExactIdentityNormalization(t *testing.T) { db, repo := prepareChannels(t) if err := db.Exec("INSERT INTO tab_promote(id,source_db,account,promote_level,parent_id,top_promote_id,busier_id) VALUES(200,'lx','zero',1,0,0,14)").Error; err != nil { t.Fatal(err) } if err := db.Exec("INSERT INTO tab_user VALUES('lx',1000,100,0,?),('lx',1001,100,0,?)", "\tabcX \r", "\t \r").Error; err != nil { t.Fatal(err) } filter := business.ChannelFilter{SortField: "playerCount", IncludeSub: true, IDCardDedup: true, Ascending: true} page, err := repo.List(context.Background(), business.UserScope{SelfID: 9, OnlySelf: true}, filter) if err != nil { t.Fatal(err) } var got []uint64 for _, row := range page.Rows { got = append(got, row.ID) } if !reflect.DeepEqual(got, []uint64{200, 102, 103, 100, 101}) { t.Fatalf("zero rows or identity normalization differ from displayed values: %v", got) } }