package appconfig import ( "context" "regexp" "testing" "github.com/DATA-DOG/go-sqlmock" ) func TestListH5LinksScopesQueryAndMergesLegacyBaseline(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("new sql mock failed: %v", err) } defer db.Close() mock.ExpectQuery(regexp.QuoteMeta(listH5LinksSQL)). WithArgs(h5LinkGroup, "yumi"). WillReturnRows(sqlmock.NewRows([]string{"app_code", "key", "description", "value", "is_deleted", "updated_at_ms"}). AddRow("", "admin", "Admin", "https://legacy.example.com/admin", false, int64(100)). AddRow("yumi", "admin", "Yumi Admin", "https://yumi.example.com/admin", false, int64(200)). AddRow("", "host", "Host", "https://legacy.example.com/host", false, int64(100)). AddRow("yumi", "host", "", "", true, int64(300)). AddRow("", "public", "", "https://legacy.example.com/public", false, int64(100))) reader := &MySQLReader{db: db} items, err := reader.ListH5Links(context.Background(), H5LinkQuery{AppCode: " YUMI "}) if err != nil { t.Fatalf("list scoped h5 links failed: %v", err) } if len(items) != 2 { t.Fatalf("unexpected merged h5 links: %+v", items) } if items[0].Key != "admin" || items[0].Label != "Yumi Admin" || items[0].URL != "https://yumi.example.com/admin" { t.Fatalf("scoped override did not replace baseline: %+v", items[0]) } if items[1].Key != "public" || items[1].Label != "public" || items[1].URL != "https://legacy.example.com/public" { t.Fatalf("legacy baseline was not retained: %+v", items[1]) } if err := mock.ExpectationsWereMet(); err != nil { t.Fatalf("sql expectations mismatch: %v", err) } } func TestMergeStoredH5LinksKeepsBaselineForUnknownFutureApp(t *testing.T) { items := mergeStoredH5Links([]storedH5Link{ {AppCode: "", Key: "host", Label: "Host", URL: "https://legacy.example.com/host"}, {AppCode: "another-app", Key: "host", Label: "Other Host", URL: "https://other.example.com/host"}, }, "future-app") if len(items) != 1 || items[0].Label != "Host" || items[0].URL != "https://legacy.example.com/host" { t.Fatalf("future app must inherit only the legacy baseline: %+v", items) } } func TestNormalizeBannerDisplayScopeAcceptsChineseLabels(t *testing.T) { cases := map[string]string{ "": "home", "首页": "home", "房间内": "room", "充值页": "recharge", "我的页": "me", "ME": "me", "ROOM": "room", "bad": "", } for input, want := range cases { if got := NormalizeBannerDisplayScope(input); got != want { t.Fatalf("scope %q mismatch: got %q want %q", input, got, want) } } } func TestBannerDisplayScopeListParsesMultiScopeValue(t *testing.T) { got := bannerDisplayScopeList("room,home,recharge,me") want := []string{"home", "room", "recharge", "me"} if len(got) != len(want) { t.Fatalf("scope count mismatch: got %+v want %+v", got, want) } for index := range want { if got[index] != want[index] { t.Fatalf("scope %d mismatch: got %+v want %+v", index, got, want) } } } func TestParseRoomRegionWhitelistSupportsObjectAndArrayPayloads(t *testing.T) { cases := []string{ `{"user_ids":["325379237278126080","1001","1001","0","bad"]}`, `["325379237278126080","1001","1001","0","bad"]`, } for _, input := range cases { got, err := parseRoomRegionWhitelist(input) if err != nil { t.Fatalf("parse whitelist %s failed: %v", input, err) } if _, ok := got[325379237278126080]; !ok { t.Fatalf("long user id missing: %+v", got) } if _, ok := got[1001]; !ok { t.Fatalf("small user id missing: %+v", got) } if _, ok := got[0]; ok { t.Fatalf("zero user id must be ignored: %+v", got) } } }