|
| 1 | +package emails |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func testUserEmailFactory() UserEmailFactory { |
| 12 | + return NewHermesUserEmailFactory(&HermesGeneratorConfig{ |
| 13 | + AppURL: "https://httpsms.com", |
| 14 | + AppName: "httpSMS", |
| 15 | + AppLogoURL: "https://httpsms.com/logo.png", |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +func TestFormatBillingDate_RendersInProvidedTimezone(t *testing.T) { |
| 20 | + // 2026-06-19 02:00 UTC |
| 21 | + timestamp := time.Date(2026, 6, 19, 2, 0, 0, 0, time.UTC) |
| 22 | + |
| 23 | + // A timezone five hours behind UTC rolls back to the previous day. |
| 24 | + behind := time.FixedZone("UTC-5", -5*60*60) |
| 25 | + assert.Equal(t, "18 June 2026", formatBillingDate(timestamp, behind)) |
| 26 | + |
| 27 | + // A timezone ahead of UTC stays on the same day. |
| 28 | + ahead := time.FixedZone("UTC+10", 10*60*60) |
| 29 | + assert.Equal(t, "19 June 2026", formatBillingDate(timestamp, ahead)) |
| 30 | + |
| 31 | + // UTC renders the underlying date as-is. |
| 32 | + assert.Equal(t, "19 June 2026", formatBillingDate(timestamp, time.UTC)) |
| 33 | +} |
| 34 | + |
| 35 | +func TestUsageLimitExceeded_IncludesBreakdownAndBillingPeriod(t *testing.T) { |
| 36 | + factory := testUserEmailFactory() |
| 37 | + user := &entities.User{ |
| 38 | + Email: "name@email.com", |
| 39 | + Timezone: "UTC", |
| 40 | + SubscriptionName: entities.SubscriptionNameProMonthly, |
| 41 | + } |
| 42 | + usage := &entities.BillingUsage{ |
| 43 | + SentMessages: 3000, |
| 44 | + ReceivedMessages: 2000, |
| 45 | + StartTimestamp: time.Date(2026, 6, 19, 0, 0, 0, 0, time.UTC), |
| 46 | + EndTimestamp: time.Date(2026, 7, 18, 23, 59, 59, 0, time.UTC), |
| 47 | + } |
| 48 | + |
| 49 | + email, err := factory.UsageLimitExceeded(user, usage) |
| 50 | + |
| 51 | + assert.NoError(t, err) |
| 52 | + assert.Equal(t, "name@email.com", email.ToEmail) |
| 53 | + assert.Equal(t, "⚠️ You have exceeded your plan limit", email.Subject) |
| 54 | + assert.Contains(t, email.Text, "limit of 5000 messages") |
| 55 | + assert.Contains(t, email.Text, "Between 19 June 2026 and 18 July 2026") |
| 56 | + assert.Contains(t, email.Text, "you sent 3000 messages and received 2000") |
| 57 | + assert.Contains(t, email.Text, "for a total of 5000") |
| 58 | +} |
| 59 | + |
| 60 | +func TestUsageLimitAlert_IncludesPercentBreakdownAndLimit(t *testing.T) { |
| 61 | + factory := testUserEmailFactory() |
| 62 | + user := &entities.User{ |
| 63 | + Email: "name@email.com", |
| 64 | + Timezone: "UTC", |
| 65 | + SubscriptionName: entities.SubscriptionNameProMonthly, |
| 66 | + } |
| 67 | + usage := &entities.BillingUsage{ |
| 68 | + SentMessages: 2500, |
| 69 | + ReceivedMessages: 1500, |
| 70 | + StartTimestamp: time.Date(2026, 6, 19, 0, 0, 0, 0, time.UTC), |
| 71 | + EndTimestamp: time.Date(2026, 7, 18, 23, 59, 59, 0, time.UTC), |
| 72 | + } |
| 73 | + |
| 74 | + email, err := factory.UsageLimitAlert(user, usage) |
| 75 | + |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.Equal(t, "name@email.com", email.ToEmail) |
| 78 | + assert.Equal(t, "⚠️ 80% Usage Limit Alert", email.Subject) |
| 79 | + assert.Contains(t, email.Text, "used 80% of your monthly SMS limit") |
| 80 | + assert.Contains(t, email.Text, "Between 19 June 2026 and 18 July 2026") |
| 81 | + assert.Contains(t, email.Text, "you sent 2500 messages and received 1500") |
| 82 | + assert.Contains(t, email.Text, "for a total of 4000 out of your 5000 message limit") |
| 83 | +} |
0 commit comments