Files
lincodex/backend/integration_test/delivery_statistics_test.go
T

130 lines
5.0 KiB
Go

//go:build integration
package integration_test
import (
"context"
"slices"
"strings"
"testing"
"time"
"github.com/google/uuid"
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
const (
deliveryStatisticsTestTimeout = 3 * time.Minute
deliveryStatisticsAddress = "metrics.fixture@example.com"
)
// TestDeliveryStatistics checks the GetDeliveryStatistics aggregate behind the
// delivery metrics against the real deliveries table and its constraints.
func TestDeliveryStatistics(t *testing.T) {
env := bootstrap(t, postgresService)
ctx, cancel := context.WithTimeout(t.Context(), deliveryStatisticsTestTimeout)
defer cancel()
pool := env.newPool(ctx, t)
store := postgresadapter.NewDeliveryStatisticsStore(pool)
empty, err := store.DeliveryStatistics(ctx)
if err != nil {
t.Fatalf("DeliveryStatistics() on an empty table error = %v", err)
}
if len(empty.Counts) != 0 || !empty.EarliestPendingAttemptAt.IsZero() {
t.Fatalf("DeliveryStatistics() on an empty table = %+v, want nothing", empty)
}
start := time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC)
recipient := createStatisticsRecipient(ctx, t, postgresadapter.NewIdentityStore(pool).Users(), start)
deliveries := postgresadapter.NewDeliveryStore(pool).Deliveries()
account := mustProviderAccount(t, "resend")
create := func(kind domain.MailKind, createdAt time.Time) uuid.UUID {
t.Helper()
content, err := domain.NewMailContent(kind, string(kind)+".v1", "Subject", "Body\n", "")
if err != nil {
t.Fatalf("NewMailContent() error = %v", err)
}
delivery, err := domain.NewDelivery(domain.NewDeliveryInput{
ID: uuid.Must(uuid.NewV7()),
RecipientUserID: recipient.ID,
Recipient: recipient.Email,
Content: content,
ProviderAccount: account,
CreatedAt: createdAt,
})
if err != nil {
t.Fatalf("NewDelivery() error = %v", err)
}
if created, err := deliveries.Create(ctx, delivery); err != nil || !created {
t.Fatalf("Create() = %t, %v; want the delivery stored", created, err)
}
return delivery.ID()
}
// New deliveries are pending and due at creation.
create(domain.MailKindVerification, start.Add(2*time.Minute))
create(domain.MailKindVerification, start.Add(time.Minute))
create(domain.MailKindNotification, start.Add(3*time.Minute))
// The status updates below satisfy the table constraints for each
// status, the way the sending worker will leave them.
setStatus := func(id uuid.UUID, statement string) {
t.Helper()
if _, err := pool.Exec(ctx, "UPDATE deliveries SET "+statement+" WHERE id = $1", id); err != nil {
t.Fatalf("set the delivery status: %v", err)
}
}
// The earliest pending delivery stops being pending, so the earliest
// due time comes from the remaining pending ones only.
setStatus(create(domain.MailKindVerification, start), "status = 'unknown'")
setStatus(create(domain.MailKindPasswordReset, start), "status = 'waiting_quota', next_attempt_at = NULL")
setStatus(create(domain.MailKindNotification, start), "status = 'accepted', next_attempt_at = NULL, provider_message_id = 'msg-1'")
setStatus(create(domain.MailKindNotification, start), "status = 'rejected', next_attempt_at = NULL")
statistics, err := store.DeliveryStatistics(ctx)
if err != nil {
t.Fatalf("DeliveryStatistics() error = %v", err)
}
got := slices.Clone(statistics.Counts)
slices.SortFunc(got, func(a, b outbound.DeliveryStatusCount) int {
return strings.Compare(string(a.Kind)+"/"+string(a.Status), string(b.Kind)+"/"+string(b.Status))
})
want := []outbound.DeliveryStatusCount{
{Kind: domain.MailKindNotification, Status: domain.DeliveryStatusAccepted, Deliveries: 1},
{Kind: domain.MailKindNotification, Status: domain.DeliveryStatusPending, Deliveries: 1},
{Kind: domain.MailKindNotification, Status: domain.DeliveryStatusRejected, Deliveries: 1},
{Kind: domain.MailKindPasswordReset, Status: domain.DeliveryStatusWaitingQuota, Deliveries: 1},
{Kind: domain.MailKindVerification, Status: domain.DeliveryStatusPending, Deliveries: 2},
{Kind: domain.MailKindVerification, Status: domain.DeliveryStatusUnknown, Deliveries: 1},
}
if !slices.Equal(got, want) {
t.Fatalf("Counts = %+v, want %+v", got, want)
}
if !statistics.EarliestPendingAttemptAt.Equal(start.Add(time.Minute)) {
t.Fatalf("EarliestPendingAttemptAt = %s, want %s", statistics.EarliestPendingAttemptAt, start.Add(time.Minute))
}
}
func createStatisticsRecipient(ctx context.Context, t *testing.T, users outbound.UserRepository, now time.Time) domain.User {
t.Helper()
hash, err := domain.NewPasswordHash(identityTestPasswordHash)
if err != nil {
t.Fatalf("NewPasswordHash() error = %v", err)
}
user, err := domain.NewUser(domain.Registration{
Email: mustEmail(t, deliveryStatisticsAddress),
PasswordHash: hash,
Scenario: domain.ScenarioCandidate,
RegisteredAt: now,
})
if err != nil {
t.Fatalf("NewUser() error = %v", err)
}
if err := users.Create(ctx, user); err != nil {
t.Fatalf("create the recipient account: %v", err)
}
return user
}