219 lines
12 KiB
Go
219 lines
12 KiB
Go
package notification
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/inbound"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestDeliveryRetryOperatorAuthorizesOneRetryAfterTheAutomaticCutoff(t *testing.T) {
|
|
h := newSenderTestHarness(t, testIdempotencyWindow)
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("initial pass = %+v, want unknown", result)
|
|
}
|
|
operator, err := NewDeliveryRetryOperator(h.transactor, h.deliveries, testAccount(t, "resend"), testIdempotencyWindow, func() time.Time { return h.now })
|
|
if err != nil {
|
|
t.Fatalf("NewDeliveryRetryOperator() error = %v", err)
|
|
}
|
|
|
|
inspection, err := operator.Inspect(t.Context(), h.delivery.ID())
|
|
if err != nil || inspection.DeliveryID != h.delivery.ID() || inspection.Status != domain.DeliveryStatusUnknown ||
|
|
inspection.ProviderAccount != "main" || inspection.AttemptCount != 1 {
|
|
t.Fatalf("Inspect() = %+v, %v; want only delivery retry metadata", inspection, err)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", false); !errors.Is(err, ErrDuplicateRiskAcknowledgementRequired) {
|
|
t.Fatalf("RetryUnknown() without acknowledgement error = %v, want acknowledgement required", err)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); !errors.Is(err, outbound.ErrDeliveryRetryNotAllowed) {
|
|
t.Fatalf("RetryUnknown() before cutoff error = %v, want not allowed", err)
|
|
}
|
|
if len(h.deliveries.retryDecisions) != 0 || len(h.transport.mails) != 1 {
|
|
t.Fatal("a denied operator decision changed audit history or sent mail")
|
|
}
|
|
|
|
h.now = h.now.Add(23 * time.Hour)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("pass at the safety cutoff = %+v, want the automatic retry to stop as unknown", result)
|
|
}
|
|
if got := h.stored().delivery; got.Status() != domain.DeliveryStatusUnknown || !got.NextAttemptAt().IsZero() {
|
|
t.Fatalf("delivery at the safety cutoff is %s due %s, want unknown without a due time", got.Status(), got.NextAttemptAt())
|
|
}
|
|
|
|
warning, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true)
|
|
if err != nil || warning != DuplicateRiskWarning {
|
|
t.Fatalf("RetryUnknown() after cutoff = %q, %v; want the duplicate-risk warning", warning, err)
|
|
}
|
|
if len(h.deliveries.retryDecisions) != 1 || h.deliveries.retryDecisions[0].actor != "operator:mail-ops" ||
|
|
!h.deliveries.retryDecisions[0].duplicateRiskAcknowledged || h.deliveries.retryDecisions[0].previousLeaseExpired {
|
|
t.Fatalf("retry audit = %+v, want one acknowledged operator decision", h.deliveries.retryDecisions)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); !errors.Is(err, outbound.ErrDeliveryRetryNotAllowed) {
|
|
t.Fatalf("second RetryUnknown() error = %v, want not allowed", err)
|
|
}
|
|
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeAccepted, ProviderMessageID: testMessageID}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassAccepted {
|
|
t.Fatalf("manual retry pass = %+v, want accepted", result)
|
|
}
|
|
if len(h.transport.mails) != 2 || h.transport.mails[0].IdempotencyKey != h.transport.mails[1].IdempotencyKey ||
|
|
h.transport.mails[0].IdempotencyKey != h.delivery.IdempotencyKey() ||
|
|
h.transport.mails[0].TextBody != h.transport.mails[1].TextBody ||
|
|
h.transport.mails[0].To.Address() != h.transport.mails[1].To.Address() {
|
|
t.Fatal("the manual retry did not reuse the pinned key and original message")
|
|
}
|
|
}
|
|
|
|
func TestDeliveryRetryOperatorRequiresAnotherDecisionAfterManualUnknown(t *testing.T) {
|
|
h := newSenderTestHarness(t, testIdempotencyWindow)
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("initial pass = %+v, want unknown", result)
|
|
}
|
|
operator, err := NewDeliveryRetryOperator(h.transactor, h.deliveries, testAccount(t, "resend"), testIdempotencyWindow, func() time.Time { return h.now })
|
|
if err != nil {
|
|
t.Fatalf("NewDeliveryRetryOperator() error = %v", err)
|
|
}
|
|
h.now = h.now.Add(23 * time.Hour)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("pass at cutoff = %+v, want unknown", result)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); err != nil {
|
|
t.Fatalf("RetryUnknown() after cutoff error = %v", err)
|
|
}
|
|
|
|
h.transport.result = outbound.SendResult{
|
|
Outcome: outbound.MailOutcomeRejected,
|
|
Rejection: outbound.MailRejectionRateLimited,
|
|
RetryAfter: 5 * time.Minute,
|
|
}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassRetry {
|
|
t.Fatalf("manual rate-limit pass = %+v, want a scheduled retry", result)
|
|
}
|
|
pending := h.stored().delivery
|
|
if pending.Status() != domain.DeliveryStatusPending || !pending.NextAttemptAt().Equal(h.now.Add(5*time.Minute)) {
|
|
t.Fatalf("manual rate-limit state = %s due %s, want pending with Retry-After", pending.Status(), pending.NextAttemptAt())
|
|
}
|
|
h.now = pending.NextAttemptAt()
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("manual uncertain pass = %+v, want unknown", result)
|
|
}
|
|
unknown := h.stored().delivery
|
|
if unknown.Status() != domain.DeliveryStatusUnknown || !unknown.NextAttemptAt().IsZero() || len(h.deliveries.retryDecisions) != 1 {
|
|
t.Fatalf("manual uncertain state = %s due %s with %d decisions, want terminal unknown and one audit decision", unknown.Status(), unknown.NextAttemptAt(), len(h.deliveries.retryDecisions))
|
|
}
|
|
calls := len(h.transport.mails)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassIdle || len(h.transport.mails) != calls {
|
|
t.Fatalf("pass after manual unknown = %+v with %d calls, want idle without a retry", result, len(h.transport.mails))
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); err != nil {
|
|
t.Fatalf("second RetryUnknown() after explicit acknowledgement = %v, want a fresh decision against the original cutoff", err)
|
|
}
|
|
if len(h.deliveries.retryDecisions) != 2 {
|
|
t.Fatalf("manual decisions = %d, want a new audit decision after the second unknown", len(h.deliveries.retryDecisions))
|
|
}
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeAccepted, ProviderMessageID: testMessageID}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassAccepted {
|
|
t.Fatalf("pass after the new decision = %+v, want accepted", result)
|
|
}
|
|
}
|
|
|
|
func TestDeliveryRetryOperatorKeepsConfirmedRefusalRetriesAfterTheDeduplicationCutoff(t *testing.T) {
|
|
h := newSenderTestHarness(t, testIdempotencyWindow)
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("initial pass = %+v, want unknown", result)
|
|
}
|
|
operator, err := NewDeliveryRetryOperator(h.transactor, h.deliveries, testAccount(t, "resend"), testIdempotencyWindow, func() time.Time { return h.now })
|
|
if err != nil {
|
|
t.Fatalf("NewDeliveryRetryOperator() error = %v", err)
|
|
}
|
|
h.now = h.now.Add(23 * time.Hour)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("automatic pass at cutoff = %+v, want unknown", result)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); err != nil {
|
|
t.Fatalf("RetryUnknown() after cutoff error = %v", err)
|
|
}
|
|
refusal := outbound.SendResult{Outcome: outbound.MailOutcomeRejected, Rejection: outbound.MailRejectionRateLimited, RetryAfter: 5 * time.Minute}
|
|
h.transport.result = refusal
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassRetry {
|
|
t.Fatalf("first manual refusal = %+v, want retry", result)
|
|
}
|
|
h.now = h.stored().delivery.NextAttemptAt().Add(25 * time.Hour)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassRetry {
|
|
t.Fatalf("confirmed refusal after the old provider window = %+v, want a scheduled retry", result)
|
|
}
|
|
if got := h.stored().delivery; got.Status() != domain.DeliveryStatusPending || got.AttemptCount() != 3 || got.NextAttemptAt().IsZero() {
|
|
t.Fatalf("delivery after repeated confirmed refusals is %s after %d attempts due %s; want pending without a fabricated unknown", got.Status(), got.AttemptCount(), got.NextAttemptAt())
|
|
}
|
|
h.now = h.stored().delivery.NextAttemptAt()
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("manual unknown result = %+v, want terminal unknown", result)
|
|
}
|
|
unknown := h.stored().delivery
|
|
if unknown.Status() != domain.DeliveryStatusUnknown || !unknown.NextAttemptAt().IsZero() {
|
|
t.Fatalf("delivery after unknown is %s due %s, want terminal unknown", unknown.Status(), unknown.NextAttemptAt())
|
|
}
|
|
calls := len(h.transport.mails)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassIdle || len(h.transport.mails) != calls {
|
|
t.Fatalf("pass after unknown = %+v with %d calls, want idle", result, len(h.transport.mails))
|
|
}
|
|
}
|
|
|
|
func TestDeliveryRetryOperatorRecoversManualSendStartedWithoutAResult(t *testing.T) {
|
|
h := newSenderTestHarness(t, testIdempotencyWindow)
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, ProviderCode: "application_error"}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("initial pass = %+v, want unknown", result)
|
|
}
|
|
operator, err := NewDeliveryRetryOperator(h.transactor, h.deliveries, testAccount(t, "resend"), testIdempotencyWindow, func() time.Time { return h.now })
|
|
if err != nil {
|
|
t.Fatalf("NewDeliveryRetryOperator() error = %v", err)
|
|
}
|
|
h.now = h.now.Add(23 * time.Hour)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown {
|
|
t.Fatalf("automatic pass at cutoff = %+v, want unknown", result)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); err != nil {
|
|
t.Fatalf("RetryUnknown() after cutoff error = %v", err)
|
|
}
|
|
refusal := outbound.SendResult{Outcome: outbound.MailOutcomeRejected, Rejection: outbound.MailRejectionRateLimited, RetryAfter: time.Minute}
|
|
h.transport.result = refusal
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassRetry {
|
|
t.Fatalf("first manual rate limit = %+v, want a confirmed refusal retry", result)
|
|
}
|
|
h.now = h.stored().delivery.NextAttemptAt()
|
|
h.transactor.calls, h.transactor.settlementDown = 0, true
|
|
if _, err := h.service.ProcessNext(t.Context(), t.Context()); !errors.Is(err, errSettlementDown) {
|
|
t.Fatalf("ProcessNext() error = %v, want lost settlement after a started manual send", err)
|
|
}
|
|
if len(h.transport.mails) != 3 || !h.stored().hasSendStarted || h.stored().sendStartedAttemptCount != h.stored().delivery.AttemptCount() {
|
|
t.Fatalf("manual send starts = %d, marker = %d, attempt_count = %d; want an unresolved send after a confirmed refusal", len(h.transport.mails), h.stored().sendStartedAttemptCount, h.stored().delivery.AttemptCount())
|
|
}
|
|
h.now = h.now.Add(testSenderLease)
|
|
h.transactor.settlementDown = false
|
|
calls := len(h.transport.mails)
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassUnknown || len(h.transport.mails) != calls {
|
|
t.Fatalf("recovery after lost manual result = %+v with %d calls, want terminal unknown without a provider call", result, len(h.transport.mails)-calls)
|
|
}
|
|
if _, err := operator.RetryUnknown(t.Context(), h.delivery.ID(), "operator:mail-ops", true); err != nil {
|
|
t.Fatalf("new decision after expired manual send lease = %v, want authorization from the original cutoff", err)
|
|
}
|
|
if len(h.deliveries.retryDecisions) != 2 || h.deliveries.retryDecisions[1].attemptCount != 3 ||
|
|
len(h.stored().attempts) != 3 || h.stored().attempts[1].Outcome != domain.DeliveryOutcomeRejected ||
|
|
h.stored().attempts[2].Outcome != domain.DeliveryOutcomeUnknown {
|
|
t.Fatalf("retry audit = %+v, attempts = %+v; want confirmed refusal then recovered unknown before the second decision", h.deliveries.retryDecisions, h.stored().attempts)
|
|
}
|
|
h.transport.result = outbound.SendResult{Outcome: outbound.MailOutcomeAccepted, ProviderMessageID: testMessageID}
|
|
if result := h.pass(t); result.Outcome != inbound.MailPassAccepted {
|
|
t.Fatalf("pass after a new manual decision = %+v, want accepted", result)
|
|
}
|
|
}
|