Files
2026-10-29 20:45:09 +07:00

107 lines
4.4 KiB
Go

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"}},
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"}},
VideoTopList: []Advertisement{
{ID: " video-top ", Images: " https://cdn.example.com/video-top.gif ", Link: " https://example.com/top "},
{ID: "video-top-empty", Images: " "},
},
AList: []Advertisement{{ID: "a-legacy", Images: "https://cdn.example.com/a.gif"}},
AppInfoAList: []Advertisement{{ID: "a", Images: "https://cdn.example.com/app-a.gif"}},
BList: []Advertisement{{ID: "b", Images: "https://cdn.example.com/b.gif"}},
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.NineGridList) != 3 {
t.Fatalf("NineGridList length = %d, want 3", len(vm.NineGridList))
}
// 详情横幅优先 videoTopList(本站后台字段),无效图片被过滤
if len(vm.DetailBannerList) != 1 || vm.DetailBannerList[0].ID != "video-top" || vm.DetailBannerList[0].Images != "https://cdn.example.com/video-top.gif" {
t.Fatalf("DetailBannerList = %#v", vm.DetailBannerList)
}
// 播放页小图标优先 appInfoAList;首页小图标无 appInfoBList 时回退 BList
if len(vm.AList) != 1 || vm.AList[0].ID != "a" {
t.Fatalf("AList = %#v", vm.AList)
}
if len(vm.BList) != 1 || vm.BList[0].ID != "b" {
t.Fatalf("BList = %#v", vm.BList)
}
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 TestAdvertisementClickTrackable(t *testing.T) {
cases := []struct {
ad Advertisement
wantID int64
trackable bool
}{
{Advertisement{ID: "1138200002", Location: 2}, 1138200002, true},
{Advertisement{ID: " 1138200012 ", Location: 12}, 1138200012, true},
{Advertisement{ID: "1138200003", Location: 0}, 1138200003, false},
{Advertisement{ID: "", Location: 3}, 0, false},
{Advertisement{ID: "0", Location: 3}, 0, false},
{Advertisement{ID: "-5", Location: 3}, 0, false},
{Advertisement{ID: "abc", Location: 3}, 0, false},
}
for _, tc := range cases {
if got := tc.ad.ClickID(); got != tc.wantID {
t.Fatalf("ClickID(%q) = %d, want %d", tc.ad.ID, got, tc.wantID)
}
if got := tc.ad.ClickTrackable(); got != tc.trackable {
t.Fatalf("ClickTrackable(%q, %d) = %v, want %v", tc.ad.ID, tc.ad.Location, got, tc.trackable)
}
}
}