package vip import ( "encoding/json" "errors" "net/http" "testing" "time" "chatapp3-golang/internal/model" ) func TestBuildPurchasePlanNewPurchase(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) configs := vipTestConfigs() plan, err := buildPurchasePlan(now, nil, configs[2], configs) if err != nil { t.Fatalf("buildPurchasePlan returned error: %v", err) } if plan.OrderType != orderTypePurchase { t.Fatalf("order type = %s, want %s", plan.OrderType, orderTypePurchase) } if plan.CurrentLevel != 0 || plan.TargetLevel != 2 { t.Fatalf("levels = %d -> %d, want 0 -> 2", plan.CurrentLevel, plan.TargetLevel) } if plan.PayableGold != 2000 { t.Fatalf("payable = %d, want 2000", plan.PayableGold) } if !plan.StartAt.Equal(now) || !plan.ExpireAt.Equal(now.AddDate(0, 0, vipDurationDay)) { t.Fatalf("period = %s -> %s, want %s -> %s", plan.StartAt, plan.ExpireAt, now, now.AddDate(0, 0, vipDurationDay)) } } func TestBuildPurchasePlanUpgradePaysDifferenceAndKeepsExpireAt(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) startAt := now.AddDate(0, 0, -5) expireAt := now.AddDate(0, 0, 25) configs := vipTestConfigs() state := &model.UserVipState{ Level: 2, Status: statusActive, StartAt: startAt, ExpireAt: expireAt, } plan, err := buildPurchasePlan(now, state, configs[4], configs) if err != nil { t.Fatalf("buildPurchasePlan returned error: %v", err) } if plan.OrderType != orderTypeUpgrade { t.Fatalf("order type = %s, want %s", plan.OrderType, orderTypeUpgrade) } if plan.PayableGold != 2000 { t.Fatalf("payable = %d, want 2000", plan.PayableGold) } if !plan.StartAt.Equal(startAt) || !plan.ExpireAt.Equal(expireAt) { t.Fatalf("upgrade period changed to %s -> %s, want %s -> %s", plan.StartAt, plan.ExpireAt, startAt, expireAt) } } func TestBuildPurchasePlanRenewExtendsThirtyDays(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) startAt := now.AddDate(0, 0, -5) expireAt := now.AddDate(0, 0, 25) configs := vipTestConfigs() state := &model.UserVipState{ Level: 3, Status: statusActive, StartAt: startAt, ExpireAt: expireAt, } plan, err := buildPurchasePlan(now, state, configs[3], configs) if err != nil { t.Fatalf("buildPurchasePlan returned error: %v", err) } if plan.OrderType != orderTypeRenew { t.Fatalf("order type = %s, want %s", plan.OrderType, orderTypeRenew) } if plan.PayableGold != 3000 { t.Fatalf("payable = %d, want 3000", plan.PayableGold) } if !plan.ExpireAt.Equal(expireAt.AddDate(0, 0, vipDurationDay)) { t.Fatalf("renew expireAt = %s, want %s", plan.ExpireAt, expireAt.AddDate(0, 0, vipDurationDay)) } } func TestBuildPurchasePlanRejectsDowngrade(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) configs := vipTestConfigs() state := &model.UserVipState{ Level: 4, Status: statusActive, StartAt: now.AddDate(0, 0, -5), ExpireAt: now.AddDate(0, 0, 25), } if _, err := buildPurchasePlan(now, state, configs[2], configs); err == nil { t.Fatal("buildPurchasePlan returned nil error for downgrade") } } func TestVipResourcePayloadAcceptsStringResourceID(t *testing.T) { var payload VipResourcePayload if err := json.Unmarshal([]byte(`{"resourceId":"2045029471274201089","name":"vip","sourceUrl":"https://example.com/a.svga","coverUrl":"https://example.com/a.png"}`), &payload); err != nil { t.Fatalf("unmarshal resource payload: %v", err) } payload = normalizeResource(payload) if payload.ResourceID.Int64() != 2045029471274201089 { t.Fatalf("resource id = %d, want 2045029471274201089", payload.ResourceID.Int64()) } if payload.URL != "https://example.com/a.svga" || payload.SourceURL != payload.URL || payload.ResourceURL != payload.URL { t.Fatalf("resource urls = url:%q sourceUrl:%q resourceUrl:%q", payload.URL, payload.SourceURL, payload.ResourceURL) } if payload.Cover != "https://example.com/a.png" || payload.CoverURL != payload.Cover { t.Fatalf("resource cover = cover:%q coverUrl:%q", payload.Cover, payload.CoverURL) } body, err := json.Marshal(payload) if err != nil { t.Fatalf("marshal resource payload: %v", err) } if got := string(body); got != `{"resourceId":"2045029471274201089","name":"vip","url":"https://example.com/a.svga","sourceUrl":"https://example.com/a.svga","resourceUrl":"https://example.com/a.svga","cover":"https://example.com/a.png","coverUrl":"https://example.com/a.png"}` { t.Fatalf("json = %s", got) } } func TestResourcePayloadUsesResourceGroupDetails(t *testing.T) { row := model.VipLevelConfig{ Level: 2, LevelCode: "VIP2", DisplayName: "VIP 2", Enabled: true, DurationDays: vipDurationDay, PriceGold: 2000, AvatarFrameResourceID: 11, AvatarFrameName: "stale frame", AvatarFrameURL: "https://example.com/stale.svga", } payload := payloadFromConfigWithResources(row, vipResourceLookup{ 11: { ID: 11, Name: "resource group frame", Cover: "https://example.com/live.png", SourceURL: "https://example.com/live.svga", }, }) if payload.AvatarFrame.Name != "resource group frame" || payload.AvatarFrame.URL != "https://example.com/live.svga" || payload.AvatarFrame.Cover != "https://example.com/live.png" { t.Fatalf("avatar frame was not loaded from resource group: %+v", payload.AvatarFrame) } } func TestApplyPlanToStateKeepsOnlyResourceIDsAndPayloadUsesLookup(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) plan := purchasePlan{ TargetLevel: 3, StartAt: now, ExpireAt: now.AddDate(0, 0, vipDurationDay), DurationDays: vipDurationDay, TargetConfig: model.VipLevelConfig{ Level: 3, DisplayName: "VIP 3", AvatarFrameResourceID: 11, AvatarFrameName: "frame", AvatarFrameURL: "https://example.com/frame.svga", EntryEffectResourceID: 12, EntryEffectName: "ride", EntryEffectURL: "https://example.com/ride.svga", ChatBubbleResourceID: 13, ChatBubbleName: "bubble", ChatBubbleURL: "https://example.com/bubble.svga", FloatPictureResourceID: 14, FloatPictureName: "float", FloatPictureURL: "https://example.com/float.svga", }, } state := &model.UserVipState{UserID: 1001, SysOrigin: "LIKEI"} applyPlanToState(state, plan, now) if state.AvatarFrameResourceID != plan.TargetConfig.AvatarFrameResourceID || state.EntryEffectResourceID != plan.TargetConfig.EntryEffectResourceID || state.ChatBubbleResourceID != plan.TargetConfig.ChatBubbleResourceID || state.FloatPictureResourceID != plan.TargetConfig.FloatPictureResourceID { t.Fatalf("state resource ids were not copied from target config") } if state.AvatarFrameName != "" || state.AvatarFrameURL != "" { t.Fatalf("state stored resource details: name=%q url=%q", state.AvatarFrameName, state.AvatarFrameURL) } payload := statePayloadFromModelWithResources(state, now, vipResourceLookup{ 11: {ID: 11, Name: "frame", Cover: "https://example.com/frame.png", SourceURL: "https://example.com/frame.svga"}, 12: {ID: 12, Name: "ride", Cover: "https://example.com/ride.png", SourceURL: "https://example.com/ride.svga"}, 13: {ID: 13, Name: "bubble", Cover: "https://example.com/bubble.png", SourceURL: "https://example.com/bubble.svga"}, 14: {ID: 14, Name: "float", Cover: "https://example.com/float.png", SourceURL: "https://example.com/float.svga"}, }) if payload.AvatarFrame.Cover != "https://example.com/frame.png" || payload.EntryEffect.Cover != "https://example.com/ride.png" || payload.ChatBubble.Cover != "https://example.com/bubble.png" || payload.FloatPicture.Cover != "https://example.com/float.png" { t.Fatalf("state payload covers were not loaded from resource lookup") } } func TestMapWalletPurchaseErrorInsufficientBalance(t *testing.T) { err := errors.New(`java api failed: status=406 body={"errorCode":5000,"errorCodeName":"INSUFFICIENT_BALANCE","errorMsg":"balance not made"}`) appErr := mapWalletPurchaseError(err) if appErr.Status != http.StatusNotAcceptable { t.Fatalf("status = %d, want %d", appErr.Status, http.StatusNotAcceptable) } if appErr.Code != "vip_insufficient_balance" { t.Fatalf("code = %s, want vip_insufficient_balance", appErr.Code) } if appErr.Message != "insufficient balance" { t.Fatalf("message = %s, want insufficient balance", appErr.Message) } } func TestMapWalletPurchaseErrorGatewayFailure(t *testing.T) { err := errors.New("java api failed: status=500 body=boom") appErr := mapWalletPurchaseError(err) if appErr.Status != http.StatusBadGateway { t.Fatalf("status = %d, want %d", appErr.Status, http.StatusBadGateway) } if appErr.Code != "vip_wallet_deduct_failed" { t.Fatalf("code = %s, want vip_wallet_deduct_failed", appErr.Code) } } func TestVIPGrantResourcesFromConfigMapsBackpackTypes(t *testing.T) { config := model.VipLevelConfig{ BadgeResourceID: 10, AvatarFrameResourceID: 11, EntryEffectResourceID: 12, ChatBubbleResourceID: 13, FloatPictureResourceID: 14, } resources := vipGrantResourcesFromConfig(config) if len(resources) != 5 { t.Fatalf("resources len = %d, want 5", len(resources)) } assertGrant := func(index int, resourceID int64, propsType string, badge bool) { t.Helper() got := resources[index] if got.ResourceID != resourceID || got.PropsType != propsType || got.Badge != badge { t.Fatalf("resource[%d] = %+v, want id=%d type=%s badge=%v", index, got, resourceID, propsType, badge) } } assertGrant(0, 10, "", true) assertGrant(1, 11, vipPropsTypeAvatarFrame, false) assertGrant(2, 12, vipPropsTypeRide, false) assertGrant(3, 13, vipPropsTypeChatBubble, false) assertGrant(4, 14, vipPropsTypeFloatPicture, false) } func TestVIPGrantDaysUntilCeilsPartialDay(t *testing.T) { now := time.Date(2026, 4, 29, 10, 0, 0, 0, time.UTC) expireAt := now.Add((24 * time.Hour) + time.Second) if got := vipGrantDaysUntil(now, expireAt); got != 2 { t.Fatalf("days = %d, want 2", got) } if got := vipGrantDaysUntil(now, now); got != 1 { t.Fatalf("expired days = %d, want 1", got) } } func vipTestConfigs() map[int]model.VipLevelConfig { configs := make(map[int]model.VipLevelConfig, levelCount) for level := 1; level <= levelCount; level++ { configs[level] = model.VipLevelConfig{ Level: level, LevelCode: levelCode(level), DisplayName: levelDisplayName(level), Enabled: true, DurationDays: vipDurationDay, PriceGold: int64(level * 1000), } } return configs }