baukit-push 0.10.8

Provider-neutral push delivery with an Expo ticket and receipt adapter, a device registry, and daily delivery claims.
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
use std::{collections::BTreeSet, error::Error, num::NonZeroU32, path::PathBuf};

use baukit_push::{
    DeliveryClaim, DeliveryClaimStore, DeliveryKind, DevicePlatform, DeviceRegistration,
    DeviceRegistry, DeviceTimeZone, DeviceToken, FakePushSender, MAX_DEVICE_TOKEN_LENGTH,
    MAX_PUSH_TICKET_ID_LENGTH, PendingReceipt, PendingReceiptStore, PostgresDeliveryClaimStore,
    PostgresDeviceRegistry, PostgresPendingReceiptStore, PushDeliveryStatus, PushMessage,
    PushOutcome, PushReceipt, PushRejection, PushSender, PushTicketId, RECEIPT_POLL_DELAY,
    RECEIPT_RETENTION, ReceiptPoll, erase_owner_delivery_claims, erase_owner_pending_receipts,
    erase_owner_push_devices, poll_pending_receipts, purge_delivery_claims,
};
use baukit_test::PostgresTestContainer;
use chrono::{DateTime, NaiveDate, TimeDelta, TimeZone as _, Utc};
use sqlx::PgPool;
use uuid::Uuid;

type TestError = Box<dyn Error + Send + Sync>;

const CAP: NonZeroU32 = NonZeroU32::new(3).expect("three is not zero");
const CONCURRENT_REGISTRATIONS: usize = 16;
const CONCURRENT_OWNERS: usize = 8;
const CONCURRENT_CLAIMS: usize = 10;
const PURGE_BATCH: NonZeroU32 = NonZeroU32::new(1).expect("one is not zero");
const CONCURRENT_POLLERS: usize = 6;
const POLL_LIMIT: NonZeroU32 = NonZeroU32::new(10).expect("ten is not zero");

fn at(minutes: i64) -> DateTime<Utc> {
    Utc.with_ymd_and_hms(2026, 9, 27, 12, 0, 0)
        .single()
        .expect("valid test instant")
        + TimeDelta::minutes(minutes)
}

fn date(day: u32) -> NaiveDate {
    NaiveDate::from_ymd_opt(2026, 9, day).expect("valid test date")
}

fn token(value: &str) -> DeviceToken {
    DeviceToken::new(value).expect("valid test token")
}

fn registration(owner_id: Uuid, value: &str, minutes: i64) -> DeviceRegistration {
    DeviceRegistration::new(owner_id, token(value), DevicePlatform::Ios, at(minutes))
}

fn dead(value: &str) -> PushOutcome {
    PushOutcome {
        token: token(value),
        status: PushDeliveryStatus::Rejected(PushRejection::DeviceNotRegistered),
    }
}

async fn fixture() -> Result<(PostgresTestContainer, PgPool), TestError> {
    let migrations = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("migrations");
    let fixture = baukit_test::start_postgres_with_migrations(migrations).await?;
    let pool = PgPool::connect(fixture.connection_url()).await?;
    sqlx::raw_sql(
        "CREATE TABLE owners (id UUID PRIMARY KEY);
         ALTER TABLE push_devices
             ADD CONSTRAINT push_devices_owner_fk
             FOREIGN KEY (owner_id) REFERENCES owners (id) ON DELETE CASCADE;
         ALTER TABLE push_delivery_claims
             ADD CONSTRAINT push_delivery_claims_owner_fk
             FOREIGN KEY (owner_id) REFERENCES owners (id) ON DELETE CASCADE;
         ALTER TABLE push_pending_receipts
             ADD CONSTRAINT push_pending_receipts_owner_fk
             FOREIGN KEY (owner_id) REFERENCES owners (id) ON DELETE CASCADE;",
    )
    .execute(&pool)
    .await?;
    Ok((fixture, pool))
}

async fn owner(pool: &PgPool) -> Result<Uuid, TestError> {
    let owner_id = Uuid::now_v7();
    sqlx::query("INSERT INTO owners (id) VALUES ($1)")
        .bind(owner_id)
        .execute(pool)
        .await?;
    Ok(owner_id)
}

async fn tokens(
    registry: &PostgresDeviceRegistry,
    owner_id: Uuid,
) -> Result<Vec<String>, TestError> {
    Ok(registry
        .list_for_owner(owner_id)
        .await?
        .iter()
        .map(|device| device.token.expose().to_owned())
        .collect())
}

async fn device_count(pool: &PgPool) -> Result<i64, TestError> {
    Ok(sqlx::query_scalar("SELECT count(*) FROM push_devices")
        .fetch_one(pool)
        .await?)
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn a_device_registers_refreshes_and_unregisters_for_its_owner_only() -> Result<(), TestError>
{
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone());
    let (alice, bob) = (owner(&pool).await?, owner(&pool).await?);
    let longest = "t".repeat(MAX_DEVICE_TOKEN_LENGTH);

    registry
        .register(registration(alice, &longest, 1).with_time_zone(DeviceTimeZone::new("UTC")?))
        .await?;
    registry
        .register(
            DeviceRegistration::new(alice, token(&longest), DevicePlatform::Android, at(5))
                .with_time_zone(DeviceTimeZone::new("Europe/Berlin")?),
        )
        .await?;
    registry.register(registration(alice, "stale", 3)).await?;

    let devices = registry.list_for_owner(alice).await?;
    assert_eq!(devices.len(), 2);
    let refreshed = &devices[0];
    assert_eq!(refreshed.token.expose(), longest);
    assert_eq!(refreshed.platform, DevicePlatform::Android);
    assert_eq!(
        refreshed.time_zone.as_ref().map(DeviceTimeZone::as_str),
        Some("Europe/Berlin")
    );
    assert_eq!(refreshed.created_at, at(1));
    assert_eq!(refreshed.last_registered_at, at(5));

    registry.register(registration(alice, "stale", 2)).await?;
    let stale = &registry.list_for_owner(alice).await?[1];
    assert_eq!(
        stale.last_registered_at,
        at(3),
        "a late refresh never moves time back"
    );

    assert!(!registry.unregister(bob, token("stale")).await?);
    assert!(registry.unregister(alice, token("stale")).await?);
    assert!(!registry.unregister(alice, token("stale")).await?);
    assert_eq!(tokens(&registry, alice).await?, [longest]);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn the_cap_evicts_the_oldest_device_deterministically() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone()).with_devices_per_owner(CAP);
    let (alice, bob) = (owner(&pool).await?, owner(&pool).await?);
    registry.register(registration(bob, "bob", 0)).await?;

    registry.register(registration(alice, "a", 1)).await?;
    registry.register(registration(alice, "c", 2)).await?;
    registry.register(registration(alice, "b", 2)).await?;
    registry.register(registration(alice, "a", 3)).await?;
    let outcome = registry.register(registration(alice, "d", 4)).await?;
    assert_eq!(outcome.evicted, 1);
    assert_eq!(
        tokens(&registry, alice).await?,
        ["d", "a", "c"],
        "equal instants evict the lower token first"
    );

    let skewed = registry.register(registration(alice, "e", -10)).await?;
    assert_eq!(skewed.evicted, 1);
    assert_eq!(
        tokens(&registry, alice).await?,
        ["d", "a", "e"],
        "the device being registered is never evicted, even with an older clock"
    );
    assert_eq!(tokens(&registry, bob).await?, ["bob"]);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn concurrent_registrations_never_overshoot_the_cap() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone()).with_devices_per_owner(CAP);
    let alice = owner(&pool).await?;

    let mut tasks = tokio::task::JoinSet::new();
    for index in 0..CONCURRENT_REGISTRATIONS {
        let registry = registry.clone();
        tasks.spawn(async move {
            let minutes = i64::try_from(index).expect("small index");
            registry
                .register(registration(alice, &format!("device-{index}"), minutes))
                .await
        });
    }
    let mut evicted = 0;
    while let Some(outcome) = tasks.join_next().await {
        evicted += outcome??.evicted;
    }

    let remaining = registry.list_for_owner(alice).await?.len();
    assert_eq!(remaining, usize::try_from(CAP.get())?);
    assert_eq!(
        evicted,
        u64::try_from(CONCURRENT_REGISTRATIONS - remaining)?
    );
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn rotation_replaces_the_predecessor_and_survives_a_race() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone()).with_devices_per_owner(CAP);
    let alice = owner(&pool).await?;
    for (value, minutes) in [("a", 1), ("b", 2), ("c", 3)] {
        registry
            .register(registration(alice, value, minutes))
            .await?;
    }

    let at_cap = registry
        .rotate(token("a"), registration(alice, "a2", 4))
        .await?;
    assert_eq!(
        at_cap.evicted, 0,
        "rotating at the cap keeps every other device"
    );
    assert_eq!(tokens(&registry, alice).await?, ["a2", "c", "b"]);

    let (left, right) = tokio::join!(
        registry.rotate(token("a2"), registration(alice, "left", 5)),
        registry.rotate(token("a2"), registration(alice, "right", 5)),
    );
    left?;
    right?;
    let after_race = tokens(&registry, alice).await?;
    assert!(!after_race.contains(&"a2".to_owned()));
    assert!(after_race.contains(&"left".to_owned()) && after_race.contains(&"right".to_owned()));
    assert_eq!(after_race.len(), usize::try_from(CAP.get())?);

    let retried = registry
        .rotate(token("a2"), registration(alice, "right", 6))
        .await?;
    assert_eq!(retried.evicted, 0, "a retried rotation is a refresh");
    assert_eq!(tokens(&registry, alice).await?, after_race);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn a_shared_token_ends_with_exactly_one_owner() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone());
    let mut owners = Vec::with_capacity(CONCURRENT_OWNERS);
    for _ in 0..CONCURRENT_OWNERS {
        owners.push(owner(&pool).await?);
    }

    let mut tasks = tokio::task::JoinSet::new();
    for (index, owner_id) in owners.iter().copied().enumerate() {
        let registry = registry.clone();
        tasks.spawn(async move {
            let minutes = i64::try_from(index).expect("small index");
            registry
                .register(registration(owner_id, "shared-device", minutes))
                .await
        });
    }
    while let Some(outcome) = tasks.join_next().await {
        outcome??;
    }

    assert_eq!(device_count(&pool).await?, 1);
    let mut holders = BTreeSet::new();
    for owner_id in &owners {
        if !registry.list_for_owner(*owner_id).await?.is_empty() {
            holders.insert(*owner_id);
        }
    }
    assert_eq!(holders.len(), 1);

    let holder = *holders.first().expect("one holder");
    let newcomer = owners
        .iter()
        .copied()
        .find(|owner_id| *owner_id != holder)
        .expect("a second owner");
    registry
        .register(registration(newcomer, "shared-device", 100))
        .await?;
    let moved = registry.list_for_owner(newcomer).await?;
    assert_eq!(moved[0].created_at, at(100), "a moved token starts fresh");
    assert!(registry.list_for_owner(holder).await?.is_empty());
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn a_dead_token_receipt_removes_the_token_in_one_call() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone());
    let (alice, bob) = (owner(&pool).await?, owner(&pool).await?);
    registry.register(registration(alice, "gone", 1)).await?;
    registry.register(registration(alice, "alive", 1)).await?;
    registry
        .register(registration(bob, "reinstalled", 1))
        .await?;

    let sent_at = at(5);
    registry
        .register(registration(bob, "reinstalled", 6))
        .await?;
    let removed = registry
        .invalidate_dead_tokens(
            &[
                dead("gone"),
                dead("gone"),
                dead("reinstalled"),
                dead("never-registered"),
                PushOutcome {
                    token: token("alive"),
                    status: PushDeliveryStatus::Rejected(PushRejection::MessageTooBig),
                },
            ],
            sent_at,
        )
        .await?;

    assert_eq!(removed, 1);
    assert_eq!(tokens(&registry, alice).await?, ["alive"]);
    assert_eq!(
        tokens(&registry, bob).await?,
        ["reinstalled"],
        "a registration after the send outlives the receipt"
    );
    assert_eq!(
        registry
            .invalidate_dead_tokens(&[dead("gone")], sent_at)
            .await?,
        0
    );
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn account_erasure_removes_every_device_claim_and_pending_ticket() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone());
    let claims = PostgresDeliveryClaimStore::new(pool.clone());
    let receipts = PostgresPendingReceiptStore::new(pool.clone());
    let kind = DeliveryKind::new("daily_reminder")?;
    let (alice, bob, carol) = (
        owner(&pool).await?,
        owner(&pool).await?,
        owner(&pool).await?,
    );
    for owner_id in [alice, bob, carol] {
        for device in ["phone", "tablet"] {
            registry
                .register(registration(owner_id, &format!("{owner_id}-{device}"), 1))
                .await?;
        }
        claims
            .claim(DeliveryClaim::new(owner_id, date(27), kind.clone()), at(0))
            .await?;
        receipts
            .record(
                vec![pending(
                    owner_id,
                    &format!("{owner_id}-ticket"),
                    &format!("{owner_id}-phone"),
                    1,
                )],
                at(16),
            )
            .await?;
    }

    assert_eq!(registry.erase_owner(alice).await?, 2);
    assert!(registry.list_for_owner(alice).await?.is_empty());
    assert_eq!(receipts.erase_owner(alice).await?, 1);

    let mut transaction = pool.begin().await?;
    assert_eq!(erase_owner_push_devices(&mut *transaction, bob).await?, 2);
    assert_eq!(
        erase_owner_delivery_claims(&mut *transaction, bob).await?,
        1
    );
    assert_eq!(
        erase_owner_pending_receipts(&mut *transaction, bob).await?,
        1
    );
    transaction.commit().await?;

    sqlx::query("DELETE FROM owners WHERE id = $1")
        .bind(carol)
        .execute(&pool)
        .await?;
    assert_eq!(device_count(&pool).await?, 0);
    let remaining_claims: i64 = sqlx::query_scalar("SELECT count(*) FROM push_delivery_claims")
        .fetch_one(&pool)
        .await?;
    assert_eq!(
        remaining_claims, 1,
        "only the port-erased owner's claim is left"
    );
    assert!(
        stored_tickets(&pool).await?.is_empty(),
        "no pending ticket outlives its owner"
    );
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn a_daily_claim_has_exactly_one_winner_until_released() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let claims = PostgresDeliveryClaimStore::new(pool.clone());
    let alice = owner(&pool).await?;
    let claim = DeliveryClaim::new(alice, date(27), DeliveryKind::new("training_reminder")?);

    let mut tasks = tokio::task::JoinSet::new();
    for _ in 0..CONCURRENT_CLAIMS {
        let claims = claims.clone();
        let claim = claim.clone();
        tasks.spawn(async move { claims.claim(claim, at(0)).await });
    }
    let mut winners = 0;
    while let Some(won) = tasks.join_next().await {
        winners += usize::from(won??);
    }
    assert_eq!(winners, 1);

    let other_kind = DeliveryClaim::new(alice, date(27), DeliveryKind::new("weekly_summary")?);
    let next_day = DeliveryClaim::new(alice, date(28), claim.kind.clone());
    assert!(claims.claim(other_kind, at(0)).await?);
    assert!(claims.claim(next_day, at(0)).await?);

    assert!(claims.release(claim.clone()).await?);
    assert!(!claims.release(claim.clone()).await?);
    assert!(claims.claim(claim, at(1)).await?);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn old_claims_purge_in_bounded_batches() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let claims = PostgresDeliveryClaimStore::new(pool.clone());
    let alice = owner(&pool).await?;
    let kind = DeliveryKind::new("daily_reminder")?;
    for day in [24, 25, 26, 27] {
        claims
            .claim(DeliveryClaim::new(alice, date(day), kind.clone()), at(0))
            .await?;
    }

    let mut purged = Vec::new();
    loop {
        let deleted = claims.purge(date(26), PURGE_BATCH).await?;
        purged.push(deleted);
        if deleted < u64::from(PURGE_BATCH.get()) {
            break;
        }
    }
    assert_eq!(purged, [1, 1, 0]);
    let kept: Vec<NaiveDate> =
        sqlx::query_scalar("SELECT local_date FROM push_delivery_claims ORDER BY local_date")
            .fetch_all(&pool)
            .await?;
    assert_eq!(kept, [date(26), date(27)]);

    let mut transaction = pool.begin().await?;
    assert_eq!(
        purge_delivery_claims(&mut *transaction, date(28), PURGE_BATCH).await?,
        1
    );
    transaction.rollback().await?;
    assert_eq!(claims.purge(date(28), PURGE_BATCH).await?, 1);
    Ok(())
}

fn ticket(value: &str) -> PushTicketId {
    PushTicketId::new(value).expect("valid test ticket")
}

fn pending(
    owner_id: Uuid,
    ticket_id: &str,
    token_value: &str,
    sent_minutes: i64,
) -> PendingReceipt {
    PendingReceipt {
        ticket: ticket(ticket_id),
        owner_id,
        token: token(token_value),
        sent_at: at(sent_minutes),
    }
}

fn ticket_ids(receipts: &[PendingReceipt]) -> Vec<String> {
    receipts
        .iter()
        .map(|receipt| receipt.ticket.as_str().to_owned())
        .collect()
}

async fn stored_tickets(pool: &PgPool) -> Result<Vec<String>, TestError> {
    Ok(
        sqlx::query_scalar("SELECT ticket_id FROM push_pending_receipts ORDER BY ticket_id")
            .fetch_all(pool)
            .await?,
    )
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn pending_receipts_round_trip_and_come_due_again_after_a_take() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let store = PostgresPendingReceiptStore::new(pool.clone());
    let alice = owner(&pool).await?;
    let longest_ticket = "t".repeat(MAX_PUSH_TICKET_ID_LENGTH);
    let longest_token = "d".repeat(MAX_DEVICE_TOKEN_LENGTH);

    let recorded = store
        .record(
            vec![
                pending(alice, &longest_ticket, &longest_token, 0),
                pending(alice, "b", "token-b", 0),
                pending(alice, "b", "token-b", 0),
            ],
            at(15),
        )
        .await?;
    assert_eq!(recorded, 2);
    assert_eq!(
        store
            .record(vec![pending(alice, "b", "other", 9)], at(99))
            .await?,
        0,
        "a recorded ticket is left unchanged"
    );
    store
        .record(vec![pending(alice, "c", "token-c", 5)], at(20))
        .await?;

    assert!(store.take_due(at(14), at(30), POLL_LIMIT).await?.is_empty());
    let first = store.take_due(at(15), at(30), POLL_LIMIT).await?;
    let mut first_ids = ticket_ids(&first);
    first_ids.sort();
    assert_eq!(first_ids, ["b".to_owned(), longest_ticket.clone()]);
    let longest = first
        .iter()
        .find(|receipt| receipt.ticket.as_str() == longest_ticket)
        .expect("the longest ticket was taken");
    assert_eq!(longest.token.expose(), longest_token);
    assert_eq!(longest.sent_at, at(0));
    assert_eq!(longest.owner_id, alice);

    assert_eq!(
        ticket_ids(&store.take_due(at(25), at(40), POLL_LIMIT).await?),
        ["c"],
        "taken tickets wait for their retry instant"
    );
    assert_eq!(store.take_due(at(30), at(45), POLL_LIMIT).await?.len(), 2);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn concurrent_pollers_take_disjoint_batches() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let store = PostgresPendingReceiptStore::new(pool.clone());
    let alice = owner(&pool).await?;
    let tickets = (0..CONCURRENT_POLLERS * 2)
        .map(|index| {
            pending(
                alice,
                &format!("ticket-{index:02}"),
                &format!("token-{index}"),
                0,
            )
        })
        .collect::<Vec<_>>();
    store.record(tickets, at(0)).await?;

    let batch = NonZeroU32::new(2).expect("two is not zero");
    let mut tasks = tokio::task::JoinSet::new();
    for _ in 0..CONCURRENT_POLLERS {
        let store = store.clone();
        tasks.spawn(async move { store.take_due(at(1), at(30), batch).await });
    }
    let mut taken = Vec::new();
    while let Some(batch) = tasks.join_next().await {
        taken.extend(ticket_ids(&batch??));
    }
    let distinct = taken.iter().collect::<BTreeSet<_>>();
    assert_eq!(distinct.len(), taken.len(), "no ticket was taken twice");
    assert_eq!(taken.len(), CONCURRENT_POLLERS * 2);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn settled_and_expired_receipts_are_deleted() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let store = PostgresPendingReceiptStore::new(pool.clone());
    let alice = owner(&pool).await?;
    store
        .record(
            vec![
                pending(alice, "a", "token-a", 0),
                pending(alice, "b", "token-b", 1),
                pending(alice, "c", "token-c", 2),
                pending(alice, "d", "token-d", 3),
            ],
            at(0),
        )
        .await?;

    assert_eq!(store.delete(vec![ticket("a"), ticket("unknown")]).await?, 1);
    assert_eq!(store.delete(Vec::new()).await?, 0);

    let mut purged = Vec::new();
    loop {
        let deleted = store.purge(at(3), PURGE_BATCH).await?;
        purged.push(deleted);
        if deleted < u64::from(PURGE_BATCH.get()) {
            break;
        }
    }
    assert_eq!(purged, [1, 1, 0]);
    assert_eq!(stored_tickets(&pool).await?, ["d"]);
    Ok(())
}

#[tokio::test]
#[ignore = "requires Docker; mandatory in the full local gate"]
async fn a_deferred_poll_invalidates_a_late_dead_token() -> Result<(), TestError> {
    let (_container, pool) = fixture().await?;
    let registry = PostgresDeviceRegistry::new(pool.clone());
    let pending_receipts = PostgresPendingReceiptStore::new(pool.clone());
    let sender = FakePushSender::new();
    let alice = owner(&pool).await?;
    let names = ["fine", "gone", "reinstalled", "slow"];
    for name in names {
        registry.register(registration(alice, name, 0)).await?;
        sender.accept_without_receipt(token(name)).await;
    }

    let sent_at = at(1);
    let outcomes = sender
        .send(
            names
                .iter()
                .map(|name| PushMessage::new(token(name), "Reminder", "Body"))
                .collect(),
        )
        .await?;
    assert_eq!(
        pending_receipts
            .record_accepted(alice, &outcomes, sent_at)
            .await?,
        4
    );

    let dead = PushReceipt::Rejected(PushRejection::DeviceNotRegistered);
    sender
        .settle_receipt(token("fine"), PushReceipt::Delivered)
        .await;
    sender.settle_receipt(token("gone"), dead.clone()).await;
    sender.settle_receipt(token("reinstalled"), dead).await;
    registry
        .register(registration(alice, "reinstalled", 10))
        .await?;

    let due_at = sent_at + RECEIPT_POLL_DELAY;
    let poll =
        poll_pending_receipts(&sender, &pending_receipts, &registry, due_at, POLL_LIMIT).await?;
    assert_eq!(
        poll,
        ReceiptPoll {
            taken: 4,
            settled: 3,
            invalidated: 1
        }
    );
    let mut remaining = tokens(&registry, alice).await?;
    remaining.sort();
    assert_eq!(remaining, ["fine", "reinstalled", "slow"]);
    assert_eq!(stored_tickets(&pool).await?.len(), 1);

    let again =
        poll_pending_receipts(&sender, &pending_receipts, &registry, due_at, POLL_LIMIT).await?;
    assert_eq!(again.taken, 0, "the unsettled ticket waits a full delay");
    assert_eq!(
        pending_receipts
            .purge(due_at + RECEIPT_RETENTION, POLL_LIMIT)
            .await?,
        1
    );
    Ok(())
}