sew-call-client 0.9.6

Sew's native voice engine: LiveKit-protocol signalling and reconnect against Sew's media server, str0m transport with TURN over UDP, TCP and TLS (relay-only when asked), MLS call keys with the mandatory E2EE checks over the call credential and the call socket, the device store, and the kessel-audio bridge.
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
//! The key session: everything between a join answer and active call keys.
//!
//! The order after a join answer:
//! 1. open the call socket and read READY's `session_id` (the join's `e2ee.gateway_session`) and
//!    the trust anchors (TOFU-pinned in the device file);
//! 2. load or enrol the device ([`crate::device::DeviceVault::ensure_device`]);
//! 3. upload a KeyPackage for `call:<channel_id>`;
//! 4. key-join: the key join route with the credential's `join_id` ([`MlsSession::key_join`],
//!    the deferred key join and every move), or inline in the voice join (the host's join carried
//!    [`MlsSession::prepare_join`]'s `e2ee`, then [`MlsSession::begin`]);
//! 5. drive `sew-e2ee`'s `CallSession`: `call_keys_delivered` in, the call key routes out,
//!    `KeyEvent`s into [`MediaKeys`], the media path's reports back as `report` inputs.
//!
//! Recording (Sew 0.7.3): the call socket's own `recording_updated` and READY's
//! `recording` reach the owner as [`KeySessionEvent::Recording`]; the owner (the engine)
//! decides which recording is the call's and feeds it back with [`MlsSession::set_recording`],
//! which `sew-e2ee` turns into the recorder key release (only for `granted`) or a rekey on a
//! withdrawal. The last state is applied again to every new key session (a move, a re-join), so
//! a new session never starts from a stale `granted`: it starts `pending` until fed.
//!
//! The credential is renewed at half its life and the socket re-authenticated (REAUTH). A device
//! the server refuses (revoked, or its certificate failed) is forgotten, enrolled once and the
//! step retried once (Sew 0.7.1's rule); the pins are kept. A pin mismatch ends with
//! `ca_pin_mismatch` and is never re-pinned.
//!
//! The session and the bridge sit behind a short-held mutex; HTTP runs with it released.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use sew_e2ee::keypackage::on_join_failure;
use sew_e2ee::{
    ApiFailure, CallSession, E2eeClient, E2eeStatus, Input, JoinId, KeyEvent, KeySink as _,
    LeftReason, LkIdentity, Output, PreparedKp, SessionState, SinkAck, TrustAnchors,
    UnknownCallBuffer,
};
use sew_proto::e2ee::{CallKeysInit, CallScope, ConsentState, E2eeJoin};
use sew_proto::id::Id;
use sew_proto::recording::{Recording, RecordingControls};
use sew_proto::voice::{MediaPolicy, VoiceServerUpdated, VoiceStateUpdated};
use tokio::sync::mpsc;
use url::Url;

use super::credential::{Bearer, CredentialGrant, RenewRefused, SharedBearer, renew_at, unix_ms};
use super::frame::FrameCrypto;
use super::keys::{MediaKeys, MediaReport, engine_caps, lock};
use super::transport::{KeyTransport, TransportError};
use crate::Clock;
use crate::device::{
    DeviceError, DeviceKey, DeviceVault, DeviceWarning, VaultConfig, is_device_refused,
};
use crate::gateway::{CallReadyInfo, CallSocket, SocketClosed, SocketError, SocketEvent};

/// How long [`MlsSession::connect`] waits for the call socket's READY.
pub const READY_TIMEOUT: Duration = Duration::from_secs(15);
/// The driver wakes at least this often (timers, the media path's reports).
pub const TICK: Duration = Duration::from_millis(100);

/// What the key session needs.
#[derive(Clone)]
#[non_exhaustive]
pub struct KeySessionConfig {
    /// The door (`JoinAnswer.api_base`).
    pub api_base: Url,
    /// The bearer: the call credential's token (a `sews_` session token only in tests).
    pub bearer: Bearer,
    /// When the credential expires (unix ms); `None`: never renewed (a `sews_` test bearer).
    pub credential_expires_at_ms: Option<u64>,
    /// The credential's `device_scope` (the stored device is keyed on it).
    pub device_scope: String,
    /// Where the device lives.
    pub vault: VaultConfig,
    /// The install id when the call socket's READY does not carry one.
    pub install_id: Option<u64>,
    /// The clock (unix ms).
    pub clock: Clock,
    /// The TLS trust of the key routes and the call socket (`None`: the platform's, as the
    /// engine's own connections).
    pub tls: Option<Arc<rustls::ClientConfig>>,
}

impl fmt::Debug for KeySessionConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KeySessionConfig")
            .field("host", &self.api_base.host_str())
            .field("bearer", &self.bearer)
            .field("device_scope", &self.device_scope)
            .finish_non_exhaustive()
    }
}

impl KeySessionConfig {
    /// A key session config with its required fields: no `credential_expires_at_ms` (never
    /// renewed), no `install_id` (READY's) and no `tls` (the platform's trust).
    pub fn new(
        api_base: Url,
        bearer: Bearer,
        device_scope: impl Into<String>,
        vault: VaultConfig,
        clock: Clock,
    ) -> KeySessionConfig {
        KeySessionConfig {
            api_base,
            bearer,
            credential_expires_at_ms: None,
            device_scope: device_scope.into(),
            vault,
            install_id: None,
            clock,
            tls: None,
        }
    }

    /// From the join answer's credential.
    pub fn from_grant(
        api_base: Url,
        grant: &CredentialGrant,
        vault: VaultConfig,
        clock: Clock,
    ) -> KeySessionConfig {
        KeySessionConfig {
            api_base,
            bearer: Bearer::new(grant.token.clone()),
            credential_expires_at_ms: Some(unix_ms(grant.expires_at)),
            device_scope: grant.device_scope.clone(),
            vault,
            install_id: None,
            clock,
            tls: None,
        }
    }
}

/// Why the key session failed (the `errors` codes in brackets).
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum KeyError {
    /// The door or the client could not be set up.
    #[error("transport: {0}")]
    Transport(String),
    /// The call socket closed before READY, or READY was unreadable.
    #[error("the call socket did not become ready")]
    NoReady,
    /// READY carried no trust anchors (`call_keys`).
    #[error("the call socket's READY has no call_keys")]
    NoTrustAnchors,
    /// No install id (neither READY nor the config).
    #[error("no install id")]
    NoInstallId,
    /// A pinned server key changed (`ca_pin_mismatch`). Only `reset_device` clears the pins.
    #[error("the server's call identity changed ({0})")]
    PinMismatch(&'static str),
    /// READY's trust anchors are malformed.
    #[error("trust anchors refused: {0}")]
    Trust(String),
    /// The device store or enrolment failed.
    #[error("device: {0}")]
    Device(#[from] DeviceError),
    /// The server refused the key join for good (`e2ee_refused`).
    #[error("key join refused ({status} {reason:?})")]
    Refused {
        /// HTTP status.
        status: u16,
        /// `details.reason`.
        reason: Option<String>,
    },
    /// The MLS session could not start or failed.
    #[error("session: {0}")]
    Session(String),
    /// The key session is closed.
    #[error("closed")]
    Closed,
}

impl From<TransportError> for KeyError {
    fn from(e: TransportError) -> Self {
        KeyError::Transport(e.to_string())
    }
}

impl From<SocketError> for KeyError {
    fn from(e: SocketError) -> Self {
        KeyError::Transport(e.to_string())
    }
}

impl KeyError {
    /// The plugin's `errors.code` / `ended.reason`.
    pub fn code(&self) -> &'static str {
        match self {
            KeyError::PinMismatch(_) => "ca_pin_mismatch",
            KeyError::Refused { .. } | KeyError::Session(_) => "e2ee_refused",
            KeyError::Device(_) => "e2ee_refused",
            KeyError::Closed => "credential_lost",
            _ => "network",
        }
    }
}

fn refused(f: &ApiFailure) -> KeyError {
    KeyError::Refused {
        status: f.status,
        reason: f.reason.clone(),
    }
}

/// Why the key session ended by itself.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyEnd {
    /// The credential is gone (`4011`, or a renewal refused): `credential_lost`. `rejoin` is
    /// true when the engine may re-join through the `JoinProvider` (`expired`).
    CredentialLost {
        /// Why.
        reason: String,
        /// Whether a re-join may follow.
        rejoin: bool,
    },
    /// Sew ended this join and said why (Sew 0.7.2): the `4011` close of the call socket named
    /// the reason ([`SocketClosed::Ended`]); `reason` is Sew's voice leave reason as it
    /// arrived (`replaced`: another device took the call over).
    Ended {
        /// Why.
        reason: String,
    },
    /// The call socket ended for good (`4004`, a protocol error).
    Socket(SocketClosed),
    /// The MLS session left (removed, or failed for good).
    Left(LeftReason),
    /// The device key could not sign in the call (a hardware key's error: `e2ee_refused`; no
    /// software fallback).
    Device(DeviceError),
}

/// What the key session tells the engine.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum KeySessionEvent {
    /// The E2EE status changed (`connected` needs `Active`).
    Status(E2eeStatus),
    /// The verified roster changed: re-check tracks that were `SubscribeDecision::Wait`.
    Roster,
    /// `voice_server_updated` (a move: reconnect signalling with its token; the key session
    /// key-joins the new `join_id` by itself).
    VoiceServerUpdated(Box<VoiceServerUpdated>),
    /// `voice_state_updated` of the call's channel (the plugin's `participants`).
    VoiceStateUpdated(Box<VoiceStateUpdated>),
    /// A recording as this member sees it (the socket's own `recording_updated`, or the call
    /// socket's READY `recording`); `None`: READY says the call has no active recording. Never
    /// fed to the keys by the session itself: see [`MlsSession::set_recording`].
    Recording(Option<Box<Recording>>),
    /// The member's Record and Stop from a server that computes them: the call socket's READY
    /// `recording_controls` (`channel_id` `None`: the call's room; `controls` `None`: no value
    /// yet) or its `recording_controls_updated`.
    RecordingControls {
        /// The room's channel, when the server named it.
        channel_id: Option<Id>,
        /// The value, when the server has one.
        controls: Option<RecordingControls>,
    },
    /// `media_policy_updated` (new layer hints, video on or off).
    MediaPolicy(Box<MediaPolicy>),
    /// A warning for the host (`key_storage_file`, `pins_reset`).
    Warning(DeviceWarning),
    /// A move's key join failed.
    KeyJoinFailed(KeyError),
    /// The key session ended by itself.
    Ended(KeyEnd),
}

struct Core<C: FrameCrypto> {
    session: Option<CallSession>,
    sink: MediaKeys<C>,
    client: E2eeClient,
    signer: DeviceKey,
    prepared: BTreeMap<[u8; 32], PreparedKp>,
    buffer: UnknownCallBuffer,
    gateway_session: String,
    voice_members: BTreeSet<Id>,
    channel: Option<Id>,
    last_verified: BTreeSet<LkIdentity>,
    /// The recording state the owner fed last ([`MlsSession::set_recording`]): applied to every
    /// new `CallSession`.
    recording: Option<(Option<Id>, ConsentState)>,
    ended: bool,
}

struct Shared<C: FrameCrypto> {
    core: Mutex<Core<C>>,
    http: KeyTransport,
    vault: tokio::sync::Mutex<DeviceVault>,
    clock: Clock,
    events: mpsc::UnboundedSender<KeySessionEvent>,
    member: Id,
    changed: tokio::sync::Notify,
}

/// One call's key session.
pub struct MlsSession<C: FrameCrypto> {
    shared: Arc<Shared<C>>,
    socket: Arc<CallSocket>,
    stop: Arc<tokio::sync::Notify>,
}

impl<C: FrameCrypto> fmt::Debug for MlsSession<C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MlsSession")
            .field("member", &self.shared.member)
            .field("status", &self.status())
            .finish_non_exhaustive()
    }
}

impl<C: FrameCrypto> Drop for MlsSession<C> {
    fn drop(&mut self) {
        self.stop.notify_one();
        self.socket.close();
    }
}

/// Builds the trust anchors from READY against the pins (TOFU; a change is refused) and pages
/// the CRL when READY's is partial.
async fn trust_from_ready(
    ready: &CallReadyInfo,
    install: u64,
    vault: &mut DeviceVault,
    http: &KeyTransport,
) -> Result<TrustAnchors, KeyError> {
    let ck = ready.call_keys.as_ref().ok_or(KeyError::NoTrustAnchors)?;
    let mut trust =
        TrustAnchors::from_ready(ck, install, vault.file_mut()).map_err(|e| match e {
            sew_e2ee::TrustError::ServerIdentityChanged { what } => KeyError::PinMismatch(what),
            other => KeyError::Trust(other.to_string()),
        })?;
    if trust.crl_partial() {
        let mut after = None;
        for _ in 0..64 {
            let Ok(page) = http.crl_page(after).await else {
                break;
            };
            trust.revoke(&page.items.iter().map(|s| s.get()).collect::<Vec<_>>());
            after = page.items.last().copied();
            if !page.has_more || after.is_none() {
                break;
            }
        }
    }
    Ok(trust)
}

impl<C: FrameCrypto> MlsSession<C> {
    /// Steps 1–2: opens the call socket, reads READY, pins the trust anchors, loads or enrols
    /// the device, and starts the driver. Events arrive on the returned receiver.
    pub async fn connect(
        cfg: KeySessionConfig,
        keys: MediaKeys<C>,
    ) -> Result<(MlsSession<C>, mpsc::UnboundedReceiver<KeySessionEvent>), KeyError> {
        let bearer = SharedBearer::new(cfg.bearer.clone());
        let tls = match &cfg.tls {
            Some(t) => t.clone(),
            None => {
                crate::config::platform_tls().map_err(|e| KeyError::Transport(e.to_string()))?
            }
        };
        let http = KeyTransport::new(&cfg.api_base, bearer.clone(), &tls)?;
        let (socket, mut socket_rx) = CallSocket::open(&cfg.api_base, bearer.clone(), tls)?;
        let ready = match tokio::time::timeout(READY_TIMEOUT, socket_rx.recv()).await {
            Ok(Some(SocketEvent::Ready(r))) => *r,
            Ok(Some(SocketEvent::Closed(SocketClosed::Revoked | SocketClosed::Ended { .. }))) => {
                return Err(KeyError::Closed);
            }
            _ => return Err(KeyError::NoReady),
        };
        let install = ready
            .install_id
            .map(|i| i.get())
            .or(cfg.install_id)
            .ok_or(KeyError::NoInstallId)?;
        let member = ready.member_id.ok_or(KeyError::NoReady)?;
        tracing::debug!(target: "sew_call_client::keys", member = %member, install, "call socket ready");
        let mut vault =
            DeviceVault::open_async(&cfg.vault, install, member, &cfg.device_scope).await?;
        let (ev_tx, ev_rx) = mpsc::unbounded_channel();
        for w in vault.take_warnings() {
            let _ = ev_tx.send(KeySessionEvent::Warning(w));
        }
        if let Some(r) = ready.recording.clone() {
            let _ = ev_tx.send(KeySessionEvent::Recording(r.map(Box::new)));
        }
        if let Some(c) = ready.recording_controls.clone() {
            let _ = ev_tx.send(KeySessionEvent::RecordingControls {
                channel_id: None,
                controls: c,
            });
        }
        let trust = trust_from_ready(&ready, install, &mut vault, &http).await?;
        let now = (cfg.clock)();
        let (identity, signer) = vault.ensure_device(&http, &trust, member, now).await?;
        let params = trust.params().map_err(|e| KeyError::Trust(e.to_string()))?;
        let client = E2eeClient::new(identity, trust);
        let buffer = UnknownCallBuffer::new(&params, client.config());
        let shared = Arc::new(Shared {
            core: Mutex::new(Core {
                session: None,
                sink: keys,
                client,
                signer,
                prepared: BTreeMap::new(),
                buffer,
                gateway_session: ready.session_id.clone(),
                voice_members: BTreeSet::new(),
                channel: None,
                last_verified: BTreeSet::new(),
                recording: None,
                ended: false,
            }),
            http,
            vault: tokio::sync::Mutex::new(vault),
            clock: cfg.clock.clone(),
            events: ev_tx,
            member,
            changed: tokio::sync::Notify::new(),
        });
        let socket = Arc::new(socket);
        let stop = Arc::new(tokio::sync::Notify::new());
        let renew = cfg
            .credential_expires_at_ms
            .filter(|_| cfg.bearer.is_call_credential())
            .map(|exp| renew_at(now, exp));
        tokio::spawn(drive(
            Arc::downgrade(&shared),
            socket.clone(),
            socket_rx,
            stop.clone(),
            renew,
        ));
        Ok((
            MlsSession {
                shared,
                socket,
                stop,
            },
            ev_rx,
        ))
    }

    /// The call socket's `session_id` (the join's `e2ee.gateway_session`).
    pub fn gateway_session(&self) -> String {
        lock(&self.shared.core).gateway_session.clone()
    }

    /// The member.
    pub fn member_id(&self) -> Id {
        self.shared.member
    }

    /// The key state the media path reads.
    pub fn media_keys(&self) -> MediaKeys<C> {
        lock(&self.shared.core).sink.clone()
    }

    /// The device in use (its id).
    pub fn device_id(&self) -> Id {
        lock(&self.shared.core).client.identity().device_id()
    }

    /// The last E2EE status.
    pub fn status(&self) -> Option<E2eeStatus> {
        lock(&self.shared.core)
            .session
            .as_ref()
            .and_then(|s| s.status().cloned())
    }

    /// The MLS session state.
    pub fn state(&self) -> Option<SessionState> {
        lock(&self.shared.core).session.as_ref().map(|s| s.state())
    }

    /// Whether a key epoch is active and the local sender has a key (`connected`).
    pub fn is_active(&self) -> bool {
        let c = lock(&self.shared.core);
        let active = c.session.as_ref().is_some_and(|s| {
            matches!(s.state(), SessionState::Active { .. }) && s.active_epoch().is_some()
        });
        active && c.sink.local_slot().is_some()
    }

    /// The local engine identity of the current join.
    pub fn lk_identity(&self) -> Option<LkIdentity> {
        lock(&self.shared.core)
            .session
            .as_ref()
            .map(|s| s.lk_identity().clone())
    }

    /// Waits until `pred` holds or `timeout` passes.
    pub async fn wait_until(&self, timeout: Duration, pred: impl Fn(&Self) -> bool) -> bool {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            if pred(self) {
                return true;
            }
            if tokio::time::Instant::now() >= deadline {
                return false;
            }
            let n = self.shared.changed.notified();
            let _ = tokio::time::timeout(Duration::from_millis(50), n).await;
        }
    }

    /// Steps 3 and 4 inline: a KeyPackage for `call:<channel>` uploaded, and the `e2ee` object
    /// of a voice or DM call join. Hand the join's `CallKeysInit` to [`MlsSession::begin`].
    pub async fn prepare_join(&self, channel: Id) -> Result<E2eeJoin, KeyError> {
        let kp = self.prepared_kp(CallScope::call(channel)).await?;
        let mut guard = lock(&self.shared.core);
        let c = &mut *guard;
        let join = kp
            .join_field(&engine_caps(), &c.gateway_session)
            .map_err(|e| KeyError::Session(e.to_string()))?;
        c.channel = Some(channel);
        c.prepared.insert(kp.key_package_ref(), kp);
        Ok(join)
    }

    /// Starts the MLS session from a voice join answer (`VoiceJoinResponse.e2ee`).
    pub async fn begin(&self, init: CallKeysInit) -> Result<(), KeyError> {
        start_session(&self.shared, init, None).await
    }

    /// Steps 3–4 for a deferred key join or a move: a KeyPackage for `call:<channel>`, then the key
    /// join with `join_id`, then the session. A stale ref is re-uploaded once; a refused device is
    /// forgotten, enrolled once and the join retried once.
    pub async fn key_join(&self, channel: Id, join_id: JoinId) -> Result<(), KeyError> {
        key_join(&self.shared, channel, join_id).await
    }

    /// Leaves: the session zeroizes its secrets, every remote key is dropped, the socket closes.
    /// (The voice leave itself is `POST /api/v1/me/voice/leave`, the engine's.)
    pub async fn leave(&self) {
        let now = (self.shared.clock)();
        {
            let mut guard = lock(&self.shared.core);
            let c = &mut *guard;
            if let Some(s) = c.session.as_mut() {
                s.leave(now);
            }
            let _ = c.sink.apply(KeyEvent::Clear);
            c.ended = true;
        }
        pump(&self.shared).await;
        self.stop.notify_one();
        self.socket.close();
    }

    /// The host's explicit `reset_device` (a Rust API, never a webview command): forgets the
    /// device, its key and the pins.
    pub async fn reset_device(&self) -> Result<(), KeyError> {
        let mut v = self.shared.vault.lock().await;
        v.reset().await?;
        Ok(())
    }

    /// The live bearer (renewed in place).
    pub fn bearer(&self) -> Bearer {
        self.shared.http.bearer().get()
    }

    /// The device's network changed: the call socket reconnects at once and resumes.
    pub fn network_changed(&self) {
        self.socket.reconnect();
    }

    /// The engine renewed the call credential: the key routes use the new token from now on,
    /// and the call socket re-authenticates with it (REAUTH keeps the `session_id`).
    pub fn renewed(&self, token: Bearer) {
        self.shared.http.bearer().set(token);
        self.socket.reauth();
    }

    /// The call's recording state as Sew said it (never local state): the live recording of
    /// the call's room (`None`: none) and this member's consent for it. `sew-e2ee` releases this
    /// device's key to the recorder only while the recording is announced, live and
    /// `Granted`, and asks for an immediate rekey when `Granted` ends (a withdrawal). Kept and
    /// applied again to each new key session of this socket.
    pub async fn set_recording(&self, recording_id: Option<Id>, my_consent: ConsentState) {
        {
            let mut guard = lock(&self.shared.core);
            let c = &mut *guard;
            if c.recording == Some((recording_id, my_consent)) {
                return;
            }
            c.recording = Some((recording_id, my_consent));
        }
        tracing::debug!(target: "sew_call_client::keys", recording = ?recording_id, consent = ?my_consent, "recording state");
        input(
            &self.shared,
            Input::Recording {
                recording_id,
                my_consent,
            },
        )
        .await;
    }

    /// The recording state last given to [`MlsSession::set_recording`].
    pub fn recording(&self) -> Option<(Option<Id>, ConsentState)> {
        lock(&self.shared.core).recording
    }

    /// The key state and the active epoch's security code (`None` before the session has a
    /// status).
    pub fn security(&self) -> Option<crate::call::CallSecurity> {
        let c = lock(&self.shared.core);
        let s = c.session.as_ref()?;
        let mut out = security_of(s.status()?);
        out.code = s.security_code().map(|code| code.display());
        Some(out)
    }

    /// The active key epoch, when the session has one.
    pub fn active_epoch(&self) -> Option<u64> {
        lock(&self.shared.core)
            .session
            .as_ref()
            .and_then(|s| s.active_epoch())
    }

    /// The engine identities bound to a leaf of the verified tree and the member each is, the
    /// local one included (the `participants` event's `member_id`).
    pub fn members(&self) -> Vec<(String, String)> {
        lock(&self.shared.core)
            .session
            .as_ref()
            .map(|s| {
                s.roster()
                    .iter()
                    .filter(|e| e.leaf_index.is_some())
                    .map(|e| (e.lk_identity.as_str().to_owned(), e.member_id.to_string()))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// For tests: the derived keys are not exposed, but the IKMs the session holds are, so a
    /// test can grep logs for them.
    #[doc(hidden)]
    pub fn live_ikms_for_test(&self) -> Vec<[u8; 32]> {
        let c = lock(&self.shared.core);
        let Some(s) = c.session.as_ref() else {
            return Vec::new();
        };
        s.key_ring().live().map(|(_, _, k)| *k.expose()).collect()
    }
}

impl<C: FrameCrypto> Shared<C> {
    fn emit(&self, e: KeySessionEvent) {
        let _ = self.events.send(e);
    }
}

/// Prepares and uploads one KeyPackage for `scope`, recovering a refused device once.
async fn upload_kp<C: FrameCrypto>(
    shared: &Arc<Shared<C>>,
    scope: CallScope,
) -> Result<PreparedKp, KeyError> {
    let mut recovered = false;
    loop {
        let now = (shared.clock)();
        let (pending, msg, signer) = {
            let mut guard = lock(&shared.core);
            let c = &mut *guard;
            let (pending, req) = c
                .client
                .prepare_key_package(scope, now)
                .map_err(|e| KeyError::Session(e.to_string()))?;
            (pending, req.message, c.signer.clone())
        };
        let mut kp = pending
            .finish(signer.sign(&msg).await?)
            .map_err(|e| KeyError::Session(e.to_string()))?;
        match shared.http.upload_key_package(&mut kp).await {
            Ok(()) => return Ok(kp),
            Err(f) if !recovered && is_device_refused(&f) => {
                recovered = true;
                recover_device(shared).await?;
            }
            Err(f) => return Err(refused(&f)),
        }
    }
}

/// Sew 0.7.1's recovery: forget the refused device (keep the pins), enrol once.
async fn recover_device<C: FrameCrypto>(shared: &Arc<Shared<C>>) -> Result<(), KeyError> {
    tracing::info!(target: "sew_call_client::keys", "the device was refused; enrolling again");
    let trust = lock(&shared.core).client.trust().clone();
    let now = (shared.clock)();
    let (identity, signer) = {
        let mut v = shared.vault.lock().await;
        v.forget_device().await?;
        v.enroll(&shared.http, &trust, shared.member, now).await?
    };
    let mut guard = lock(&shared.core);
    let c = &mut *guard;
    let cfg = c.client.config().clone();
    c.client = E2eeClient::new(identity, trust).with_config(cfg);
    c.signer = signer;
    // Packages signed by the old device are useless now.
    c.prepared.clear();
    Ok(())
}

impl<C: FrameCrypto> MlsSession<C> {
    async fn prepared_kp(&self, scope: CallScope) -> Result<PreparedKp, KeyError> {
        upload_kp(&self.shared, scope).await
    }
}

async fn key_join<C: FrameCrypto>(
    shared: &Arc<Shared<C>>,
    channel: Id,
    join_id: JoinId,
) -> Result<(), KeyError> {
    let scope = CallScope::call(channel);
    let mut ref_retried = false;
    let mut device_retried = false;
    loop {
        let kp = upload_kp(shared, scope).await?;
        let body = {
            let c = lock(&shared.core);
            kp.key_join_request(join_id.clone(), &engine_caps(), &c.gateway_session)
                .map_err(|e| KeyError::Session(e.to_string()))?
        };
        match shared.http.key_join(&body).await {
            Ok(init) => {
                lock(&shared.core).channel = Some(channel);
                return start_session(shared, init, Some(kp)).await;
            }
            Err(f) if !device_retried && is_device_refused(&f) => {
                device_retried = true;
                recover_device(shared).await?;
            }
            Err(f) => match on_join_failure(&f, ref_retried) {
                Ok(()) => ref_retried = true,
                Err(_) => return Err(refused(&f)),
            },
        }
    }
}

async fn start_session<C: FrameCrypto>(
    shared: &Arc<Shared<C>>,
    init: CallKeysInit,
    kp: Option<PreparedKp>,
) -> Result<(), KeyError> {
    let now = (shared.clock)();
    {
        let mut guard = lock(&shared.core);
        let c = &mut *guard;
        let kp = match kp {
            Some(k) => k,
            None => c
                .prepared
                .pop_last()
                .map(|(_, k)| k)
                .ok_or_else(|| KeyError::Session("no prepared key package".to_owned()))?,
        };
        let call_id = init.call_id.clone();
        if let Some(old) = c.session.as_mut() {
            // A move: the old call's keys go (its leave is the server's).
            old.leave(now);
        }
        let _ = c.sink.apply(KeyEvent::Clear);
        let session = kp
            .into_session(&c.client, init, &c.gateway_session, now)
            .map_err(|e| KeyError::Session(e.to_string()))?;
        tracing::debug!(target: "sew_call_client::keys", call = %call_id.as_str(), "key session started");
        c.session = Some(session);
        c.prepared.clear();
        let held = c.buffer.take(now, &call_id);
        let recording = c.recording;
        if let Some(s) = c.session.as_mut() {
            for m in held {
                let _ = s.handle(now, Input::Server(m));
            }
            // A new session starts `pending`: the recording state as last fed.
            if let Some((recording_id, my_consent)) = recording {
                let _ = s.handle(
                    now,
                    Input::Recording {
                        recording_id,
                        my_consent,
                    },
                );
            }
        }
    }
    pump(shared).await;
    Ok(())
}

/// Applies key events, answers signatures and self-checks, and POSTs every request (lock
/// released), feeding the answers back, until the session is quiet. Signatures are made with
/// the lock released too: a hardware key's `sign` is async and may take its time.
async fn pump<C: FrameCrypto>(shared: &Arc<Shared<C>>) {
    loop {
        let (sends, roster_changed, ended, signs, key) = {
            let mut guard = lock(&shared.core);
            let c = &mut *guard;
            let now = (shared.clock)();
            let mut sends = Vec::new();
            let mut signs = Vec::new();
            let mut roster = false;
            let Some(session) = c.session.as_mut() else {
                return;
            };
            while let Some(o) = session.poll_output() {
                match o {
                    Output::Send(id, req) => sends.push((id, req)),
                    Output::Key(ev) => {
                        let fence = match &ev {
                            KeyEvent::Fence { id } => Some(*id),
                            _ => None,
                        };
                        match c.sink.apply(ev) {
                            Ok(SinkAck::Now) => {
                                if let Some(f) = fence {
                                    let _ = session.handle(now, Input::Installed { fence: f });
                                }
                            }
                            Ok(SinkAck::Later(_)) => {}
                            Err(e) => {
                                tracing::warn!(target: "sew_call_client::keys", error = %e, "the bridge refused a key event");
                            }
                        }
                    }
                    Output::NeedDeviceSignature { request, message } => {
                        signs.push((request, message));
                    }
                    Output::CheckSenderSlot { .. } => {
                        if let Some(observed) = c.sink.local_slot() {
                            let _ = session.handle(now, Input::SenderSlotObserved { observed });
                        }
                    }
                    Output::Status(s) => shared.emit(KeySessionEvent::Status(s)),
                    Output::Roster => roster = true,
                    _ => {}
                }
            }
            let me = session.lk_identity().clone();
            let verified: BTreeSet<LkIdentity> = session
                .roster()
                .iter()
                .filter(|e| e.leaf_index.is_some() && e.lk_identity != me)
                .map(|e| e.lk_identity.clone())
                .collect();
            if verified != c.last_verified {
                c.sink.set_verified(verified.clone());
                c.last_verified = verified;
                roster = true;
            }
            let ended = match session.state() {
                SessionState::Left { reason } if !c.ended => {
                    c.ended = true;
                    Some(reason)
                }
                _ => None,
            };
            (sends, roster, ended, signs, c.signer.clone())
        };
        if roster_changed {
            shared.emit(KeySessionEvent::Roster);
        }
        if let Some(r) = ended
            && !matches!(r, LeftReason::Left)
        {
            shared.emit(KeySessionEvent::Ended(KeyEnd::Left(r)));
        }
        shared.changed.notify_waiters();
        if sends.is_empty() && signs.is_empty() {
            return;
        }
        for (request, message) in signs {
            match key.sign(&message).await {
                Ok(sig) => {
                    let now = (shared.clock)();
                    let mut guard = lock(&shared.core);
                    if let Some(s) = guard.session.as_mut() {
                        let _ = s.handle(now, Input::DeviceSignature { request, sig });
                    }
                }
                Err(e) => {
                    // No software fallback: the hardware key could not sign, so the session
                    // cannot re-add this device. End it.
                    tracing::warn!(target: "sew_call_client::keys", error = %e, "the device key could not sign");
                    let first = {
                        let mut c = lock(&shared.core);
                        !std::mem::replace(&mut c.ended, true)
                    };
                    if first {
                        shared.emit(KeySessionEvent::Ended(KeyEnd::Device(e)));
                    }
                    return;
                }
            }
        }
        for (id, req) in sends {
            let r = shared.http.key_request(&req).await;
            let now = (shared.clock)();
            let mut guard = lock(&shared.core);
            let c = &mut *guard;
            if let Some(s) = c.session.as_mut() {
                let _ = s.handle(now, Input::Reply(id, r));
            }
        }
    }
}

/// Feeds one input to the session (if any), then pumps.
async fn input<C: FrameCrypto>(shared: &Arc<Shared<C>>, i: Input) {
    {
        let now = (shared.clock)();
        let mut guard = lock(&shared.core);
        let c = &mut *guard;
        if let Some(s) = c.session.as_mut()
            && let Err(e) = s.handle(now, i)
        {
            tracing::debug!(target: "sew_call_client::keys", error = %e, "input refused");
        }
    }
    pump(shared).await;
}

fn report_input(r: MediaReport) -> Input {
    match r {
        MediaReport::EngineMisbehaviour { kind, identity } => {
            Input::EngineMisbehaviour { kind, identity }
        }
        MediaReport::Undecryptable {
            identity,
            since,
            failures,
            reason,
            slot,
        } => Input::SdkUndecryptable {
            identity,
            since,
            failures,
            reason,
            missing_index: slot,
        },
        MediaReport::DecryptOk { identity } => Input::SdkDecryptOk { identity },
    }
}

async fn on_socket<C: FrameCrypto>(shared: &Arc<Shared<C>>, ev: SocketEvent) -> Option<KeyEnd> {
    match ev {
        SocketEvent::CallKeys(m) => {
            let m = *m;
            let now = (shared.clock)();
            let known = {
                let mut guard = lock(&shared.core);
                let c = &mut *guard;
                let call = sew_e2ee::session::call_id_of(&m).cloned();
                let current = c.session.as_ref().map(|s| s.call_id().clone());
                match (call, current) {
                    (Some(call), Some(cur)) if call != cur => {
                        let size = serde_json::to_vec(&m).map(|v| v.len()).unwrap_or(1024);
                        c.buffer.push(now, call, m, size);
                        None
                    }
                    (Some(call), None) => {
                        let size = serde_json::to_vec(&m).map(|v| v.len()).unwrap_or(1024);
                        c.buffer.push(now, call, m, size);
                        None
                    }
                    _ => Some(m),
                }
            };
            if let Some(m) = known {
                input(shared, Input::Server(m)).await;
            }
            None
        }
        SocketEvent::Ready(r) => {
            // A new gateway session (RESUME refused): re-bind with the `state` route.
            if let Some(rec) = r.recording.clone() {
                shared.emit(KeySessionEvent::Recording(rec.map(Box::new)));
            }
            if let Some(c) = r.recording_controls.clone() {
                shared.emit(KeySessionEvent::RecordingControls {
                    channel_id: None,
                    controls: c,
                });
            }
            {
                let mut guard = lock(&shared.core);
                let c = &mut *guard;
                c.gateway_session = r.session_id.clone();
                if let Some(ck) = &r.call_keys
                    && let Err(e) = c.client.trust_mut().refresh(ck)
                {
                    tracing::warn!(target: "sew_call_client::keys", error = %e, "READY's trust anchors refused");
                }
            }
            input(
                shared,
                Input::GatewayReidentified {
                    gateway_session: r.session_id,
                },
            )
            .await;
            None
        }
        SocketEvent::Resumed => {
            input(shared, Input::GatewayResumed).await;
            None
        }
        SocketEvent::VoiceServerUpdated(u) => {
            shared.emit(KeySessionEvent::VoiceServerUpdated(u.clone()));
            let s = shared.clone();
            let (channel, join) = (u.channel_id, u.e2ee.join_id.clone());
            tokio::spawn(async move {
                if let Err(e) = key_join(&s, channel, join).await {
                    s.emit(KeySessionEvent::KeyJoinFailed(e));
                }
            });
            None
        }
        SocketEvent::VoiceStateUpdated(u) => {
            let members = {
                let mut guard = lock(&shared.core);
                let c = &mut *guard;
                let here = c.channel;
                if u.channel_id.is_some() && u.channel_id == here {
                    c.voice_members.insert(u.member_id);
                } else {
                    c.voice_members.remove(&u.member_id);
                }
                c.voice_members.iter().copied().collect::<Vec<_>>()
            };
            shared.emit(KeySessionEvent::VoiceStateUpdated(u));
            input(shared, Input::VoiceMembers(members)).await;
            None
        }
        SocketEvent::RecordingUpdated(r) => {
            shared.emit(KeySessionEvent::Recording(Some(r)));
            None
        }
        SocketEvent::MediaPolicyUpdated(m) => {
            shared.emit(KeySessionEvent::MediaPolicy(m));
            None
        }
        SocketEvent::RecordingControls(u) => {
            shared.emit(KeySessionEvent::RecordingControls {
                channel_id: Some(u.channel_id),
                controls: Some(u.controls()),
            });
            None
        }
        SocketEvent::Closed(SocketClosed::Revoked) => Some(KeyEnd::CredentialLost {
            reason: "session_revoked".to_owned(),
            rejoin: false,
        }),
        SocketEvent::Closed(SocketClosed::Ended { reason }) => Some(KeyEnd::Ended { reason }),
        SocketEvent::Closed(SocketClosed::Closed) => None,
        SocketEvent::Closed(other) => Some(KeyEnd::Socket(other)),
    }
}

async fn drive<C: FrameCrypto>(
    weak: std::sync::Weak<Shared<C>>,
    socket: Arc<CallSocket>,
    mut rx: mpsc::Receiver<SocketEvent>,
    stop: Arc<tokio::sync::Notify>,
    mut renew: Option<u64>,
) {
    loop {
        let next = {
            let Some(shared) = weak.upgrade() else {
                return;
            };
            let c = lock(&shared.core);
            let now = (shared.clock)();
            let mut next = now + TICK.as_millis() as u64;
            if let Some(t) = c.session.as_ref().and_then(|s| s.poll_timeout()) {
                next = next.min(t);
            }
            if let Some(t) = renew {
                next = next.min(t);
            }
            next.saturating_sub(now)
        };
        let ev = tokio::select! {
            _ = stop.notified() => return,
            e = rx.recv() => match e {
                Some(e) => Some(e),
                None => return,
            },
            _ = tokio::time::sleep(Duration::from_millis(next)) => None,
        };
        let Some(shared) = weak.upgrade() else {
            return;
        };
        if let Some(e) = ev
            && let Some(end) = on_socket(&shared, e).await
        {
            shared.emit(KeySessionEvent::Ended(end));
            return;
        }
        // Timers.
        let now = (shared.clock)();
        {
            let mut guard = lock(&shared.core);
            let c = &mut *guard;
            if let Some(s) = c.session.as_mut()
                && s.poll_timeout().is_some_and(|t| t <= now)
            {
                s.handle_timeout(now);
            }
            c.buffer.expire(now);
        }
        // The media path's reports (the `report` route, the receiver check).
        let reports = lock(&shared.core).sink.take_reports();
        for r in reports {
            input(&shared, report_input(r)).await;
        }
        pump(&shared).await;
        // The credential.
        if let Some(at) = renew
            && now >= at
        {
            match shared.http.renew_credential().await {
                Ok(a) => {
                    socket.reauth();
                    renew = Some(renew_at(now, unix_ms(a.expires_at)));
                }
                Err(f) if f.is_retryable() => {
                    renew = Some(now + 5_000);
                }
                Err(f) => {
                    let r = RenewRefused::from_failure(&f);
                    shared.emit(KeySessionEvent::Ended(KeyEnd::CredentialLost {
                        reason: f.reason.clone().unwrap_or_else(|| f.status.to_string()),
                        rejoin: r.allows_rejoin(),
                    }));
                    return;
                }
            }
        }
    }
}

/// A key status as the engine's [`crate::call::CallSecurity`] (without the code).
pub(crate) fn security_of(s: &E2eeStatus) -> crate::call::CallSecurity {
    let mut out = crate::call::CallSecurity::default();
    out.state = match s {
        E2eeStatus::Preparing => "preparing",
        E2eeStatus::Joining { since } => {
            out.since_ms = Some(*since);
            "joining"
        }
        E2eeStatus::Active { epoch, since } => {
            out.epoch = Some(*epoch);
            out.since_ms = Some(*since);
            "active"
        }
        E2eeStatus::Securing {
            since,
            show_after_ms,
        } => {
            out.since_ms = Some(*since);
            out.show_after_ms = Some(*show_after_ms);
            "securing"
        }
        E2eeStatus::Recovering { reason, since } => {
            out.since_ms = Some(*since);
            out.reason = Some((*reason).to_owned());
            "recovering"
        }
        E2eeStatus::DeviceNotEnrolled => "device_not_enrolled",
        E2eeStatus::ServerIdentityChanged => "server_identity_changed",
        E2eeStatus::Unsupported { reason } => {
            out.reason = Some(reason.clone());
            "unsupported"
        }
        E2eeStatus::Failed { reason } => {
            out.reason = Some((*reason).to_owned());
            "failed"
        }
        E2eeStatus::Left { reason } => {
            out.reason = Some(match reason {
                LeftReason::Left => "left".to_owned(),
                LeftReason::Removed(r) => format!("removed:{}", r.as_str()),
                LeftReason::Failed(r) => format!("failed:{r}"),
                _ => "other".to_owned(),
            });
            "left"
        }
        _ => "unknown",
    }
    .to_owned();
    out
}

#[cfg(test)]
mod security_tests {
    use super::*;
    use sew_proto::e2ee::RemoveReason;

    #[test]
    fn each_key_status_is_its_security_state() {
        let s = security_of(&E2eeStatus::Active {
            epoch: 7,
            since: 1_000,
        });
        assert_eq!(
            (s.state.as_str(), s.epoch, s.since_ms, s.code),
            ("active", Some(7), Some(1_000), None)
        );
        let s = security_of(&E2eeStatus::Securing {
            since: 5,
            show_after_ms: 1_500,
        });
        assert_eq!(
            (s.state.as_str(), s.since_ms, s.show_after_ms, s.epoch),
            ("securing", Some(5), Some(1_500), None)
        );
        let s = security_of(&E2eeStatus::Recovering {
            reason: "readd",
            since: 9,
        });
        assert_eq!(
            (s.state.as_str(), s.reason.as_deref()),
            ("recovering", Some("readd"))
        );
        assert_eq!(security_of(&E2eeStatus::Preparing).state, "preparing");
        assert_eq!(
            security_of(&E2eeStatus::Joining { since: 3 }).since_ms,
            Some(3)
        );
        assert_eq!(
            security_of(&E2eeStatus::DeviceNotEnrolled).state,
            "device_not_enrolled"
        );
        assert_eq!(
            security_of(&E2eeStatus::ServerIdentityChanged).state,
            "server_identity_changed"
        );
        let s = security_of(&E2eeStatus::Unsupported {
            reason: "no_insertable_streams".to_owned(),
        });
        assert_eq!(
            (s.state.as_str(), s.reason.as_deref()),
            ("unsupported", Some("no_insertable_streams"))
        );
        let s = security_of(&E2eeStatus::Failed {
            reason: "recovery_timeout",
        });
        assert_eq!(s.reason.as_deref(), Some("recovery_timeout"));
        let s = security_of(&E2eeStatus::Left {
            reason: LeftReason::Removed(RemoveReason::PermsLost),
        });
        assert_eq!(
            (s.state.as_str(), s.reason.as_deref()),
            ("left", Some("removed:perms_lost"))
        );
        let s = security_of(&E2eeStatus::Left {
            reason: LeftReason::Left,
        });
        assert_eq!(s.reason.as_deref(), Some("left"));
    }

    #[test]
    fn the_security_json_writes_only_what_it_has() {
        let code = "01234 56789 01234 56789 01234 56789";
        let s = crate::call::CallSecurity::active(3, Some(code.into()));
        assert_eq!(
            serde_json::to_value(&s).unwrap_or_default(),
            serde_json::json!({"state": "active", "epoch": 3, "code": code})
        );
        assert_eq!(
            serde_json::to_value(crate::call::CallSecurity::new("joining")).unwrap_or_default(),
            serde_json::json!({"state": "joining"})
        );
    }
}