package configser import "testing" func TestAppConfigVMQRTextUsesAppAddr(t *testing.T) { cfg := AppConfigVM{ AppAddr: " https://download.example.com/app ", ForeverURLAddr: "https://www.example.com/", } if got, want := cfg.QRText(), "https://download.example.com/app"; got != want { t.Fatalf("QRText() = %q, want %q", got, want) } } func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) { vm := BuildAppConfigVM(&ConfigInfoResp{ NewAddressInfo: NewAddressInfo{Telegram: " https://t.me/business ", QQ: " 2168045324 ", OfficialAppAddr: " https://app.example.com/download\\ "}, Config: ConfigInfo{PotaoGroup: " https://potato.example.com/group\\ "}, Advitise: AdvitiseData{ HomeList: []Advertisement{{ID: "home", Images: "https://cdn.example.com/home.gif"}}, DetailTopList: []Advertisement{{ID: "top", Images: " https://cdn.example.com/top.gif "}}, NineGridList: []Advertisement{ {ID: "banner-1", Images: "https://cdn.example.com/banner-1.gif"}, {ID: "banner-empty", Images: " "}, {ID: "banner-2", Images: "https://cdn.example.com/banner-2.gif"}, {ID: "banner-3", Images: "https://cdn.example.com/banner-3.gif"}, }, DetailBannerList: []Advertisement{{ID: "detail", Images: "https://cdn.example.com/detail.gif"}}, BottomBanner: []Advertisement{ {ID: " bottom ", Images: " https://cdn.example.com/bottom.gif ", Link: " https://example.com/bottom "}, {ID: "bottom-empty", Images: " "}, }, DialogList: []Advertisement{ {ID: " 1 ", Images: " https://cdn.example.com/ad.webp\\ ", Link: " https://example.com ", Title: " 广告一 "}, {ID: "2", Images: " ", Title: "无图广告"}, }, }, }) if vm.Telegram != "https://t.me/business" { t.Fatalf("Telegram = %q, want sanitized business Telegram URL", vm.Telegram) } if vm.PotaoGroup != "https://potato.example.com/group" { t.Fatalf("PotaoGroup = %q, want sanitized config.potaoGroup", vm.PotaoGroup) } if vm.QQ != "2168045324" { t.Fatalf("QQ = %q, want trimmed QQ group", vm.QQ) } if vm.OfficialAppAddr != "https://app.example.com/download" { t.Fatalf("OfficialAppAddr = %q, want sanitized official app URL", vm.OfficialAppAddr) } if len(vm.Advitise.HomeList) != 1 || vm.Advitise.HomeList[0].ID != "home" { t.Fatalf("Advitise.HomeList = %#v", vm.Advitise.HomeList) } if len(vm.Advitise.NineGridList) != 4 { t.Fatalf("Advitise.NineGridList length = %d, want raw length 4", len(vm.Advitise.NineGridList)) } if len(vm.DetailTopList) != 1 || vm.DetailTopList[0].ID != "top" { t.Fatalf("DetailTopList = %#v", vm.DetailTopList) } if len(vm.NineGridList) != 3 { t.Fatalf("NineGridList length = %d, want 3", len(vm.NineGridList)) } if len(vm.DetailBannerList) != 1 || vm.DetailBannerList[0].ID != "detail" { t.Fatalf("DetailBannerList = %#v", vm.DetailBannerList) } if len(vm.BottomBanner) != 1 || vm.BottomBanner[0].ID != "bottom" || vm.BottomBanner[0].Images != "https://cdn.example.com/bottom.gif" { t.Fatalf("BottomBanner = %#v", vm.BottomBanner) } if len(vm.DialogList) != 1 { t.Fatalf("DialogList length = %d, want 1", len(vm.DialogList)) } got := vm.DialogList[0] if got.ID != "1" || got.Images != "https://cdn.example.com/ad.webp" || got.Link != "https://example.com" || got.Title != "广告一" { t.Fatalf("DialogList[0] = %#v", got) } } func TestNineGridRowReturnsTwoItemsPerRow(t *testing.T) { cfg := AppConfigVM{NineGridList: []Advertisement{{ID: "1"}, {ID: "2"}, {ID: "3"}}} if got := cfg.NineGridRow(0); len(got) != 2 || got[0].ID != "1" || got[1].ID != "2" { t.Fatalf("NineGridRow(0) = %#v", got) } if got := cfg.NineGridRow(1); len(got) != 1 || got[0].ID != "3" { t.Fatalf("NineGridRow(1) = %#v", got) } if got := cfg.NineGridRow(2); len(got) != 0 { t.Fatalf("NineGridRow(2) = %#v, want empty", got) } }