tauri-plugin-socketio 0.2.0

Tauri 2 plugin: named Socket.IO v4 clients (socketio-next) shared by Rust and the webview
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
//! The registry of named clients, the subscriptions that forward them to webviews, and the
//! server acks waiting for an answer from a webview.
//!
//! Each client has one [`Hub`]: a single pair of listeners on the client that fans events and
//! states out to the client's subscriptions. Subscriptions are plain records in the hub, so
//! ending one drops its channel at once; the hub's lock orders every message of a client.
//!
//! Lock order: `clients` → a hub's `state`. Hub callbacks (the client's event task) take only
//! their hub's lock, and no lock is held across an await.

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};

use socketio_next::{
    AckSender, Client, ClientBuilder, ConnectionState, Event, ListenerId, Payload,
};
use tauri::ipc::Channel;
use tokio::sync::watch;

use crate::models::{Access, ClientInfo, Dynamic, Message, State};
use crate::{Error, Result};

/// Server acks wait this long for the webview's answer, then are forgotten.
pub const ACK_TTL: Duration = Duration::from_secs(120);
/// At most this many server acks of one client wait for one webview; past it the webview's own
/// oldest is forgotten (never another webview's).
pub const MAX_PENDING_ACKS_PER_WEBVIEW: usize = 256;
/// At most this many subscriptions per webview (over all clients).
pub const MAX_SUBSCRIPTIONS_PER_WEBVIEW: usize = 64;

/// Clients one webview may have connected (and not disconnected) at once; they are
/// disconnected when its window is destroyed.
pub const MAX_CLIENTS_PER_WEBVIEW: usize = 64;
/// How long a JS `connect` waits for the client's event task to forward the connected state.
const SETTLE: Duration = Duration::from_secs(2);

/// Identifies one subscription (unique for the life of the app).
pub type SubscriptionId = u64;

/// Named socket.io clients shared by Rust and the webviews. Get it with
/// [`SocketIoExt::socketio`](crate::SocketIoExt::socketio); cheap to clone.
///
/// Ids are unique: [`connect`](Self::connect) and [`insert`](Self::insert) fail with
/// [`Error::AlreadyExists`] for an id in use, so a client is never replaced behind the back of
/// whoever holds it. [`remove`](Self::remove) it first. [`reserve`](Self::reserve) an id in
/// `setup` when Rust registers it later: webviews then cannot take it.
#[derive(Clone, Default)]
pub struct SocketIo {
    inner: Arc<Inner>,
}

#[derive(Default)]
struct Inner {
    clients: Mutex<HashMap<String, Entry>>,
    reserved: Mutex<HashSet<String>>,
    /// Connects in flight: their ids and subscription slots.
    pending: Mutex<Pending>,
    /// Bumped when a webview loads a new page: a JS connect that started before is stale.
    generations: Mutex<HashMap<String, u64>>,
    /// URL prefixes a webview may connect to; empty: any.
    allowed: Vec<url::Url>,
    next_sub: AtomicU64,
}

struct Entry {
    client: Client,
    access: Access,
    hub: Arc<Hub>,
    /// The hub's listeners when they were added after the client was built (`insert`).
    listeners: Vec<ListenerId>,
    /// Auth, headers and query a webview may replace (clients a webview connected).
    dynamic: Option<Arc<Mutex<Dynamic>>>,
    /// The webview that connected it; it is disconnected when that window is destroyed.
    owner: Option<String>,
}

/// Fans one client out to its subscriptions; keeps the acks offered to webviews.
struct Hub {
    state: Mutex<HubState>,
    /// Counts forwarded states, so `connect` can wait for the connected one.
    forwarded: watch::Sender<u64>,
}

struct HubState {
    subs: HashMap<SubscriptionId, Sub>,
    /// The last state forwarded: the snapshot new subscriptions start from.
    last: Option<State>,
    acks: HashMap<u64, PendingAck>,
    next_ack: u64,
    /// Server acks are offered to webviews: Full clients, until they disconnect.
    offer_acks: bool,
    /// Removed from the registry: forwards nothing more.
    closed: bool,
}

struct Sub {
    channel: Channel<Message>,
    events: Option<HashSet<String>>,
    /// The webview it belongs to; `None` for subscriptions made from Rust (no acks offered).
    webview: Option<String>,
}

struct PendingAck {
    sender: AckSender,
    at: Instant,
    /// The webviews it was offered to; the first answer from one of them wins.
    webviews: Vec<String>,
}

/// A subscription before it is attached: what the webview asked for.
pub(crate) struct SubscribeArgs {
    pub events: Option<Vec<String>>,
    pub channel: Channel<Message>,
    pub webview: Option<String>,
}

impl SubscribeArgs {
    fn into_sub(self) -> Sub {
        Sub {
            channel: self.channel,
            events: self.events.map(|e| e.into_iter().collect()),
            webview: self.webview,
        }
    }
}

fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
    m.lock().unwrap_or_else(PoisonError::into_inner)
}

impl fmt::Debug for SocketIo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SocketIo")
            .field("clients", &self.ids())
            .field("reserved", &lock(&self.inner.reserved).len())
            .field("allowed_urls", &self.inner.allowed.len())
            .finish()
    }
}

impl SocketIo {
    pub(crate) fn with_allowed(allowed: Vec<url::Url>) -> Self {
        SocketIo {
            inner: Arc::new(Inner {
                allowed,
                ..Inner::default()
            }),
        }
    }

    /// Keeps `id` for Rust: a webview's `connect` with it fails with [`Error::Reserved`], while
    /// [`connect`](Self::connect) and [`insert`](Self::insert) from Rust work. Call it in
    /// `setup` (it is synchronous) for every id Rust registers later, so no page script can
    /// take the id first. Reservations last for the life of the app.
    pub fn reserve(&self, id: impl Into<String>) {
        lock(&self.inner.reserved).insert(id.into());
    }

    /// Builds `builder`, connects and registers the client under `id` with [`Access::Full`].
    /// Fails with the connect error, or [`Error::AlreadyExists`] (then the new client is
    /// dropped, which closes it).
    pub async fn connect(&self, id: impl Into<String>, builder: ClientBuilder) -> Result<Client> {
        Ok(self.connect_with(id.into(), builder, None, None).await?.0)
    }

    /// The webview's connect: reservations, the URL allow-list and the subscription cap apply;
    /// the subscription is attached before connecting (no event after CONNECT is missed) and
    /// starts with a snapshot of the connected state.
    pub(crate) async fn connect_from_webview(
        &self,
        id: String,
        url: &str,
        engine_path: Option<&str>,
        builder: ClientBuilder,
        dynamic: Arc<Mutex<Dynamic>>,
        sub: SubscribeArgs,
    ) -> Result<(SubscriptionId, State)> {
        if lock(&self.inner.reserved).contains(&id) {
            return Err(Error::Reserved(id));
        }
        if !self.url_allowed(url, engine_path) {
            return Err(Error::UrlNotAllowed);
        }
        let (_, sub) = self
            .connect_with(id, builder, Some(sub), Some(dynamic))
            .await?;
        Ok(sub.expect("a subscription was given"))
    }

    async fn connect_with(
        &self,
        id: String,
        builder: ClientBuilder,
        sub: Option<SubscribeArgs>,
        dynamic: Option<Arc<Mutex<Dynamic>>>,
    ) -> Result<(Client, Option<(SubscriptionId, State)>)> {
        let webview = sub.as_ref().and_then(|s| s.webview.clone());
        // The id and (for a webview) one subscription slot are held while connecting, so
        // concurrent connects can neither share an id nor exceed the cap.
        let pending = {
            let clients = lock(&self.inner.clients);
            let mut pending = lock(&self.inner.pending);
            if clients.contains_key(&id) || pending.ids.contains(&id) {
                return Err(Error::AlreadyExists(id));
            }
            if let Some(label) = &webview {
                let held = pending.slots.get(label).copied().unwrap_or_default();
                check_cap(&clients, label, held)?;
                *pending.slots.entry(label.clone()).or_default() += 1;
            }
            pending.ids.insert(id.clone());
            PendingGuard {
                inner: &self.inner,
                id: id.clone(),
                webview: webview.clone(),
            }
        };
        let generation = webview.as_deref().map(|l| self.generation(l));
        let hub = Hub::new(true);
        let sub_id = sub.map(|args| {
            let sub_id = self.next_sub();
            lock(&hub.state).subs.insert(sub_id, args.into_sub());
            sub_id
        });
        let (on_event, on_state) = (Arc::clone(&hub), Arc::clone(&hub));
        let client = builder
            .on_any(move |event: Event, _: Client| {
                on_event.event(event);
                std::future::ready(())
            })
            .on_state_change(move |state: ConnectionState, _: Client| {
                on_state.state(&state);
                std::future::ready(())
            })
            .connect()
            .await?;
        #[cfg(test)]
        let id_for_test = id.clone();
        {
            let mut clients = lock(&self.inner.clients);
            if clients.contains_key(&id) {
                return Err(Error::AlreadyExists(id));
            }
            clients.insert(
                id,
                Entry {
                    client: client.clone(),
                    access: Access::Full,
                    hub: Arc::clone(&hub),
                    listeners: Vec::new(),
                    owner: dynamic.as_ref().and(webview.clone()),
                    dynamic,
                },
            );
            // registered: the client and its subscription count from here, the pending id
            // and slot do not any more
            drop(pending);
        }
        #[cfg(test)]
        tests::after_insert(&id_for_test).await;
        let Some(sub_id) = sub_id else {
            return Ok((client, None));
        };
        // The page that asked reloaded meanwhile: its subscription would talk to the new page.
        if let (Some(label), Some(before)) = (&webview, generation) {
            if self.generation(label) != before {
                hub.remove_sub(sub_id, None);
            }
        }
        hub.settle(&client).await;
        let state = hub.snapshot_to(sub_id, &client);
        Ok((client, Some((sub_id, state))))
    }

    /// Registers a client built elsewhere, e.g. by Rust code that must stay free of Tauri.
    /// With [`Access::ReadOnly`] the webview can observe it but never emit, ack or disconnect
    /// it, whatever its capabilities allow. The registry keeps a clone; the client lives until
    /// it is removed and every other clone is dropped.
    pub fn insert(&self, id: impl Into<String>, client: Client, access: Access) -> Result<()> {
        let id = id.into();
        let mut clients = lock(&self.inner.clients);
        if clients.contains_key(&id) {
            return Err(Error::AlreadyExists(id));
        }
        let hub = Hub::new(access == Access::Full);
        let (on_event, on_state) = (Arc::clone(&hub), Arc::clone(&hub));
        let listeners = vec![
            client.on_any(move |event: Event, _: Client| {
                on_event.event(event);
                std::future::ready(())
            }),
            client.on_state_change(move |state: ConnectionState, _: Client| {
                on_state.state(&state);
                std::future::ready(())
            }),
        ];
        clients.insert(
            id,
            Entry {
                client,
                access,
                hub,
                listeners,
                dynamic: None,
                owner: None,
            },
        );
        Ok(())
    }

    /// A clone of the client registered under `id`.
    pub fn get(&self, id: &str) -> Option<Client> {
        lock(&self.inner.clients).get(id).map(|e| e.client.clone())
    }

    /// The access the webview has to `id`.
    pub fn access(&self, id: &str) -> Option<Access> {
        lock(&self.inner.clients).get(id).map(|e| e.access)
    }

    /// The registered ids, sorted.
    pub fn ids(&self) -> Vec<String> {
        let mut ids: Vec<_> = lock(&self.inner.clients).keys().cloned().collect();
        ids.sort();
        ids
    }

    /// Unregisters `id`: its subscriptions end (their channels are dropped) and its pending
    /// server acks are forgotten. Does **not** disconnect: the client is handed back (call
    /// [`Client::disconnect`] on it, or drop it; dropping the last clone closes it).
    pub fn remove(&self, id: &str) -> Option<Client> {
        let entry = lock(&self.inner.clients).remove(id)?;
        Some(entry.release())
    }

    /// Forwards the client's state transitions and events (all, or only `events`) to
    /// `channel` as [`Message`]s, starting with a snapshot of the current state. Ends with
    /// [`unsubscribe`](Self::unsubscribe), [`remove`](Self::remove) or when a send fails.
    /// Server acks are not offered to subscriptions made from Rust (answer them in a handler).
    pub fn subscribe(
        &self,
        id: &str,
        events: Option<Vec<String>>,
        channel: Channel<Message>,
    ) -> Result<SubscriptionId> {
        Ok(self
            .subscribe_with(
                id,
                SubscribeArgs {
                    events,
                    channel,
                    webview: None,
                },
            )?
            .0)
    }

    pub(crate) fn subscribe_with(
        &self,
        id: &str,
        args: SubscribeArgs,
    ) -> Result<(SubscriptionId, State)> {
        let clients = lock(&self.inner.clients);
        if let Some(label) = &args.webview {
            let held = lock(&self.inner.pending)
                .slots
                .get(label)
                .copied()
                .unwrap_or_default();
            check_cap(&clients, label, held)?;
        }
        let entry = clients
            .get(id)
            .ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
        let sub_id = self.next_sub();
        let client = &entry.client;
        let state = entry
            .hub
            .attach(sub_id, args.into_sub(), || State::from(&client.state()));
        Ok((sub_id, state))
    }

    /// Stops a subscription; `false` when it was gone already.
    pub fn unsubscribe(&self, id: &str, subscription: SubscriptionId) -> bool {
        self.unsubscribe_from(id, subscription, None)
    }

    /// `webview`: only that webview's subscription (the webview's `unsubscribe`).
    pub(crate) fn unsubscribe_from(
        &self,
        id: &str,
        subscription: SubscriptionId,
        webview: Option<&str>,
    ) -> bool {
        lock(&self.inner.clients)
            .get(id)
            .is_some_and(|e| e.hub.remove_sub(subscription, webview))
    }

    /// A webview starts a new page or is gone: its subscriptions end, its acks are forgotten,
    /// and a JS connect it started meanwhile does not attach.
    pub(crate) fn drop_webview(&self, label: &str) {
        *lock(&self.inner.generations)
            .entry(label.to_owned())
            .or_default() += 1;
        for entry in lock(&self.inner.clients).values() {
            entry.hub.drop_webview(label);
        }
    }

    /// Disconnects and unregisters every client webview `label` connected (its window is gone).
    pub(crate) async fn disconnect_owned(&self, label: &str) {
        let owned: Vec<Entry> = {
            let mut clients = lock(&self.inner.clients);
            let ids: Vec<String> = clients
                .iter()
                .filter(|(_, e)| e.owner.as_deref() == Some(label))
                .map(|(id, _)| id.clone())
                .collect();
            ids.iter().filter_map(|id| clients.remove(id)).collect()
        };
        for entry in owned {
            entry.hub.stop_acks();
            let _ = entry.client.disconnect().await;
            entry.release();
        }
    }

    fn generation(&self, label: &str) -> u64 {
        lock(&self.inner.generations)
            .get(label)
            .copied()
            .unwrap_or_default()
    }

    fn next_sub(&self) -> SubscriptionId {
        self.inner.next_sub.fetch_add(1, Ordering::Relaxed) + 1
    }

    /// The connection goes where an allowed prefix says: same scheme, host and port, and the
    /// **engine.io path** (the `path` option, default `/socket.io/`; the URL's own path is the
    /// namespace and replaced by it) under the prefix's path on a `/` boundary. The namespace
    /// is not part of the check. No allow-list: anything.
    pub(crate) fn url_allowed(&self, url: &str, engine_path: Option<&str>) -> bool {
        if self.inner.allowed.is_empty() {
            return true;
        }
        // dot segments and backslashes are refused outright: they mean something else to the
        // URL parser (and to some servers) than to a prefix comparison
        if let Some(path) = engine_path {
            let lower = path.to_ascii_lowercase();
            if path.contains("..")
                || lower.contains("%2e")
                || lower.contains("%25")
                || lower.contains("%u")
                || path.contains('\\')
                || path.chars().any(char::is_control)
            {
                return false;
            }
        }
        // the URL the connection really uses, built by socketio-next itself
        let mut builder = ClientBuilder::new(url);
        if let Some(path) = engine_path {
            builder = builder.path(path);
        }
        let Ok(engine) = builder.engine_url() else {
            return false;
        };
        let path = engine.path();
        self.inner.allowed.iter().any(|p| {
            let base = p.path().trim_end_matches('/');
            p.scheme() == engine.scheme()
                && p.host_str() == engine.host_str()
                && p.port_or_known_default() == engine.port_or_known_default()
                && (base.is_empty() || path.strip_prefix(base).is_some_and(|r| r.starts_with('/')))
        })
    }

    /// The client for a webview command that drives it: [`Access::Full`] only.
    pub(crate) fn full(&self, id: &str) -> Result<Client> {
        match lock(&self.inner.clients).get(id) {
            None => Err(Error::UnknownClient(id.to_owned())),
            Some(e) if e.access == Access::ReadOnly => Err(Error::AccessDenied(id.to_owned())),
            Some(e) => Ok(e.client.clone()),
        }
    }

    /// `client` for observing commands (any access).
    pub(crate) fn any(&self, id: &str) -> Result<Client> {
        self.get(id)
            .ok_or_else(|| Error::UnknownClient(id.to_owned()))
    }

    /// Disconnects a [`Access::Full`] client and unregisters it. Its subscriptions see the
    /// final `closed` state; no server ack is offered any more.
    pub(crate) async fn disconnect(&self, id: &str) -> Result<()> {
        let entry = {
            let mut clients = lock(&self.inner.clients);
            match clients.get(id).map(|e| e.access) {
                None => return Err(Error::UnknownClient(id.to_owned())),
                Some(Access::ReadOnly) => return Err(Error::AccessDenied(id.to_owned())),
                Some(Access::Full) => clients.remove(id).expect("checked under the lock"),
            }
        };
        entry.hub.stop_acks();
        let result = entry.client.disconnect().await;
        entry.release();
        Ok(result?)
    }

    /// Changes what the next connect attempt of `id` sends (a client a webview connected,
    /// [`Access::Full`]): `f` edits its auth, headers and query.
    pub(crate) fn configure(&self, id: &str, f: impl FnOnce(&mut Dynamic)) -> Result<()> {
        let clients = lock(&self.inner.clients);
        let entry = clients
            .get(id)
            .ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
        if entry.access == Access::ReadOnly {
            return Err(Error::AccessDenied(id.to_owned()));
        }
        let dynamic = entry
            .dynamic
            .as_ref()
            .ok_or_else(|| Error::NotConfigurable(id.to_owned()))?;
        f(&mut crate::models::lock(dynamic));
        Ok(())
    }

    /// Reconnects a client a webview connected. A `disconnect` racing it may unregister the
    /// client meanwhile: then the revived connection is closed again (nothing could see or
    /// close it any more) and the id is reported unknown.
    pub(crate) async fn reconnect(&self, id: &str) -> Result<Client> {
        let (client, hub) = self.webview_client(id)?;
        let result = client.reconnect().await;
        let still_registered = lock(&self.inner.clients)
            .get(id)
            .is_some_and(|e| Arc::ptr_eq(&e.hub, &hub));
        if !still_registered {
            let _ = client.disconnect().await;
            return Err(Error::UnknownClient(id.to_owned()));
        }
        result?;
        Ok(client)
    }

    /// A client a webview connected ([`Access::Full`], not registered from Rust): what
    /// `reconnect` may restart, with its hub (the entry's identity).
    fn webview_client(&self, id: &str) -> Result<(Client, Arc<Hub>)> {
        let clients = lock(&self.inner.clients);
        let entry = clients
            .get(id)
            .ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
        if entry.access == Access::ReadOnly {
            return Err(Error::AccessDenied(id.to_owned()));
        }
        if entry.dynamic.is_none() {
            return Err(Error::NotConfigurable(id.to_owned()));
        }
        Ok((entry.client.clone(), Arc::clone(&entry.hub)))
    }

    /// Answers server ack `ack_id` of client `id`, offered to `webview`.
    pub(crate) fn ack(&self, id: &str, ack_id: u64, webview: &str, payload: Payload) -> Result<()> {
        let hub = {
            let clients = lock(&self.inner.clients);
            match clients.get(id) {
                None => return Err(Error::UnknownClient(id.to_owned())),
                Some(e) if e.access == Access::ReadOnly => {
                    return Err(Error::AccessDenied(id.to_owned()));
                }
                Some(e) => Arc::clone(&e.hub),
            }
        };
        let sender = hub
            .take_ack(ack_id, webview)
            .ok_or(Error::UnknownAck(ack_id))?;
        Ok(sender.send(payload)?)
    }

    pub(crate) fn list(&self) -> Vec<ClientInfo> {
        let clients = lock(&self.inner.clients);
        let mut out: Vec<_> = clients
            .iter()
            .map(|(id, e)| ClientInfo {
                id: id.clone(),
                access: e.access,
                namespace: e.client.namespace().to_owned(),
                state: State::from(&e.client.state()),
            })
            .collect();
        out.sort_by(|a, b| a.id.cmp(&b.id));
        out
    }

    #[cfg(test)]
    fn hub(&self, id: &str) -> Arc<Hub> {
        Arc::clone(&lock(&self.inner.clients)[id].hub)
    }
}

/// Ids and subscription slots held by connects in flight.
#[derive(Default)]
struct Pending {
    ids: HashSet<String>,
    slots: HashMap<String, usize>,
}

/// Gives the pending id and slot back when the connect ends (success or failure; on success
/// the client and its subscription are registered by then).
struct PendingGuard<'a> {
    inner: &'a Inner,
    id: String,
    webview: Option<String>,
}

impl Drop for PendingGuard<'_> {
    fn drop(&mut self) {
        let mut pending = lock(&self.inner.pending);
        pending.ids.remove(&self.id);
        if let Some(label) = &self.webview {
            if let Some(n) = pending.slots.get_mut(label) {
                *n = n.saturating_sub(1);
            }
        }
    }
}

/// A connect by webview `label` (holding `held` connects in flight) fits under both caps.
fn check_cap(clients: &HashMap<String, Entry>, label: &str, held: usize) -> Result<()> {
    let count: usize = clients.values().map(|e| e.hub.count(label)).sum::<usize>() + held;
    if count >= MAX_SUBSCRIPTIONS_PER_WEBVIEW {
        return Err(Error::TooManySubscriptions(MAX_SUBSCRIPTIONS_PER_WEBVIEW));
    }
    let owned = clients
        .values()
        .filter(|e| e.owner.as_deref() == Some(label))
        .count();
    if owned + held >= MAX_CLIENTS_PER_WEBVIEW {
        return Err(Error::TooManyClients(MAX_CLIENTS_PER_WEBVIEW));
    }
    Ok(())
}

impl Entry {
    /// Ends the hub and takes its listeners off the client; hands the client back.
    fn release(self) -> Client {
        self.hub.close();
        for id in self.listeners {
            self.client.off_listener(id);
        }
        self.client
    }
}

impl Hub {
    fn new(offer_acks: bool) -> Arc<Hub> {
        Arc::new(Hub {
            state: Mutex::new(HubState {
                subs: HashMap::new(),
                last: None,
                acks: HashMap::new(),
                next_ack: 0,
                offer_acks,
                closed: false,
            }),
            forwarded: watch::Sender::new(0),
        })
    }

    fn event(&self, event: Event) {
        let mut st = lock(&self.state);
        if st.closed || st.subs.is_empty() {
            return;
        }
        let name = event.name().to_owned();
        let wanted = |s: &Sub| s.events.as_ref().is_none_or(|e| e.contains(&name));
        // One ack id per server ack, offered to every webview that gets the event.
        let mut ack_id = None;
        if let Some(sender) = event.ack_sender().filter(|_| st.offer_acks) {
            let mut webviews: Vec<String> = st
                .subs
                .values()
                .filter(|s| wanted(s))
                .filter_map(|s| s.webview.clone())
                .collect();
            webviews.sort();
            webviews.dedup();
            if !webviews.is_empty() {
                st.acks.retain(|_, a| a.at.elapsed() < ACK_TTL);
                for label in &webviews {
                    st.make_room(label);
                }
                st.next_ack += 1;
                let id = st.next_ack;
                st.acks.insert(
                    id,
                    PendingAck {
                        sender,
                        at: Instant::now(),
                        webviews,
                    },
                );
                ack_id = Some(id);
            }
        }
        let args: crate::Args = event.into_payload().into();
        st.send_all(|s| {
            wanted(s).then(|| Message::Event {
                event: name.clone(),
                args: args.clone(),
                ack_id: ack_id.filter(|_| s.webview.is_some()),
            })
        });
    }

    fn state(&self, state: &ConnectionState) {
        self.forward_state(State::from(state));
    }

    fn forward_state(&self, state: State) {
        let mut st = lock(&self.state);
        if st.closed {
            return;
        }
        st.last = Some(state.clone());
        st.send_all(|_| {
            Some(Message::State {
                state: state.clone(),
                snapshot: false,
            })
        });
        drop(st);
        self.forwarded.send_modify(|n| *n += 1);
    }

    /// Waits (bounded) until the event task forwarded a state past the initial connect, so a
    /// snapshot taken now is the connected state and nothing older follows it.
    async fn settle(&self, client: &Client) {
        let mut rx = self.forwarded.subscribe();
        let settled = |hub: &Hub| {
            !matches!(
                lock(&hub.state).last,
                None | Some(State::Connecting { attempt: 0 })
            )
        };
        let _ = tokio::time::timeout(SETTLE, async {
            while !settled(self) && !client.state().is_closed() {
                if rx.changed().await.is_err() {
                    return;
                }
            }
        })
        .await;
    }

    /// Sends the snapshot (the last forwarded state, else the client's) as the first state
    /// message of `sub`, under the lock that orders every message of this client.
    fn snapshot_to(&self, sub: SubscriptionId, client: &Client) -> State {
        self.snapshot_or(sub, || State::from(&client.state()))
    }

    fn snapshot_or(&self, sub: SubscriptionId, fallback: impl FnOnce() -> State) -> State {
        Self::snapshot_locked(&mut lock(&self.state), sub, fallback)
    }

    /// Adds `sub` and sends its snapshot in one critical section: the snapshot is its very
    /// first message.
    fn attach(&self, id: SubscriptionId, sub: Sub, fallback: impl FnOnce() -> State) -> State {
        let mut st = lock(&self.state);
        st.subs.insert(id, sub);
        Self::snapshot_locked(&mut st, id, fallback)
    }

    fn snapshot_locked(
        st: &mut HubState,
        sub: SubscriptionId,
        fallback: impl FnOnce() -> State,
    ) -> State {
        let state = st.last.clone().unwrap_or_else(fallback);
        if let Some(s) = st.subs.get(&sub) {
            let message = Message::State {
                state: state.clone(),
                snapshot: true,
            };
            if s.channel.send(message).is_err() {
                st.subs.remove(&sub);
            }
        }
        state
    }

    fn remove_sub(&self, sub: SubscriptionId, webview: Option<&str>) -> bool {
        let mut st = lock(&self.state);
        match st.subs.get(&sub) {
            Some(s) if webview.is_none_or(|w| s.webview.as_deref() == Some(w)) => {
                st.subs.remove(&sub);
                true
            }
            _ => false,
        }
    }

    fn drop_webview(&self, label: &str) {
        let mut st = lock(&self.state);
        st.subs.retain(|_, s| s.webview.as_deref() != Some(label));
        st.acks.retain(|_, a| {
            a.webviews.retain(|w| w != label);
            !a.webviews.is_empty()
        });
    }

    fn count(&self, label: &str) -> usize {
        lock(&self.state)
            .subs
            .values()
            .filter(|s| s.webview.as_deref() == Some(label))
            .count()
    }

    fn take_ack(&self, ack_id: u64, webview: &str) -> Option<AckSender> {
        let mut st = lock(&self.state);
        let ok = st
            .acks
            .get(&ack_id)
            .is_some_and(|a| a.at.elapsed() < ACK_TTL && a.webviews.iter().any(|w| w == webview));
        ok.then(|| st.acks.remove(&ack_id).expect("checked").sender)
    }

    fn stop_acks(&self) {
        let mut st = lock(&self.state);
        st.offer_acks = false;
        st.acks.clear();
    }

    fn close(&self) {
        let mut st = lock(&self.state);
        st.closed = true;
        st.subs.clear();
        st.acks.clear();
    }
}

impl HubState {
    /// Sends `make(sub)` to every subscription it returns a message for; a failed send (the
    /// webview is gone) ends that subscription.
    fn send_all(&mut self, make: impl Fn(&Sub) -> Option<Message>) {
        self.subs.retain(|_, s| match make(s) {
            Some(message) => s.channel.send(message).is_ok(),
            None => true,
        });
    }

    /// Keeps `label` under its ack budget by forgetting its own oldest offer.
    ///
    /// ponytail: O(pending acks) per server ack, bounded by the per-webview budget times the
    /// webviews; an ordered queue per webview if that shows up in a profile.
    fn make_room(&mut self, label: &str) {
        let mine = || {
            self.acks
                .iter()
                .filter(|(_, a)| a.webviews.iter().any(|w| w == label))
        };
        if mine().count() < MAX_PENDING_ACKS_PER_WEBVIEW {
            return;
        }
        let Some(oldest) = mine().min_by_key(|(_, a)| a.at).map(|(id, _)| *id) else {
            return;
        };
        let ack = self.acks.get_mut(&oldest).expect("found above");
        ack.webviews.retain(|w| w != label);
        if ack.webviews.is_empty() {
            self.acks.remove(&oldest);
        }
    }
}

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

    /// (client id, reached, go): the connect of that id (only: other tests run in parallel)
    /// waits right after it registered its client.
    type Pause = (String, Arc<Notify>, Arc<Notify>);
    static PAUSE: OnceLock<Mutex<Option<Pause>>> = OnceLock::new();

    pub(super) async fn after_insert(id: &str) {
        let pause = lock(PAUSE.get_or_init(Mutex::default)).clone();
        if let Some((target, reached, go)) = pause {
            if target == id {
                reached.notify_one();
                go.notified().await;
            }
        }
    }

    /// Once the client is registered, its pending id and slot are given back (they counted
    /// twice against the cap while the connect settled).
    #[tokio::test(flavor = "multi_thread")]
    async fn a_registered_connect_holds_no_pending_slot() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let io = SocketIo::default();
            let (reached, go) = (Arc::new(Notify::new()), Arc::new(Notify::new()));
            *lock(PAUSE.get_or_init(Mutex::default)) =
                Some(("p".into(), Arc::clone(&reached), Arc::clone(&go)));
            let (ch, _rx) = channel();
            let connecting = {
                let io = io.clone();
                tokio::spawn(async move {
                    let url = conformance_server();
                    let builder = ClientBuilder::new(url.clone());
                    io.connect_from_webview(
                        "p".into(),
                        &url,
                        None,
                        builder,
                        Default::default(),
                        sub(ch, "w"),
                    )
                    .await
                })
            };
            reached.notified().await;
            *lock(PAUSE.get().unwrap()) = None;
            {
                let pending = lock(&io.inner.pending);
                assert!(pending.ids.is_empty(), "the id is still pending");
                assert_eq!(pending.slots.get("w").copied().unwrap_or_default(), 0);
            }
            go.notify_one();
            connecting.await.unwrap().unwrap();
            io.remove("p").unwrap().disconnect().await.unwrap();
        })
        .await
        .expect("test `a_registered_connect_holds_no_pending_slot` timed out")
    }
    use socketio_next_testing::{conformance_server, socket_io_server};
    use tauri::ipc::InvokeResponseBody;

    fn channel() -> (
        Channel<Message>,
        std::sync::mpsc::Receiver<serde_json::Value>,
    ) {
        let (tx, rx) = std::sync::mpsc::channel();
        let channel = Channel::new(move |body| {
            if let InvokeResponseBody::Json(json) = body {
                let _ = tx.send(serde_json::from_str(&json).unwrap());
            }
            Ok(())
        });
        (channel, rx)
    }

    fn sub(channel: Channel<Message>, webview: &str) -> SubscribeArgs {
        SubscribeArgs {
            events: None,
            channel,
            webview: Some(webview.to_owned()),
        }
    }

    /// A real server event with an ack (they are only made by the client): `question`.
    async fn question(client_events: &mut tokio::sync::mpsc::UnboundedReceiver<Event>) -> Event {
        loop {
            let e = client_events.recv().await.unwrap();
            if e.name() == "question" {
                return e;
            }
        }
    }

    async fn asking_client() -> (Client, tokio::sync::mpsc::UnboundedReceiver<Event>) {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        let client = ClientBuilder::new(conformance_server())
            .on("question", move |event: Event, _: Client| {
                let _ = tx.send(event);
                std::future::ready(())
            })
            .connect()
            .await
            .unwrap();
        (client, rx)
    }

    /// Finding 7: once a disconnect started, server acks are no longer offered or kept.
    #[tokio::test(flavor = "multi_thread")]
    async fn no_acks_are_minted_after_stop_acks() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let (client, mut events) = asking_client().await;
            let hub = Hub::new(true);
            let (ch, rx) = channel();
            lock(&hub.state).subs.insert(1, sub(ch, "w").into_sub());
            client.emit("ask", ()).unwrap();
            let q = question(&mut events).await;
            hub.stop_acks();
            hub.event(q);
            let m = rx.recv_timeout(Duration::from_secs(5)).unwrap();
            assert_eq!(m["event"], "question");
            assert!(m.get("ackId").is_none(), "{m}");
            assert!(lock(&hub.state).acks.is_empty());
            client.disconnect().await.unwrap();
        })
        .await
        .expect("test `no_acks_are_minted_after_stop_acks` timed out")
    }

    /// Finding 3: a webview's flood of acks forgets only its own oldest offers.
    #[tokio::test(flavor = "multi_thread")]
    async fn ack_budget_is_per_webview() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let (client, mut events) = asking_client().await;
            let hub = Hub::new(true);
            let (greedy, _g) = channel();
            let (polite, polite_rx) = channel();
            lock(&hub.state)
                .subs
                .insert(1, sub(greedy, "greedy").into_sub());
            // the polite webview gets the first question only
            lock(&hub.state)
                .subs
                .insert(2, sub(polite, "polite").into_sub());
            client.emit("ask", ()).unwrap();
            hub.event(question(&mut events).await);
            let first = polite_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"]
                .as_u64()
                .unwrap();
            hub.remove_sub(2, None);
            let q = question_with_ack(&client, &mut events).await;
            for _ in 0..MAX_PENDING_ACKS_PER_WEBVIEW + 10 {
                hub.event(q.clone());
            }
            let greedy_count = lock(&hub.state)
                .acks
                .values()
                .filter(|a| a.webviews.iter().any(|w| w == "greedy"))
                .count();
            assert_eq!(greedy_count, MAX_PENDING_ACKS_PER_WEBVIEW);
            assert!(
                hub.take_ack(first, "greedy").is_none(),
                "greedy's own oldest went"
            );
            assert!(
                hub.take_ack(first, "polite").is_some(),
                "the polite webview kept its ack"
            );
            client.disconnect().await.unwrap();
        })
        .await
        .expect("test `ack_budget_is_per_webview` timed out")
    }

    async fn question_with_ack(
        client: &Client,
        events: &mut tokio::sync::mpsc::UnboundedReceiver<Event>,
    ) -> Event {
        client.emit("ask", ()).unwrap();
        question(events).await
    }

    /// Finding 8: one server ack, one ack id for every subscription, answered once.
    #[tokio::test(flavor = "multi_thread")]
    async fn one_ack_id_per_server_ack() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let (client, mut events) = asking_client().await;
            let hub = Hub::new(true);
            let (a, a_rx) = channel();
            let (b, b_rx) = channel();
            lock(&hub.state).subs.insert(1, sub(a, "w1").into_sub());
            lock(&hub.state).subs.insert(2, sub(b, "w2").into_sub());
            hub.event(question_with_ack(&client, &mut events).await);
            let ida = a_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"].clone();
            let idb = b_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"].clone();
            assert_eq!(ida, idb);
            let id = ida.as_u64().unwrap();
            assert!(hub.take_ack(id, "w3").is_none(), "not offered to w3");
            assert!(hub.take_ack(id, "w2").is_some());
            assert!(hub.take_ack(id, "w1").is_none(), "answered once");
            client.disconnect().await.unwrap();
        })
        .await
        .expect("test `one_ack_id_per_server_ack` timed out")
    }

    /// Finding 5: the snapshot is the first state message and nothing older follows it, even
    /// while states are being forwarded.
    #[test]
    fn snapshot_is_ordered_with_transitions() {
        let hub = Hub::new(true);
        let (ch0, _rx0) = channel();
        lock(&hub.state).subs.insert(1, sub(ch0, "w").into_sub());
        let writer = {
            let hub = Arc::clone(&hub);
            std::thread::spawn(move || {
                for attempt in 1..=2000 {
                    hub.forward_state(State::Connecting { attempt });
                }
            })
        };
        // subscribe while the writer runs
        let mut seqs = Vec::new();
        for n in 0..20u64 {
            let (ch, rx) = channel();
            let snapshot = hub.attach(100 + n, sub(ch, "w").into_sub(), || State::Connecting {
                attempt: 0,
            });
            seqs.push((snapshot, rx));
        }
        writer.join().unwrap();
        for (snapshot, rx) in seqs {
            let attempts: Vec<u64> = rx
                .try_iter()
                .map(|m| m["state"]["attempt"].as_u64().unwrap())
                .collect();
            let State::Connecting { attempt } = snapshot else {
                unreachable!()
            };
            assert_eq!(attempts[0], u64::from(attempt), "the snapshot comes first");
            assert!(
                attempts.windows(2).all(|w| w[0] < w[1]),
                "ordered: {attempts:?}"
            );
        }
    }

    /// The real `snapshot_to` with the client fallback and the last forwarded state.
    #[tokio::test(flavor = "multi_thread")]
    async fn snapshot_uses_the_last_forwarded_state() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let client = ClientBuilder::new(socket_io_server())
                .connect()
                .await
                .unwrap();
            let hub = Hub::new(true);
            let (ch, rx) = channel();
            lock(&hub.state).subs.insert(1, sub(ch, "w").into_sub());
            assert!(
                matches!(hub.snapshot_to(1, &client), State::Connected { .. }),
                "fallback"
            );
            hub.forward_state(State::Disconnected {
                reason: "transport close".into(),
                error: None,
            });
            let (ch2, rx2) = channel();
            lock(&hub.state).subs.insert(2, sub(ch2, "w").into_sub());
            assert!(matches!(
                hub.snapshot_to(2, &client),
                State::Disconnected { .. }
            ));
            assert_eq!(rx2.recv().unwrap()["snapshot"], true);
            assert_eq!(rx.recv().unwrap()["state"]["status"], "connected");
            client.disconnect().await.unwrap();
        })
        .await
        .expect("test `snapshot_uses_the_last_forwarded_state` timed out")
    }

    /// Finding 4: a page reload while its connect is in flight leaves no subscription.
    #[tokio::test(flavor = "multi_thread")]
    async fn reload_during_connect_attaches_nothing() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let io = SocketIo::default();
            let (ch, _rx) = channel();
            let connecting = {
                let io = io.clone();
                tokio::spawn(async move {
                    // /slow answers CONNECT after 200 ms
                    let url = format!("{}/slow", socket_io_server());
                    let builder = ClientBuilder::new(url.clone());
                    io.connect_from_webview(
                        "slow".into(),
                        &url,
                        None,
                        builder,
                        Default::default(),
                        sub(ch, "w"),
                    )
                    .await
                })
            };
            tokio::time::sleep(Duration::from_millis(50)).await;
            io.drop_webview("w");
            connecting.await.unwrap().unwrap();
            assert_eq!(io.hub("slow").count("w"), 0);
            io.remove("slow").unwrap().disconnect().await.unwrap();
        })
        .await
        .expect("test `reload_during_connect_attaches_nothing` timed out")
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_reloaded_webview_loses_its_subscriptions_and_acks() {
        tokio::time::timeout(std::time::Duration::from_secs(90), async move {
            let io = SocketIo::default();
            io.connect("c", ClientBuilder::new(conformance_server()))
                .await
                .unwrap();
            let (a, _) = channel();
            let (b, _) = channel();
            let (sa, _) = io.subscribe_with("c", sub(a, "a")).unwrap();
            let (sb, _) = io.subscribe_with("c", sub(b, "b")).unwrap();
            io.drop_webview("a");
            assert!(!io.unsubscribe("c", sa));
            assert!(io.unsubscribe("c", sb));
            io.remove("c").unwrap().disconnect().await.unwrap();
        })
        .await
        .expect("test `a_reloaded_webview_loses_its_subscriptions_and_acks` timed out")
    }

    #[test]
    fn url_allow_list() {
        let io = SocketIo::with_allowed(vec![
            url::Url::parse("https://api.example.com").unwrap(),
            url::Url::parse("wss://rt.example.com/v1/").unwrap(),
        ]);
        let ok = |url: &str, path: Option<&str>| io.url_allowed(url, path);
        assert!(ok("https://api.example.com", None));
        assert!(ok(
            "https://api.example.com:443/admin?x=1",
            Some("/anything/")
        ));
        assert!(!ok("https://api.example.com.evil.com", None));
        assert!(!ok("https://api.example.com:8443", None));
        assert!(!ok("http://api.example.com", None));
        // the engine.io path is what is checked: the URL's path is only the namespace
        assert!(ok("wss://rt.example.com", Some("/v1/socket.io")));
        assert!(ok("wss://rt.example.com/v1", Some("v1/")));
        assert!(
            !ok("wss://rt.example.com/v1", None),
            "default /socket.io/ is outside /v1/"
        );
        assert!(!ok("wss://rt.example.com/v1/chat", Some("/v10/")));
        assert!(!ok("not a url", None));
        assert!(SocketIo::default().url_allowed("http://anything", Some("/x/")));
        // dot segments and backslashes cannot climb out of the prefix
        for path in [
            "/v1/../admin/",
            "/v1/%2e%2e/admin/",
            "/v1/%2E%2E/admin/",
            "/v1/..\\admin/",
            "/v1/.%2e/admin/",
            // the URL parser drops tabs and newlines: these are `..` once parsed
            "/v1/.\n./admin/",
            "/v1/\t../admin/",
            "/v1/%252e%252e/admin/",
            "/v1/%u002e%u002e/admin/",
        ] {
            assert!(!ok("wss://rt.example.com", Some(path)), "{path:?}");
        }
        // a plain path inside the prefix passes the textual checks and the resolution
        assert!(ok("wss://rt.example.com", Some("/v1/./socket.io/")));
        // only the resolved URL gets this right: `/v1/./socket.io/` is `/v1/socket.io/`,
        // inside a prefix of exactly that path (the raw text is not)
        let deep = SocketIo::with_allowed(vec![
            url::Url::parse("wss://rt.example.com/v1/socket.io/").unwrap(),
        ]);
        assert!(deep.url_allowed("wss://rt.example.com", Some("/v1/./socket.io/")));
        assert!(!deep.url_allowed("wss://rt.example.com", Some("/v1/./other/")));
    }

    #[test]
    fn unknown_ids() {
        let io = SocketIo::default();
        let channel = Channel::new(|_: InvokeResponseBody| Ok(()));
        assert!(matches!(
            io.subscribe("x", None, channel),
            Err(Error::UnknownClient(_))
        ));
        assert!(matches!(io.full("x"), Err(Error::UnknownClient(_))));
        assert!(matches!(
            io.ack("x", 1, "w", Payload::empty()),
            Err(Error::UnknownClient(_))
        ));
        assert!(!io.unsubscribe("x", 1));
        assert!(io.remove("x").is_none());
        assert!(io.ids().is_empty());
    }
}