this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//! Push notification sink — delivers via Expo, APNs, or FCM
//!
//! Uses the `PushProvider` trait to abstract the push notification backend.
//! The default implementation is `ExpoPushProvider` which sends via the
//! Expo Push API (<https://exp.host/--/api/v2/push/send>).
//!
//! # Retry strategy
//!
//! Failed sends are retried up to 3 times with exponential backoff:
//! - Attempt 1: immediate
//! - Attempt 2: after 100ms
//! - Attempt 3: after 500ms
//! - Attempt 4: after 2s
//!
//! Only server errors (5xx) and network errors are retried.
//! Client errors (4xx) fail immediately.

use crate::config::sinks::SinkType;
use crate::events::sinks::Sink;
use crate::events::sinks::device_tokens::DeviceTokenStore;
use crate::events::sinks::preferences::NotificationPreferencesStore;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

#[cfg(feature = "push")]
use reqwest;

/// Push message to send to a provider
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushMessage {
    /// Device push token
    pub to: String,

    /// Notification title
    pub title: String,

    /// Notification body
    pub body: String,

    /// Extra data payload (passed to the app when notification is tapped)
    #[serde(default, skip_serializing_if = "Value::is_null")]
    pub data: Value,

    /// Sound to play (default: "default")
    #[serde(default = "default_sound")]
    pub sound: String,
}

fn default_sound() -> String {
    "default".to_string()
}

/// Result of a push send attempt
#[derive(Debug, Clone)]
pub enum PushResult {
    /// Successfully sent
    Success,
    /// Failed with retriable error (server error, network issue)
    RetriableError(String),
    /// Failed with non-retriable error (invalid token, etc.)
    PermanentError(String),
}

/// Trait for push notification providers
///
/// Abstracts the backend used to send push notifications.
/// Implementations: `ExpoPushProvider` (default), future: `ApnsPushProvider`, `FcmPushProvider`
#[async_trait]
pub trait PushProvider: Send + Sync + std::fmt::Debug {
    /// Send a batch of push messages
    ///
    /// Returns one `PushResult` per message, in the same order.
    async fn send_batch(&self, messages: Vec<PushMessage>) -> Vec<PushResult>;

    /// Provider name for logging
    fn name(&self) -> &str;
}

/// Expo Push API provider
///
/// Sends push notifications via the Expo Push API.
/// Works with Expo push tokens (format: "ExponentPushToken\[xxx\]").
///
/// Requires the `push` feature to be enabled.
#[cfg(feature = "push")]
#[derive(Debug)]
pub struct ExpoPushProvider {
    client: reqwest::Client,
    api_url: String,
}

#[cfg(feature = "push")]
impl ExpoPushProvider {
    /// Create with default Expo API URL
    pub fn new() -> Self {
        Self {
            client: reqwest::Client::new(),
            api_url: "https://exp.host/--/api/v2/push/send".to_string(),
        }
    }

    /// Create with a custom API URL (for testing)
    pub fn with_url(url: String) -> Self {
        Self {
            client: reqwest::Client::new(),
            api_url: url,
        }
    }
}

#[cfg(feature = "push")]
impl Default for ExpoPushProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "push")]
#[async_trait]
impl PushProvider for ExpoPushProvider {
    async fn send_batch(&self, messages: Vec<PushMessage>) -> Vec<PushResult> {
        if messages.is_empty() {
            return Vec::new();
        }

        // Expo API accepts an array of messages
        let response = self.client.post(&self.api_url).json(&messages).send().await;

        match response {
            Ok(resp) => {
                let status = resp.status();
                if status.is_success() {
                    // Parse Expo's response to check per-ticket status
                    match resp.json::<ExpoResponse>().await {
                        Ok(expo_resp) => expo_resp
                            .data
                            .into_iter()
                            .map(|ticket| match ticket.status.as_str() {
                                "ok" => PushResult::Success,
                                "error" => {
                                    let msg = ticket
                                        .message
                                        .unwrap_or_else(|| "unknown error".to_string());
                                    // DeviceNotRegistered → permanent error
                                    if ticket.details.as_ref().is_some_and(|d| {
                                        d.get("error")
                                            .and_then(|e| e.as_str())
                                            .is_some_and(|e| e == "DeviceNotRegistered")
                                    }) {
                                        PushResult::PermanentError(msg)
                                    } else {
                                        PushResult::RetriableError(msg)
                                    }
                                }
                                _ => PushResult::RetriableError(format!(
                                    "unexpected status: {}",
                                    ticket.status
                                )),
                            })
                            .collect(),
                        Err(e) => {
                            // Couldn't parse response — treat all as retriable
                            vec![
                                PushResult::RetriableError(format!(
                                    "failed to parse Expo response: {}",
                                    e
                                ));
                                messages.len()
                            ]
                        }
                    }
                } else if status.is_server_error() {
                    vec![
                        PushResult::RetriableError(format!("server error: {}", status));
                        messages.len()
                    ]
                } else {
                    // 4xx → permanent error
                    let body = resp.text().await.unwrap_or_default();
                    vec![
                        PushResult::PermanentError(format!("client error {}: {}", status, body));
                        messages.len()
                    ]
                }
            }
            Err(e) => {
                // Network error → retriable
                vec![PushResult::RetriableError(format!("network error: {}", e)); messages.len()]
            }
        }
    }

    fn name(&self) -> &str {
        "expo"
    }
}

/// Expo Push API response format
#[cfg(feature = "push")]
#[derive(Debug, Deserialize)]
struct ExpoResponse {
    data: Vec<ExpoTicket>,
}

/// Individual push ticket from Expo
#[cfg(feature = "push")]
#[derive(Debug, Deserialize)]
struct ExpoTicket {
    status: String,
    #[serde(default)]
    message: Option<String>,
    #[serde(default)]
    details: Option<Value>,
}

/// Retry configuration for push delivery
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts (excluding the first attempt)
    pub max_retries: u32,
    /// Backoff durations for each retry attempt
    pub backoff: Vec<Duration>,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            backoff: vec![
                Duration::from_millis(100),
                Duration::from_millis(500),
                Duration::from_secs(2),
            ],
        }
    }
}

/// Push notification sink
///
/// Receives payloads from the `deliver` operator and sends push
/// notifications to all registered device tokens for the recipient.
///
/// # Preferences
///
/// If a `NotificationPreferencesStore` is attached via `with_preferences`,
/// the sink checks user preferences before sending. Disabled notification
/// types are silently dropped (same pattern as `InAppNotificationSink`).
///
/// # Stale token cleanup
///
/// When a push provider returns `PermanentError` (e.g., `DeviceNotRegistered`),
/// the corresponding device token is automatically unregistered from the store.
#[derive(Debug)]
pub struct PushNotificationSink {
    /// Device token store
    device_tokens: Arc<DeviceTokenStore>,

    /// Push provider (Expo by default)
    provider: Arc<dyn PushProvider>,

    /// Retry configuration
    retry_config: RetryConfig,

    /// Optional preferences store (checks before delivering)
    preferences: Option<Arc<NotificationPreferencesStore>>,
}

impl PushNotificationSink {
    /// Create with default Expo provider and retry config
    ///
    /// Requires the `push` feature to be enabled.
    #[cfg(feature = "push")]
    pub fn new(device_tokens: Arc<DeviceTokenStore>) -> Self {
        Self {
            device_tokens,
            provider: Arc::new(ExpoPushProvider::new()),
            retry_config: RetryConfig::default(),
            preferences: None,
        }
    }

    /// Create with a custom push provider
    pub fn with_provider(
        device_tokens: Arc<DeviceTokenStore>,
        provider: Arc<dyn PushProvider>,
    ) -> Self {
        Self {
            device_tokens,
            provider,
            retry_config: RetryConfig::default(),
            preferences: None,
        }
    }

    /// Create with custom provider and retry config
    pub fn with_config(
        device_tokens: Arc<DeviceTokenStore>,
        provider: Arc<dyn PushProvider>,
        retry_config: RetryConfig,
    ) -> Self {
        Self {
            device_tokens,
            provider,
            retry_config,
            preferences: None,
        }
    }

    /// Attach a preferences store to check before sending
    ///
    /// When set, the sink checks `is_enabled(recipient, notification_type)`
    /// before sending. Disabled types are silently dropped.
    pub fn with_preferences(mut self, preferences: Arc<NotificationPreferencesStore>) -> Self {
        self.preferences = Some(preferences);
        self
    }

    /// Send messages with retry logic
    ///
    /// Returns the list of tokens that had permanent errors (e.g., `DeviceNotRegistered`).
    /// The caller should unregister these tokens from the store.
    async fn send_with_retry(&self, messages: Vec<PushMessage>) -> Result<Vec<String>> {
        let mut pending = messages;
        let mut attempt = 0;
        let mut permanently_failed_tokens: Vec<String> = Vec::new();

        loop {
            let results = self.provider.send_batch(pending.clone()).await;

            let mut failed: Vec<PushMessage> = Vec::new();
            let mut permanent_errors: Vec<String> = Vec::new();

            for (msg, result) in pending.iter().zip(results.iter()) {
                match result {
                    PushResult::Success => {}
                    PushResult::RetriableError(err) => {
                        tracing::warn!(
                            token = %msg.to,
                            error = %err,
                            attempt = attempt + 1,
                            "push: retriable error"
                        );
                        failed.push(msg.clone());
                    }
                    PushResult::PermanentError(err) => {
                        tracing::error!(
                            token = %msg.to,
                            error = %err,
                            "push: permanent error (will not retry)"
                        );
                        permanently_failed_tokens.push(msg.to.clone());
                        permanent_errors.push(err.clone());
                    }
                }
            }

            if failed.is_empty() {
                if permanent_errors.is_empty() {
                    return Ok(permanently_failed_tokens);
                } else {
                    // All retriable sent, but some had permanent errors
                    // Still return the failed tokens for cleanup
                    return Ok(permanently_failed_tokens);
                }
            }

            attempt += 1;
            if attempt > self.retry_config.max_retries {
                return Err(anyhow!(
                    "push: {} message(s) failed after {} retries",
                    failed.len(),
                    self.retry_config.max_retries
                ));
            }

            // Backoff before retry
            let backoff_idx = (attempt as usize - 1).min(self.retry_config.backoff.len() - 1);
            let delay = self.retry_config.backoff[backoff_idx];
            tracing::debug!(
                attempt = attempt,
                delay_ms = delay.as_millis(),
                remaining = failed.len(),
                "push: retrying after backoff"
            );
            tokio::time::sleep(delay).await;

            pending = failed;
        }
    }
}

#[async_trait]
impl Sink for PushNotificationSink {
    async fn deliver(
        &self,
        payload: Value,
        recipient_id: Option<&str>,
        context_vars: &HashMap<String, Value>,
    ) -> Result<()> {
        // Determine recipient
        let recipient = super::resolve_recipient(recipient_id, &payload, context_vars)
            .ok_or_else(|| anyhow!("push sink: recipient_id not found"))?;

        // Check preferences before sending (same pattern as InAppNotificationSink)
        if let Some(prefs_store) = &self.preferences {
            let notification_type = payload
                .get("notification_type")
                .and_then(|v| v.as_str())
                .unwrap_or("generic");

            if !prefs_store.is_enabled(&recipient, notification_type).await {
                tracing::debug!(
                    recipient = %recipient,
                    notification_type = %notification_type,
                    "push sink: notification type disabled by user preferences, skipping"
                );
                return Ok(());
            }
        }

        // Get device tokens
        let tokens = self.device_tokens.get_tokens(&recipient).await;
        if tokens.is_empty() {
            tracing::debug!(
                recipient = %recipient,
                "push sink: no device tokens registered, skipping"
            );
            return Ok(());
        }

        // Extract notification fields
        let title = payload
            .get("title")
            .and_then(|v| v.as_str())
            .unwrap_or("Notification")
            .to_string();

        let body = payload
            .get("body")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let data = payload.get("data").cloned().unwrap_or(Value::Null);

        // Build messages — one per device token
        let messages: Vec<PushMessage> = tokens
            .into_iter()
            .map(|dt| PushMessage {
                to: dt.token,
                title: title.clone(),
                body: body.clone(),
                data: data.clone(),
                sound: "default".to_string(),
            })
            .collect();

        tracing::debug!(
            recipient = %recipient,
            token_count = messages.len(),
            provider = self.provider.name(),
            "push sink: sending notifications"
        );

        // Send with retry; collect permanently failed tokens for cleanup
        let stale_tokens = self.send_with_retry(messages).await?;

        // Unregister stale tokens (e.g., DeviceNotRegistered)
        for token in &stale_tokens {
            tracing::info!(
                recipient = %recipient,
                token = %token,
                "push sink: unregistering stale device token"
            );
            self.device_tokens.unregister(&recipient, token).await;
        }

        Ok(())
    }

    fn name(&self) -> &str {
        "push"
    }

    fn sink_type(&self) -> SinkType {
        SinkType::Push
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events::sinks::device_tokens::Platform;
    use serde_json::json;
    use std::sync::atomic::{AtomicUsize, Ordering};

    // ── Mock push provider ──────────────────────────────────────────

    /// Shared state for the mock push provider
    #[derive(Debug, Clone)]
    struct MockState {
        results: Arc<tokio::sync::Mutex<Vec<Vec<PushResult>>>>,
        call_count: Arc<AtomicUsize>,
        received: Arc<tokio::sync::Mutex<Vec<Vec<PushMessage>>>>,
    }

    /// A mock push provider that records calls and returns configurable results
    #[derive(Debug)]
    struct MockPushProvider {
        state: MockState,
    }

    impl MockPushProvider {
        fn new(results: Vec<Vec<PushResult>>) -> (Self, MockState) {
            let state = MockState {
                results: Arc::new(tokio::sync::Mutex::new(results)),
                call_count: Arc::new(AtomicUsize::new(0)),
                received: Arc::new(tokio::sync::Mutex::new(Vec::new())),
            };
            (
                Self {
                    state: state.clone(),
                },
                state,
            )
        }

        /// Provider that always succeeds
        fn always_success() -> (Self, MockState) {
            Self::new(Vec::new())
        }
    }

    #[async_trait]
    impl PushProvider for MockPushProvider {
        async fn send_batch(&self, messages: Vec<PushMessage>) -> Vec<PushResult> {
            let call_idx = self.state.call_count.fetch_add(1, Ordering::SeqCst);
            self.state.received.lock().await.push(messages.clone());

            let mut results = self.state.results.lock().await;
            if call_idx < results.len() {
                results[call_idx].drain(..).collect()
            } else {
                // Default: all success
                vec![PushResult::Success; messages.len()]
            }
        }

        fn name(&self) -> &str {
            "mock"
        }
    }

    fn fast_retry_config() -> RetryConfig {
        RetryConfig {
            max_retries: 3,
            backoff: vec![
                Duration::from_millis(1),
                Duration::from_millis(1),
                Duration::from_millis(1),
            ],
        }
    }

    // ── Tests ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_push_deliver_success() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register(
                "user-A",
                "ExponentPushToken[abc]".to_string(),
                Platform::Ios,
            )
            .await;

        let (provider, state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        let payload = json!({
            "title": "New follower",
            "body": "Alice followed you",
            "recipient_id": "user-A",
            "data": {"screen": "profile"}
        });

        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        let calls = state.received.lock().await;
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].len(), 1);
        assert_eq!(calls[0][0].to, "ExponentPushToken[abc]");
        assert_eq!(calls[0][0].title, "New follower");
        assert_eq!(calls[0][0].body, "Alice followed you");
        assert_eq!(calls[0][0].data, json!({"screen": "profile"}));
    }

    #[tokio::test]
    async fn test_push_deliver_multiple_tokens() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;
        tokens
            .register("user-A", "token-2".to_string(), Platform::Android)
            .await;

        let (provider, state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        let payload = json!({
            "title": "Test",
            "body": "Hello",
            "recipient_id": "user-A"
        });

        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        let calls = state.received.lock().await;
        assert_eq!(calls[0].len(), 2);
        assert_eq!(calls[0][0].to, "token-1");
        assert_eq!(calls[0][1].to, "token-2");
    }

    #[tokio::test]
    async fn test_push_deliver_no_tokens_skips() {
        let tokens = Arc::new(DeviceTokenStore::new());
        let (provider, state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        // Should succeed silently (no tokens registered)
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        // Provider should not have been called
        assert_eq!(state.call_count.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_push_deliver_no_recipient_error() {
        let tokens = Arc::new(DeviceTokenStore::new());
        let (provider, _state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        let payload = json!({"title": "Test"});
        let result = sink.deliver(payload, None, &HashMap::new()).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("recipient_id"));
    }

    #[tokio::test]
    async fn test_push_retry_on_server_error() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        // First call: retriable error, second call: success
        let (provider, state) = MockPushProvider::new(vec![
            vec![PushResult::RetriableError("server error: 500".to_string())],
            vec![PushResult::Success],
        ]);

        let sink =
            PushNotificationSink::with_config(tokens, Arc::new(provider), fast_retry_config());

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        // Should have been called twice (initial + 1 retry)
        assert_eq!(state.call_count.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn test_push_no_retry_on_permanent_error() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        let (provider, state) = MockPushProvider::new(vec![vec![PushResult::PermanentError(
            "DeviceNotRegistered".to_string(),
        )]]);

        let sink = PushNotificationSink::with_config(
            tokens.clone(),
            Arc::new(provider),
            fast_retry_config(),
        );

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        // Permanent errors are now handled gracefully: token cleaned up, no error
        let result = sink.deliver(payload, None, &HashMap::new()).await;
        assert!(result.is_ok());

        // Should only have been called once (no retry)
        assert_eq!(state.call_count.load(Ordering::SeqCst), 1);

        // Stale token should have been cleaned up
        assert_eq!(tokens.token_count("user-A").await, 0);
    }

    #[tokio::test]
    async fn test_push_max_retries_exceeded() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        // Always returns retriable error
        let (provider, state) = MockPushProvider::new(vec![
            vec![PushResult::RetriableError("error 1".to_string())],
            vec![PushResult::RetriableError("error 2".to_string())],
            vec![PushResult::RetriableError("error 3".to_string())],
            vec![PushResult::RetriableError("error 4".to_string())],
        ]);

        let sink =
            PushNotificationSink::with_config(tokens, Arc::new(provider), fast_retry_config());

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        let result = sink.deliver(payload, None, &HashMap::new()).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("after 3 retries"));

        // 1 initial + 3 retries = 4 calls
        assert_eq!(state.call_count.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn test_push_explicit_recipient_overrides_payload() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-B", "token-B".to_string(), Platform::Ios)
            .await;

        let (provider, state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        // Payload says user-A, but explicit param says user-B
        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        sink.deliver(payload, Some("user-B"), &HashMap::new())
            .await
            .unwrap();

        let calls = state.received.lock().await;
        assert_eq!(calls[0][0].to, "token-B");
    }

    #[tokio::test]
    async fn test_push_message_serialization() {
        let msg = PushMessage {
            to: "ExponentPushToken[abc]".to_string(),
            title: "Hello".to_string(),
            body: "World".to_string(),
            data: json!({"screen": "home"}),
            sound: "default".to_string(),
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["to"], "ExponentPushToken[abc]");
        assert_eq!(json["title"], "Hello");
        assert_eq!(json["body"], "World");
        assert_eq!(json["data"]["screen"], "home");
        assert_eq!(json["sound"], "default");
    }

    #[tokio::test]
    async fn test_push_message_null_data_omitted() {
        let msg = PushMessage {
            to: "token".to_string(),
            title: "Test".to_string(),
            body: "Body".to_string(),
            data: Value::Null,
            sound: "default".to_string(),
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert!(!json.as_object().unwrap().contains_key("data"));
    }

    #[test]
    fn test_sink_name_and_type() {
        let tokens = Arc::new(DeviceTokenStore::new());
        let (provider, _state) = MockPushProvider::always_success();
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));
        assert_eq!(sink.name(), "push");
        assert_eq!(sink.sink_type(), SinkType::Push);
    }

    // ── Preferences integration tests ─────────────────────────────

    #[tokio::test]
    async fn test_push_with_preferences_disabled_type_skipped() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        let prefs = Arc::new(NotificationPreferencesStore::new());
        prefs.disable_type("user-A", "new_like").await;

        let (provider, state) = MockPushProvider::always_success();
        let sink =
            PushNotificationSink::with_provider(tokens, Arc::new(provider)).with_preferences(prefs);

        // Deliver a disabled type — should be skipped
        let payload = json!({
            "title": "New like",
            "notification_type": "new_like",
            "recipient_id": "user-A"
        });
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();
        assert_eq!(state.call_count.load(Ordering::SeqCst), 0);

        // Deliver an enabled type — should send
        let payload = json!({
            "title": "New follower",
            "notification_type": "new_follower",
            "recipient_id": "user-A"
        });
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();
        assert_eq!(state.call_count.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_push_with_preferences_muted_user_skipped() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        let prefs = Arc::new(NotificationPreferencesStore::new());
        prefs.mute("user-A").await;

        let (provider, state) = MockPushProvider::always_success();
        let sink =
            PushNotificationSink::with_provider(tokens, Arc::new(provider)).with_preferences(prefs);

        let payload = json!({
            "title": "Test",
            "notification_type": "new_follower",
            "recipient_id": "user-A"
        });
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();
        assert_eq!(state.call_count.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_push_without_preferences_delivers_all() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "token-1".to_string(), Platform::Ios)
            .await;

        let (provider, state) = MockPushProvider::always_success();
        // No preferences store
        let sink = PushNotificationSink::with_provider(tokens, Arc::new(provider));

        let payload = json!({
            "title": "Test",
            "notification_type": "new_like",
            "recipient_id": "user-A"
        });
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();
        assert_eq!(state.call_count.load(Ordering::SeqCst), 1);
    }

    // ── Stale token cleanup tests ─────────────────────────────────

    #[tokio::test]
    async fn test_push_permanent_error_unregisters_stale_token() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "good-token".to_string(), Platform::Ios)
            .await;
        tokens
            .register("user-A", "stale-token".to_string(), Platform::Android)
            .await;
        assert_eq!(tokens.token_count("user-A").await, 2);

        // First token succeeds, second gets DeviceNotRegistered
        let (provider, _state) = MockPushProvider::new(vec![vec![
            PushResult::Success,
            PushResult::PermanentError("DeviceNotRegistered".to_string()),
        ]]);

        let sink = PushNotificationSink::with_config(
            tokens.clone(),
            Arc::new(provider),
            fast_retry_config(),
        );

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        // Should succeed (stale token cleaned up silently)
        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        // Stale token should be unregistered
        assert_eq!(tokens.token_count("user-A").await, 1);
        let remaining = tokens.get_tokens("user-A").await;
        assert_eq!(remaining[0].token, "good-token");
    }

    #[tokio::test]
    async fn test_push_all_tokens_permanent_error_cleans_all() {
        let tokens = Arc::new(DeviceTokenStore::new());
        tokens
            .register("user-A", "dead-1".to_string(), Platform::Ios)
            .await;
        tokens
            .register("user-A", "dead-2".to_string(), Platform::Android)
            .await;

        let (provider, _state) = MockPushProvider::new(vec![vec![
            PushResult::PermanentError("DeviceNotRegistered".to_string()),
            PushResult::PermanentError("DeviceNotRegistered".to_string()),
        ]]);

        let sink = PushNotificationSink::with_config(
            tokens.clone(),
            Arc::new(provider),
            fast_retry_config(),
        );

        let payload = json!({
            "title": "Test",
            "recipient_id": "user-A"
        });

        sink.deliver(payload, None, &HashMap::new()).await.unwrap();

        // All tokens cleaned up
        assert_eq!(tokens.token_count("user-A").await, 0);
    }
}