diff --git a/backend/internal/service/antigravity_subscription_service.go b/backend/internal/service/antigravity_subscription_service.go index 04559be8a1..74ab6d87c2 100644 --- a/backend/internal/service/antigravity_subscription_service.go +++ b/backend/internal/service/antigravity_subscription_service.go @@ -21,9 +21,14 @@ func NormalizeAntigravitySubscription(resp *antigravity.LoadCodeAssistResponse) if resp == nil { return AntigravitySubscriptionResult{PlanType: "Free"} } + tierID := resp.GetTier() + planType := antigravity.TierIDToPlanType(tierID) if len(resp.IneligibleTiers) > 0 { + if planType == "" || planType == "Free" { + planType = "Abnormal" + } result := AntigravitySubscriptionResult{ - PlanType: "Abnormal", + PlanType: planType, SubscriptionStatus: antigravitySubscriptionAbnormal, } if resp.IneligibleTiers[0] != nil { @@ -31,8 +36,7 @@ func NormalizeAntigravitySubscription(resp *antigravity.LoadCodeAssistResponse) } return result } - tierID := resp.GetTier() return AntigravitySubscriptionResult{ - PlanType: antigravity.TierIDToPlanType(tierID), + PlanType: planType, } } diff --git a/backend/internal/service/antigravity_subscription_test.go b/backend/internal/service/antigravity_subscription_test.go new file mode 100644 index 0000000000..5e8c32a35e --- /dev/null +++ b/backend/internal/service/antigravity_subscription_test.go @@ -0,0 +1,66 @@ +package service + +import ( + "testing" + + "github.com/Wei-Shaw/sub2api/internal/pkg/antigravity" + "github.com/stretchr/testify/assert" +) + +func TestNormalizeAntigravitySubscription_PaidTierWithIneligible(t *testing.T) { + resp := &antigravity.LoadCodeAssistResponse{ + PaidTier: &antigravity.PaidTierInfo{ID: "g1-pro-tier"}, + IneligibleTiers: []*antigravity.IneligibleTier{ + {ReasonMessage: "location validation required"}, + }, + } + + result := NormalizeAntigravitySubscription(resp) + + assert.Equal(t, "Pro", result.PlanType, "paid tier should preserve Pro even with ineligible tiers") + assert.Equal(t, "abnormal", result.SubscriptionStatus) + assert.Equal(t, "location validation required", result.SubscriptionError) +} + +func TestNormalizeAntigravitySubscription_FreeTierWithIneligible(t *testing.T) { + resp := &antigravity.LoadCodeAssistResponse{ + PaidTier: &antigravity.PaidTierInfo{ID: "free-tier"}, + IneligibleTiers: []*antigravity.IneligibleTier{ + {ReasonMessage: "some warning"}, + }, + } + + result := NormalizeAntigravitySubscription(resp) + + assert.Equal(t, "Abnormal", result.PlanType, "free tier with ineligible should be Abnormal") + assert.Equal(t, "abnormal", result.SubscriptionStatus) +} + +func TestNormalizeAntigravitySubscription_NoIneligible(t *testing.T) { + resp := &antigravity.LoadCodeAssistResponse{ + PaidTier: &antigravity.PaidTierInfo{ID: "g1-ultra-tier"}, + } + + result := NormalizeAntigravitySubscription(resp) + + assert.Equal(t, "Ultra", result.PlanType) + assert.Empty(t, result.SubscriptionStatus) +} + +func TestNormalizeAntigravitySubscription_NilResponse(t *testing.T) { + result := NormalizeAntigravitySubscription(nil) + assert.Equal(t, "Free", result.PlanType) +} + +func TestNormalizeAntigravitySubscription_NoTierWithIneligible(t *testing.T) { + resp := &antigravity.LoadCodeAssistResponse{ + IneligibleTiers: []*antigravity.IneligibleTier{ + {ReasonMessage: "unknown issue"}, + }, + } + + result := NormalizeAntigravitySubscription(resp) + + assert.Equal(t, "Abnormal", result.PlanType, "no tier + ineligible should be Abnormal") + assert.Equal(t, "abnormal", result.SubscriptionStatus) +}