tangram_core_rs 0.2.1

A framework for real-time analysis of ADS-B and Mode S surveillance data
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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
use super::utils::decode_jwt;
use super::{listen_to_redis, Channel, ChannelControl, ChannelError, ChannelMessage};
use futures::SinkExt;
use futures::StreamExt;
use itertools::Itertools;
use redis::aio::MultiplexedConnection;
use redis::AsyncCommands;
use redis::RedisResult;
use serde::{Deserialize, Serialize};
use serde_json::json;
use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use std::collections::HashMap;
use std::fmt;
use std::fmt::{Display, Error};
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, instrument, warn, Instrument};

/// reply data structures
#[derive(Clone, Debug, Serialize_tuple)]
pub struct ServerMessage {
    pub join_ref: Option<String>, // null when it's heartbeat, or initialized from server
    pub event_ref: String,
    pub topic: String, // `channel`
    pub event: String,
    pub payload: ServerPayload,
}

#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum ServerPayload {
    ServerResponse(ServerResponse),
    ServerJsonValue(serde_json::Value),
}

#[derive(Clone, Debug, Serialize)]
pub struct ServerResponse {
    pub status: String,
    pub response: Response,
}

/// Deserialized from websocket
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Response {
    #[serde(rename = "join")]
    Join { id: String },

    #[serde(rename = "heartbeat")]
    Heartbeat {},

    #[serde(rename = "datetime")]
    Datetime { datetime: String, counter: u32 },

    #[serde(rename = "message")]
    Message { message: String },

    #[serde(rename = "null")]
    Empty {},
}

impl fmt::Display for ServerMessage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Format the response based on its variant
        // let response_str = match &self.payload.response {
        //     Response::Empty {} => "Empty".to_string(),
        //     Response::Join {} => "Join".to_string(),
        //     Response::Heartbeat {} => "Heartbeat".to_string(),
        //     Response::Datetime { datetime, counter } => {
        //         format!("<Datetime '{}' {}>", datetime, counter)
        //     }
        //     Response::Message { message } => format!("{{message: {}}}", message),
        // };
        let join_ref = self.join_ref.clone().unwrap_or("None".to_string());

        let response_str = "...";
        let payload_display = match self.payload {
            ServerPayload::ServerResponse(ref resp) => format!(
                "<Payload status={}, response={}>",
                resp.status, response_str
            ),
            ServerPayload::ServerJsonValue(ref value) => format!("<ServerJsonResponse {}>", value),
        };
        write!(
            f,
            "Message join_ref={}, ref={}, topic={}, event={}, {}",
            join_ref, self.event_ref, self.topic, self.event, payload_display
        )
    }
}

// request data structures
// RequestMessage is a message from client through websocket
// it's deserialized from a JSON array
#[derive(Debug, Deserialize_tuple)]
struct RequestMessage {
    join_ref: Option<String>, // null when it's heartbeat
    event_ref: String,
    topic: String, // `channel`
    event: String,
    payload: RequestPayload,
}

impl Display for RequestMessage {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), Error> {
        write!(
            formatter,
            "<RequestMessage: join_ref={:?}, ref={}, topic={}, event={}, payload=...>",
            self.join_ref, self.event_ref, self.topic, self.event
        )
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
enum RequestPayload {
    Join { token: String },
    Message { message: String },
    JsonValue(serde_json::Value), // This allows any JSON data to be submitted
}

pub struct State {
    pub ctl: Mutex<ChannelControl>,
    pub redis_client: redis::Client,
    pub id_length: u8,
    pub jwt_secret: String,
    pub jwt_expiration_secs: i64,
}

impl State {}

#[instrument(skip(ws, state, user_token))]
pub async fn axum_on_connected(
    ws: axum::extract::ws::WebSocket,
    state: Arc<State>,
    user_token: Option<String>,
) {
    info!("params: {:?}", user_token);

    let conn_id = nanoid::nanoid!(8).to_string();
    state.ctl.lock().await.conn_add_tx(conn_id.clone()).await;
    info!("new connection connected: {}", conn_id);

    let (mut ws_tx, mut ws_rx) = ws.split();

    // conn rx => ws tx
    let ws_tx_state = state.clone();
    let ws_tx_conn_id = conn_id.clone();
    let span_tx = tracing::info_span!("ws_tx");
    let mut ws_tx_task = tokio::spawn(
        async move {
            info!("launch websocket tx task (conn rx => ws tx) ...");

            let mut conn_rx = ws_tx_state
                .ctl
                .lock()
                .await
                .conn_rx(ws_tx_conn_id.clone())
                .await
                .unwrap();
            loop {
                match conn_rx.recv().await {
                    Ok(channel_message) => {
                        let ChannelMessage::Reply(reply_message) = channel_message;
                        let text_result = serde_json::to_string(&reply_message);
                        if text_result.is_err() {
                            error!(
                                error = %text_result.err().unwrap(),
                                "fail to serialize reply message"
                            );
                            break;
                        }
                        let text = text_result.unwrap();
                        let sending_result = ws_tx
                            .send(axum::extract::ws::Message::Text(text.into()))
                            .await;
                        if sending_result.is_err() {
                            error!(
                                error = %sending_result.err().unwrap(),
                                "websocket tx sending failed"
                            );
                            break; // what happend? exit if the connection is lost
                        }
                    }
                    Err(e) => {
                        error!(error = ?e, "rx error");
                        break;
                    }
                }
            }
        }
        .instrument(span_tx),
    );

    let ws_rx_state = state.clone();
    let ws_rx_conn_id = conn_id.clone();
    let ws_rx_user_token = user_token.clone();
    let span_rx = tracing::info_span!("ws_rx");
    let mut ws_rx_task = tokio::spawn(
        async move {
            info!("websocket rx handling (ws rx =>) ...");
            let mut redis_conn = ws_rx_state
                .redis_client
                .get_multiplexed_async_connection()
                .await
                .unwrap();

            // Read all messages from websocket rx, process or dispatch to each channel
            while let Some(msg_result) = ws_rx.next().await {
                match msg_result {
                    Ok(msg) => match msg {
                        axum::extract::ws::Message::Text(text) => {
                            if let Err(e) = handle_message(
                                ws_rx_state.clone(),
                                ws_rx_user_token.clone(),
                                &ws_rx_conn_id,
                                &text,
                                &mut redis_conn,
                            )
                            .await
                            {
                                error!(error = ?e, "handle_message error");
                            }
                        }
                        axum::extract::ws::Message::Close(_) => {
                            debug!("close frame received");
                            break;
                        }
                        // TODO: handle ping/pong/binary (e.g. arrow recordbatches)
                        _ => {}
                    },
                    Err(e) => {
                        error!(error = ?e, "rx error");
                        break;
                    }
                }
            }
        }
        .instrument(span_rx),
    );

    // Wait for either task to finish: when one ends, always wait for the other
    tokio::select! {
        _ = (&mut ws_tx_task) => {
            info!("ws_tx_task exits.");

            ws_rx_task.abort();
            info!("ws_rx_task aborts.");
        },
        _ = (&mut ws_rx_task) => {
            info!("ws_rx_task exits.");

            ws_tx_task.abort();
            info!("ws_tx_task aborts.");
        },
    }

    state.ctl.lock().await.conn_cleanup(conn_id.clone()).await;
    info!("connection closed");
    // Except for phoenix/admin/system, if this is the last agent of the channel, clean up channel related resources
}

#[instrument(skip(state, user_token, redis_conn, text), fields(conn_id = %conn_id))]
async fn handle_message(
    state: Arc<State>,
    user_token: Option<String>,
    conn_id: &str,
    text: &str,
    redis_conn: &mut redis::aio::MultiplexedConnection,
) -> RedisResult<()> {
    let rm_result = serde_json::from_str::<RequestMessage>(text);
    if rm_result.is_err() {
        error!(
            error = ?rm_result.err(),
            %text,
            "deserialization error"
        );
        // Clean up all agents for conn_id
        // state.ctl.lock().await.agent_rm(conn_id).await;
        return Ok(());
    }
    let rm: RequestMessage = rm_result.unwrap();
    let channel_name = &rm.topic;
    let join_ref = &rm.join_ref;
    let event_ref = &rm.event_ref;
    let event = &rm.event;
    let payload = &rm.payload;

    if channel_name == "phoenix" && event == "heartbeat" {
        // it continues to publish events to the Redis
        ok_reply(conn_id, None, event_ref, "phoenix", state.clone()).await;

        // payload is {}, so add some info here
        let message = format!(r#"{{"conn_id": "{}"}}"#, conn_id); // double {{ and }} to escape
        publish_event(redis_conn, "from:phoenix:heartbeat".to_string(), message).await;
        // continue;
    }

    if event == "phx_join" {
        // If there is no channel, create one

        // Start a new relay task (agent rx => conn tx), needs to be cleared when agent leaves
        // Each join in the connection will generate this task; when the connection is broken, it will exit automatically
        let _relay_task = handle_join(user_token, &rm, state.clone(), conn_id).await;
        debug!("join processed");
        // continue;
    }

    if event == "phx_leave" {
        handle_leave(
            state.clone(),
            conn_id,
            join_ref.clone(),
            event_ref,
            channel_name.clone(),
        )
        .await;
        debug!("leave processed");
    }

    // all events are dispatched to redis
    // iredis --url redis://localhost:6379 psubscribe 'from*'
    let redis_topic = format!("from:{}:{}", channel_name, event.clone());
    let message = serde_json::to_string(&payload).unwrap();
    publish_event(redis_conn, redis_topic, message).await;
    Ok(())
}

// iredis --url redis://localhost:6379 psubscribe 'from*'
async fn publish_event(
    redis_conn: &mut redis::aio::MultiplexedConnection,
    redis_topic: String,
    message: String,
) {
    let publish_result: RedisResult<String> = redis_conn
        .publish(redis_topic.clone(), message.clone())
        .await;
    if let Err(e) = publish_result {
        error!("fail to publish to redis: {}", e)
    }
}

pub fn is_special_channel(ch: &str) -> bool {
    let excludes: Vec<&str> = vec!["phoenix", "admin", "system"];
    excludes.contains(&ch)
}

#[instrument(skip(ctl))]
pub async fn add_channel(ctl: &Mutex<ChannelControl>, channel_name: String) {
    let ctl = ctl.lock().await;

    let mut channels = ctl.channels.lock().await;
    let channel_exists = channels.contains_key(&channel_name);
    if channel_exists {
        warn!("channel already exists");
    }

    channels
        .entry(channel_name.clone())
        .or_insert_with(|| Channel::new(channel_name.clone(), None));
    warn!("channel added");

    let channel_names = channels.keys().cloned().collect::<Vec<String>>();
    info!(
        channels_count = channel_names.len(),
        ?channel_names,
        "channel created"
    );

    let meta = json!({
        "channel": channel_name,
        "channels": channels.keys().cloned().collect::<Vec<String>>(),
    });
    ctl.pub_meta_event("channe".into(), "add".into(), meta)
        .await;
}

/// launch a tokio thread to listen to redis topic
/// For each channel, when the first agent connects, this thread is created, and when the last one leaves, it is destroyed
/// phoenix, system, admin: these 3 are created directly and always exist
#[instrument(skip(state, ctl, redis_client))]
pub async fn launch_channel_redis_listen_task(
    state: Arc<State>,
    ctl: &Mutex<ChannelControl>,
    channel_name: String,
    redis_client: redis::Client,
) {
    let ctl = ctl.lock().await;
    let mut channels = ctl.channels.lock().await;
    let channel: &mut Channel = channels.get_mut(&channel_name).unwrap();
    if channel.redis_listen_task.is_some() {
        warn!("redis_listen_task already exists");
        return;
    }
    channel.redis_listen_task = Some(tokio::spawn(listen_to_redis(
        state,
        channel.tx.clone(),
        redis_client,
        channel_name.clone(),
    )));
    info!("redis_listen_task launched");
}

// Add agent tx, join channel, spawn agent/conn relay task, ack joining
#[instrument(skip(user_token, rm, state), fields(channel = %rm.topic, join_ref = ?rm.join_ref))]
async fn handle_join(
    user_token: Option<String>,
    rm: &RequestMessage,
    state: Arc<State>,
    conn_id: &str,
) -> Result<JoinHandle<()>, ChannelError> {
    // First try to see if join payload contains token, then check user_token
    let token = match &rm.payload {
        RequestPayload::Join { token } => Ok(token.clone()),
        _ => user_token.ok_or_else(|| {
            error!(payload = ?rm.payload, "invalid payload");
            ChannelError::BadToken
        }),
    }?;
    let claims = match decode_jwt(&token, state.jwt_secret.clone()).await {
        Ok(claims) => claims,
        Err(e) => {
            error!(error = %e, %token, "fail to decode JWT");
            return Err(ChannelError::BadToken);
        }
    };

    debug!(?claims, "jwt claims");

    let channel_name = rm.topic.clone();
    if is_special_channel(&channel_name) {
        info!("channel is special, ignored");
    } else {
        add_channel(&state.ctl, channel_name.clone()).await;
        launch_channel_redis_listen_task(
            state.clone(),
            &state.ctl,
            channel_name.clone(),
            state.redis_client.clone(),
        )
        .await;
    }

    let agent_id = format!(
        "{}:{}:{}",
        conn_id,
        channel_name.clone(),
        rm.join_ref.clone().unwrap()
    );
    let join_ref = rm.join_ref.clone();
    let event_ref = rm.event_ref.clone();

    info!(%agent_id, "agent joining");
    state
        .ctl
        .lock()
        .await
        .agent_add(agent_id.to_string(), None)
        .await; // Agent is not created here
    match state
        .ctl
        .lock()
        .await
        .channel_join(
            &channel_name.clone(),
            agent_id.to_string(),
            claims.id.clone(),
        )
        .await
    {
        Ok(_) => {}
        Err(e) => {
            // What happens to relay task when connection is lost?
            error!(error = %e, "fail to join");
            return Err(e);
        }
    }

    // Forward messages from agent rx to conn tx
    // This needs to be ready before join is complete to avoid losing messages
    let relay_state = state.clone();
    let local_join_ref = rm.join_ref.clone();
    let local_conn_id = conn_id.to_string();
    let local_agent_id = agent_id.clone();
    let span_relay = tracing::info_span!("relay", agent_id = %local_agent_id);
    let relay_task = tokio::spawn(
        async move {
            let mut agent_rx = relay_state
                .ctl
                .lock()
                .await
                .agent_rx(local_agent_id.clone())
                .await
                .unwrap();
            let conn_tx = relay_state
                .ctl
                .lock()
                .await
                .conn_tx(local_conn_id.to_string())
                .await
                .unwrap();

            debug!("agent => conn established");
            loop {
                match agent_rx.recv().await {
                    Ok(mut channel_message) => {
                        let ChannelMessage::Reply(ref mut reply) = channel_message;
                        reply.join_ref = local_join_ref.clone();
                        // agent rx => conn tx => conn rx => ws tx
                        if conn_tx.send(channel_message.clone()).is_err() {
                            // fails when there's no reciever, connection lost, stop forwarding
                            debug!("connection closed, stopping relay");
                            break;
                        }
                    }
                    Err(broadcast::error::RecvError::Lagged(skipped)) => {
                        warn!(skipped, "agent rx lagged");
                        continue;
                    }
                    Err(broadcast::error::RecvError::Closed) => {
                        debug!("agent rx closed"); // expected during cleanup
                        break;
                    }
                }
            }
        }
        .instrument(span_relay),
    );

    // phx_reply, confirm join event
    ok_reply(
        conn_id,
        join_ref.clone(),
        &event_ref,
        &channel_name,
        state.clone(),
    )
    .await;
    info!("acked");

    if channel_name == "admin" {
        info!("handling admin initialization ...");
        let ctl = state.ctl.lock().await;
        let channels = ctl.channels.lock().await;
        for (name, channel) in channels.iter() {
            // let channel = channels.get(&channel_name).unwrap();
            let meta = json!({"channel": name, "agents": *channel.agents.lock().await});
            ctl.pub_meta_event("channel".into(), "list".into(), meta)
                .await;
        }
    }

    // presence state, to current agent
    presence_state(
        conn_id,
        join_ref.clone(),
        &event_ref,
        &channel_name,
        state.clone(),
    )
    .await;

    // presence diff, broadcast
    let mut redis_conn = state
        .redis_client
        .get_multiplexed_async_connection()
        .await
        .unwrap();
    presence_diff(
        &mut redis_conn,
        channel_name.clone(),
        agent_id.clone(),
        claims.id.clone(),
        PresenceAction::Join,
    )
    .await;

    Ok(relay_task)
}

#[instrument(skip(state), fields(channel = %channel_name, join_ref = ?join_ref))]
async fn handle_leave(
    state: Arc<State>,
    conn_id: &str,
    join_ref: Option<String>,
    event_ref: &str,
    channel_name: String,
) {
    let agent_id = format!("{}:{}:{}", conn_id, channel_name, join_ref.clone().unwrap());
    let external_id_opt = state.ctl.lock().await.agent_rm(agent_id.clone()).await;
    let agent_count = state
        .ctl
        .lock()
        .await
        .channel_leave(channel_name.clone(), agent_id.clone())
        .await
        .unwrap();
    if agent_count == 0 && !is_special_channel(&channel_name) {
        state
            .ctl
            .lock()
            .await
            .channel_remove_if_empty(channel_name.clone())
            .await;
    }
    ok_reply(conn_id, join_ref, event_ref, &channel_name, state.clone()).await;

    if external_id_opt.is_none() {
        error!("agent not found");
        return;
    }
    info!("send presense_diff");
    let mut redis_conn = state
        .redis_client
        .get_multiplexed_async_connection()
        .await
        .unwrap();
    presence_diff(
        &mut redis_conn,
        channel_name.clone(),
        agent_id.clone(),
        external_id_opt.unwrap(),
        PresenceAction::Leave,
    )
    .await;
}

async fn ok_reply(
    conn_id: &str,
    join_ref: Option<String>,
    event_ref: &str,
    channel_name: &str,
    state: Arc<State>,
) {
    let response = match join_ref {
        None => Response::Empty {}, // heartbeat
        Some(ref join_ref) => Response::Join {
            id: format!("{}:{}:{}", conn_id, channel_name, join_ref),
        }, // join
    };
    let join_reply_message = ServerMessage {
        join_ref: join_ref.clone(),
        event_ref: event_ref.to_string(),
        topic: channel_name.to_string(),
        event: "phx_reply".to_string(),
        payload: ServerPayload::ServerResponse(ServerResponse {
            status: "ok".to_string(),
            response,
        }),
    };
    state
        .ctl
        .lock()
        .await
        .conn_send(
            conn_id.to_string(),
            ChannelMessage::Reply(join_reply_message),
        )
        .await
        .unwrap();
    // let text = serde_json::to_string(&join_reply_message).unwrap();
    // debug!("sent to connection {}: {}", &conn_id, text);
}

async fn presence_state(
    conn_id: &str,
    join_ref: Option<String>,
    event_ref: &str,
    channel_name: &str,
    state: Arc<State>,
) {
    let hashed_agents = state
        .ctl
        .lock()
        .await
        .agents
        .lock()
        .await
        .iter()
        .filter(|(_, agent)| agent.channel == channel_name)
        .into_group_map_by(|(_, agent)| &agent.external_id)
        .into_iter()
        .map(|(external_id, group)| {
            (external_id.clone(), {
                json!({
                    "metas": group.into_iter()
                        .map(|(id, _)| json!({ "phx_ref": id }))
                        .collect::<Vec<_>>()
                })
            })
        })
        .collect::<HashMap<_, _>>();
    let reply = ServerMessage {
        join_ref: join_ref.clone(),
        event_ref: event_ref.to_string(),
        topic: channel_name.to_string(),
        event: "presence_state".to_string(),
        payload: ServerPayload::ServerJsonValue(json!(hashed_agents)),
    };
    state
        .ctl
        .lock()
        .await
        .conn_send(conn_id.to_string(), ChannelMessage::Reply(reply))
        .await
        .unwrap();
    info!("sent");
}

#[derive(Debug)]
pub enum PresenceAction {
    Join,
    Leave,
}

#[instrument(skip(redis_conn, items))]
pub async fn presence_diff_many(
    redis_conn: &mut MultiplexedConnection,
    channel_name: String,
    action: PresenceAction,
    items: serde_json::Value,
) {
    let diff = match action {
        PresenceAction::Join => json!({"joins": items, "leaves": {}}),
        PresenceAction::Leave => json!({"joins": {}, "leaves": items}),
    };
    let redis_topic = format!("to:{}:presence_diff", channel_name);
    let message = serde_json::to_string(&diff).unwrap();
    let publish_result: RedisResult<String> = redis_conn
        .publish(redis_topic.clone(), message.clone())
        .await;
    if let Err(e) = publish_result {
        error!(error = %e, "fail to publish to redis");
    } else {
        info!(?action, "sent");
    }
}

/// broadcast presence_diff over redis
#[instrument(skip(redis_conn))]
pub async fn presence_diff(
    redis_conn: &mut MultiplexedConnection,
    channel_name: String,
    agent_id: String,
    external_id: String,
    action: PresenceAction,
) {
    let items = json!({
        external_id.clone(): {
            "metas": [
                {"phx_ref": agent_id.clone()}
            ],
        },
    });
    let diff = match action {
        PresenceAction::Join => json!({"joins": items, "leaves": {}}),
        PresenceAction::Leave => json!({"joins": {}, "leaves": items}),
    };
    let redis_topic = format!("to:{}:presence_diff", channel_name);
    let message = serde_json::to_string(&diff).unwrap();
    let publish_result: RedisResult<String> = redis_conn
        .publish(redis_topic.clone(), message.clone())
        .await;
    if let Err(e) = publish_result {
        error!(error = %e, "fail to publish to redis");
    } else {
        info!(?action, "sent");
    }
}

// Send a timestamp every second
#[instrument(skip(state))]
pub async fn datetime_handler(state: Arc<State>, channel_name: String) {
    tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;

    info!("launch system/datetime task...");
    let mut counter = 0;
    let event = "datetime";
    loop {
        let now = chrono::Local::now();
        let message = ServerMessage {
            join_ref: None,
            event_ref: counter.to_string(),
            topic: channel_name.to_string(),
            event: event.to_string(),
            payload: ServerPayload::ServerResponse(ServerResponse {
                status: "ok".to_string(),
                response: Response::Datetime {
                    datetime: now.to_rfc3339_opts(chrono::SecondsFormat::Millis, false),
                    counter,
                },
            }),
        };
        match state
            .ctl
            .lock()
            .await
            .channel_broadcast(channel_name.to_string(), ChannelMessage::Reply(message))
            .await
        {
            Ok(0) => {} // no client
            Ok(_) => {} // debug!("datetime > {}", text),
            Err(ChannelError::ChannelEmpty) => {}
            Err(e) => {
                error!(
                    error = %e,
                    %event,
                    "fail to broadcast"
                );
            }
        }

        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        counter += 1;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::channel::utils::generate_jwt;
    use axum::{
        extract::{Query, State as AxumState, WebSocketUpgrade},
        response::IntoResponse,
        routing::get,
        Router,
    };
    use futures::{SinkExt, StreamExt};
    use serde::Deserialize;
    use serde_json::json;
    use std::sync::Arc;
    use tokio::net::{TcpListener, TcpStream};
    use tokio::sync::Mutex;
    use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};

    #[derive(Debug, Deserialize)]
    struct WebSocketParams {
        #[serde(rename = "userToken")]
        user_token: Option<String>,

        #[allow(dead_code)]
        #[serde(rename = "vsn")]
        version: Option<String>,
    }

    async fn axum_websocket_handler(
        ws: WebSocketUpgrade,
        Query(params): Query<WebSocketParams>,
        AxumState(state): AxumState<Arc<State>>,
    ) -> impl IntoResponse {
        let user_token = params.user_token.clone();
        ws.on_upgrade(move |socket| axum_on_connected(socket, state, user_token))
    }

    async fn setup_test_server() -> (String, Arc<State>, String) {
        let redis_url = "redis://127.0.0.1:6379".to_string();
        let redis_client = redis::Client::open(redis_url.clone()).unwrap();
        let channel_control = ChannelControl::new(Arc::new(redis_client.clone()));
        let state = Arc::new(State {
            ctl: Mutex::new(channel_control),
            redis_client,
            id_length: 8,
            jwt_secret: "secret".into(),
            jwt_expiration_secs: 3600,
        });

        // use unique channel name for each test to avoid pub/sub interference
        let rand_suffix = nanoid::nanoid!(8);
        let system_channel = format!("system_{}", rand_suffix);

        // Setup channels
        state
            .ctl
            .lock()
            .await
            .channel_add("phoenix".into(), None)
            .await;
        state
            .ctl
            .lock()
            .await
            .channel_add(system_channel.clone(), None)
            .await;
        state
            .ctl
            .lock()
            .await
            .channel_add("streaming".into(), None)
            .await;

        // Spawn system task
        tokio::spawn(datetime_handler(state.clone(), system_channel.clone()));

        // Create Axum router with websocket handler
        let app = Router::new()
            .route("/websocket", get(axum_websocket_handler))
            .with_state(state.clone());

        // Bind to a random available port
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let addr = format!("ws://127.0.0.1:{}/websocket", port);

        // Start the server
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        // Return the websocket URL, state, and the random channel name
        (addr, state, system_channel)
    }

    async fn connect_client(
        addr: &str,
    ) -> (
        futures::stream::SplitSink<
            WebSocketStream<MaybeTlsStream<TcpStream>>,
            tokio_tungstenite::tungstenite::Message,
        >,
        futures::stream::SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    ) {
        let (ws_stream, _) = connect_async(addr).await.expect("Failed to connect");
        ws_stream.split()
    }

    async fn gen_token(state: &Arc<State>, channel: &str, id: &str) -> String {
        generate_jwt(
            id.to_string(),
            channel.to_string(),
            state.jwt_secret.clone(),
            state.jwt_expiration_secs,
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn test_ws_websocket_connection() {
        let (addr, _, _) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        // Test initial connection with heartbeat
        let heartbeat = r#"[null,"1","phoenix","heartbeat",{}]"#;
        tx.send(Message::text(heartbeat)).await.unwrap();

        if let Some(Ok(msg)) = rx.next().await {
            let response: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
            assert_eq!(response[2], "phoenix");
            assert_eq!(response[4]["status"], "ok");
        }
    }

    #[tokio::test]
    async fn test_ws_channel_join_leave_flow() {
        let (addr, state, sys_chan) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        let token = gen_token(&state, &sys_chan, "user1").await;

        // Join system channel
        let join_msg = format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            sys_chan, token
        );
        tx.send(Message::text(join_msg)).await.unwrap();

        // Wait for and verify join response (may need to skip other messages)
        let mut join_confirmed = false;
        // Use a longer timeout and handle timeout error properly
        let timeout_duration = std::time::Duration::from_secs(5);
        'join_loop: for _ in 0..10 {
            // Try up to 10 messages before giving up
            match tokio::time::timeout(timeout_duration, rx.next()).await {
                Ok(Some(Ok(msg))) => {
                    let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();

                    // Print for debugging purposes during test development
                    // println!("Received message: {}", resp);

                    // Check if this is the join response we're looking for
                    if resp[1] == "ref1" && resp[2] == sys_chan && resp[3] == "phx_reply" {
                        assert_eq!(resp[4]["status"], "ok");
                        join_confirmed = true;
                        break 'join_loop;
                    }
                    // Otherwise it's some other message like presence_state - ignore and continue
                }
                Ok(Some(Err(_))) => panic!("WebSocket error during join"),
                Ok(None) => panic!("WebSocket closed unexpectedly during join"),
                Err(_) => panic!("Timed out waiting for join response"),
            }
        }

        assert!(
            join_confirmed,
            "Never received join confirmation after reading multiple messages"
        );

        // Check system channel has our agent
        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            let agents = channels.get(&sys_chan).unwrap().agents.lock().await;
            assert_eq!(agents.len(), 1);
        }

        // Leave channel
        let leave_msg = format!(r#"["1","ref2","{}","phx_leave",{{}}]"#, sys_chan);
        tx.send(Message::text(leave_msg)).await.unwrap();

        // Wait for and verify leave response
        let mut leave_confirmed = false;
        'leave_loop: for _ in 0..10 {
            // Try up to 10 messages before giving up
            match tokio::time::timeout(timeout_duration, rx.next()).await {
                Ok(Some(Ok(msg))) => {
                    let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();

                    // Print for debugging purposes during test development
                    // println!("Received message: {}", resp);

                    // Check if this is the leave response we're looking for
                    if resp[1] == "ref2" && resp[2] == sys_chan && resp[3] == "phx_reply" {
                        assert_eq!(resp[4]["status"], "ok");
                        leave_confirmed = true;
                        break 'leave_loop;
                    }
                    // Otherwise it's some other message - ignore and continue
                }
                Ok(Some(Err(_))) => panic!("WebSocket error during leave"),
                Ok(None) => panic!("WebSocket closed unexpectedly during leave"),
                Err(_) => panic!("Timed out waiting for leave response"),
            }
        }

        assert!(
            leave_confirmed,
            "Never received leave confirmation after reading multiple messages"
        );

        // Verify channel state
        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            if let Some(channel) = channels.get(&sys_chan) {
                let agents = channel.agents.lock().await;
                assert_eq!(agents.len(), 0);
            }
        }

        // Explicitly close the connection and wait for it to finish
        drop(tx);
        drop(rx);

        // Give time for cleanup tasks to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    }

    #[tokio::test]
    async fn test_ws_connection_close_websocket() {
        let (addr, state, sys_chan) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        let token = gen_token(&state, &sys_chan, "user1").await;

        // Join system channel
        let join_msg = format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            sys_chan, token
        );
        tx.send(Message::text(join_msg)).await.unwrap();

        // Wait for join response
        if let Some(Ok(_)) = rx.next().await {}

        // Give time for join to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Verify agent joined
        let agent_count = {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            if let Some(channel) = channels.get(&sys_chan) {
                channel.agents.lock().await.len()
            } else {
                0
            }
        };
        assert_eq!(agent_count, 1, "Agent should be joined");

        // Close connection by dropping handles
        drop(tx);
        drop(rx);

        // Give time for cleanup
        tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

        // Verify agent was removed
        let agent_count = {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            if let Some(channel) = channels.get(&sys_chan) {
                channel.agents.lock().await.len()
            } else {
                0
            }
        };
        assert_eq!(
            agent_count, 0,
            "Agent should be removed after connection close"
        );
    }

    // Add a new test for multiple clients that doesn't depend on specific agent IDs
    #[tokio::test]
    async fn test_ws_multiple_clients_fixed() {
        let (addr, state, sys_chan) = setup_test_server().await;

        // Connect and join with multiple clients
        let mut clients = vec![];
        for i in 0..3 {
            let (mut tx, mut rx) = connect_client(&addr).await;

            let token = gen_token(&state, &sys_chan, &format!("user{}", i)).await;

            // Join system channel
            let join_msg = format!(
                r#"["{}","ref{}","{}","phx_join",{{"token":"{}"}}]"#,
                i, i, sys_chan, token
            );
            tx.send(Message::text(join_msg)).await.unwrap();

            // Verify join response
            if let Some(Ok(_)) = tokio::time::timeout(std::time::Duration::from_secs(2), rx.next())
                .await
                .unwrap()
            {}

            clients.push((tx, rx));
        }

        // Give time for all joins to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

        // Verify channel has 3 agents
        let agent_count = {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            let system_channel = channels.get(&sys_chan).unwrap();
            let agents = system_channel.agents.lock().await;
            agents.len()
        };

        assert_eq!(agent_count, 3, "Should have 3 agents connected");

        // Clean up
        drop(clients);
    }

    #[tokio::test]
    async fn test_ws_flow_server() {
        let (_addr, state, sys_chan) = setup_test_server().await;

        let ctl = state.ctl.lock().await;
        let channels = ctl.channels.lock().await;

        // Check our random channel exists
        assert!(channels.contains_key("phoenix"));
        assert!(channels.contains_key(&sys_chan));
        assert!(channels.contains_key("streaming"));

        let agents = channels.get(&sys_chan).unwrap().agents.lock().await;
        assert_eq!(agents.len(), 0);
    }

    #[tokio::test]
    async fn test_ws_flow_join_leave() {
        let (addr, state, sys_chan) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        let token = gen_token(&state, &sys_chan, "user1").await;

        // Join system channel
        let join_msg = format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            sys_chan, token
        );
        tx.send(Message::text(join_msg)).await.unwrap();

        // Verify join response (filter out presence_state)
        loop {
            if let Some(Ok(msg)) = rx.next().await {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
                if resp[3] == "phx_reply" && resp[1] == "ref1" {
                    assert_eq!(resp[2], sys_chan);
                    assert_eq!(resp[4]["status"], "ok");
                    break;
                }
            } else {
                panic!("Stream ended or error before join reply");
            }
        }

        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            let agents = channels.get(&sys_chan).unwrap().agents.lock().await;
            // Can't know the specific agent_id
            // assert_eq!(*agents, vec!["foobar"]);
            assert_eq!(agents.len(), 1);
        }

        // Leave channel
        let leave_msg = format!(r#"["1","ref2","{}","phx_leave",{{}}]"#, sys_chan);
        tx.send(Message::text(leave_msg)).await.unwrap();

        // Verify leave response
        loop {
            if let Some(Ok(msg)) = rx.next().await {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
                if resp[3] == "phx_reply" && resp[1] == "ref2" {
                    assert_eq!(resp[2], sys_chan);
                    assert_eq!(resp[4]["status"], "ok");
                    break;
                }
            } else {
                panic!("Stream ended or error before leave reply");
            }
        }

        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            if let Some(channel) = channels.get(&sys_chan) {
                let agents = channel.agents.lock().await;
                assert_eq!(agents.len(), 0);
            }
        }
    }

    #[tokio::test]
    async fn test_ws_multiple_clients() {
        let (addr, state, sys_chan) = setup_test_server().await;

        // Connect multiple clients
        let mut clients = vec![];
        assert_eq!(clients.len(), 0);

        for i in 0..3 {
            let (mut tx, mut rx) = connect_client(&addr).await;

            let token = gen_token(&state, &sys_chan, &format!("user{}", i)).await;

            // Join system channel
            let join_msg = format!(
                r#"["{}","ref{}","{}","phx_join",{{"token":"{}"}}]"#,
                i, i, sys_chan, token
            );
            tx.send(Message::text(join_msg)).await.unwrap();

            // Verify join
            if let Some(Ok(msg)) = rx.next().await {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
                assert_eq!(resp[4]["status"], "ok");
            }
            clients.push((tx, rx));
        }

        assert_eq!(clients.len(), 3);

        let ctl = state.ctl.lock().await;
        let channels = ctl.channels.lock().await;

        let agents = channels.get(&sys_chan).unwrap().agents.lock().await;
        // there should be 3 unique agent ids since we use different user IDs for tokens
        assert_eq!(agents.len(), 3);
    }

    #[tokio::test]
    async fn test_ws_message_broadcast() {
        let (addr, state, sys_chan) = setup_test_server().await;
        let (mut tx1, mut rx1) = connect_client(&addr).await;
        let (mut tx2, mut rx2) = connect_client(&addr).await;

        // Both clients join system channel
        for (tx, i) in [(&mut tx1, 1), (&mut tx2, 2)] {
            let token = gen_token(&state, &sys_chan, &format!("user{}", i)).await;
            let join_msg = format!(
                r#"["{}","ref{}","{}","phx_join",{{"token":"{}"}}]"#,
                i, i, sys_chan, token
            );
            tx.send(Message::text(join_msg)).await.unwrap();

            // Wait for join response
            if let Some(Ok(_)) = if i == 1 {
                rx1.next().await
            } else {
                rx2.next().await
            } {}
        }

        // Broadcast message to system channel
        let message = ServerMessage {
            join_ref: None,
            event_ref: "broadcast".to_string(),
            topic: sys_chan.clone(),
            event: "test".to_string(),
            payload: ServerPayload::ServerResponse(ServerResponse {
                status: "ok".to_string(),
                response: Response::Message {
                    message: "test broadcast".to_string(),
                },
            }),
        };

        state
            .ctl
            .lock()
            .await
            .channel_broadcast(sys_chan.clone(), ChannelMessage::Reply(message))
            .await
            .unwrap();

        // Both clients should receive the message
        for rx in [&mut rx1, &mut rx2] {
            if let Some(Ok(msg)) = rx.next().await {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
                if resp[1] == "broadcast" {
                    assert_eq!(resp[4]["response"]["message"], "test broadcast");
                }
            }
        }
    }

    #[tokio::test]
    async fn test_ws_invalid_messages() {
        let (addr, _, _) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        // Send invalid JSON
        tx.send(Message::text("invalid json")).await.unwrap();

        // Send invalid message format
        tx.send(Message::text(r#"["invalid","format"]"#))
            .await
            .unwrap();

        // Send to non-existent channel
        let invalid_channel = r#"["1","ref1","nonexistent","phx_join",{"token":"test"}]"#;
        tx.send(Message::text(invalid_channel)).await.unwrap();

        // Connection should still be alive
        let heartbeat = r#"[null,"1","phoenix","heartbeat",{}]"#;
        tx.send(Message::text(heartbeat)).await.unwrap();

        if let Some(Ok(msg)) = rx.next().await {
            let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
            assert_eq!(resp[2], "phoenix");
            assert_eq!(resp[4]["status"], "ok");
        }
    }

    #[tokio::test]
    async fn test_ws_system_channel() {
        let (addr, state, sys_chan) = setup_test_server().await;
        let (mut tx, mut rx) = connect_client(&addr).await;

        // Join system channel
        let token = gen_token(&state, &sys_chan, "user1").await;
        let join_msg = format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            sys_chan, token
        );
        tx.send(Message::text(join_msg)).await.unwrap();

        // Should receive initial join response
        if let Some(Ok(msg)) = rx.next().await {
            let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
            assert_eq!(resp[2], sys_chan);
            assert_eq!(resp[4]["status"], "ok");
        }

        // Should receive datetime updates
        match tokio::time::timeout(std::time::Duration::from_secs(5), rx.next()).await {
            Ok(Some(Ok(msg))) => {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();
                if resp[2] == sys_chan && resp[3] == "datetime" {
                    assert!(resp[4]["response"]["datetime"].is_string());
                }
            }
            _ => panic!("Timed out waiting for datetime update"),
        }
    }

    // #[test]
    // fn test_response_invalid_json() {
    //     // Missing type field
    //     let json = r#"{"message": "hello"}"#;
    //     assert!(serde_json::from_str::<RedisResponse>(json).is_err());
    //
    //     // Invalid type value
    //     let json = r#"{"type": "invalid"}"#;
    //     assert!(serde_json::from_str::<RedisResponse>(json).is_err());
    //
    //     // Missing required fields
    //     let json = r#"{"type": "datetime", "datetime": "2024-01-01"}"#;
    //     assert!(serde_json::from_str::<RedisResponse>(json).is_err());
    // }

    #[test]
    fn test_ws_request_json_heartbeat() {
        // Test full message with join payload
        let msg: RequestMessage =
            serde_json::from_str(r#"["1", "ref1", "room123", "heartbeat", {}]"#).unwrap();

        assert_eq!(msg.join_ref, Some("1".to_string()));
        assert_eq!(msg.event_ref, "ref1");
        assert_eq!(msg.topic, "room123");
        assert_eq!(msg.event, "heartbeat");
        assert_eq!(msg.payload, serde_json::from_value(json!({})).unwrap());
    }

    #[test]
    fn test_ws_request_json_join() {
        // Test full message with join payload
        let msg: RequestMessage = serde_json::from_str(
            r#"["1", "ref1", "room123", "phx_join", {"token": "secret_token"}]"#,
        )
        .unwrap();

        assert_eq!(msg.join_ref, Some("1".to_string()));
        assert_eq!(msg.event_ref, "ref1");
        assert_eq!(msg.topic, "room123");
        assert_eq!(msg.event, "phx_join");
        assert_eq!(
            msg.payload,
            RequestPayload::Join {
                token: "secret_token".to_string()
            }
        );
    }

    #[test]
    fn test_ws_request_json_message() {
        let json = r#"["1", "ref4", "room123", "message", {"message": "Hello, World!"}]"#;

        let msg: RequestMessage = serde_json::from_str(json).unwrap();
        assert_eq!(msg.event, "message");
        assert_eq!(
            msg.payload,
            RequestPayload::Message {
                message: "Hello, World!".to_string()
            }
        );

        // Test just the payload
        // let payload: RequestPayload = serde_json::from_value(json!({"message": "test message"})).unwrap();
        // assert_eq!(payload, RequestPayload::Message("test message".to_string()));
    }

    #[test]
    fn test_ws_request_json_message_payload() {
        let payload: RequestPayload = serde_json::from_value(json!({})).unwrap();
        assert_eq!(payload, RequestPayload::JsonValue(json!({})));

        let payload: RequestPayload =
            serde_json::from_value(json!({"token": "another_token"})).unwrap();
        assert_eq!(
            payload,
            RequestPayload::Join {
                token: "another_token".to_string()
            }
        );

        let payload: RequestPayload =
            serde_json::from_value(json!({ "message": "test message" })).unwrap();
        assert_eq!(
            payload,
            RequestPayload::Message {
                message: "test message".to_string()
            }
        );
    }

    #[test]
    fn test_ws_request_json_invalid() {
        // Invalid array length
        assert!(serde_json::from_str::<RequestMessage>(r#"["1", "ref1", "room123"]"#).is_err());
        assert!(
            serde_json::from_str::<RequestMessage>(r#"["1", "ref1", "room123", "phx_join"]"#)
                .is_err()
        );

        // Invalid join payload (wrong format)
        assert!(serde_json::from_str::<RequestMessage>(
            r#"["1", "ref1", "room123", "phx_join", null]"#
        )
        .is_ok());
        assert!(serde_json::from_str::<RequestMessage>(
            r#"["1", "ref1", "room123", "phx_join", 23]"#
        )
        .is_ok());
        assert!(serde_json::from_str::<RequestMessage>(
            r#"["1", "ref1", "room123", "phx_join", 12.4]"#
        )
        .is_ok());
        assert!(serde_json::from_str::<RequestMessage>(
            r#"["1", "ref1", "room123", "phx_join", "nulldirect_token"]"#
        )
        .is_ok());
        assert!(serde_json::from_str::<RequestMessage>(
            r#"["1", "ref1", "room123", "phx_join", [1, null, "foobar"]]"#
        )
        .is_ok());

        // Invalid type for number elements
        assert!(serde_json::from_str::<RequestMessage>(
            r#"[123, "ref1", "room123", "phx_join", {"token": "secret"}]"#
        )
        .is_err());
    }

    async fn expect_msg_content(
        rx: &mut futures::stream::SplitStream<
            tokio_tungstenite::WebSocketStream<
                tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
            >,
        >,
        content: &str,
    ) {
        let timeout = std::time::Duration::from_secs(2);
        let start = std::time::Instant::now();

        loop {
            if start.elapsed() > timeout {
                panic!("Timeout waiting for message containing '{}'", content);
            }

            match tokio::time::timeout(std::time::Duration::from_millis(100), rx.next()).await {
                Ok(Some(Ok(msg))) => {
                    let s = msg.to_string();
                    if s.contains(content) {
                        return;
                    }
                }
                _ => continue,
            }
        }
    }

    // Verify that the Redis listener thread survives when all subscribers disconnect
    // and correctly relay messages when a new subscriber joins later.
    #[tokio::test]
    async fn test_redis_listener_survival_on_zero_subscribers() {
        let (addr, state, channel_name) = setup_test_server().await;

        let (mut tx_a, mut rx_a) = connect_client(&addr).await;
        let token_a = gen_token(&state, &channel_name, "user_a").await;
        tx_a.send(Message::text(format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            channel_name, token_a
        )))
        .await
        .unwrap();

        // consume messages until join is confirmed (ignore presence noise)
        expect_msg_content(&mut rx_a, "phx_reply").await;

        // connect B, client A should get it
        let mut redis_conn = state
            .redis_client
            .get_multiplexed_async_connection()
            .await
            .unwrap();
        let redis_topic = format!("to:{}:test_event", channel_name);
        redis_conn
            .publish::<_, _, ()>(&redis_topic, r#"{"type":"message","message":"one"}"#)
            .await
            .unwrap();

        expect_msg_content(&mut rx_a, "one").await;

        // disconnect A -> 0 subscribers -> triggers potential suicide
        drop(tx_a);
        drop(rx_a);
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        redis_conn
            .publish::<_, _, ()>(&redis_topic, r#"{"type":"message","message":"void"}"#)
            .await
            .unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // connect B
        let (mut tx_b, mut rx_b) = connect_client(&addr).await;
        let token_b = gen_token(&state, &channel_name, "user_b").await;
        tx_b.send(Message::text(format!(
            r#"["2","ref2","{}","phx_join",{{"token":"{}"}}]"#,
            channel_name, token_b
        )))
        .await
        .unwrap();
        expect_msg_content(&mut rx_b, "phx_reply").await;

        // publish message, B should get it
        redis_conn
            .publish::<_, _, ()>(&redis_topic, r#"{"type":"message","message":"two"}"#)
            .await
            .unwrap();
        expect_msg_content(&mut rx_b, "two").await;
    }

    // Verify that we can handle nested topics (e.g. weather:wind) and raw JSON payloads (no 'type' field)
    #[tokio::test]
    async fn test_nested_topics_and_raw_json() {
        let (addr, state, _) = setup_test_server().await;
        let nested_channel = "weather:wind";

        let (mut tx, mut rx) = connect_client(&addr).await;
        let token = gen_token(&state, nested_channel, "user_nested").await;

        tx.send(Message::text(format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            nested_channel, token
        )))
        .await
        .unwrap();
        expect_msg_content(&mut rx, "phx_reply").await;

        let mut redis_conn = state
            .redis_client
            .get_multiplexed_async_connection()
            .await
            .unwrap();

        let raw_topic = "to:weather:wind:update";
        let raw_payload = r#"{"temperature": 25.5, "unit": "C"}"#;

        redis_conn
            .publish::<_, _, ()>(raw_topic, raw_payload)
            .await
            .unwrap();

        // find the specific update event, ignoring presence events
        let timeout = std::time::Duration::from_secs(2);
        let start = std::time::Instant::now();
        let mut found = false;

        while start.elapsed() < timeout {
            if let Ok(Some(Ok(msg))) =
                tokio::time::timeout(std::time::Duration::from_millis(100), rx.next()).await
            {
                let resp: serde_json::Value = serde_json::from_str(&msg.to_string()).unwrap();

                // check if this is the message we want: [join_ref, ref, topic, event, payload]
                if resp.get(3).and_then(|v| v.as_str()) == Some("update") {
                    assert_eq!(resp[2], "weather:wind", "Topic parsed incorrectly");
                    assert_eq!(resp[4]["temperature"], 25.5, "Raw JSON payload corrupted");
                    found = true;
                    break;
                }
            }
        }
        assert!(found, "Did not receive 'update' event with correct payload");
    }

    // Verify that channels are removed from memory after the last connection drops
    #[tokio::test]
    async fn test_ghost_channel_cleanup() {
        let (addr, state, _) = setup_test_server().await;
        let ghost_channel = format!("temp_{}", nanoid::nanoid!(4));

        let (mut tx, rx) = connect_client(&addr).await;
        let token = gen_token(&state, &ghost_channel, "ghost_user").await;
        tx.send(Message::text(format!(
            r#"["1","ref1","{}","phx_join",{{"token":"{}"}}]"#,
            ghost_channel, token
        )))
        .await
        .unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            assert!(
                channels.contains_key(&ghost_channel),
                "Channel should exist"
            );
        }

        // abrupt disconnect (drop socket)
        drop(tx);
        drop(rx);

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        {
            let ctl = state.ctl.lock().await;
            let channels = ctl.channels.lock().await;
            assert!(
                !channels.contains_key(&ghost_channel),
                "Ghost channel leaked after disconnect"
            );
        }
    }
}