syrup-rail 0.5.0

Validated domain types and lifecycle policy for Syrup Rail billing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use chrono::{DateTime, Duration, Utc};
use thiserror::Error;

use crate::{
    BillingContactSnapshot, BillingPeriod, BillingScopeId, ChargeAmount, GatewayAccountId,
    GatewayAccountMode, GatewayProviderKey, IdempotencyKey, PaymentAttempt,
    PaymentAttemptFingerprint, PaymentAttemptId, PaymentAttemptIdentity, PaymentAttemptKind,
    PaymentAttemptRequest, PaymentAttemptTarget, PaymentMethodId, PlanKey, RenewalFailurePolicy,
    ResolvedGateway, SubscriberId, SubscriptionId, SubscriptionPaymentStateSnapshot,
    SubscriptionStatus,
};

pub const RENEWAL_DISPATCH_LIMIT: i64 = 100;
pub const RENEWAL_INFRASTRUCTURE_RETRY_AFTER_SECONDS: i64 = 24 * 60 * 60;
pub const RENEWAL_RATE_LIMIT_SLOW_RETRY_AFTER_SECONDS: i64 = 24 * 60 * 60;
pub const MAX_RENEWAL_INFRASTRUCTURE_ATTEMPTS_PER_PERIOD_CONFIGURATION: i64 = 8;
pub const RENEWAL_RATE_LIMIT_FAST_RETRY_ATTEMPTS: i64 = 5;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RenewalFailureDisposition {
    RetryScheduled { retry_at: DateTime<Utc> },
    RemainPastDue { exhausted_at: DateTime<Utc> },
    MarkUnpaid { ended_at: DateTime<Utc> },
}

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
pub enum RenewalFailurePolicyError {
    #[error("automatic renewal failure count is one-based")]
    ZeroFailureCount,
    #[error("dunning retry timestamp overflowed the supported date range")]
    RetryTimestampOverflow,
}

pub fn renewal_failure_disposition(
    policy: &RenewalFailurePolicy,
    automatic_failure_count: u16,
    failed_at: DateTime<Utc>,
) -> Result<RenewalFailureDisposition, RenewalFailurePolicyError> {
    let schedule_index = automatic_failure_count
        .checked_sub(1)
        .ok_or(RenewalFailurePolicyError::ZeroFailureCount)?;
    if let Some(delay) = policy
        .schedule()
        .retry_delays()
        .get(usize::from(schedule_index))
    {
        let retry_at = failed_at
            .checked_add_signed(Duration::seconds(i64::from(delay.seconds().get())))
            .ok_or(RenewalFailurePolicyError::RetryTimestampOverflow)?;
        return Ok(RenewalFailureDisposition::RetryScheduled { retry_at });
    }
    Ok(match policy.exhaustion() {
        crate::DunningExhaustion::RemainPastDue => RenewalFailureDisposition::RemainPastDue {
            exhausted_at: failed_at,
        },
        crate::DunningExhaustion::MarkUnpaid => RenewalFailureDisposition::MarkUnpaid {
            ended_at: failed_at,
        },
    })
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenewalDispatch {
    billing_scope_id: BillingScopeId,
    subscription_id: SubscriptionId,
    required_gateway_account_mode: GatewayAccountMode,
    period_start_at: DateTime<Utc>,
    attempt_sequence_count: i64,
}

impl RenewalDispatch {
    pub const fn new(
        billing_scope_id: BillingScopeId,
        subscription_id: SubscriptionId,
        required_gateway_account_mode: GatewayAccountMode,
        period_start_at: DateTime<Utc>,
        attempt_sequence_count: i64,
    ) -> Self {
        Self {
            billing_scope_id,
            subscription_id,
            required_gateway_account_mode,
            period_start_at,
            attempt_sequence_count,
        }
    }

    pub const fn billing_scope_id(&self) -> BillingScopeId {
        self.billing_scope_id
    }

    pub const fn subscription_id(&self) -> SubscriptionId {
        self.subscription_id
    }

    /// Deployment mode of the durable subscription that owns this work.
    pub const fn required_gateway_account_mode(&self) -> GatewayAccountMode {
        self.required_gateway_account_mode
    }

    pub const fn period_start_at(&self) -> &DateTime<Utc> {
        &self.period_start_at
    }

    pub const fn attempt_sequence_count(&self) -> i64 {
        self.attempt_sequence_count
    }
}

/// Continuation key returned by a prior renewal-dispatch page.
///
/// `observed_at` is the PostgreSQL clock timestamp captured when the scan's
/// first page was read. Every continuation retains that same eligibility-time
/// bound. The remaining fields are the last returned row's strict ascending
/// `(next_payment_attempt_at, subscription_id)` key; they are data values,
/// never caller-supplied SQL.
///
/// Hosts may reconstruct this value from their own trusted persisted page
/// state. It must originate from a prior renewal page and must never be
/// accepted from an end user; the host remains the trusted dispatch boundary.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RenewalDispatchPageCursor {
    observed_at: DateTime<Utc>,
    next_payment_attempt_at: DateTime<Utc>,
    subscription_id: SubscriptionId,
    required_gateway_account_mode: Option<GatewayAccountMode>,
}

impl RenewalDispatchPageCursor {
    pub const fn new(
        observed_at: DateTime<Utc>,
        next_payment_attempt_at: DateTime<Utc>,
        subscription_id: SubscriptionId,
        required_gateway_account_mode: Option<GatewayAccountMode>,
    ) -> Self {
        Self {
            observed_at,
            next_payment_attempt_at,
            subscription_id,
            required_gateway_account_mode,
        }
    }

    /// Database time observed for the first page of this scan.
    pub const fn observed_at(self) -> DateTime<Utc> {
        self.observed_at
    }

    /// Scheduling timestamp from the last row returned by the prior page.
    pub const fn next_payment_attempt_at(self) -> DateTime<Utc> {
        self.next_payment_attempt_at
    }

    /// Subscription identifier from the last row returned by the prior page.
    pub const fn subscription_id(self) -> SubscriptionId {
        self.subscription_id
    }

    /// Mode filter that owns this scan, or `None` for a central all-mode scan.
    pub const fn required_gateway_account_mode(self) -> Option<GatewayAccountMode> {
        self.required_gateway_account_mode
    }
}

/// One deterministic, bounded page of due renewal dispatches.
///
/// A next cursor is present only when PostgreSQL observed another eligible row
/// after this page. For candidates whose ordering and eligibility do not
/// change during traversal, strict keyset order avoids offset and timestamp-tie
/// gaps or repeats. This is not a cross-page snapshot: inserted, retimed, or
/// newly unblocked candidates behind the continuation key can wait for a fresh
/// scan. Hosts own queue/outbox dispatch and should still submit every item
/// through the normal renewal preflight, which revalidates mutable current
/// state.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenewalDispatchPage {
    dispatches: Vec<RenewalDispatch>,
    next_cursor: Option<RenewalDispatchPageCursor>,
}

impl RenewalDispatchPage {
    pub fn new(
        dispatches: Vec<RenewalDispatch>,
        next_cursor: Option<RenewalDispatchPageCursor>,
    ) -> Self {
        Self {
            dispatches,
            next_cursor,
        }
    }

    pub fn dispatches(&self) -> &[RenewalDispatch] {
        &self.dispatches
    }

    pub fn into_dispatches(self) -> Vec<RenewalDispatch> {
        self.dispatches
    }

    pub const fn next_cursor(&self) -> Option<RenewalDispatchPageCursor> {
        self.next_cursor
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ChargeRenewal {
    billing_scope_id: BillingScopeId,
    subscription_id: SubscriptionId,
    period_start_at: DateTime<Utc>,
}

impl ChargeRenewal {
    pub const fn new(
        billing_scope_id: BillingScopeId,
        subscription_id: SubscriptionId,
        period_start_at: DateTime<Utc>,
    ) -> Self {
        Self {
            billing_scope_id,
            subscription_id,
            period_start_at,
        }
    }

    pub const fn billing_scope_id(&self) -> BillingScopeId {
        self.billing_scope_id
    }

    pub const fn subscription_id(&self) -> SubscriptionId {
        self.subscription_id
    }

    pub const fn period_start_at(&self) -> &DateTime<Utc> {
        &self.period_start_at
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RenewalAttemptState {
    pub attempt_sequence_count: i64,
    pub automatic_infrastructure_attempt_count: i64,
    pub last_automatic_infrastructure_failure_at: Option<DateTime<Utc>>,
    pub rate_limited_attempt_count: i64,
    pub last_rate_limited_at: Option<DateTime<Utc>>,
    pub has_blocking_attempt: bool,
}

impl RenewalAttemptState {
    pub fn blocks_automatic_retry(&self, now: DateTime<Utc>) -> bool {
        self.automatic_infrastructure_attempt_count
            >= MAX_RENEWAL_INFRASTRUCTURE_ATTEMPTS_PER_PERIOD_CONFIGURATION
            || self.has_blocking_attempt
            || !retry_window_elapsed(
                self.last_automatic_infrastructure_failure_at,
                now,
                RENEWAL_INFRASTRUCTURE_RETRY_AFTER_SECONDS,
            )
            || !retry_window_elapsed(
                self.last_rate_limited_at,
                now,
                rate_limit_retry_after_seconds(self.rate_limited_attempt_count),
            )
    }
}

pub const fn rate_limit_retry_after_seconds(attempt_count: i64) -> i64 {
    if attempt_count >= RENEWAL_RATE_LIMIT_FAST_RETRY_ATTEMPTS {
        RENEWAL_RATE_LIMIT_SLOW_RETRY_AFTER_SECONDS
    } else {
        crate::gateway::GATEWAY_MUTATION_RATE_LIMIT_RETRY_AFTER_SECONDS
    }
}

fn retry_window_elapsed(
    last_attempt_at: Option<DateTime<Utc>>,
    now: DateTime<Utc>,
    retry_after_seconds: i64,
) -> bool {
    last_attempt_at.is_none_or(|last_attempt_at| {
        last_attempt_at <= now - Duration::seconds(retry_after_seconds)
    })
}

pub fn renewal_attempt_idempotency_key(
    subscription_id: SubscriptionId,
    period_start_at: DateTime<Utc>,
    attempt_sequence_count: i64,
) -> Result<IdempotencyKey, crate::IdempotencyKeyError> {
    IdempotencyKey::new(format!(
        "subscription-renewal:{subscription_id}:{}:{attempt_sequence_count}",
        period_start_at.timestamp()
    ))
}

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
pub enum SubscriptionRenewalReservationBuildError {
    #[error("resolved gateway identity does not match the renewal scope")]
    GatewayIdentityMismatch,
    #[error("only subscription-renewal attempts can become renewal reservations")]
    AttemptKindMismatch,
    #[error("subscription renewal has an invalid payment-state snapshot")]
    InvalidPaymentState,
    #[error("subscription renewal attempt has an invalid charge amount")]
    InvalidCharge,
    #[error("subscription renewal idempotency key is invalid")]
    InvalidIdempotencyKey,
}

/// Validated subscription terms read while preparing one renewal attempt.
///
/// The PostgreSQL owner constructs this from a locked subscription row before
/// creating the secret-free reservation. Keeping the exact optimistic payment
/// state together with the charge period prevents individual row fields from
/// being reconstructed by each caller.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubscriptionRenewalLockedTerms {
    gateway_account_id: GatewayAccountId,
    expected_state: SubscriptionPaymentStateSnapshot,
    period: BillingPeriod,
    charge: ChargeAmount,
    attempt_sequence_count: i64,
}

impl SubscriptionRenewalLockedTerms {
    pub const fn new(
        gateway_account_id: GatewayAccountId,
        expected_state: SubscriptionPaymentStateSnapshot,
        period: BillingPeriod,
        charge: ChargeAmount,
        attempt_sequence_count: i64,
    ) -> Self {
        Self {
            gateway_account_id,
            expected_state,
            period,
            charge,
            attempt_sequence_count,
        }
    }
}

/// Secret-free authority for one exact automatic recurring charge.
#[derive(Clone, Eq, PartialEq)]
pub struct SubscriptionRenewalReservation {
    identity: PaymentAttemptIdentity,
    provider_key: GatewayProviderKey,
    request: PaymentAttemptRequest,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SubscriptionRenewalReservationRejection {
    SubscriptionNotFound,
    PaymentNotDue,
    AttemptInProgress,
    PaymentMethodUpdateInProgress,
    RetryBlocked,
    GatewayAccountModeChanged,
    GatewayConfigurationChanged,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SubscriptionRenewalReservationOutcome {
    Reserved(Box<SubscriptionRenewalReservation>, Box<PaymentAttempt>),
    Rejected(SubscriptionRenewalReservationRejection),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SubscriptionRenewalSubmissionRejection {
    BillingStateChanged,
    GatewayConfigurationChanged,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SubscriptionRenewalSubmissionOutcome {
    Admitted(PaymentAttempt),
    AlreadyAdmitted(PaymentAttempt),
    Rejected {
        attempt: PaymentAttempt,
        reason: SubscriptionRenewalSubmissionRejection,
    },
}

#[derive(Debug)]
pub enum SubscriptionRenewalOutcome {
    Noop,
    Payment(Box<crate::SubscriptionEnrollmentPaymentResult>),
    NotSubmitted {
        payment: Box<crate::SubscriptionEnrollmentPaymentResult>,
        error: crate::GatewayNotSubmittedError,
    },
}

impl SubscriptionRenewalReservation {
    #[allow(clippy::too_many_arguments)]
    pub fn from_locked_subscription(
        command: ChargeRenewal,
        gateway: &ResolvedGateway,
        attempt_id: PaymentAttemptId,
        subscriber_id: SubscriberId,
        plan_key: PlanKey,
        payment_method_id: PaymentMethodId,
        initial_transaction_id: crate::GatewayTransactionId,
        status: SubscriptionStatus,
        period: BillingPeriod,
        charge: ChargeAmount,
        attempt_sequence_count: i64,
        required_gateway_account_mode: GatewayAccountMode,
    ) -> Result<Self, SubscriptionRenewalReservationBuildError> {
        if gateway.billing_scope_id() != command.billing_scope_id() {
            return Err(SubscriptionRenewalReservationBuildError::GatewayIdentityMismatch);
        }
        let expected_state = SubscriptionPaymentStateSnapshot::new(
            command.subscription_id(),
            payment_method_id,
            initial_transaction_id,
            status,
        )
        .map_err(|_| SubscriptionRenewalReservationBuildError::InvalidPaymentState)?;
        Self::from_locked_subscription_terms(
            command,
            gateway,
            attempt_id,
            subscriber_id,
            plan_key,
            SubscriptionRenewalLockedTerms::new(
                gateway.gateway_account_id(),
                expected_state,
                period,
                charge,
                attempt_sequence_count,
            ),
            required_gateway_account_mode,
        )
    }

    /// Builds a renewal reservation from validated terms read under the
    /// subscription lock.
    pub fn from_locked_subscription_terms(
        command: ChargeRenewal,
        gateway: &ResolvedGateway,
        attempt_id: PaymentAttemptId,
        subscriber_id: SubscriberId,
        plan_key: PlanKey,
        terms: SubscriptionRenewalLockedTerms,
        required_gateway_account_mode: GatewayAccountMode,
    ) -> Result<Self, SubscriptionRenewalReservationBuildError> {
        let SubscriptionRenewalLockedTerms {
            gateway_account_id,
            expected_state,
            period,
            charge,
            attempt_sequence_count,
        } = terms;
        if gateway.billing_scope_id() != command.billing_scope_id()
            || gateway_account_id != gateway.gateway_account_id()
        {
            return Err(SubscriptionRenewalReservationBuildError::GatewayIdentityMismatch);
        }
        if expected_state.subscription_id() != command.subscription_id() {
            return Err(SubscriptionRenewalReservationBuildError::InvalidPaymentState);
        }
        let identity = PaymentAttemptIdentity::new(
            attempt_id,
            command.billing_scope_id(),
            subscriber_id,
            gateway_account_id,
            gateway.gateway_configuration_id(),
            required_gateway_account_mode,
        );
        let idempotency_key = renewal_attempt_idempotency_key(
            command.subscription_id(),
            *command.period_start_at(),
            attempt_sequence_count,
        )
        .map_err(|_| SubscriptionRenewalReservationBuildError::InvalidIdempotencyKey)?;
        let fingerprint = PaymentAttemptFingerprint::for_subscription_renewal(
            &plan_key,
            command.subscription_id(),
            expected_state.payment_method_id(),
            *period.start_at(),
            charge.money(),
        );
        let target = PaymentAttemptTarget::SubscriptionRenewal {
            plan_key,
            payment_method_id: expected_state.payment_method_id(),
            period,
            expected_state,
        };
        let request = PaymentAttemptRequest::new(
            target,
            idempotency_key,
            fingerprint,
            charge.money(),
            gateway
                .mutation_reference_factory()
                .for_attempt(PaymentAttemptKind::SubscriptionRenewal, attempt_id),
            BillingContactSnapshot::new(None, None),
        );
        Ok(Self {
            identity,
            provider_key: gateway.provider_key().clone(),
            request,
        })
    }

    pub fn from_attempt(
        attempt: &PaymentAttempt,
        provider_key: GatewayProviderKey,
    ) -> Result<Self, SubscriptionRenewalReservationBuildError> {
        let PaymentAttemptTarget::SubscriptionRenewal {
            plan_key,
            payment_method_id,
            period,
            expected_state,
        } = attempt.request().target()
        else {
            return Err(SubscriptionRenewalReservationBuildError::AttemptKindMismatch);
        };
        ChargeAmount::try_from(attempt.request().amount())
            .map_err(|_| SubscriptionRenewalReservationBuildError::InvalidCharge)?;
        if !attempt
            .request()
            .fingerprint()
            .matches_subscription_renewal(
                plan_key,
                expected_state.subscription_id(),
                *payment_method_id,
                *period.start_at(),
                attempt.request().amount(),
            )
        {
            return Err(SubscriptionRenewalReservationBuildError::AttemptKindMismatch);
        }
        Ok(Self {
            identity: attempt.identity(),
            provider_key,
            request: attempt.request().clone(),
        })
    }

    pub const fn identity(&self) -> PaymentAttemptIdentity {
        self.identity
    }

    pub const fn provider_key(&self) -> &GatewayProviderKey {
        &self.provider_key
    }

    pub const fn request(&self) -> &PaymentAttemptRequest {
        &self.request
    }

    pub const fn plan_key(&self) -> &PlanKey {
        match self.request.target().plan_key() {
            Some(plan_key) => plan_key,
            None => unreachable!(),
        }
    }

    pub const fn subscription_id(&self) -> SubscriptionId {
        match self.request.target().subscription_id() {
            Some(subscription_id) => subscription_id,
            None => unreachable!(),
        }
    }

    pub const fn period(&self) -> &BillingPeriod {
        match self.request.target().period() {
            Some(period) => period,
            None => unreachable!(),
        }
    }

    pub const fn expected_state(&self) -> &SubscriptionPaymentStateSnapshot {
        match self.request.target().subscription_payment_state_snapshot() {
            Some(expected_state) => expected_state,
            None => unreachable!(),
        }
    }
}

impl std::fmt::Debug for SubscriptionRenewalReservation {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("SubscriptionRenewalReservation")
            .field("identity", &self.identity)
            .field("provider_key", &self.provider_key)
            .field("request", &self.request)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use uuid::Uuid;

    #[test]
    fn renewal_dispatch_page_keeps_its_observed_scan_and_strict_key() {
        let observed_at = DateTime::from_timestamp(1_700_000_000, 0).unwrap();
        let next_payment_attempt_at = observed_at + Duration::seconds(60);
        let subscription_id = SubscriptionId::new(Uuid::from_u128(2));
        let cursor = RenewalDispatchPageCursor::new(
            observed_at,
            next_payment_attempt_at,
            subscription_id,
            Some(GatewayAccountMode::Test),
        );
        let dispatch = RenewalDispatch::new(
            BillingScopeId::new(Uuid::from_u128(1)),
            subscription_id,
            GatewayAccountMode::Test,
            observed_at,
            3,
        );
        let page = RenewalDispatchPage::new(vec![dispatch.clone()], Some(cursor));

        assert_eq!(cursor.observed_at(), observed_at);
        assert_eq!(cursor.next_payment_attempt_at(), next_payment_attempt_at);
        assert_eq!(cursor.subscription_id(), subscription_id);
        assert_eq!(
            cursor.required_gateway_account_mode(),
            Some(GatewayAccountMode::Test)
        );
        assert_eq!(
            dispatch.required_gateway_account_mode(),
            GatewayAccountMode::Test
        );
        assert_eq!(page.dispatches(), std::slice::from_ref(&dispatch));
        assert_eq!(page.next_cursor(), Some(cursor));
        assert_eq!(page.into_dispatches(), vec![dispatch]);
    }

    #[test]
    fn retry_boundaries_are_inclusive() {
        let now = DateTime::from_timestamp(1_700_000_000, 0).unwrap();
        let mut state = RenewalAttemptState {
            last_automatic_infrastructure_failure_at: Some(
                now - Duration::seconds(RENEWAL_INFRASTRUCTURE_RETRY_AFTER_SECONDS),
            ),
            ..RenewalAttemptState::default()
        };
        assert!(!state.blocks_automatic_retry(now));
        state.last_automatic_infrastructure_failure_at = Some(
            now - Duration::seconds(RENEWAL_INFRASTRUCTURE_RETRY_AFTER_SECONDS.saturating_sub(1)),
        );
        assert!(state.blocks_automatic_retry(now));
    }

    #[test]
    fn fifth_gateway_throttle_switches_to_daily_pacing() {
        assert_eq!(rate_limit_retry_after_seconds(4), 60);
        assert_eq!(
            rate_limit_retry_after_seconds(5),
            RENEWAL_RATE_LIMIT_SLOW_RETRY_AFTER_SECONDS
        );
    }

    fn failure_policy(
        delays: &[u32],
        exhaustion: crate::DunningExhaustion,
    ) -> RenewalFailurePolicy {
        RenewalFailurePolicy::new(
            crate::DunningSchedule::from_seconds(delays.iter().copied()).unwrap(),
            exhaustion,
            crate::PastDueAccessPolicy::SuspendImmediately,
        )
    }

    #[test]
    fn dunning_failure_count_is_one_based_and_indexes_the_current_step() {
        let failed_at = DateTime::from_timestamp(1_700_000_000, 0).unwrap();
        let policy = failure_policy(&[60, 300], crate::DunningExhaustion::RemainPastDue);
        assert_eq!(
            renewal_failure_disposition(&policy, 0, failed_at),
            Err(RenewalFailurePolicyError::ZeroFailureCount)
        );
        assert_eq!(
            renewal_failure_disposition(&policy, 1, failed_at),
            Ok(RenewalFailureDisposition::RetryScheduled {
                retry_at: failed_at + Duration::seconds(60),
            })
        );
        assert_eq!(
            renewal_failure_disposition(&policy, 2, failed_at),
            Ok(RenewalFailureDisposition::RetryScheduled {
                retry_at: failed_at + Duration::seconds(300),
            })
        );
        assert_eq!(
            renewal_failure_disposition(&policy, 3, failed_at),
            Ok(RenewalFailureDisposition::RemainPastDue {
                exhausted_at: failed_at,
            })
        );
    }

    #[test]
    fn empty_schedule_and_mark_unpaid_exhaust_immediately() {
        let failed_at = DateTime::from_timestamp(1_700_000_000, 0).unwrap();
        let policy = failure_policy(&[], crate::DunningExhaustion::MarkUnpaid);
        assert_eq!(
            renewal_failure_disposition(&policy, 1, failed_at),
            Ok(RenewalFailureDisposition::MarkUnpaid {
                ended_at: failed_at,
            })
        );
    }

    #[test]
    fn retry_timestamp_overflow_is_typed() {
        let policy = failure_policy(&[u32::MAX], crate::DunningExhaustion::RemainPastDue);
        assert_eq!(
            renewal_failure_disposition(&policy, 1, DateTime::<Utc>::MAX_UTC),
            Err(RenewalFailurePolicyError::RetryTimestampOverflow)
        );
    }
}