package domain import ( "testing" "time" ) func TestCalculateSubscriptionExpiresAt_lifetime(t *testing.T) { start := time.Date(2026, 6, 10, 12, 0, 0, 0, time.UTC) got := CalculateSubscriptionExpiresAt(start, true, 1, "MONTH") if !got.Equal(LifetimeSubscriptionExpiresAt) { t.Fatalf("expected lifetime expiry, got %v", got) } } func TestCalculateSubscriptionExpiresAt_usesDurationWhenNotLifetime(t *testing.T) { start := time.Date(2026, 6, 10, 12, 0, 0, 0, time.UTC) got := CalculateSubscriptionExpiresAt(start, false, 1, "MONTH") want := start.AddDate(0, 1, 0) if !got.Equal(want) { t.Fatalf("got %v, want %v", got, want) } } func TestIsLifetimeExpiry(t *testing.T) { if !IsLifetimeExpiry(LifetimeSubscriptionExpiresAt) { t.Fatal("sentinel should be lifetime") } if IsLifetimeExpiry(time.Date(2026, 12, 31, 0, 0, 0, 0, time.UTC)) { t.Fatal("normal date should not be lifetime") } } func TestNormalizeLifetimePlanCreateDuration(t *testing.T) { v, u := NormalizeLifetimePlanCreateDuration(true, 3, "MONTH") if v != LifetimeDurationValue || u != LifetimeDurationUnit { t.Fatalf("got %d %s", v, u) } v, u = NormalizeLifetimePlanCreateDuration(false, 3, "MONTH") if v != 3 || u != "MONTH" { t.Fatalf("got %d %s", v, u) } v, u = NormalizeLifetimePlanCreateDuration(false, 3, "month") if v != 3 || u != "MONTH" { t.Fatalf("expected trimmed uppercase unit, got %d %s", v, u) } } func TestNormalizeSubscriptionPlanPricing(t *testing.T) { _, price, err := NormalizeSubscriptionPlanPricing(true, 10) if err != ErrFreePlanMustHaveZeroPrice || price != 0 { t.Fatalf("expected free plan price error, got price=%v err=%v", price, err) } isFree, price, err := NormalizeSubscriptionPlanPricing(true, 0) if err != nil || !isFree || price != 0 { t.Fatalf("expected free zero price, got isFree=%v price=%v err=%v", isFree, price, err) } _, _, err = NormalizeSubscriptionPlanPricing(false, 0) if err != ErrPaidPlanMustHavePositivePrice { t.Fatalf("expected paid plan error, got %v", err) } isFree, price, err = NormalizeSubscriptionPlanPricing(false, 99) if err != nil || isFree || price != 99 { t.Fatalf("expected paid plan 99, got isFree=%v price=%v err=%v", isFree, price, err) } } func TestSameSubscriptionPlanPeriod_ignoresFreeVsPaid(t *testing.T) { free := SubscriptionPlanPeriodFromCreate(CreateSubscriptionPlanInput{ Category: "LEARN_ENGLISH", IsLifetime: false, DurationValue: 1, DurationUnit: "MONTH", IsFree: true, }) paid := SubscriptionPlanPeriodFromCreate(CreateSubscriptionPlanInput{ Category: "LEARN_ENGLISH", IsLifetime: false, DurationValue: 1, DurationUnit: "MONTH", IsFree: false, Price: 99, }) if !SameSubscriptionPlanPeriod(free, paid) { t.Fatal("free and paid plans with same category and period must conflict") } } func TestSubscriptionPlanPeriodFromCreate(t *testing.T) { lifetime := SubscriptionPlanPeriodFromCreate(CreateSubscriptionPlanInput{ Category: "learn_english", IsLifetime: true, DurationValue: 6, DurationUnit: "MONTH", }) if lifetime.Category != "LEARN_ENGLISH" || !lifetime.IsLifetime { t.Fatalf("unexpected lifetime period: %+v", lifetime) } if lifetime.DurationValue != LifetimeDurationValue || lifetime.DurationUnit != LifetimeDurationUnit { t.Fatalf("lifetime duration not normalized: %+v", lifetime) } monthly := SubscriptionPlanPeriodFromCreate(CreateSubscriptionPlanInput{ Category: "IELTS", IsLifetime: false, DurationValue: 1, DurationUnit: "MONTH", }) if monthly.Category != "IELTS" || monthly.IsLifetime || monthly.DurationValue != 1 || monthly.DurationUnit != "MONTH" { t.Fatalf("unexpected monthly period: %+v", monthly) } } func TestSubscriptionGrantsContentAccess(t *testing.T) { if !SubscriptionGrantsContentAccess("LEARN_ENGLISH", "LEARN_ENGLISH") { t.Fatal("LEARN_ENGLISH plan should unlock LMS content") } if SubscriptionGrantsContentAccess("LEARN_ENGLISH", "IELTS") { t.Fatal("LEARN_ENGLISH plan should not unlock exam prep content") } if !SubscriptionGrantsContentAccess("IELTS", "IELTS") { t.Fatal("IELTS plan should unlock IELTS content") } if SubscriptionGrantsContentAccess("IELTS", "DUOLINGO") { t.Fatal("IELTS plan should not unlock Duolingo content") } if !SubscriptionGrantsContentAccess("DUOLINGO", "DUOLINGO") { t.Fatal("DUOLINGO plan should unlock Duolingo content") } }