package vidhelpser import ( "testing" "91porn-server/models/v/vidmod" ) func TestFreeTrialBadgeContextFields(t *testing.T) { const ( uid = uint64(1001) publisherID = uint64(2002) ) tests := []struct { name string ctx FreeTrialBadgeContext newsType string originCoins int64 freeArea bool publisherID uint64 wantShow bool wantRemaining uint64 wantCanUse bool }{ { name: "eligible vip video", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, true), newsType: vidmod.SP, publisherID: publisherID, wantShow: true, wantRemaining: 2, wantCanUse: true, }, { name: "switch only hides badge", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, false), newsType: vidmod.SHORT, publisherID: publisherID, wantRemaining: 2, wantCanUse: true, }, { name: "active vip does not use trial", ctx: NewFreeTrialBadgeContext(uid, 3, 2, true, true), newsType: vidmod.SP, publisherID: publisherID, wantRemaining: 2, }, { name: "coin video", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, true), newsType: vidmod.SP, originCoins: 10, publisherID: publisherID, wantRemaining: 2, }, { name: "free area", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, true), newsType: vidmod.SP, freeArea: true, publisherID: publisherID, wantRemaining: 2, }, { name: "own video", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, true), newsType: vidmod.SP, publisherID: uid, wantRemaining: 2, }, { name: "advertisement", ctx: NewFreeTrialBadgeContext(uid, 3, 2, false, true), newsType: vidmod.AD_SP, publisherID: publisherID, wantRemaining: 2, }, { name: "remaining count is clamped", ctx: NewFreeTrialBadgeContext(uid, 3, 99, false, true), newsType: vidmod.SP, publisherID: publisherID, wantShow: true, wantRemaining: 3, wantCanUse: true, }, { name: "no remaining count hides badge", ctx: NewFreeTrialBadgeContext(uid, 3, 0, false, true), newsType: vidmod.SP, publisherID: publisherID, wantRemaining: 0, wantCanUse: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { show, remaining, canUse := tt.ctx.Fields(tt.newsType, tt.originCoins, tt.freeArea, tt.publisherID) if show != tt.wantShow || remaining != tt.wantRemaining || canUse != tt.wantCanUse { t.Fatalf( "Fields() = (%v, %d, %v), want (%v, %d, %v)", show, remaining, canUse, tt.wantShow, tt.wantRemaining, tt.wantCanUse, ) } }) } } func TestApplyFreeTrialBadgeToVideoInfos(t *testing.T) { ctx := NewFreeTrialBadgeContext(1001, 3, 2, false, true) video := &vidmod.VideoInfo{ VideoBase: vidmod.VideoBase{ NewsType: vidmod.SP, OriginCoins: 0, }, UInfo: vidmod.Publisher{ UInfo: vidmod.UInfo{UID: 2002}, }, } ApplyFreeTrialBadgeToVideoInfos(ctx, []*vidmod.VideoInfo{video}) if !video.ShowFreeTrialBadge || !video.CanUseFreeTrial || video.FreeTrialRemaining != 2 { t.Fatalf("unexpected badge fields: %+v", video.VideoBase) } }