sockudo-adapter 4.3.1

Connection adapters and horizontal scaling for Sockudo
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
use crate::connection_manager::ConnectionManager;
use dashmap::DashMap;
use sockudo_core::app::App;
use sockudo_core::error::Result;
use sockudo_core::metrics::MetricsInterface;
use sockudo_core::presence_history::{
    PresenceHistoryEventCause, PresenceHistoryEventKind, PresenceHistoryStore,
    PresenceHistoryTransitionRecord,
};
use sockudo_core::websocket::SocketId;
use sockudo_protocol::messages::PusherMessage;
use sockudo_webhook::WebhookIntegration;
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, error, warn};

/// Lock key for presence operations to prevent TOCTOU races
/// Format: "app_id:channel:user_id"
fn presence_lock_key(app_id: &str, channel: &str, user_id: &str) -> String {
    format!("{}:{}:{}", app_id, channel, user_id)
}

/// Centralized presence channel management functionality
/// This module handles presence member removal logic that needs to be
/// consistent across different disconnect paths (sync, async, direct unsubscribe)
pub struct PresenceManager {
    /// Per-user locks to prevent TOCTOU races during presence operations
    /// Maps "app_id:channel:user_id" -> async mutex for true per-key exclusivity
    presence_locks: DashMap<String, Arc<Mutex<()>>>,
}

impl Default for PresenceManager {
    fn default() -> Self {
        Self::new()
    }
}

impl PresenceManager {
    pub fn new() -> Self {
        Self {
            presence_locks: DashMap::new(),
        }
    }

    fn get_presence_lock(&self, lock_key: &str) -> Arc<Mutex<()>> {
        self.presence_locks
            .entry(lock_key.to_string())
            .or_insert_with(|| Arc::new(Mutex::new(())))
            .clone()
    }

    /// Handles presence member addition including both webhook and broadcast
    /// Only sends events if this is the user's FIRST connection to the presence channel
    ///
    /// FIX: Uses atomic check-and-act pattern to prevent TOCTOU race conditions
    #[allow(clippy::too_many_arguments)]
    pub async fn handle_member_added(
        &self,
        connection_manager: Arc<dyn ConnectionManager + Send + Sync>,
        presence_history_store: Arc<dyn PresenceHistoryStore + Send + Sync>,
        presence_history_enabled: bool,
        webhook_integration: Option<&Arc<WebhookIntegration>>,
        metrics: Option<&Arc<dyn MetricsInterface + Send + Sync>>,
        app_config: &App,
        channel: &str,
        user_id: &str,
        user_info: Option<&sonic_rs::Value>,
        excluding_socket: Option<&SocketId>,
        retention: Option<sockudo_core::presence_history::PresenceHistoryRetentionPolicy>,
    ) -> Result<()> {
        debug!(
            "Processing presence member addition for user {} in channel {} (app: {})",
            user_id, channel, app_config.id
        );

        let lock_key = presence_lock_key(&app_config.id, channel, user_id);

        // FIX: Acquire per-user lock to prevent TOCTOU race between checking connection count
        // and sending member_added events. Without this lock:
        // - Thread A: checks count=0, about to send member_added
        // - Thread B: checks count=0, sends member_added (duplicate!)
        // - Thread A: sends member_added (duplicate!)
        //
        // With lock:
        // - Thread A: acquires lock, checks count=0, sends member_added, releases lock
        // - Thread B: acquires lock, checks count=1, skips member_added, releases lock
        let presence_lock = self.get_presence_lock(&lock_key);
        let _lock_guard = presence_lock.lock().await;

        // Check if user already had connections in this presence channel (excluding current socket)
        // This check is now protected by the lock
        let had_other_connections = Self::user_has_other_connections_in_presence_channel(
            Arc::clone(&connection_manager),
            &app_config.id,
            channel,
            user_id,
            excluding_socket,
        )
        .await?;

        if !had_other_connections {
            debug!(
                "User {} is joining channel {} for the first time, sending member_added events",
                user_id, channel
            );

            // Send member_added webhook
            if let Some(webhook_integration) = webhook_integration
                && let Err(e) = webhook_integration
                    .send_member_added(app_config, channel, user_id)
                    .await
            {
                warn!(
                    "Failed to send member_added webhook for user {} in channel {}: {}",
                    user_id, channel, e
                );
            }

            // Broadcast member_added event to existing clients in the channel
            let member_added_msg = sockudo_protocol::messages::PusherMessage::member_added(
                channel.to_string(),
                user_id.to_string(),
                user_info.cloned(),
            );
            Self::broadcast_to_channel(
                Arc::clone(&connection_manager),
                &app_config.id,
                channel,
                member_added_msg,
                excluding_socket,
            )
            .await?;

            let _ = Self::broadcast_to_channel(
                Arc::clone(&connection_manager),
                &app_config.id,
                &format!("[meta]{channel}"),
                sockudo_protocol::messages::PusherMessage {
                    event: Some("sockudo_internal:member_added".to_string()),
                    channel: Some(format!("[meta]{channel}")),
                    data: Some(sockudo_protocol::messages::MessageData::Json(
                        sonic_rs::json!({
                            "channel": channel,
                            "user_id": user_id,
                        }),
                    )),
                    name: None,
                    user_id: None,
                    tags: None,
                    sequence: None,
                    conflation_key: None,
                    message_id: None,
                    stream_id: None,
                    serial: None,
                    idempotency_key: None,
                    extras: None,
                    delta_sequence: None,
                    delta_conflation_key: None,
                },
                None,
            )
            .await;

            debug!(
                "Successfully processed member_added for user {} in channel {}",
                user_id, channel
            );

            if presence_history_enabled {
                self.record_presence_transition(
                    presence_history_store,
                    metrics,
                    PresenceHistoryTransitionRecord {
                        app_id: app_config.id.clone(),
                        channel: channel.to_string(),
                        event_kind: PresenceHistoryEventKind::MemberAdded,
                        cause: PresenceHistoryEventCause::Join,
                        user_id: user_id.to_string(),
                        connection_id: excluding_socket.map(ToString::to_string),
                        user_info: user_info.cloned(),
                        dead_node_id: None,
                        dedupe_key: format!(
                            "member_added:{}:{}:{}:{}",
                            app_config.id,
                            channel,
                            user_id,
                            excluding_socket
                                .map(ToString::to_string)
                                .unwrap_or_else(|| "_".to_string())
                        ),
                        published_at_ms: sockudo_core::history::now_ms(),
                        retention: retention.clone().unwrap_or(
                            sockudo_core::presence_history::PresenceHistoryRetentionPolicy {
                                retention_window_seconds: 3600,
                                max_events_per_channel: None,
                                max_bytes_per_channel: None,
                            },
                        ),
                    },
                )
                .await;
            }
        } else {
            debug!(
                "User {} already has {} connections in channel {}, skipping member_added events",
                user_id,
                if had_other_connections { "other" } else { "no" },
                channel
            );
        }

        // Lock is released here when _lock_guard goes out of scope

        // Always broadcast presence join to all nodes for cluster replication (for every connection)
        // This is done OUTSIDE the lock to avoid holding it during network I/O
        if let Some(excluding_socket) = excluding_socket
            && let Some(horizontal_adapter) = connection_manager.as_horizontal_adapter()
        {
            horizontal_adapter
                .broadcast_presence_join(
                    &app_config.id,
                    channel,
                    user_id,
                    &excluding_socket.to_string(),
                    user_info.cloned(),
                )
                .await
                .map_err(|e| {
                    error!("Failed to broadcast presence join: {}", e);
                    e
                })
                .ok(); // Log but don't fail the operation
        }

        // Opportunistically cap lock-map growth on high-cardinality presence churn.
        self.cleanup_stale_locks();

        Ok(())
    }

    /// Handles presence member removal including both webhook and broadcast
    /// This centralizes the logic that was duplicated across sync cleanup,
    /// async cleanup, and direct unsubscribe paths
    ///
    /// FIX: Uses atomic check-and-act pattern to prevent TOCTOU race conditions
    #[allow(clippy::too_many_arguments)]
    pub async fn handle_member_removed(
        &self,
        connection_manager: &Arc<dyn ConnectionManager + Send + Sync>,
        presence_history_store: Arc<dyn PresenceHistoryStore + Send + Sync>,
        presence_history_enabled: bool,
        webhook_integration: Option<&Arc<WebhookIntegration>>,
        metrics: Option<&Arc<dyn MetricsInterface + Send + Sync>>,
        app_config: &App,
        channel: &str,
        user_id: &str,
        excluding_socket: Option<&SocketId>,
        cause: PresenceHistoryEventCause,
        dead_node_id: Option<&str>,
        retention: Option<sockudo_core::presence_history::PresenceHistoryRetentionPolicy>,
    ) -> Result<()> {
        debug!(
            "Processing presence member removal for user {} in channel {} (app: {})",
            user_id, channel, app_config.id
        );

        let lock_key = presence_lock_key(&app_config.id, channel, user_id);

        // FIX: Acquire per-user lock to prevent TOCTOU race between checking connection count
        // and sending member_removed events. Without this lock:
        // - Thread A (disconnect socket 1): checks count=1, about to send member_removed
        // - Thread B (disconnect socket 2): checks count=1, sends member_removed
        // - Thread A: sends member_removed (duplicate! user still has socket 2... wait, no they don't)
        //
        // The real race is:
        // - Thread A (disconnect): checks count=1 (only this socket), prepares member_removed
        // - Thread B (connect): checks count=1 (sees socket being disconnected), skips member_added
        // - Thread A: sends member_removed
        // Result: User is connected but appears removed!
        //
        // With lock:
        // - Operations are serialized per user+channel, ensuring consistent state
        let presence_lock = self.get_presence_lock(&lock_key);
        let _lock_guard = presence_lock.lock().await;

        // Check if user has other connections in this presence channel
        // This check is now protected by the lock
        let has_other_connections = Self::user_has_other_connections_in_presence_channel(
            Arc::clone(connection_manager),
            &app_config.id,
            channel,
            user_id,
            excluding_socket,
        )
        .await?;

        if !has_other_connections {
            debug!(
                "User {} has no other connections in channel {}, sending member_removed events",
                user_id, channel
            );

            // Send member_removed webhook with 3-second delay
            // Per Pusher spec: delay prevents spurious webhooks from momentary disconnects.
            // If the user reconnects within the delay, no webhook is sent.
            if let Some(webhook_integration) = webhook_integration {
                let wi = Arc::clone(webhook_integration);
                let cm = Arc::clone(connection_manager);
                let app = app_config.clone();
                let ch = channel.to_string();
                let uid = user_id.to_string();
                tokio::spawn(async move {
                    tokio::time::sleep(std::time::Duration::from_secs(3)).await;
                    let still_gone = match Self::user_has_other_connections_in_presence_channel(
                        cm, &app.id, &ch, &uid, None,
                    )
                    .await
                    {
                        Ok(has_connections) => !has_connections,
                        Err(_) => true,
                    };
                    if still_gone && let Err(e) = wi.send_member_removed(&app, &ch, &uid).await {
                        tracing::warn!(
                            "Failed to send member_removed webhook for user {} in channel {}: {}",
                            uid,
                            ch,
                            e
                        );
                    }
                });
            }

            // Broadcast member_removed event to remaining clients in the channel
            let member_removed_msg =
                PusherMessage::member_removed(channel.to_string(), user_id.to_string());
            Self::broadcast_to_channel(
                Arc::clone(connection_manager),
                &app_config.id,
                channel,
                member_removed_msg,
                excluding_socket,
            )
            .await?;

            let _ = Self::broadcast_to_channel(
                Arc::clone(connection_manager),
                &app_config.id,
                &format!("[meta]{channel}"),
                PusherMessage {
                    event: Some("sockudo_internal:member_removed".to_string()),
                    channel: Some(format!("[meta]{channel}")),
                    data: Some(sockudo_protocol::messages::MessageData::Json(
                        sonic_rs::json!({
                            "channel": channel,
                            "user_id": user_id,
                        }),
                    )),
                    name: None,
                    user_id: None,
                    tags: None,
                    sequence: None,
                    conflation_key: None,
                    message_id: None,
                    stream_id: None,
                    serial: None,
                    idempotency_key: None,
                    extras: None,
                    delta_sequence: None,
                    delta_conflation_key: None,
                },
                None,
            )
            .await;

            debug!(
                "Successfully processed member_removed for user {} in channel {}",
                user_id, channel
            );

            if presence_history_enabled {
                self.record_presence_transition(
                    presence_history_store,
                    metrics,
                    PresenceHistoryTransitionRecord {
                        app_id: app_config.id.clone(),
                        channel: channel.to_string(),
                        event_kind: PresenceHistoryEventKind::MemberRemoved,
                        cause,
                        user_id: user_id.to_string(),
                        connection_id: excluding_socket.map(ToString::to_string),
                        user_info: None,
                        dead_node_id: dead_node_id.map(ToString::to_string),
                        dedupe_key: format!(
                            "member_removed:{}:{}:{}:{}:{}",
                            app_config.id,
                            channel,
                            user_id,
                            excluding_socket
                                .map(ToString::to_string)
                                .unwrap_or_else(|| "_".to_string()),
                            dead_node_id.unwrap_or("_")
                        ),
                        published_at_ms: sockudo_core::history::now_ms(),
                        retention: retention.clone().unwrap_or(
                            sockudo_core::presence_history::PresenceHistoryRetentionPolicy {
                                retention_window_seconds: 3600,
                                max_events_per_channel: None,
                                max_bytes_per_channel: None,
                            },
                        ),
                    },
                )
                .await;
            }
        } else {
            debug!(
                "User {} has other connections in channel {}, skipping member_removed events",
                user_id, channel
            );
        }

        // Lock is released here when _lock_guard goes out of scope

        // Always broadcast presence leave to all nodes for cluster replication (for every disconnection)
        // This is done OUTSIDE the lock to avoid holding it during network I/O
        if let Some(excluding_socket) = excluding_socket
            && let Some(horizontal_adapter) = connection_manager.as_horizontal_adapter()
        {
            horizontal_adapter
                .broadcast_presence_leave(
                    &app_config.id,
                    channel,
                    user_id,
                    &excluding_socket.to_string(),
                )
                .await
                .map_err(|e| {
                    error!("Failed to broadcast presence leave: {}", e);
                    e
                })
                .ok(); // Log but don't fail the operation
        }

        // Opportunistically cap lock-map growth on high-cardinality presence churn.
        self.cleanup_stale_locks();

        Ok(())
    }

    /// Cleanup stale lock entries periodically
    /// Call this from a background task to prevent unbounded growth of the locks map
    pub fn cleanup_stale_locks(&self) {
        if self.presence_locks.len() > 100_000 {
            let stale_keys: Vec<String> = self
                .presence_locks
                .iter()
                .filter_map(|entry| {
                    let lock = entry.value();
                    let in_use = Arc::strong_count(lock) > 1 || lock.try_lock().is_err();
                    if in_use {
                        None
                    } else {
                        Some(entry.key().clone())
                    }
                })
                .collect();

            warn!(
                "Presence locks map has {} entries, removing {} stale entries",
                self.presence_locks.len(),
                stale_keys.len()
            );

            for key in stale_keys {
                self.presence_locks.remove(&key);
            }
        }
    }

    /// Check if a user has other connections in a presence channel
    /// Uses the same logic as the original working implementation:
    /// Get all user's sockets and check if any are still subscribed to this channel (excluding specified socket)
    async fn user_has_other_connections_in_presence_channel(
        connection_manager: Arc<dyn ConnectionManager + Send + Sync>,
        app_id: &str,
        channel: &str,
        user_id: &str,
        excluding_socket: Option<&SocketId>,
    ) -> Result<bool> {
        // Use cluster-wide connection check for multi-node support
        let subscribed_count = connection_manager
            .count_user_connections_in_channel(user_id, app_id, channel, excluding_socket)
            .await?;

        let has_other_connections = subscribed_count > 0;

        Ok(has_other_connections)
    }

    /// Broadcast a message to all clients in a channel, optionally excluding one socket
    async fn broadcast_to_channel(
        connection_manager: Arc<dyn ConnectionManager + Send + Sync>,
        app_id: &str,
        channel: &str,
        message: PusherMessage,
        excluding_socket: Option<&SocketId>,
    ) -> Result<()> {
        connection_manager
            .send(channel, message, excluding_socket, app_id, None)
            .await
    }

    async fn record_presence_transition(
        &self,
        presence_history_store: Arc<dyn PresenceHistoryStore + Send + Sync>,
        metrics: Option<&Arc<dyn MetricsInterface + Send + Sync>>,
        record: PresenceHistoryTransitionRecord,
    ) {
        if let Err(error) = presence_history_store
            .record_transition(record.clone())
            .await
        {
            warn!(
                "Failed to record presence history transition for user {} in channel {}: {}",
                record.user_id, record.channel, error
            );
            if let Some(metrics) = metrics {
                metrics.mark_presence_history_write_failure(&record.app_id);
            }
        }
    }
}

/// Static helper functions for backward compatibility with code that doesn't have
/// access to a PresenceManager instance. These use a global lock map.
///
/// NOTE: Prefer using the instance methods when possible for better testability
/// and to allow cleanup of stale locks.
mod static_helpers {
    use super::*;
    use std::sync::LazyLock;

    /// Global presence manager for static helper functions
    static GLOBAL_PRESENCE_MANAGER: LazyLock<PresenceManager> = LazyLock::new(PresenceManager::new);

    /// Get the global presence manager instance
    pub fn global() -> &'static PresenceManager {
        &GLOBAL_PRESENCE_MANAGER
    }
}

/// Re-export static helper access for backward compatibility
pub fn global_presence_manager() -> &'static PresenceManager {
    static_helpers::global()
}

#[cfg(test)]
mod tests {
    use super::*;
    use ahash::AHashMap;
    use async_trait::async_trait;
    use sockudo_core::app::{App, AppPolicy};
    use sockudo_core::channel::PresenceMemberInfo;
    use sockudo_core::namespace::Namespace;
    use sockudo_core::presence_history::{
        MemoryPresenceHistoryStore, PresenceHistoryDirection, PresenceHistoryEventCause,
        PresenceHistoryEventKind, PresenceHistoryReadRequest, PresenceHistoryRetentionPolicy,
    };
    use sockudo_protocol::WireFormat;
    use sockudo_ws::axum_integration::WebSocketWriter;
    use std::any::Any;
    use std::collections::VecDeque;
    use std::sync::Mutex as StdMutex;

    #[test]
    fn test_presence_lock_key_format() {
        let key = presence_lock_key("app123", "presence-room", "user456");
        assert_eq!(key, "app123:presence-room:user456");
    }

    #[test]
    fn test_presence_manager_creation() {
        let manager = PresenceManager::new();
        assert_eq!(manager.presence_locks.len(), 0);
    }

    #[test]
    fn test_cleanup_stale_locks_threshold() {
        let manager = PresenceManager::new();

        // Add entries below threshold
        for i in 0..100 {
            manager
                .presence_locks
                .insert(format!("key_{}", i), Arc::new(Mutex::new(())));
        }

        manager.cleanup_stale_locks();
        // Should not clear because below 100_000 threshold
        assert_eq!(manager.presence_locks.len(), 100);
    }

    struct ScriptedConnectionManager {
        counts: StdMutex<VecDeque<usize>>,
    }

    impl ScriptedConnectionManager {
        fn new(counts: Vec<usize>) -> Self {
            Self {
                counts: StdMutex::new(counts.into()),
            }
        }
    }

    #[async_trait]
    impl ConnectionManager for ScriptedConnectionManager {
        async fn init(&self) {}
        async fn get_namespace(&self, _app_id: &str) -> Option<Arc<Namespace>> {
            None
        }
        async fn add_socket(
            &self,
            _socket_id: SocketId,
            _socket: WebSocketWriter,
            _app_id: &str,
            _app_manager: Arc<dyn sockudo_core::app::AppManager + Send + Sync>,
            _buffer_config: sockudo_core::websocket::WebSocketBufferConfig,
            _protocol_version: sockudo_protocol::ProtocolVersion,
            _wire_format: WireFormat,
            _echo_messages: bool,
        ) -> Result<()> {
            Ok(())
        }
        async fn get_connection(
            &self,
            _socket_id: &SocketId,
            _app_id: &str,
        ) -> Option<sockudo_core::websocket::WebSocketRef> {
            None
        }
        async fn remove_connection(&self, _socket_id: &SocketId, _app_id: &str) -> Result<()> {
            Ok(())
        }
        async fn send_message(
            &self,
            _app_id: &str,
            _socket_id: &SocketId,
            _message: PusherMessage,
        ) -> Result<()> {
            Ok(())
        }
        async fn send(
            &self,
            _channel: &str,
            _message: PusherMessage,
            _except: Option<&SocketId>,
            _app_id: &str,
            _start_time_ms: Option<f64>,
        ) -> Result<()> {
            Ok(())
        }
        async fn get_channel_members(
            &self,
            _app_id: &str,
            _channel: &str,
        ) -> Result<AHashMap<String, PresenceMemberInfo>> {
            Ok(AHashMap::new())
        }
        async fn get_channel_sockets(
            &self,
            _app_id: &str,
            _channel: &str,
        ) -> Result<Vec<SocketId>> {
            Ok(Vec::new())
        }
        async fn remove_channel(&self, _app_id: &str, _channel: &str) {}
        async fn is_in_channel(
            &self,
            _app_id: &str,
            _channel: &str,
            _socket_id: &SocketId,
        ) -> Result<bool> {
            Ok(false)
        }
        async fn get_user_sockets(
            &self,
            _user_id: &str,
            _app_id: &str,
        ) -> Result<Vec<sockudo_core::websocket::WebSocketRef>> {
            Ok(Vec::new())
        }
        async fn cleanup_connection(
            &self,
            _app_id: &str,
            _ws: sockudo_core::websocket::WebSocketRef,
        ) {
        }
        async fn terminate_connection(&self, _app_id: &str, _user_id: &str) -> Result<()> {
            Ok(())
        }
        async fn add_channel_to_sockets(
            &self,
            _app_id: &str,
            _channel: &str,
            _socket_id: &SocketId,
        ) {
        }
        async fn get_channel_socket_count_info(
            &self,
            _app_id: &str,
            _channel: &str,
        ) -> crate::connection_manager::ChannelSocketCount {
            crate::connection_manager::ChannelSocketCount {
                count: 0,
                complete: true,
            }
        }
        async fn get_channel_socket_count(&self, _app_id: &str, _channel: &str) -> usize {
            0
        }
        async fn add_to_channel(
            &self,
            _app_id: &str,
            _channel: &str,
            _socket_id: &SocketId,
        ) -> Result<bool> {
            Ok(false)
        }
        async fn remove_from_channel(
            &self,
            _app_id: &str,
            _channel: &str,
            _socket_id: &SocketId,
        ) -> Result<bool> {
            Ok(false)
        }
        async fn get_presence_member(
            &self,
            _app_id: &str,
            _channel: &str,
            _socket_id: &SocketId,
        ) -> Option<PresenceMemberInfo> {
            None
        }
        async fn terminate_user_connections(&self, _app_id: &str, _user_id: &str) -> Result<()> {
            Ok(())
        }
        async fn add_user(&self, _ws: sockudo_core::websocket::WebSocketRef) -> Result<()> {
            Ok(())
        }
        async fn remove_user(&self, _ws: sockudo_core::websocket::WebSocketRef) -> Result<()> {
            Ok(())
        }
        async fn remove_user_socket(
            &self,
            _user_id: &str,
            _socket_id: &SocketId,
            _app_id: &str,
        ) -> Result<()> {
            Ok(())
        }
        async fn count_user_connections_in_channel(
            &self,
            _user_id: &str,
            _app_id: &str,
            _channel: &str,
            _excluding_socket: Option<&SocketId>,
        ) -> Result<usize> {
            Ok(self.counts.lock().unwrap().pop_front().unwrap_or_default())
        }
        async fn get_channels_with_socket_count(
            &self,
            _app_id: &str,
        ) -> Result<AHashMap<String, usize>> {
            Ok(AHashMap::new())
        }
        async fn get_sockets_count(&self, _app_id: &str) -> Result<usize> {
            Ok(0)
        }
        async fn get_namespaces(&self) -> Result<Vec<(String, Arc<Namespace>)>> {
            Ok(Vec::new())
        }
        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
        async fn check_health(&self) -> Result<()> {
            Ok(())
        }
        fn get_node_id(&self) -> String {
            "test-node".to_string()
        }
        fn as_horizontal_adapter(
            &self,
        ) -> Option<&dyn crate::connection_manager::HorizontalAdapterInterface> {
            None
        }
    }

    fn test_app() -> App {
        App::from_policy(
            "app-1".to_string(),
            "key".to_string(),
            "secret".to_string(),
            true,
            AppPolicy::default(),
        )
    }

    fn default_retention() -> PresenceHistoryRetentionPolicy {
        PresenceHistoryRetentionPolicy {
            retention_window_seconds: 3600,
            max_events_per_channel: None,
            max_bytes_per_channel: None,
        }
    }

    #[tokio::test]
    async fn presence_manager_records_authoritative_join_and_leave_history() {
        let manager = PresenceManager::new();
        let connection_manager: Arc<dyn ConnectionManager + Send + Sync> =
            Arc::new(ScriptedConnectionManager::new(vec![0, 0]));
        let store = Arc::new(MemoryPresenceHistoryStore::new(Default::default()));
        let app = test_app();
        let socket = SocketId::new();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket),
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_removed(
                &connection_manager,
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                Some(&socket),
                PresenceHistoryEventCause::Disconnect,
                None,
                Some(default_retention()),
            )
            .await
            .unwrap();

        let page = store
            .read_page(PresenceHistoryReadRequest {
                app_id: app.id.clone(),
                channel: "presence-room".to_string(),
                direction: PresenceHistoryDirection::OldestFirst,
                limit: 10,
                cursor: None,
                bounds: Default::default(),
            })
            .await
            .unwrap();

        assert_eq!(page.items.len(), 2);
        assert_eq!(page.items[0].event, PresenceHistoryEventKind::MemberAdded);
        assert_eq!(page.items[1].event, PresenceHistoryEventKind::MemberRemoved);
        assert_eq!(page.items[1].cause, PresenceHistoryEventCause::Disconnect);
    }

    #[tokio::test]
    async fn presence_manager_skips_non_authoritative_socket_churn_history() {
        let manager = PresenceManager::new();
        let connection_manager: Arc<dyn ConnectionManager + Send + Sync> =
            Arc::new(ScriptedConnectionManager::new(vec![0, 1, 1, 0]));
        let store = Arc::new(MemoryPresenceHistoryStore::new(Default::default()));
        let app = test_app();
        let socket_a = SocketId::new();
        let socket_b = SocketId::new();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket_a),
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket_b),
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_removed(
                &connection_manager,
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                Some(&socket_a),
                PresenceHistoryEventCause::Disconnect,
                None,
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_removed(
                &connection_manager,
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                Some(&socket_b),
                PresenceHistoryEventCause::Disconnect,
                None,
                Some(default_retention()),
            )
            .await
            .unwrap();

        let page = store
            .read_page(PresenceHistoryReadRequest {
                app_id: app.id.clone(),
                channel: "presence-room".to_string(),
                direction: PresenceHistoryDirection::OldestFirst,
                limit: 10,
                cursor: None,
                bounds: Default::default(),
            })
            .await
            .unwrap();

        assert_eq!(page.items.len(), 2);
        assert_eq!(page.items[0].event, PresenceHistoryEventKind::MemberAdded);
        assert_eq!(page.items[1].event, PresenceHistoryEventKind::MemberRemoved);
    }

    #[tokio::test]
    async fn presence_manager_suppresses_duplicate_authoritative_join_reports() {
        let manager = PresenceManager::new();
        let connection_manager: Arc<dyn ConnectionManager + Send + Sync> =
            Arc::new(ScriptedConnectionManager::new(vec![0, 0]));
        let store = Arc::new(MemoryPresenceHistoryStore::new(Default::default()));
        let app = test_app();
        let socket_a = SocketId::new();
        let socket_b = SocketId::new();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket_a),
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket_b),
                Some(default_retention()),
            )
            .await
            .unwrap();

        let page = store
            .read_page(PresenceHistoryReadRequest {
                app_id: app.id.clone(),
                channel: "presence-room".to_string(),
                direction: PresenceHistoryDirection::OldestFirst,
                limit: 10,
                cursor: None,
                bounds: Default::default(),
            })
            .await
            .unwrap();

        assert_eq!(page.items.len(), 1);
        assert_eq!(page.items[0].event, PresenceHistoryEventKind::MemberAdded);
    }

    #[tokio::test]
    async fn presence_manager_suppresses_duplicate_authoritative_remove_reports() {
        let manager = PresenceManager::new();
        let connection_manager: Arc<dyn ConnectionManager + Send + Sync> =
            Arc::new(ScriptedConnectionManager::new(vec![0, 0, 0]));
        let store = Arc::new(MemoryPresenceHistoryStore::new(Default::default()));
        let app = test_app();
        let socket = SocketId::new();

        manager
            .handle_member_added(
                Arc::clone(&connection_manager),
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                Some(&socket),
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_removed(
                &connection_manager,
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                Some(&socket),
                PresenceHistoryEventCause::Disconnect,
                None,
                Some(default_retention()),
            )
            .await
            .unwrap();

        manager
            .handle_member_removed(
                &connection_manager,
                store.clone(),
                true,
                None,
                None,
                &app,
                "presence-room",
                "user-1",
                None,
                PresenceHistoryEventCause::OrphanCleanup,
                Some("dead-node"),
                Some(default_retention()),
            )
            .await
            .unwrap();

        let page = store
            .read_page(PresenceHistoryReadRequest {
                app_id: app.id.clone(),
                channel: "presence-room".to_string(),
                direction: PresenceHistoryDirection::OldestFirst,
                limit: 10,
                cursor: None,
                bounds: Default::default(),
            })
            .await
            .unwrap();

        assert_eq!(page.items.len(), 2);
        assert_eq!(page.items[0].event, PresenceHistoryEventKind::MemberAdded);
        assert_eq!(page.items[1].event, PresenceHistoryEventKind::MemberRemoved);
        assert_eq!(page.items[1].cause, PresenceHistoryEventCause::Disconnect);
    }
}