Files
lincodex/backend/internal/usecase/notification/delivery_status_test.go
T

85 lines
3.6 KiB
Go

package notification
import (
"context"
"errors"
"testing"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
type statusDeliveries struct {
outbound.DeliveryRepository
snapshot outbound.DeliveryStatusSnapshot
err error
asked []uuid.UUID
}
func (deliveries *statusDeliveries) LatestForRecipient(_ context.Context, recipientUserID uuid.UUID, _ domain.MailKind) (outbound.DeliveryStatusSnapshot, error) {
deliveries.asked = append(deliveries.asked, recipientUserID)
return deliveries.snapshot, deliveries.err
}
func TestDeliveryStatusReaderShowsOnlyTheUserSafeStatus(t *testing.T) {
userID := uuid.Must(uuid.NewV7())
resetAt := time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC)
changed := time.Date(2026, 10, 6, 22, 0, 0, 0, time.UTC)
tests := []struct {
name string
status domain.DeliveryStatus
next time.Time
wantWaiting bool
wantUntil time.Time
}{
{name: "waiting for a known reset", status: domain.DeliveryStatusWaitingQuota, next: resetAt, wantWaiting: true, wantUntil: resetAt},
{name: "waiting for an unknown reset", status: domain.DeliveryStatusWaitingQuota, wantWaiting: true},
{name: "pending shows no wait time", status: domain.DeliveryStatusPending, next: resetAt},
{name: "unknown", status: domain.DeliveryStatusUnknown},
{name: "accepted", status: domain.DeliveryStatusAccepted},
{name: "rejected", status: domain.DeliveryStatusRejected},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
deliveries := &statusDeliveries{snapshot: outbound.DeliveryStatusSnapshot{
DeliveryID: uuid.Must(uuid.NewV7()), Kind: domain.MailKindVerification, Status: test.status,
NextAttemptAt: test.next, CreatedAt: changed, UpdatedAt: changed,
}}
status, found, err := NewDeliveryStatusReader(deliveries).LatestForUser(t.Context(), userID, domain.MailKindVerification)
if err != nil || !found {
t.Fatalf("LatestForUser() = %t, %v; want a status", found, err)
}
if status.Status != test.status || status.Kind != domain.MailKindVerification || !status.UpdatedAt.Equal(changed) {
t.Fatalf("status = %+v", status)
}
if status.WaitingForQuota() != test.wantWaiting || !status.WaitingUntil.Equal(test.wantUntil) {
t.Fatalf("waiting %t until %s, want %t until %s", status.WaitingForQuota(), status.WaitingUntil, test.wantWaiting, test.wantUntil)
}
if len(deliveries.asked) != 1 || deliveries.asked[0] != userID {
t.Fatal("the reader did not scope the query to the user")
}
})
}
}
func TestDeliveryStatusReaderWithoutADelivery(t *testing.T) {
reader := NewDeliveryStatusReader(&statusDeliveries{err: outbound.ErrDeliveryNotFound})
if _, found, err := reader.LatestForUser(t.Context(), uuid.Must(uuid.NewV7()), domain.MailKindNotification); err != nil || found {
t.Fatalf("LatestForUser() = %t, %v; want nothing found", found, err)
}
failing := NewDeliveryStatusReader(&statusDeliveries{err: errStorageDown})
if _, _, err := failing.LatestForUser(t.Context(), uuid.Must(uuid.NewV7()), domain.MailKindNotification); !errors.Is(err, errStorageDown) {
t.Fatalf("LatestForUser() error = %v, want the storage error", err)
}
if _, _, err := reader.LatestForUser(t.Context(), uuid.Nil, domain.MailKindNotification); !errors.Is(err, domain.ErrInvalidDelivery) {
t.Fatalf("LatestForUser(nil user) error = %v, want ErrInvalidDelivery", err)
}
if _, _, err := reader.LatestForUser(t.Context(), uuid.Must(uuid.NewV7()), "invitation"); !errors.Is(err, domain.ErrInvalidMailContent) {
t.Fatalf("LatestForUser(unknown kind) error = %v, want ErrInvalidMailContent", err)
}
}