rigatoni-core 0.2.0

Core traits, pipeline orchestration, and MongoDB integration for Rigatoni CDC/Data Replication framework
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
// Copyright 2025 Rigatoni Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! `MongoDB` Change Stream Wrapper
//!
//! This module provides an ergonomic async wrapper around `MongoDB` change streams with:
//! - Automatic resume token management
//! - Reconnection with exponential backoff
//! - Graceful error handling
//! - Pipeline filtering support
//! - Pre/post-image configuration
//!
//! # Architecture
//!
//! The [`ChangeStreamListener`] implements the [`Stream`] trait from `futures`,
//! making it composable with other async stream utilities. It wraps MongoDB's
//! native change stream and adds production-ready reliability features.
//!
//! ## Resume Token Flow
//!
//! ```text
//! ┌─────────────────┐
//! │ MongoDB Cluster │
//! └────────┬────────┘
//!          │ Change Events
//!//! ┌──────────────────────┐
//! │ ChangeStreamListener │◄─── Resume Token Callback
//! └──────────────────────┘      (persists to storage)
//!//!          │ ChangeEvent
//!//! ┌─────────────────┐
//! │ Pipeline Stage  │
//! └─────────────────┘
//! ```
//!
//! ## Reconnection Algorithm
//!
//! When a connection error occurs:
//! 1. Load the last persisted resume token
//! 2. Attempt reconnection with exponential backoff: 100ms, 200ms, 400ms, ...
//! 3. Max backoff capped at `max_backoff_ms`
//! 4. Fail after `max_reconnect_attempts` attempts
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```rust,no_run
//! use rigatoni_core::stream::{ChangeStreamListener, ChangeStreamConfig};
//! use mongodb::{Client, options::ClientOptions};
//! use futures::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Connect to MongoDB
//! let client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
//! let client = Client::with_options(client_options)?;
//! let collection = client.database("mydb").collection("users");
//!
//! // Configure change stream
//! let config = ChangeStreamConfig::builder()
//!     .full_document_before_change()  // Enable pre-images
//!     .full_document_update_lookup()  // Include full document on updates
//!     .max_reconnect_attempts(10)
//!     .build()?;
//!
//! // Create listener with resume token callback
//! let mut listener = ChangeStreamListener::new(
//!     collection,
//!     config,
//!     |token| {
//!         // Persist resume token to storage
//!         println!("Saving resume token: {:?}", token);
//!         Box::pin(async { Ok(()) })
//!     },
//! ).await?;
//!
//! // Consume events
//! while let Some(result) = listener.next().await {
//!     match result {
//!         Ok(ackable) => {
//!             let event = ackable.event_ref();
//!             println!("Received: {:?}", event.operation);
//!             ackable.ack(); // Acknowledge after processing
//!         }
//!         Err(e) => {
//!             eprintln!("Stream error: {}", e);
//!             break;
//!         }
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## With Pipeline Filtering
//!
//! ```rust,no_run
//! use rigatoni_core::stream::{ChangeStreamListener, ChangeStreamConfig};
//! use bson::doc;
//! use futures::StreamExt;
//! # use mongodb::{Client, options::ClientOptions};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = Client::with_options(ClientOptions::parse("mongodb://localhost:27017").await?)?;
//! # let collection = client.database("mydb").collection("users");
//!
//! // Only watch insert and update operations
//! let pipeline = vec![
//!     doc! {
//!         "$match": {
//!             "operationType": { "$in": ["insert", "update"] }
//!         }
//!     }
//! ];
//!
//! let config = ChangeStreamConfig::builder()
//!     .pipeline(pipeline)
//!     .build()?;
//!
//! let mut listener = ChangeStreamListener::new(
//!     collection,
//!     config,
//!     |_| Box::pin(async { Ok(()) }),
//! ).await?;
//!
//! while let Some(result) = listener.next().await {
//!     let ackable = result?;
//!     ackable.ack();
//!     // Only insert/update events will be received
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Multi-Collection Scenario
//!
//! ```rust,no_run
//! use rigatoni_core::stream::ChangeStreamListener;
//! use futures::StreamExt;
//! use tokio::task::JoinSet;
//! # use mongodb::{Client, options::ClientOptions};
//! # use rigatoni_core::stream::ChangeStreamConfig;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! # let client = Client::with_options(ClientOptions::parse("mongodb://localhost:27017").await?)?;
//! let db = client.database("mydb");
//!
//! let mut tasks = JoinSet::new();
//!
//! // Watch multiple collections concurrently
//! for collection_name in &["users", "orders", "products"] {
//!     let collection = db.collection(collection_name);
//!     let config = ChangeStreamConfig::default();
//!
//!     tasks.spawn(async move {
//!         let mut listener = ChangeStreamListener::new(
//!             collection,
//!             config,
//!             |_| Box::pin(async { Ok(()) }),
//!         ).await?;
//!
//!         while let Some(result) = listener.next().await {
//!             let ackable = result?;
//!             println!("{}: {:?}", collection_name, ackable.event_ref().operation);
//!             ackable.ack();
//!         }
//!
//!         Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
//!     });
//! }
//!
//! // Wait for all streams
//! while let Some(result) = tasks.join_next().await {
//!     result??;
//! }
//! # Ok(())
//! # }
//! ```

use crate::event::{ChangeEvent, ConversionError};
use bson::Document;
use futures::Stream;
use mongodb::{
    change_stream::event::{ChangeStreamEvent, ResumeToken},
    error::{Error as MongoError, ErrorKind as MongoErrorKind},
    options::ChangeStreamOptions,
    Collection,
};
use std::{
    future::Future,
    pin::Pin,
    sync::{Arc, Mutex},
    task::{Context, Poll},
    time::Duration,
};
use thiserror::Error;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

/// Errors that can occur during change stream operations.
#[derive(Debug, Error)]
pub enum StreamError {
    /// MongoDB connection error (may be retryable)
    #[error("Connection error: {message}")]
    Connection {
        message: String,
        #[source]
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
        /// MongoDB error code
        code: Option<i32>,
        /// MongoDB error labels (e.g., "RetryableWriteError")
        labels: Vec<String>,
    },

    /// Failed to convert MongoDB event to ChangeEvent
    #[error("Event conversion failed: {0}")]
    Conversion(#[from] ConversionError),

    /// Resume token persistence failed
    #[error("Resume token persistence failed: {0}")]
    ResumeTokenPersistence(String),

    /// Stream was invalidated (collection dropped/renamed)
    #[error("Stream invalidated: {reason}")]
    Invalidated { reason: String },

    /// Maximum reconnection attempts exceeded
    #[error("Max reconnection attempts ({0}) exceeded")]
    MaxReconnectAttemptsExceeded(u32),

    /// Resume token is invalid or oplog truncated (error code 286)
    #[error("Invalid resume token (code {code}): oplog may be truncated")]
    InvalidResumeToken { code: i32 },

    /// Configuration error
    #[error("Configuration error: {0}")]
    Configuration(String),
}

impl From<MongoError> for StreamError {
    fn from(err: MongoError) -> Self {
        Self::from_mongo_error(err)
    }
}

impl StreamError {
    /// Creates a StreamError from a MongoDB error with proper classification.
    ///
    /// This method checks error codes and labels for accurate categorization.
    pub fn from_mongo_error(err: MongoError) -> Self {
        // Extract error code
        let code = match err.kind.as_ref() {
            MongoErrorKind::Command(cmd_err) => Some(cmd_err.code),
            _ => None,
        };

        // Check for invalid resume token (code 286: ChangeStreamFatalError)
        if code == Some(286) {
            return Self::InvalidResumeToken { code: 286 };
        }

        // Extract labels from the error
        // Note: In MongoDB Rust driver, labels() method returns &HashSet<String>
        let labels: Vec<String> = err.labels().iter().cloned().collect();

        Self::Connection {
            message: err.to_string(),
            source: Some(Box::new(err)),
            code,
            labels,
        }
    }

    /// Returns true if this error is retryable with reconnection.
    ///
    /// Uses MongoDB error codes and labels for accurate classification:
    /// - Error labels: RetryableWriteError, TransientTransactionError, NetworkError
    /// - Transient error codes: 6, 7, 43, 89, 91, 10107, 11600, 11602, 13435, 13436
    ///
    /// Non-retryable errors:
    /// - Invalid resume token (286)
    /// - Stream invalidated (collection dropped)
    /// - Authentication/authorization errors
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        match self {
            Self::Connection { code, labels, .. } => {
                // Check error labels first (most reliable)
                if labels.iter().any(|l| {
                    l == "RetryableWriteError"
                        || l == "TransientTransactionError"
                        || l == "NetworkError"
                }) {
                    return true;
                }

                // Check specific transient error codes
                if let Some(c) = code {
                    matches!(
                        c,
                        // Network errors
                        6 |    // HostUnreachable
                        7 |    // HostNotFound
                        89 |   // NetworkTimeout
                        91 |   // ShutdownInProgress
                        // Replication errors (transient during failover)
                        10107 | // NotMaster / NotPrimary
                        11600 | // InterruptedAtShutdown
                        11602 | // InterruptedDueToReplStateChange
                        13435 | // NotMasterNoSlaveOk
                        13436 | // NotMasterOrSecondary / NotPrimaryOrSecondary
                        // Cursor errors (can retry with resume token)
                        43 // CursorNotFound
                    )
                } else {
                    // No code available, be conservative
                    false
                }
            }
            Self::Invalidated { .. } => false,
            Self::InvalidResumeToken { .. } => false,
            Self::MaxReconnectAttemptsExceeded(_) => false,
            Self::Conversion(_) => false,
            Self::ResumeTokenPersistence(_) => false,
            Self::Configuration(_) => false,
        }
    }

    /// Returns the error category for metrics/logging.
    #[must_use]
    pub fn category(&self) -> &'static str {
        match self {
            Self::Connection { .. } => "connection",
            Self::Conversion(_) => "conversion",
            Self::ResumeTokenPersistence(_) => "persistence",
            Self::Invalidated { .. } => "invalidated",
            Self::MaxReconnectAttemptsExceeded(_) => "max_retries",
            Self::InvalidResumeToken { .. } => "invalid_token",
            Self::Configuration(_) => "configuration",
        }
    }
}

/// Event with acknowledgment capability for correct resume token semantics.
///
/// This type ensures that resume tokens are persisted AFTER the user successfully
/// processes the event, providing at-least-once delivery semantics.
///
/// # Important
///
/// You MUST call `.ack()` after successfully processing the event. If you don't
/// call `.ack()`, the resume token will not be persisted, and the stream cannot
/// reliably resume after a crash.
///
/// # Examples
///
/// ```rust,no_run
/// use rigatoni_core::stream::{ChangeStreamListener, ChangeStreamConfig};
/// use futures::StreamExt;
/// # use mongodb::{Client, options::ClientOptions};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// # let client = Client::with_options(ClientOptions::parse("mongodb://localhost:27017").await?)?;
/// # let collection = client.database("mydb").collection("test");
/// let mut listener = ChangeStreamListener::new(
///     collection,
///     ChangeStreamConfig::default(),
///     |_| Box::pin(async { Ok(()) }),
/// ).await?;
///
/// while let Some(ackable) = listener.next().await {
///     let ackable = ackable?;
///
///     // Access the event
///     let event = ackable.event_ref();
///     println!("Processing: {:?}", event.operation);
///
///     // Process the event
///     process_event(event).await?;
///
///     // IMPORTANT: Acknowledge after successful processing
///     ackable.ack();
/// }
/// # Ok(())
/// # }
/// # async fn process_event(event: &rigatoni_core::event::ChangeEvent) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { Ok(()) }
/// ```
#[derive(Debug, Clone)]
pub struct AckableEvent {
    /// The change event
    pub event: ChangeEvent,

    /// Resume token for this event
    resume_token: Document,

    /// Channel to send ack
    ack_sender: mpsc::UnboundedSender<Document>,

    /// Shared reference to last resume token (updated on ack)
    last_resume_token: Arc<Mutex<Option<Document>>>,
}

impl AckableEvent {
    /// Acknowledge processing of this event, persisting the resume token.
    ///
    /// Call this AFTER successfully processing the event to ensure at-least-once semantics.
    /// The token will be sent to a background persistence task asynchronously.
    ///
    /// This method also updates the internal last_resume_token used for reconnection,
    /// ensuring that reconnection only resumes from successfully processed events.
    ///
    /// If the persistence task has stopped, this will log a warning but not fail.
    pub fn ack(self) {
        // Update last resume token for reconnection (only after user confirms processing)
        if let Ok(mut last_token) = self.last_resume_token.lock() {
            *last_token = Some(self.resume_token.clone());
        }

        // Send token for persistence
        if self.ack_sender.send(self.resume_token).is_err() {
            warn!("Failed to send ack - persistence task may have stopped");
        }
    }

    /// Get a reference to the change event without consuming the `AckableEvent`.
    ///
    /// Use this to access the event data while keeping the ability to call `.ack()` later.
    #[must_use]
    pub fn event_ref(&self) -> &ChangeEvent {
        &self.event
    }

    /// Consume the `AckableEvent` and return the inner `ChangeEvent`.
    ///
    /// # Warning
    ///
    /// After calling this, you cannot call `.ack()`. Only use this if you don't
    /// want to persist the resume token.
    #[must_use]
    pub fn into_event(self) -> ChangeEvent {
        self.event
    }
}

/// Configuration for change stream behavior.
///
/// Use [`ChangeStreamConfigBuilder`] to construct instances:
///
/// ```rust
/// use rigatoni_core::stream::ChangeStreamConfig;
///
/// let config = ChangeStreamConfig::builder()
///     .full_document_update_lookup()
///     .max_reconnect_attempts(10)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct ChangeStreamConfig {
    /// MongoDB aggregation pipeline to filter events
    pub pipeline: Vec<Document>,

    /// Whether to include full document for update operations
    pub full_document_on_update: bool,

    /// Whether to include document before change (requires MongoDB 6.0+)
    pub full_document_before_change: bool,

    /// Initial backoff in milliseconds
    pub initial_backoff_ms: u64,

    /// Maximum backoff in milliseconds
    pub max_backoff_ms: u64,

    /// Maximum number of reconnection attempts (0 = infinite)
    pub max_reconnect_attempts: u32,

    /// Batch size for fetching events
    pub batch_size: Option<u32>,

    /// Resume token to start from (if any)
    pub resume_token: Option<Document>,

    /// Backoff jitter factor (0.0 to 1.0)
    /// Default: 0.1 (10% jitter)
    pub backoff_jitter: f64,
}

impl Default for ChangeStreamConfig {
    fn default() -> Self {
        Self {
            pipeline: Vec::new(),
            full_document_on_update: false,
            full_document_before_change: false,
            initial_backoff_ms: 100,
            max_backoff_ms: 30_000, // 30 seconds
            max_reconnect_attempts: 5,
            batch_size: None,
            resume_token: None,
            backoff_jitter: 0.1, // 10% jitter to prevent thundering herd
        }
    }
}

impl ChangeStreamConfig {
    /// Creates a new builder for configuring a change stream.
    #[must_use]
    pub fn builder() -> ChangeStreamConfigBuilder {
        ChangeStreamConfigBuilder::default()
    }

    /// Validates the configuration.
    ///
    /// Returns an error if:
    /// - `initial_backoff_ms` is 0
    /// - `initial_backoff_ms` > `max_backoff_ms`
    /// - `backoff_jitter` is not in range [0.0, 1.0]
    pub fn validate(&self) -> Result<(), StreamError> {
        if self.initial_backoff_ms == 0 {
            return Err(StreamError::Configuration(
                "initial_backoff_ms must be greater than 0".to_string(),
            ));
        }

        if self.initial_backoff_ms > self.max_backoff_ms {
            return Err(StreamError::Configuration(format!(
                "initial_backoff_ms ({}) must be <= max_backoff_ms ({})",
                self.initial_backoff_ms, self.max_backoff_ms
            )));
        }

        if !(0.0..=1.0).contains(&self.backoff_jitter) {
            return Err(StreamError::Configuration(format!(
                "backoff_jitter ({}) must be between 0.0 and 1.0",
                self.backoff_jitter
            )));
        }

        Ok(())
    }

    /// Calculates backoff duration with jitter for the given attempt.
    ///
    /// Uses exponential backoff: `initial_ms * 2^(attempt-1)`, capped at `max_backoff_ms`.
    /// Adds random jitter to prevent thundering herd: ±(base * jitter_factor / 2).
    fn calculate_backoff(&self, attempt: u32) -> Duration {
        // Exponential backoff: initial * 2^(attempt-1)
        let base_ms = self
            .initial_backoff_ms
            .saturating_mul(1_u64 << attempt.saturating_sub(1))
            .min(self.max_backoff_ms);

        // Add jitter: random value in range [base * (1 - jitter/2), base * (1 + jitter/2)]
        if self.backoff_jitter > 0.0 {
            let jitter_range = (base_ms as f64) * self.backoff_jitter;
            let jitter = (rand::random::<f64>() * jitter_range) - (jitter_range / 2.0);
            let final_ms = ((base_ms as f64) + jitter).max(0.0) as u64;
            Duration::from_millis(final_ms)
        } else {
            Duration::from_millis(base_ms)
        }
    }

    /// Converts this config to MongoDB's `ChangeStreamOptions`.
    fn to_mongo_options(&self) -> ChangeStreamOptions {
        let mut options = ChangeStreamOptions::default();

        // Set full document options
        if self.full_document_on_update {
            options.full_document = Some(mongodb::options::FullDocumentType::UpdateLookup);
        }

        if self.full_document_before_change {
            options.full_document_before_change =
                Some(mongodb::options::FullDocumentBeforeChangeType::WhenAvailable);
        }

        // Set batch size
        options.batch_size = self.batch_size;

        // Set resume token if provided
        // Note: We store resume tokens as Document for persistence,
        // but MongoDB expects ResumeToken. We'll deserialize from BSON bytes.
        if let Some(ref token_doc) = self.resume_token {
            // Serialize Document to bytes, then deserialize as ResumeToken
            if let Ok(bytes) = bson::to_vec(token_doc) {
                if let Ok(resume_token) = bson::from_slice::<ResumeToken>(&bytes) {
                    options.resume_after = Some(resume_token);
                }
            }
        }

        options
    }
}

/// Builder for [`ChangeStreamConfig`].
#[derive(Debug, Default)]
pub struct ChangeStreamConfigBuilder {
    pipeline: Vec<Document>,
    full_document_on_update: bool,
    full_document_before_change: bool,
    initial_backoff_ms: Option<u64>,
    max_backoff_ms: Option<u64>,
    max_reconnect_attempts: Option<u32>,
    batch_size: Option<u32>,
    resume_token: Option<Document>,
    backoff_jitter: Option<f64>,
}

impl ChangeStreamConfigBuilder {
    /// Sets the aggregation pipeline for filtering events.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rigatoni_core::stream::ChangeStreamConfig;
    /// use bson::doc;
    ///
    /// let config = ChangeStreamConfig::builder()
    ///     .pipeline(vec![
    ///         doc! { "$match": { "operationType": "insert" } }
    ///     ])
    ///     .build();
    /// ```
    #[must_use]
    pub fn pipeline(mut self, pipeline: Vec<Document>) -> Self {
        self.pipeline = pipeline;
        self
    }

    /// Enables full document retrieval for update operations.
    ///
    /// When enabled, MongoDB will include the entire updated document
    /// in the change event (requires additional lookup).
    #[must_use]
    pub fn full_document_update_lookup(mut self) -> Self {
        self.full_document_on_update = true;
        self
    }

    /// Enables pre-image for change events (requires MongoDB 6.0+).
    ///
    /// When enabled, the document state before the change will be included.
    /// Requires pre-image collection to be configured on the collection.
    #[must_use]
    pub fn full_document_before_change(mut self) -> Self {
        self.full_document_before_change = true;
        self
    }

    /// Sets the initial backoff duration in milliseconds.
    ///
    /// Default: 100ms
    #[must_use]
    pub fn initial_backoff_ms(mut self, ms: u64) -> Self {
        self.initial_backoff_ms = Some(ms);
        self
    }

    /// Sets the maximum backoff duration in milliseconds.
    ///
    /// Default: 30,000ms (30 seconds)
    #[must_use]
    pub fn max_backoff_ms(mut self, ms: u64) -> Self {
        self.max_backoff_ms = Some(ms);
        self
    }

    /// Sets the maximum number of reconnection attempts.
    ///
    /// Set to 0 for infinite retries (use with caution).
    /// Default: 5
    #[must_use]
    pub fn max_reconnect_attempts(mut self, attempts: u32) -> Self {
        self.max_reconnect_attempts = Some(attempts);
        self
    }

    /// Sets the batch size for fetching events from MongoDB.
    ///
    /// Larger batches can improve throughput but increase memory usage.
    #[must_use]
    pub fn batch_size(mut self, size: u32) -> Self {
        self.batch_size = Some(size);
        self
    }

    /// Sets a resume token to start streaming from.
    ///
    /// If provided, the stream will resume from this point instead
    /// of starting from the current time.
    #[must_use]
    pub fn resume_token(mut self, token: Document) -> Self {
        self.resume_token = Some(token);
        self
    }

    /// Sets the backoff jitter factor (0.0 to 1.0).
    ///
    /// Jitter adds randomness to backoff delays to prevent thundering herd.
    /// A value of 0.1 means ±10% randomness.
    ///
    /// Default: 0.1
    #[must_use]
    pub fn backoff_jitter(mut self, jitter: f64) -> Self {
        self.backoff_jitter = Some(jitter);
        self
    }

    /// Builds the configuration.
    ///
    /// # Errors
    ///
    /// Returns `StreamError::Configuration` if validation fails:
    /// - `initial_backoff_ms` must be > 0
    /// - `initial_backoff_ms` must be <= `max_backoff_ms`
    /// - `backoff_jitter` must be between 0.0 and 1.0
    pub fn build(self) -> Result<ChangeStreamConfig, StreamError> {
        let config = ChangeStreamConfig {
            pipeline: self.pipeline,
            full_document_on_update: self.full_document_on_update,
            full_document_before_change: self.full_document_before_change,
            initial_backoff_ms: self.initial_backoff_ms.unwrap_or(100),
            max_backoff_ms: self.max_backoff_ms.unwrap_or(30_000),
            max_reconnect_attempts: self.max_reconnect_attempts.unwrap_or(5),
            batch_size: self.batch_size,
            resume_token: self.resume_token,
            backoff_jitter: self.backoff_jitter.unwrap_or(0.1),
        };

        config.validate()?;
        Ok(config)
    }
}

/// Callback for persisting resume tokens.
///
/// This callback is invoked after each successfully processed event.
/// It should persist the token to durable storage (database, file, etc.)
/// to enable resuming after crashes or restarts.
///
/// # Returns
///
/// - `Ok(())` if token was successfully persisted
/// - `Err(String)` if persistence failed (will propagate as StreamError)
///
/// # Example
///
/// ```rust
/// use bson::Document;
/// use std::future::Future;
/// use std::pin::Pin;
///
/// fn save_to_redis(
///     token: Document,
/// ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>> {
///     Box::pin(async move {
///         // Save to Redis
///         // redis_client.set("resume_token", token).await
///         //     .map_err(|e| e.to_string())?;
///         Ok(())
///     })
/// }
/// ```
pub type ResumeTokenCallback =
    Box<dyn Fn(Document) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>> + Send + Sync>;

/// Type alias for the reconnection future to reduce complexity
type ReconnectFuture = Pin<
    Box<
        dyn Future<
                Output = Result<
                    mongodb::change_stream::ChangeStream<ChangeStreamEvent<Document>>,
                    StreamError,
                >,
            > + Send,
    >,
>;

/// State of the change stream listener.
enum StreamState {
    /// Stream is active and consuming events
    Active,
    /// Stream encountered an error and is reconnecting
    Reconnecting(ReconnectFuture),
    /// Stream is closed (terminal state)
    Closed,
}

/// A MongoDB change stream listener with automatic reconnection.
///
/// Implements the `Stream` trait, yielding `Result<ChangeEvent, StreamError>`.
///
/// # Lifecycle
///
/// 1. **Active**: Consuming events normally
/// 2. **Reconnecting**: Connection lost, attempting to reconnect with exponential backoff
/// 3. **Closed**: Terminal state after fatal error or explicit close
///
/// # Thread Safety
///
/// `ChangeStreamListener` is `Send` but not `Sync`. Each listener should be
/// owned by a single task. For multi-collection scenarios, spawn separate
/// tasks with separate listeners.
///
/// # Resume Token Semantics
///
/// Resume tokens are only updated when the user calls `.ack()` on an `AckableEvent`.
/// This ensures at-least-once delivery semantics: if the stream crashes before
/// the user acknowledges an event, reconnection will resume from before that event.
pub struct ChangeStreamListener {
    /// The MongoDB collection being watched
    collection: Collection<Document>,

    /// Configuration for the change stream
    config: ChangeStreamConfig,

    /// The underlying MongoDB change stream
    stream: Option<mongodb::change_stream::ChangeStream<ChangeStreamEvent<Document>>>,

    /// Callback for persisting resume tokens
    resume_token_callback: ResumeTokenCallback,

    /// Current state of the listener
    state: StreamState,

    /// Number of reconnection attempts made
    reconnect_attempts: u32,

    /// Last successfully processed resume token (shared with AckableEvent)
    last_resume_token: Arc<Mutex<Option<Document>>>,

    /// Channel sender for acking tokens (sends to persistence task)
    ack_sender: mpsc::UnboundedSender<Document>,

    /// Background task that persists tokens
    _persistence_task: tokio::task::JoinHandle<()>,
}

impl ChangeStreamListener {
    /// Creates a new change stream listener.
    ///
    /// # Arguments
    ///
    /// * `collection` - MongoDB collection to watch
    /// * `config` - Change stream configuration
    /// * `resume_token_callback` - Callback to persist resume tokens
    ///
    /// # Errors
    ///
    /// Returns `StreamError::Connection` if initial connection fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use rigatoni_core::stream::{ChangeStreamListener, ChangeStreamConfig};
    /// use mongodb::{Client, options::ClientOptions};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = Client::with_options(
    ///     ClientOptions::parse("mongodb://localhost:27017").await?
    /// )?;
    /// let collection = client.database("mydb").collection("users");
    ///
    /// let listener = ChangeStreamListener::new(
    ///     collection,
    ///     ChangeStreamConfig::default(),
    ///     |token| Box::pin(async move {
    ///         println!("Token: {:?}", token);
    ///         Ok(())
    ///     }),
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new<F>(
        collection: Collection<Document>,
        config: ChangeStreamConfig,
        resume_token_callback: F,
    ) -> Result<Self, StreamError>
    where
        F: Fn(Document) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>
            + Send
            + Sync
            + 'static,
    {
        info!(
            "Initializing change stream for {}.{}",
            collection.namespace().db,
            collection.namespace().coll
        );

        let options = config.to_mongo_options();
        let stream = if config.pipeline.is_empty() {
            collection.watch().with_options(options).await?
        } else {
            collection
                .watch()
                .pipeline(config.pipeline.clone())
                .with_options(options)
                .await?
        };

        // Create mpsc channel for token persistence
        let (ack_sender, mut ack_receiver) = mpsc::unbounded_channel::<Document>();

        // Spawn background task to persist tokens
        let resume_token_callback_clone = Box::new(resume_token_callback);
        let persistence_task = tokio::spawn(async move {
            while let Some(token) = ack_receiver.recv().await {
                if let Err(e) = resume_token_callback_clone(token).await {
                    warn!("Failed to persist resume token: {}", e);
                }
            }
            debug!("Token persistence task shutting down");
        });

        Ok(Self {
            collection,
            config,
            stream: Some(stream),
            resume_token_callback: Box::new(|_| Box::pin(async { Ok(()) })),
            state: StreamState::Active,
            reconnect_attempts: 0,
            last_resume_token: Arc::new(Mutex::new(None)),
            ack_sender,
            _persistence_task: persistence_task,
        })
    }

    /// Async reconnection function that doesn't borrow self.
    ///
    /// This is called from within the reconnection future in poll_next.
    /// It returns the new stream on success.
    async fn reconnect_async(
        collection: Collection<Document>,
        config: ChangeStreamConfig,
        last_resume_token: Option<Document>,
        mut reconnect_attempts: u32,
    ) -> Result<mongodb::change_stream::ChangeStream<ChangeStreamEvent<Document>>, StreamError>
    {
        reconnect_attempts += 1;

        // Check if max attempts exceeded
        if config.max_reconnect_attempts > 0 && reconnect_attempts > config.max_reconnect_attempts {
            error!(
                attempts = reconnect_attempts,
                "Max reconnection attempts exceeded"
            );
            return Err(StreamError::MaxReconnectAttemptsExceeded(
                config.max_reconnect_attempts,
            ));
        }

        // Calculate exponential backoff with jitter
        let backoff = config.calculate_backoff(reconnect_attempts);

        warn!(
            attempt = reconnect_attempts,
            backoff_ms = backoff.as_millis(),
            "Reconnecting to change stream"
        );

        // Sleep before retry
        tokio::time::sleep(backoff).await;

        // Build options with resume token if available
        let mut options = config.to_mongo_options();
        if let Some(ref token_doc) = last_resume_token {
            debug!("Resuming from token: {:?}", token_doc);
            // Serialize Document to bytes, then deserialize as ResumeToken
            if let Ok(bytes) = bson::to_vec(token_doc) {
                if let Ok(resume_token) = bson::from_slice::<ResumeToken>(&bytes) {
                    options.resume_after = Some(resume_token);
                }
            }
        }

        // Attempt to reopen stream
        let stream = if config.pipeline.is_empty() {
            collection.watch().with_options(options).await?
        } else {
            collection
                .watch()
                .pipeline(config.pipeline.clone())
                .with_options(options)
                .await?
        };

        info!(
            attempt = reconnect_attempts,
            "Successfully reconnected to change stream"
        );

        Ok(stream)
    }

    /// Attempts to reconnect the change stream with exponential backoff.
    ///
    /// This method is called internally when a retryable error occurs.
    /// It will:
    /// 1. Calculate backoff duration
    /// 2. Sleep for backoff period
    /// 3. Attempt to reopen stream with last resume token
    /// 4. Reset attempts on success
    ///
    /// # Note
    ///
    /// This method is now deprecated in favor of reconnect_async.
    #[allow(dead_code)]
    async fn reconnect(&mut self) -> Result<(), StreamError> {
        self.reconnect_attempts += 1;

        // Check if max attempts exceeded
        if self.config.max_reconnect_attempts > 0
            && self.reconnect_attempts > self.config.max_reconnect_attempts
        {
            error!(
                attempts = self.reconnect_attempts,
                "Max reconnection attempts exceeded"
            );
            return Err(StreamError::MaxReconnectAttemptsExceeded(
                self.config.max_reconnect_attempts,
            ));
        }

        // Calculate exponential backoff: initial * 2^(attempts-1)
        let backoff_ms = self
            .config
            .initial_backoff_ms
            .saturating_mul(2_u64.saturating_pow(self.reconnect_attempts - 1))
            .min(self.config.max_backoff_ms);

        warn!(
            attempt = self.reconnect_attempts,
            backoff_ms = backoff_ms,
            "Reconnecting to change stream"
        );

        // Sleep before retry
        tokio::time::sleep(Duration::from_millis(backoff_ms)).await;

        // Build options with resume token if available
        let mut options = self.config.to_mongo_options();
        if let Ok(last_token) = self.last_resume_token.lock() {
            if let Some(ref token_doc) = *last_token {
                debug!("Resuming from token: {:?}", token_doc);
                // Serialize Document to bytes, then deserialize as ResumeToken
                if let Ok(bytes) = bson::to_vec(token_doc) {
                    if let Ok(resume_token) = bson::from_slice::<ResumeToken>(&bytes) {
                        options.resume_after = Some(resume_token);
                    }
                }
            }
        }

        // Attempt to reopen stream
        let stream = if self.config.pipeline.is_empty() {
            self.collection.watch().with_options(options).await?
        } else {
            self.collection
                .watch()
                .pipeline(self.config.pipeline.clone())
                .with_options(options)
                .await?
        };

        info!(
            attempt = self.reconnect_attempts,
            "Successfully reconnected to change stream"
        );

        self.stream = Some(stream);
        self.state = StreamState::Active;
        self.reconnect_attempts = 0; // Reset on success

        Ok(())
    }

    /// Processes a MongoDB change stream event.
    ///
    /// Converts to ChangeEvent, persists resume token, and handles invalidation.
    ///
    /// # Note
    ///
    /// Currently not used in poll_next due to async/await borrowing constraints.
    /// The logic is inlined in poll_next instead.
    #[allow(dead_code)]
    async fn process_event(
        &mut self,
        event: ChangeStreamEvent<Document>,
    ) -> Result<ChangeEvent, StreamError> {
        // Extract resume token before conversion
        let resume_token = bson::to_document(&event.id)
            .map_err(|e| StreamError::ResumeTokenPersistence(e.to_string()))?;

        // Convert to ChangeEvent
        let change_event = ChangeEvent::try_from(event)?;

        // Check for invalidation
        if change_event.is_invalidate() {
            let reason = format!(
                "Collection {}.{} was dropped or renamed",
                self.collection.namespace().db,
                self.collection.namespace().coll
            );
            error!("{}", reason);
            self.state = StreamState::Closed;
            return Err(StreamError::Invalidated { reason });
        }

        // Persist resume token
        (self.resume_token_callback)(resume_token.clone())
            .await
            .map_err(StreamError::ResumeTokenPersistence)?;

        // Update last resume token
        if let Ok(mut last_token) = self.last_resume_token.lock() {
            *last_token = Some(resume_token);
        }

        Ok(change_event)
    }

    /// Closes the change stream.
    ///
    /// This is a graceful shutdown that closes the underlying MongoDB cursor.
    /// After calling this, the stream will return `None` on next poll.
    pub async fn close(&mut self) {
        info!("Closing change stream");
        self.state = StreamState::Closed;
        self.stream = None;
    }
}

impl Stream for ChangeStreamListener {
    type Item = Result<AckableEvent, StreamError>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();

        // If closed, return None
        if matches!(this.state, StreamState::Closed) {
            return Poll::Ready(None);
        }

        // If reconnecting, poll the reconnection future
        if let StreamState::Reconnecting(ref mut reconnect_fut) = this.state {
            match reconnect_fut.as_mut().poll(cx) {
                Poll::Ready(Ok(new_stream)) => {
                    // Reconnection succeeded, transition back to Active
                    info!("Reconnection successful, resuming stream");
                    this.stream = Some(new_stream);
                    this.state = StreamState::Active;
                    this.reconnect_attempts = 0;
                    // Wake to poll the stream immediately
                    cx.waker().wake_by_ref();
                    return Poll::Pending;
                }
                Poll::Ready(Err(e)) => {
                    // Reconnection failed permanently
                    error!("Reconnection failed: {}", e);
                    this.state = StreamState::Closed;
                    return Poll::Ready(Some(Err(e)));
                }
                Poll::Pending => {
                    // Still reconnecting
                    return Poll::Pending;
                }
            }
        }

        // Poll the underlying stream
        if let Some(ref mut stream) = this.stream {
            use futures::StreamExt;
            let mut stream_pin = Pin::new(stream);
            match stream_pin.poll_next_unpin(cx) {
                Poll::Ready(Some(Ok(event))) => {
                    // Extract resume token
                    let resume_token = match bson::to_document(&event.id) {
                        Ok(token) => token,
                        Err(e) => {
                            return Poll::Ready(Some(Err(StreamError::ResumeTokenPersistence(
                                e.to_string(),
                            ))))
                        }
                    };

                    // Convert to ChangeEvent
                    let change_event = match ChangeEvent::try_from(event) {
                        Ok(evt) => evt,
                        Err(e) => return Poll::Ready(Some(Err(StreamError::Conversion(e)))),
                    };

                    // Check for invalidation
                    if change_event.is_invalidate() {
                        let reason = format!(
                            "Collection {}.{} was dropped or renamed",
                            this.collection.namespace().db,
                            this.collection.namespace().coll
                        );
                        error!("{}", reason);
                        this.state = StreamState::Closed;
                        return Poll::Ready(Some(Err(StreamError::Invalidated { reason })));
                    }

                    // Create AckableEvent - user must call ack() after processing
                    // Note: last_resume_token is NOT updated here - only when user calls ack()
                    // This ensures at-least-once semantics
                    let ackable = AckableEvent {
                        event: change_event,
                        resume_token,
                        ack_sender: this.ack_sender.clone(),
                        last_resume_token: this.last_resume_token.clone(),
                    };

                    Poll::Ready(Some(Ok(ackable)))
                }
                Poll::Ready(Some(Err(e))) => {
                    // MongoDB error
                    let stream_err = StreamError::from_mongo_error(e);

                    if stream_err.is_retryable() {
                        warn!("Retryable error occurred: {}", stream_err);

                        // Create reconnection future
                        let collection = this.collection.clone();
                        let config = this.config.clone();
                        let last_resume_token =
                            this.last_resume_token.lock().ok().and_then(|t| t.clone());
                        let reconnect_attempts = this.reconnect_attempts;

                        let reconnect_fut = Box::pin(async move {
                            Self::reconnect_async(
                                collection,
                                config,
                                last_resume_token,
                                reconnect_attempts,
                            )
                            .await
                        });

                        this.state = StreamState::Reconnecting(reconnect_fut);
                        // Wake to attempt reconnection on next poll
                        cx.waker().wake_by_ref();
                        Poll::Pending
                    } else {
                        error!("Fatal error occurred: {}", stream_err);
                        this.state = StreamState::Closed;
                        Poll::Ready(Some(Err(stream_err)))
                    }
                }
                Poll::Ready(None) => {
                    // Stream ended (should not happen for change streams)
                    warn!("Change stream ended unexpectedly");
                    this.state = StreamState::Closed;
                    Poll::Ready(None)
                }
                Poll::Pending => Poll::Pending,
            }
        } else {
            // No stream available
            this.state = StreamState::Closed;
            Poll::Ready(None)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio::sync::Mutex;

    #[test]
    fn test_default_config() {
        let config = ChangeStreamConfig::default();
        assert_eq!(config.pipeline.len(), 0);
        assert!(!config.full_document_on_update);
        assert!(!config.full_document_before_change);
        assert_eq!(config.initial_backoff_ms, 100);
        assert_eq!(config.max_backoff_ms, 30_000);
        assert_eq!(config.max_reconnect_attempts, 5);
    }

    #[test]
    fn test_config_builder() {
        let config = ChangeStreamConfig::builder()
            .full_document_update_lookup()
            .full_document_before_change()
            .max_reconnect_attempts(10)
            .initial_backoff_ms(200)
            .max_backoff_ms(60_000)
            .batch_size(100)
            .build()
            .unwrap();

        assert!(config.full_document_on_update);
        assert!(config.full_document_before_change);
        assert_eq!(config.max_reconnect_attempts, 10);
        assert_eq!(config.initial_backoff_ms, 200);
        assert_eq!(config.max_backoff_ms, 60_000);
        assert_eq!(config.batch_size, Some(100));
    }

    #[test]
    fn test_config_with_pipeline() {
        use bson::doc;

        let pipeline = vec![doc! { "$match": { "operationType": "insert" } }];

        let config = ChangeStreamConfig::builder()
            .pipeline(pipeline.clone())
            .build()
            .unwrap();

        assert_eq!(config.pipeline.len(), 1);
        assert_eq!(config.pipeline[0], pipeline[0]);
    }

    #[test]
    fn test_stream_error_is_retryable() {
        // Connection errors with retryable labels are retryable
        let err = StreamError::Connection {
            message: "connection refused".to_string(),
            source: None,
            code: Some(6), // HostUnreachable
            labels: vec![],
        };
        assert!(err.is_retryable());

        // Invalidated is not retryable
        let err = StreamError::Invalidated {
            reason: "collection dropped".to_string(),
        };
        assert!(!err.is_retryable());

        // Max attempts exceeded is not retryable
        let err = StreamError::MaxReconnectAttemptsExceeded(5);
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_stream_error_category() {
        let err = StreamError::Invalidated {
            reason: "test".to_string(),
        };
        assert_eq!(err.category(), "invalidated");

        let err = StreamError::MaxReconnectAttemptsExceeded(5);
        assert_eq!(err.category(), "max_retries");

        let err = StreamError::Configuration("test".to_string());
        assert_eq!(err.category(), "configuration");
    }

    #[tokio::test]
    async fn test_resume_token_callback() {
        use bson::doc;

        let saved_token = Arc::new(Mutex::new(None));
        let saved_token_clone = saved_token.clone();

        let callback = move |token: Document| {
            let saved = saved_token_clone.clone();
            Box::pin(async move {
                *saved.lock().await = Some(token);
                Ok(())
            }) as Pin<Box<dyn Future<Output = Result<(), String>> + Send>>
        };

        let test_token = doc! { "_data": "test123" };
        callback(test_token.clone()).await.unwrap();

        let saved = saved_token.lock().await;
        assert_eq!(*saved, Some(test_token));
    }

    // Note: test_stream_state_transitions removed because StreamState::Reconnecting
    // now contains a Future which doesn't implement PartialEq

    #[test]
    fn test_config_resume_token() {
        use bson::doc;

        let token = doc! { "_data": "token123" };
        let config = ChangeStreamConfig::builder()
            .resume_token(token.clone())
            .build()
            .unwrap();

        assert_eq!(config.resume_token, Some(token));
    }

    // Note: Integration tests with real MongoDB would go in tests/integration_test.rs
    // and would use testcontainers for a real MongoDB instance.
}