88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
package databi
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hyapp-admin-server/internal/model"
|
|
"hyapp-admin-server/internal/repository"
|
|
)
|
|
|
|
func testAccess() repository.MoneyAccess {
|
|
return repository.MoneyAccess{
|
|
UserID: 7,
|
|
Scopes: []model.UserMoneyScope{
|
|
{UserID: 7, AppCode: "lalu", RegionID: 0},
|
|
{UserID: 7, AppCode: "aslan", RegionID: 11},
|
|
{UserID: 7, AppCode: "aslan", RegionID: 12},
|
|
},
|
|
}
|
|
}
|
|
|
|
func mustLocation(t *testing.T, name string) *time.Location {
|
|
t.Helper()
|
|
location, err := time.LoadLocation(name)
|
|
if err != nil {
|
|
t.Fatalf("load location %s: %v", name, err)
|
|
}
|
|
return location
|
|
}
|
|
|
|
func TestRegionDisplay(t *testing.T) {
|
|
regions := []RegionInfo{{AppCode: "aslan", RegionID: 11, RegionCode: "MENA", RegionName: "中东"}}
|
|
if _, name := regionDisplay(regions, 0); name != "全部区域" {
|
|
t.Fatalf("expected 全部区域 for region 0, got %s", name)
|
|
}
|
|
if code, name := regionDisplay(regions, 11); code != "MENA" || name != "中东" {
|
|
t.Fatalf("unexpected display for region 11: %s %s", code, name)
|
|
}
|
|
if _, name := regionDisplay(regions, 99); name != "区域 99" {
|
|
t.Fatalf("expected fallback name for unknown region, got %s", name)
|
|
}
|
|
}
|
|
|
|
func TestTopLevelMetricsStripsStructuralKeys(t *testing.T) {
|
|
day1Rate := 0.5
|
|
out := topLevelMetrics(map[string]any{
|
|
"recharge_usd_minor": int64(5),
|
|
"daily_series": []any{"x"},
|
|
"country_breakdown": []any{"y"},
|
|
"daily_country_breakdown": []any{"z"},
|
|
"report_metric_sources": []any{},
|
|
"retention": map[string]any{
|
|
"day1_base_users": int64(6),
|
|
"day1_rate": day1Rate,
|
|
"day1_users": int64(3),
|
|
},
|
|
})
|
|
if _, ok := out["daily_series"]; ok {
|
|
t.Fatalf("expected daily_series stripped")
|
|
}
|
|
if out["recharge_usd_minor"] != int64(5) {
|
|
t.Fatalf("expected metric kept, got %v", out["recharge_usd_minor"])
|
|
}
|
|
if out["d1_retention_rate"] != day1Rate || out["d1_retention_users"] != int64(3) || out["d1_retention_base_users"] != int64(6) {
|
|
t.Fatalf("expected nested retention flattened, got %#v", out)
|
|
}
|
|
}
|
|
|
|
// legacy 雪花区域 ID 超出 JS 安全整数,历史 scope 里存的是 float64 圆整值;
|
|
// 归一与展示必须能容差还原(线上「区域 20453...600」事故的回归测试)。
|
|
func TestRegionDisplayToleratesRoundedIDs(t *testing.T) {
|
|
exact := int64(2049084769873498113)
|
|
rounded := int64(float64(exact))
|
|
if exact == rounded {
|
|
t.Fatalf("fixture should lose precision")
|
|
}
|
|
regions := []RegionInfo{{AppCode: "aslan", RegionID: exact, RegionCode: "AR", RegionName: "阿语区"}}
|
|
if _, name := regionDisplay(regions, rounded); name != "阿语区" {
|
|
t.Fatalf("expected rounded id to resolve region name, got %s", name)
|
|
}
|
|
if got := normalizeRegionID(regions, rounded); got != exact {
|
|
t.Fatalf("expected normalize to exact id, got %d", got)
|
|
}
|
|
if !regionInCatalog(regions, rounded) {
|
|
t.Fatalf("expected rounded id to match catalog")
|
|
}
|
|
}
|