rusteero 3.0.1

Async Rust client for the Eero (Amazon) mesh Wi-Fi cloud API — a port of the Python eero-api library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
//! Pluggable credential storage.
//!
//! Ported from `eero-api`'s `src/eero/api/auth_storage.py` (`v8.4.1`): [`CredentialStore`] mirrors
//! the `CredentialStorage` abstract base class (`auth_storage.py:129-166`) and each backend below
//! mirrors one concrete class.
//!
//! # Backends
//!
//! - [`MemoryStore`] — ported from `MemoryStorage` (`auth_storage.py:609-642`): in-process only,
//!   never touches disk or the OS keyring.
//! - [`FileStore`] — ported from `FileStorage` (`auth_storage.py:425-606`): a single JSON file,
//!   written atomically with owner-only (`0600`) permissions.
//! - [`ChainedStore`] — ported from `ChainedStorage` (`auth_storage.py:645-805`): a primary
//!   backend with a fallback, read-through and write-through.
//! - `KeyringStore` (behind `feature = "keyring"`, default-on) — ported from `KeyringStorage`
//!   (`auth_storage.py:309-423`): the OS-native credential store, sharing an entry with `eero-api`.
//!
//! [`create_storage`] selects among all four with the same four-way matrix as
//! `auth_storage.py:807-830`'s `create_storage()`.
//!
//! # Verified saves
//!
//! A backend can accept a write and retain nothing (a keyring backend that silently drops
//! writes, a file replaced under the writer). So the shipped [`FileStore`], `KeyringStore` and
//! [`ChainedStore`] read the store back after every save, and `save` returns `Ok(())` only if the
//! read-back found the saved token. A write that was accepted but not retained is
//! [`StorageError::NotRetained`]; a write the backend rejected is the backend's own error.
//!
//! # Async callers
//!
//! The trait is synchronous, so an async caller reaches any `Arc<dyn CredentialStore>` without
//! blocking its own worker thread through [`load_async`], [`save_async`] and [`clear_async`]:
//! thin `tokio::task::spawn_blocking` adapters. Each public store operation is one blocking-pool
//! hop, including any read-back or in-place migration it performs. Their docs cover what dropping
//! the returned future does (nothing: the work runs to completion).

pub mod chained;
pub mod file;
#[cfg(feature = "keyring")]
pub mod keyring;
pub mod memory;
#[cfg(test)]
pub(crate) mod test_support;

pub use crate::error::StorageError;
pub use chained::ChainedStore;
pub use file::FileStore;
#[cfg(feature = "keyring")]
pub use keyring::KeyringStore;
pub use memory::MemoryStore;

use std::panic::AssertUnwindSafe;
use std::path::PathBuf;
use std::sync::Arc;

use tokio::task::{JoinError, JoinHandle};

use crate::auth::Session;

/// Re-saves a legacy (pre-`schema_version`) record a backend's `load()` just parsed, in the
/// current schema, and logs whether the store retained it.
///
/// Ported from `_log_migration_readback` (`auth_storage.py:84-104`) and its two callers
/// (`KeyringStorage._load_sync` `:346-364`, `FileStorage._load_sync` `:461-494`). `save` is the
/// backend's own *unlocked* save, which already reads the store back (see the module docs), so
/// its `Ok` is the verified migration: this logs `debug!` on `Ok` and `warn!` otherwise. It runs
/// inside the load that found the legacy record, on the same thread and under the same operation
/// lock, so a load is one unit of work. The read-back is the one inside `save`, never a call back
/// into `load`, so a backend that accepts the write but keeps serving the legacy record cannot
/// recurse into another migration.
///
/// It **never fails the read**: `session` (the in-memory value already parsed) is always what the
/// caller gets back, whether or not the migration write was retained. Neither log line ever
/// includes the credential value itself.
pub(crate) fn migrate_and_verify(
    session: &Session,
    backend: &str,
    save: impl FnOnce(&Session) -> Result<(), StorageError>,
) {
    tracing::debug!("Migrated legacy credential record in {backend} storage");
    if save(session).is_ok() {
        tracing::debug!("Migration read-back verified for {backend} storage");
    } else {
        tracing::warn!(
            "Migration read-back did not match for {backend} storage; in-memory credentials \
             are still returned to the caller"
        );
    }
}

/// A pluggable backend for persisting a [`Session`] across process restarts.
///
/// Mirrors the `CredentialStorage` abstract base class (`auth_storage.py:129-166`), which
/// declares three `async` abstract methods (`load`, `save`, `clear`) with no default bodies.
///
/// This trait is deliberately **synchronous**, not `async`, even though every method it
/// mirrors is `async def` in Python: every native OS keyring API (`keyring` crate,
/// `feature = "keyring"`) is a blocking call, so an async trait here would only move the
/// blocking work around, not remove it, while forcing `async-trait` boxing on every
/// implementation for no benefit. Async callers invoke these methods through [`load_async`],
/// [`save_async`] and [`clear_async`] (or their own `tokio::task::spawn_blocking`).
///
/// Implementations must be `Send + Sync` (usable from any task/thread) and `Debug` (so a
/// `Client` holding one behind an `Arc<dyn CredentialStore>` can still derive/implement
/// `Debug` itself).
///
/// # Concurrency
///
/// The shipped [`FileStore`] and `KeyringStore` serialise their own `load`/`save`/`clear` with an
/// internal lock that every clone of the store shares, so a `save` and a `clear` on one instance
/// cannot interleave and call order decides the outcome (`auth_storage.py:190-235`). Separate
/// instances addressing the same file or keyring entry are *not* coordinated with each other.
/// [`MemoryStore`] is lock-guarded, and [`ChainedStore`] inherits its backends' serialisation (it
/// does not make an operation atomic across the two). Third-party implementations that touch
/// shared external state should serialise their own operations the same way, because async
/// callers can have several in flight on different blocking-pool threads.
pub trait CredentialStore: Send + Sync + std::fmt::Debug {
    /// Loads the stored session.
    ///
    /// Returns `Ok(`[`Session::empty`]`())`, **not** an error, when nothing has ever been
    /// stored — mirroring every Python backend's "never seen before" behaviour (e.g.
    /// `MemoryStorage.__init__`'s fresh `AuthCredentials()`, `auth_storage.py:616-618`, or
    /// `FileStorage.load()`'s "file not found" branch, `auth_storage.py:463-466`).
    ///
    /// # Errors
    ///
    /// Returns [`StorageError`] only when the backend itself is unusable (e.g. an I/O failure
    /// reading a file, or a keyring backend reporting an error) — never merely because nothing
    /// has been saved yet. A `Client` builder warns on such a failure by default and treats it
    /// as fatal only on request.
    fn load(&self) -> Result<Session, StorageError>;

    /// Persists `session`, overwriting whatever was previously stored.
    ///
    /// The shipped backends return `Ok(())` only after a read-back found the saved token (see
    /// the module docs), like `CredentialStorage.save()` returning `True` only when a read-back
    /// found the saved `session_id` (`auth_storage.py:142-161`). A [`MemoryStore`] record counts
    /// as retained even though it does not outlive the process. A third-party implementation is
    /// trusted: `Ok(())` from it is taken to mean the session was persisted.
    ///
    /// # Errors
    ///
    /// Returns [`StorageError`] if the backend could not persist the session (e.g. a file
    /// system permission failure), or [`StorageError::NotRetained`] if the backend accepted the
    /// write but a read-back did not find the saved session. Unlike every Python backend (which
    /// never raises and returns `False`, logging the cause at DEBUG/ERROR,
    /// `auth_storage.py:385-407,541-593`), this port surfaces the failure as a typed error so a
    /// caller can decide whether it is fatal.
    fn save(&self, session: &Session) -> Result<(), StorageError>;

    /// Removes the stored session entirely.
    ///
    /// Mirrors `clear()` (e.g. `auth_storage.py:409-423,595-606,640-642`): unlike
    /// [`CredentialStore::save`] with an [`Session::empty`], this removes the underlying
    /// storage entry/file rather than overwriting it with null fields.
    ///
    /// # Errors
    ///
    /// Returns [`StorageError`] if the backend failed to remove the stored entry. Removing an
    /// entry that was never there is *not* an error (mirrors every Python backend's tolerant
    /// `clear()`, e.g. the swallowed `PasswordDeleteError` at `auth_storage.py:418-420`).
    fn clear(&self) -> Result<(), StorageError>;

    /// Whether the store itself logs, at `WARN` or above, every failure that
    /// [`save`](Self::save) and [`clear`](Self::clear) return.
    ///
    /// `false` by default, the right answer for an implementation that returns its errors and
    /// logs nothing: the transport then logs each failure itself (at `WARN` under the default
    /// [`StorageFailures::Warn`](crate::transport::StorageFailures::Warn) policy). The shipped
    /// [`FileStore`], [`ChainedStore`] and (unless silenced with
    /// `KeyringStore::warn_on_unpersisted(false)`) `KeyringStore` return `true`, because they
    /// log `eero-api`'s fixed texts for their own failures; the transport then logs the same
    /// failure at `DEBUG` only, so a failure is one line and not two.
    ///
    /// Override it to return `true` only if *every* `Err` the store can return from `save` or
    /// `clear` is logged by the store before it returns.
    ///
    /// Failures of the [`save_async`]/[`clear_async`] adapters themselves are not covered: a store
    /// that panicked, a blocking task cancelled at runtime shutdown or one that could not start
    /// (the `"credential-store-task"` backend) never reached the store's logging, so the
    /// transport logs those at `WARN` whatever this returns.
    fn reports_failures(&self) -> bool {
        false
    }
}

// ===================== async spawn_blocking adapters =====================

/// The backend name of every [`StorageError::Backend`] the async adapters raise on behalf of the
/// blocking task itself (a panic, a cancellation, a task that could not start), as opposed to an
/// error the store returned.
const TASK_BACKEND: &str = "credential-store-task";

/// Whether `err` is a failure of the async adapters' blocking task (a store that panicked, a task
/// cancelled at runtime shutdown, a task that could not start) rather than an error the store
/// itself returned.
///
/// The store never got to log such a failure, whatever its
/// [`reports_failures`](CredentialStore::reports_failures) says, so the caller that receives it
/// must report it.
pub(crate) fn is_task_failure(err: &StorageError) -> bool {
    matches!(err, StorageError::Backend { backend, .. } if backend == TASK_BACKEND)
}

/// Maps a panicked/cancelled blocking task's `JoinError` onto a [`StorageError`].
///
/// [`CredentialStore`]'s methods are synchronous by design (this module's own docs), so
/// [`load_async`]/[`save_async`]/[`clear_async`] run them via `tokio::task::spawn_blocking`; a
/// `JoinError` there means the store implementation itself panicked (or the runtime is shutting
/// down), never that it returned an ordinary `Err`. This is surfaced as a
/// [`StorageError::Backend`] rather than propagating the panic into the caller's own task.
///
/// # Security
///
/// The message deliberately records only [`tokio::task::JoinError::id`],
/// [`tokio::task::JoinError::is_panic`] and [`tokio::task::JoinError::is_cancelled`] — **never**
/// `join_err`'s own `Display`. A previous version of this function interpolated `join_err`
/// directly, on the premise that `JoinError`'s `Display` never carries a credential; that premise
/// is false for the tokio this crate builds against (checked on 1.54.1), whose
/// `JoinError::Display` renders a panicking task's payload verbatim
/// (`task {id} panicked with message {panic_str:?}`,
/// `tokio-1.54.1/src/runtime/task/error.rs:139-146`). `CredentialStore` is a public,
/// third-party-implementable trait, so a backend that panics via `unwrap()`/`expect()` on a value
/// carrying the serialized session would put that text straight into this error's `message`,
/// which this crate may log at WARN or return to a caller. Interpolating the `Display` here would
/// reopen exactly the credential-leak path this module otherwise guards against.
fn join_error_to_storage_error(join_err: &JoinError) -> StorageError {
    StorageError::Backend {
        backend: TASK_BACKEND.to_owned(),
        message: format!(
            "blocking credential-store task {} failed: panicked={}, cancelled={}",
            join_err.id(),
            join_err.is_panic(),
            join_err.is_cancelled()
        ),
    }
}

/// Runs `spawn`, turning a panic out of it into a [`StorageError`] instead of unwinding into the
/// caller.
///
/// `tokio::task::spawn_blocking` panics when it cannot start the task: the OS refused to create a
/// worker thread and no blocking-pool thread is alive to take the work (`OS can't spawn worker
/// thread: ..`, `tokio-1.54.1/src/runtime/blocking/pool.rs:385-387`), or it is called outside a
/// runtime. A save that cannot start must stay a handled storage failure (`save()` never raises
/// in `eero-api`, `auth_storage.py:262-280`), so the panic is caught here. The message is fixed
/// and never interpolates the panic payload. The default panic hook still prints the original
/// panic message, and nothing can be caught under `panic = "abort"`.
///
/// Split from [`spawn_store_task`] so a test can inject a spawner that panics.
fn spawn_guarded<R>(spawn: impl FnOnce() -> JoinHandle<R>) -> Result<JoinHandle<R>, StorageError> {
    std::panic::catch_unwind(AssertUnwindSafe(spawn)).map_err(|_payload| StorageError::Backend {
        backend: TASK_BACKEND.to_owned(),
        message: "blocking credential-store task could not start".to_owned(),
    })
}

/// Hands `work` to the blocking pool, or reports why it could not be started.
///
/// The caller's `tracing` dispatcher and current span travel with the work. A blocking-pool
/// thread knows nothing of a subscriber installed with `tracing::subscriber::set_default` or
/// `with_default` on the calling thread, so without this every line a store logs (the fixed
/// `WARN`s of the keyring and file stores, say) would go to the global subscriber or nowhere,
/// instead of to whoever is observing the task that asked for the store call.
fn spawn_store_task<R: Send + 'static>(
    work: impl FnOnce() -> R + Send + 'static,
) -> Result<JoinHandle<R>, StorageError> {
    let dispatch = tracing::dispatcher::get_default(Clone::clone);
    let span = tracing::Span::current();
    spawn_guarded(|| {
        tokio::task::spawn_blocking(move || {
            tracing::dispatcher::with_default(&dispatch, || span.in_scope(work))
        })
    })
}

/// Flattens a joined blocking task's outcome into the store's own result, mapping a `JoinError`
/// through [`join_error_to_storage_error`].
fn flatten_join<T>(joined: Result<Result<T, StorageError>, JoinError>) -> Result<T, StorageError> {
    match joined {
        Ok(result) => result,
        Err(join_err) => Err(join_error_to_storage_error(&join_err)),
    }
}

/// Runs `store.load()` on a blocking thread via `tokio::task::spawn_blocking`, so an async
/// caller never blocks its own worker thread on a synchronous backend call (a native OS keyring
/// API, in particular, always blocks).
///
/// The whole operation is one hop: a load that finds a legacy record also performs the migration
/// write and its read-back on that same thread, inside the store's own operation lock
/// (`auth_storage.py:346-364`). Needs a running Tokio runtime; it works on both the current-thread
/// and the multi-thread flavours, and a store can be reused across successive runtimes.
///
/// # Errors
///
/// Propagates the store's own [`StorageError`] unchanged. A failure of the blocking task itself is
/// a [`StorageError::Backend`] with the backend name `"credential-store-task"` and a fixed
/// message that never carries a panic payload: the store panicked, the runtime was shutting down,
/// or the task could not be started (the OS refused a worker thread). The same holds for
/// [`save_async`] and [`clear_async`].
///
/// # Cancellation
///
/// The store call is handed to the blocking pool in the first poll of the returned future,
/// before it first yields. Dropping the future (or aborting the task that awaits it) after that
/// does not cancel anything: a running blocking task cannot be interrupted, and one still waiting
/// for the store's operation lock runs as soon as the lock frees. So a `save` always finishes its
/// atomic write, leaving the old or the new record and never a partial one, and a later
/// operation on the same store instance waits for it instead of racing it. The cancelled caller
/// never sees the result. This differs from `eero-api`, where cancelling an operation still
/// waiting for the lock skips it (`auth_storage.py:205-213`): here dropping a future never skips
/// a store write or clear, so the store cannot end up disagreeing with an in-memory session that
/// was already changed. Dropping the future before its first poll never starts the task at all.
///
/// # Blocking threads
///
/// Each call occupies one thread of the runtime's blocking pool for as long as the store call
/// takes. A store that serialises its operations behind a lock (the shipped file and keyring
/// stores do) holds that thread while it waits.
///
/// # Logging
///
/// What the store logs on the blocking thread is recorded under the `tracing` dispatcher and
/// span that were current when the future was first polled, so a subscriber scoped to the
/// calling task sees it. The same holds for [`save_async`] and [`clear_async`].
pub async fn load_async(store: Arc<dyn CredentialStore>) -> Result<Session, StorageError> {
    flatten_join(spawn_store_task(move || store.load())?.await)
}

/// Runs `store.save(&session)` on a blocking thread, with the read-back and everything else the
/// store does on save in the same hop. See [`load_async`] for the cancellation, blocking-thread
/// and failure contract this shares.
///
/// # Errors
///
/// See [`load_async`]. A store that accepted the write but did not retain it reports
/// [`StorageError::NotRetained`].
pub async fn save_async(
    store: Arc<dyn CredentialStore>,
    session: Session,
) -> Result<(), StorageError> {
    flatten_join(spawn_store_task(move || store.save(&session))?.await)
}

/// Runs `store.clear()` on a blocking thread. See [`load_async`] for the cancellation,
/// blocking-thread and failure contract this shares.
///
/// # Errors
///
/// See [`load_async`].
pub async fn clear_async(store: Arc<dyn CredentialStore>) -> Result<(), StorageError> {
    flatten_join(spawn_store_task(move || store.clear())?.await)
}

// ===================== create_storage factory =====================

/// Configuration for [`create_storage`]'s backend-selection matrix.
///
/// Mirrors `create_storage()`'s two parameters (`auth_storage.py:807-810`,
/// `use_keyring: bool = True, cookie_file: Optional[str] = None`) as fields rather than function
/// arguments, so a caller can build one with struct-update syntax and thread a single value
/// through. Unlike Python's default
/// (`use_keyring=True`), [`StorageConfig::default`] is the conservative, explicit-opt-in Rust
/// idiom: both fields default to "off" (`Default::default()` derives `false`/`None`, which
/// [`create_storage`] maps to a bare [`MemoryStore`]) — a library should not silently reach for
/// the OS keyring unless a caller asks for it.
#[derive(Debug, Clone, Default)]
pub struct StorageConfig {
    /// Whether to prefer the OS keyring as the primary (or sole) backend.
    ///
    /// Ignored under `--no-default-features` (no `keyring` feature, hence no `KeyringStore`
    /// to build): [`create_storage`] degrades to the next-best backend instead of failing to
    /// compile or panicking — see that function's docs for the exact degradation.
    pub use_keyring: bool,
    /// An optional file path used as the fallback backend (when [`Self::use_keyring`] is also
    /// `true`) or the sole backend (when it is `false`).
    pub cookie_file: Option<PathBuf>,
}

impl StorageConfig {
    /// Equivalent to [`StorageConfig::default`]: both fields off, yielding a bare [`MemoryStore`]
    /// from [`create_storage`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

/// Builds the [`CredentialStore`] backend selected by `config`, mirroring `create_storage()`'s
/// four-way matrix (`auth_storage.py:807-830`), evaluated in the same order:
///
/// | `use_keyring` | `cookie_file` | Result |
/// |---|---|---|
/// | `true` | `Some` | [`ChainedStore`] with `KeyringStore` primary, [`FileStore`] fallback |
/// | `true` | `None` | bare `KeyringStore` |
/// | `false` | `Some` | bare [`FileStore`] |
/// | `false` | `None` | bare [`MemoryStore`] |
///
/// In the chained row the keyring is built with `warn_on_unpersisted(false)`: a keyring that
/// retains nothing is routine and handled there (the chain falls back to the file), so only the
/// bare-keyring row warns when a save is not retained (`auth_storage.py:823`).
///
/// Never fails and never panics — like the Python original, this is a total function over its
/// input.
///
/// # Feature `keyring`
///
/// Under `--no-default-features` there is no `KeyringStore` to build at all, so
/// `config.use_keyring` is ignored and the matrix degrades to whichever non-keyring backend the
/// row would otherwise have included: the `true`+`Some` row becomes a bare `FileStore` (no
/// keyring to chain in front of it) and the `true`+`None` row becomes a bare `MemoryStore` (no
/// keyring, and nothing to fall back to).
#[cfg(feature = "keyring")]
#[must_use]
pub fn create_storage(config: &StorageConfig) -> Arc<dyn CredentialStore> {
    match (config.use_keyring, &config.cookie_file) {
        (true, Some(path)) => Arc::new(ChainedStore::new(
            Arc::new(KeyringStore::new().warn_on_unpersisted(false)),
            Arc::new(FileStore::new(path.clone())),
        )),
        (true, None) => Arc::new(KeyringStore::new()),
        (false, Some(path)) => Arc::new(FileStore::new(path.clone())),
        (false, None) => Arc::new(MemoryStore::new()),
    }
}

/// See the `#[cfg(feature = "keyring")]` overload's docs for the full matrix and the
/// degradation this `--no-default-features` build applies.
#[cfg(not(feature = "keyring"))]
#[must_use]
pub fn create_storage(config: &StorageConfig) -> Arc<dyn CredentialStore> {
    match &config.cookie_file {
        Some(path) => Arc::new(FileStore::new(path.clone())),
        None => Arc::new(MemoryStore::new()),
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex, PoisonError, mpsc};
    use std::thread::ThreadId;
    use std::time::{Duration, Instant};

    use tracing::Level;

    use super::test_support::capture_logs;
    use super::{
        CredentialStore, FileStore, StorageConfig, StorageError, clear_async, create_storage,
        load_async, migrate_and_verify, save_async, spawn_guarded,
    };
    use crate::auth::Session;

    // ===================== StorageConfig =====================

    #[test]
    fn storage_config_default_and_new_are_both_off() {
        let default = StorageConfig::default();
        assert!(!default.use_keyring);
        assert!(default.cookie_file.is_none());

        let via_new = StorageConfig::new();
        assert!(!via_new.use_keyring);
        assert!(via_new.cookie_file.is_none());
    }

    // ===================== create_storage matrix — feature = "keyring" =====================
    //
    // The `true`+`None` and `true`+`Some` rows deliberately never call `.save`/`.load`/`.clear`
    // on the resulting store: `KeyringStore::new()` uses the *shared* `eero-api` entry
    // (`SERVICE_NAME`/`ACCOUNT_NAME`), and this container happens to have no reachable
    // keyring backend (see `keyring.rs`'s own tests), but a real backend on a developer's
    // machine would make an actual OS call against a real, shared credential entry. Constructing
    // a `KeyringStore` never touches the OS (only `load`/`save`/`clear` open an `Entry`), so
    // identifying it via `Debug` (which just prints the two field strings, never opens an entry)
    // is representative and safe regardless of the environment this test runs in.
    #[cfg(feature = "keyring")]
    mod keyring_feature_matrix {
        use std::path::PathBuf;

        use super::{Session, StorageConfig, create_storage};

        #[test]
        fn neither_yields_a_working_memory_store() {
            let store = create_storage(&StorageConfig::default());
            assert!(format!("{store:?}").contains("MemoryStore"));
            store
                .save(&Session::from_token("tok"))
                .expect("MemoryStore never fails");
            assert_eq!(
                store.load().expect("load never fails").expose_token(),
                "tok"
            );
        }

        #[test]
        fn file_only_yields_a_working_file_store() {
            let dir = tempfile::tempdir().expect("tempdir");
            let path = dir.path().join("cookies.json");
            let store = create_storage(&StorageConfig {
                use_keyring: false,
                cookie_file: Some(path.clone()),
            });
            let debug = format!("{store:?}");
            assert!(debug.contains("FileStore"));
            assert!(!debug.contains("ChainedStore"));
            store
                .save(&Session::from_token("tok"))
                .expect("file save succeeds");
            assert!(path.exists());
            assert_eq!(
                store.load().expect("file load succeeds").expose_token(),
                "tok"
            );
        }

        #[test]
        fn keyring_only_yields_a_bare_keyring_store_with_no_file_fallback() {
            let store = create_storage(&StorageConfig {
                use_keyring: true,
                cookie_file: None,
            });
            let debug = format!("{store:?}");
            assert!(debug.contains("KeyringStore"));
            assert!(!debug.contains("ChainedStore"));
            assert!(!debug.contains("FileStore"));
            assert!(
                debug.contains("warn_on_unpersisted: true"),
                "a bare keyring warns when a save is not retained: {debug}"
            );
        }

        #[test]
        fn both_yields_a_chained_store_of_keyring_primary_and_file_fallback() {
            let store = create_storage(&StorageConfig {
                use_keyring: true,
                cookie_file: Some(PathBuf::from("/tmp/rusteero-test-does-not-touch-disk.json")),
            });
            let debug = format!("{store:?}");
            assert!(debug.contains("ChainedStore"));
            assert!(debug.contains("KeyringStore"));
            assert!(debug.contains("FileStore"));
            assert!(
                debug.contains("warn_on_unpersisted: false"),
                "a keyring in front of a file fallback is silenced: {debug}"
            );
        }
    }

    // ===================== create_storage matrix — --no-default-features =====================
    //
    // With no `KeyringStore` to build at all, every row here is a `MemoryStore` or `FileStore` —
    // both perfectly safe to exercise for real (no shared OS state).
    #[cfg(not(feature = "keyring"))]
    mod no_keyring_feature_matrix {
        use super::{Session, StorageConfig, create_storage};

        #[test]
        fn neither_yields_a_working_memory_store() {
            let store = create_storage(&StorageConfig::default());
            assert!(format!("{store:?}").contains("MemoryStore"));
            store
                .save(&Session::from_token("tok"))
                .expect("MemoryStore never fails");
            assert_eq!(
                store.load().expect("load never fails").expose_token(),
                "tok"
            );
        }

        #[test]
        fn file_only_yields_a_working_file_store() {
            let dir = tempfile::tempdir().expect("tempdir");
            let path = dir.path().join("cookies.json");
            let store = create_storage(&StorageConfig {
                use_keyring: false,
                cookie_file: Some(path.clone()),
            });
            store
                .save(&Session::from_token("tok"))
                .expect("file save succeeds");
            assert!(path.exists());
        }

        #[test]
        fn keyring_requested_with_no_file_degrades_to_a_working_memory_store() {
            let store = create_storage(&StorageConfig {
                use_keyring: true,
                cookie_file: None,
            });
            assert!(format!("{store:?}").contains("MemoryStore"));
            store
                .save(&Session::from_token("tok"))
                .expect("MemoryStore never fails");
            assert_eq!(
                store.load().expect("load never fails").expose_token(),
                "tok"
            );
        }

        #[test]
        fn keyring_requested_with_a_file_degrades_to_a_working_file_store() {
            let dir = tempfile::tempdir().expect("tempdir");
            let path = dir.path().join("cookies.json");
            let store = create_storage(&StorageConfig {
                use_keyring: true,
                cookie_file: Some(path.clone()),
            });
            assert!(format!("{store:?}").contains("FileStore"));
            store
                .save(&Session::from_token("tok"))
                .expect("file save succeeds");
            assert!(path.exists());
        }
    }

    // ===================== async adapters: happy path =====================

    #[tokio::test]
    async fn save_load_clear_async_round_trip_through_a_shared_store() {
        let store: Arc<dyn CredentialStore> = Arc::new(super::MemoryStore::new());
        save_async(Arc::clone(&store), Session::from_token("tok-async"))
            .await
            .expect("save succeeds");
        let loaded = load_async(Arc::clone(&store)).await.expect("load succeeds");
        assert_eq!(loaded.expose_token(), "tok-async");

        clear_async(Arc::clone(&store))
            .await
            .expect("clear succeeds");
        assert!(!load_async(store).await.expect("load succeeds").is_valid());
    }

    /// A [`CredentialStore`] whose every method returns an ordinary `Err` — isolates an
    /// unexceptional store failure (this test) from a panicking store (the `JoinError` tests
    /// below), which take a different branch inside `load_async`/`save_async`/`clear_async`.
    #[derive(Debug, Default)]
    struct AlwaysFailsStore;

    impl CredentialStore for AlwaysFailsStore {
        fn load(&self) -> Result<Session, StorageError> {
            Err(StorageError::Backend {
                backend: "always-fails".to_owned(),
                message: "deliberate test failure".to_owned(),
            })
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            Err(StorageError::Backend {
                backend: "always-fails".to_owned(),
                message: "deliberate test failure".to_owned(),
            })
        }

        fn clear(&self) -> Result<(), StorageError> {
            Err(StorageError::Backend {
                backend: "always-fails".to_owned(),
                message: "deliberate test failure".to_owned(),
            })
        }
    }

    #[tokio::test]
    async fn save_async_propagates_the_stores_own_error_unchanged() {
        let store: Arc<dyn CredentialStore> = Arc::new(AlwaysFailsStore);
        let err = save_async(store, Session::from_token("tok"))
            .await
            .expect_err("the store always fails");
        assert!(matches!(err, StorageError::Backend { backend, .. } if backend == "always-fails"));
    }

    // ===================== async adapters: JoinError path =====================

    /// A [`CredentialStore`] whose every method panics, isolating the `JoinError` branch of
    /// `load_async`/`save_async`/`clear_async` (a panicked blocking task) from an ordinary `Err`
    /// returned by the store itself ([`AlwaysFailsStore`], above).
    #[derive(Debug, Default)]
    struct PanickingStore;

    impl CredentialStore for PanickingStore {
        fn load(&self) -> Result<Session, StorageError> {
            panic!("PanickingStore::load always panics");
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            panic!("PanickingStore::save always panics");
        }

        fn clear(&self) -> Result<(), StorageError> {
            panic!("PanickingStore::clear always panics");
        }
    }

    #[tokio::test]
    async fn load_async_converts_a_panicking_store_into_a_storage_error() {
        let store: Arc<dyn CredentialStore> = Arc::new(PanickingStore);
        let err = load_async(store)
            .await
            .expect_err("a panicking store must not take down the caller's task");
        assert!(
            matches!(&err, StorageError::Backend { backend, .. } if backend == "credential-store-task")
        );
        assert!(
            err.to_string().contains("panicked=true"),
            "expected the panic flag recorded in the message, got {err}"
        );
    }

    #[tokio::test]
    async fn save_async_converts_a_panicking_store_into_a_storage_error() {
        let store: Arc<dyn CredentialStore> = Arc::new(PanickingStore);
        let err = save_async(store, Session::from_token("tok"))
            .await
            .expect_err("a panicking store must not take down the caller's task");
        assert!(
            matches!(&err, StorageError::Backend { backend, .. } if backend == "credential-store-task")
        );
        assert!(
            err.to_string().contains("panicked=true"),
            "expected the panic flag recorded in the message, got {err}"
        );
    }

    #[tokio::test]
    async fn clear_async_converts_a_panicking_store_into_a_storage_error() {
        let store: Arc<dyn CredentialStore> = Arc::new(PanickingStore);
        let err = clear_async(store)
            .await
            .expect_err("a panicking store must not take down the caller's task");
        assert!(
            matches!(&err, StorageError::Backend { backend, .. } if backend == "credential-store-task")
        );
        assert!(
            err.to_string().contains("panicked=true"),
            "expected the panic flag recorded in the message, got {err}"
        );
    }

    // ===================== async adapters: a panic payload never reaches the error =====================

    /// A [`CredentialStore`] whose panic payload is a distinctive, easy-to-grep string, so the
    /// test below can assert it never reaches a [`StorageError`]'s `Display`/`Debug` output.
    #[derive(Debug, Default)]
    struct PanickingWithCredentialLikePayloadStore;

    impl CredentialStore for PanickingWithCredentialLikePayloadStore {
        fn load(&self) -> Result<Session, StorageError> {
            panic!("session_token=leaked-secret-should-never-appear-in-storage-error");
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            panic!("session_token=leaked-secret-should-never-appear-in-storage-error");
        }

        fn clear(&self) -> Result<(), StorageError> {
            panic!("session_token=leaked-secret-should-never-appear-in-storage-error");
        }
    }

    #[tokio::test]
    async fn load_async_never_lets_the_panic_payload_reach_the_storage_error() {
        let store: Arc<dyn CredentialStore> = Arc::new(PanickingWithCredentialLikePayloadStore);
        let err = load_async(store)
            .await
            .expect_err("a panicking store must not take down the caller's task");

        let display = err.to_string();
        let debug = format!("{err:?}");
        assert!(
            !display.contains("leaked-secret") && !debug.contains("leaked-secret"),
            "the JoinError's own Display must never be interpolated into a StorageError (S4): \
             display={display:?} debug={debug:?}"
        );
    }

    // ===================== migration: one unit, no recursion =====================

    /// A store holding a legacy blob in memory that records the thread of every call, to prove a
    /// migrating load stays on the one blocking thread the adapter picked.
    #[derive(Debug)]
    struct ThreadRecordingStore {
        blob: Mutex<String>,
        threads: Mutex<Vec<ThreadId>>,
    }

    impl ThreadRecordingStore {
        fn record(&self) {
            self.threads
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .push(std::thread::current().id());
        }
    }

    impl CredentialStore for ThreadRecordingStore {
        fn load(&self) -> Result<Session, StorageError> {
            self.record();
            let blob = self
                .blob
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .clone();
            let (session, migrated) = Session::from_json_migrating(&blob)?;
            if migrated {
                migrate_and_verify(&session, "recording", |migrated| self.save(migrated));
            }
            Ok(session)
        }

        fn save(&self, session: &Session) -> Result<(), StorageError> {
            self.record();
            *self.blob.lock().unwrap_or_else(PoisonError::into_inner) = session.to_json()?;
            Ok(())
        }

        fn clear(&self) -> Result<(), StorageError> {
            self.record();
            Ok(())
        }
    }

    #[tokio::test]
    async fn load_async_runs_a_migrating_load_on_one_blocking_thread() {
        let store = Arc::new(ThreadRecordingStore {
            blob: Mutex::new(r#"{"user_token":"tok"}"#.to_owned()),
            threads: Mutex::new(Vec::new()),
        });

        let session = load_async(Arc::clone(&store) as _)
            .await
            .expect("load succeeds");

        assert_eq!(session.expose_token(), "tok");
        let stored = store.blob.lock().expect("blob lock").clone();
        assert!(
            stored.contains(r#""schema_version":2"#),
            "the legacy record was migrated in place: {stored}"
        );
        let mut threads = store.threads.lock().expect("threads lock").clone();
        assert!(
            threads.len() >= 2,
            "the load and the migration write both ran"
        );
        threads.sort_by_key(|id| format!("{id:?}"));
        threads.dedup();
        assert_eq!(threads.len(), 1, "one thread hop for the whole load");
        assert_ne!(threads[0], std::thread::current().id());
    }

    #[tokio::test]
    async fn load_async_migrates_a_legacy_file_record_in_one_call() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("cookies.json");
        std::fs::write(&path, r#"{"user_token":"tok"}"#).expect("seed the legacy record");
        let store: Arc<dyn CredentialStore> = Arc::new(FileStore::new(&path));

        let session = load_async(Arc::clone(&store)).await.expect("load succeeds");

        assert_eq!(session.expose_token(), "tok");
        let on_disk = std::fs::read_to_string(&path).expect("the file still exists");
        assert!(on_disk.contains(r#""schema_version":2"#), "{on_disk}");
    }

    /// A store that accepts every write and keeps serving the legacy record it started with.
    #[derive(Debug, Default)]
    struct StubbornStore {
        loads: AtomicUsize,
        saves: AtomicUsize,
    }

    impl CredentialStore for StubbornStore {
        fn load(&self) -> Result<Session, StorageError> {
            self.loads.fetch_add(1, Ordering::SeqCst);
            let (session, migrated) = Session::from_json_migrating(r#"{"user_token":"tok"}"#)?;
            if migrated {
                migrate_and_verify(&session, "stubborn", |migrated| self.save(migrated));
            }
            Ok(session)
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            self.saves.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }

        fn clear(&self) -> Result<(), StorageError> {
            Ok(())
        }
    }

    #[test]
    fn a_store_that_keeps_serving_the_legacy_record_does_not_recurse() {
        let store = StubbornStore::default();

        let session = store.load().expect("load succeeds");

        assert_eq!(session.expose_token(), "tok");
        assert_eq!(store.loads.load(Ordering::SeqCst), 1, "no re-entrant load");
        assert_eq!(store.saves.load(Ordering::SeqCst), 1, "one migration write");
    }

    #[test]
    fn migrate_and_verify_logs_the_outcome_of_the_save_and_never_the_token() {
        let (_guard, logs) = capture_logs();
        let session = Session::from_token("legacy_token");

        migrate_and_verify(&session, "keyring", |_| Ok(()));
        assert_eq!(
            logs.count(
                Level::DEBUG,
                "Migrated legacy credential record in keyring storage"
            ),
            1
        );
        assert_eq!(
            logs.count(
                Level::DEBUG,
                "Migration read-back verified for keyring storage"
            ),
            1
        );
        assert!(logs.messages(Level::WARN).is_empty());

        migrate_and_verify(&session, "file", |_| {
            Err(StorageError::NotRetained {
                backend: "file".to_owned(),
            })
        });
        assert_eq!(
            logs.messages(Level::WARN),
            vec![
                "Migration read-back did not match for file storage; in-memory credentials are \
                 still returned to the caller"
                    .to_owned()
            ]
        );
        assert!(!logs.mentions("legacy_token"));
    }

    // ===================== async adapters: cancellation, responsiveness, spawn failure =====================

    /// A store over a [`FileStore`] whose `save` and `clear` park, on the blocking-pool thread that
    /// runs them, until the test releases them, and report when they finish.
    #[derive(Debug)]
    struct GatedStore {
        inner: FileStore,
        entered: Mutex<mpsc::Sender<ThreadId>>,
        release: Mutex<mpsc::Receiver<()>>,
        finished: Mutex<mpsc::Sender<()>>,
    }

    /// The test's side of a [`GatedStore`].
    struct Gate {
        entered: Arc<Mutex<mpsc::Receiver<ThreadId>>>,
        release: mpsc::Sender<()>,
        finished: Arc<Mutex<mpsc::Receiver<()>>>,
    }

    fn gated(inner: FileStore) -> (Arc<GatedStore>, Gate) {
        let (entered_tx, entered_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel();
        let (finished_tx, finished_rx) = mpsc::channel();
        let store = Arc::new(GatedStore {
            inner,
            entered: Mutex::new(entered_tx),
            release: Mutex::new(release_rx),
            finished: Mutex::new(finished_tx),
        });
        let gate = Gate {
            entered: Arc::new(Mutex::new(entered_rx)),
            release: release_tx,
            finished: Arc::new(Mutex::new(finished_rx)),
        };
        (store, gate)
    }

    impl GatedStore {
        fn park(&self) {
            let _ = self
                .entered
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .send(std::thread::current().id());
            let _ = self
                .release
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .recv_timeout(Duration::from_secs(5));
        }

        fn finish(&self) {
            let _ = self
                .finished
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .send(());
        }
    }

    impl CredentialStore for GatedStore {
        fn load(&self) -> Result<Session, StorageError> {
            self.inner.load()
        }

        fn save(&self, session: &Session) -> Result<(), StorageError> {
            self.park();
            let result = self.inner.save(session);
            self.finish();
            result
        }

        fn clear(&self) -> Result<(), StorageError> {
            self.park();
            let result = self.inner.clear();
            self.finish();
            result
        }
    }

    /// Waits for the next signal without blocking the test's runtime.
    async fn next<T: Send + 'static>(receiver: &Arc<Mutex<mpsc::Receiver<T>>>) -> T {
        let receiver = Arc::clone(receiver);
        tokio::task::spawn_blocking(move || {
            receiver
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .recv_timeout(Duration::from_secs(5))
                .expect("the gated store signals in time")
        })
        .await
        .expect("the waiting task finishes")
    }

    /// Reads the token straight from disk.
    fn token_on_disk(path: &Path) -> Option<String> {
        let contents = std::fs::read_to_string(path).ok()?;
        Some(
            Session::from_json(&contents)
                .ok()?
                .expose_token()
                .to_owned(),
        )
    }

    #[tokio::test]
    async fn dropping_save_async_does_not_interrupt_the_blocking_write() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("cookies.json");
        FileStore::new(&path)
            .save(&Session::from_token("old"))
            .expect("seed save succeeds");
        let (store, gate) = gated(FileStore::new(&path));

        let task = tokio::spawn(save_async(store, Session::from_token("new")));
        next(&gate.entered).await;
        task.abort();
        assert!(
            task.await
                .expect_err("the awaiting task was aborted")
                .is_cancelled()
        );
        assert_eq!(
            token_on_disk(&path).as_deref(),
            Some("old"),
            "the parked write has not happened yet, and nothing partial is on disk"
        );

        // The worker thread keeps going: the cancelled caller just never sees the result.
        gate.release.send(()).expect("the worker is parked");
        next(&gate.finished).await;
        let reloaded = load_async(Arc::new(FileStore::new(&path)))
            .await
            .expect("load succeeds");
        assert_eq!(reloaded.expose_token(), "new");

        let listing: Vec<_> = std::fs::read_dir(dir.path())
            .expect("read dir")
            .filter_map(Result::ok)
            .map(|entry| entry.file_name())
            .collect();
        assert_eq!(
            listing,
            vec![std::ffi::OsString::from("cookies.json")],
            "no temporary file is left behind"
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&path)
                .expect("file exists")
                .permissions()
                .mode()
                & 0o777;
            assert_eq!(mode, 0o600);
        }
    }

    /// Runs `ticks` short sleeps on the test's current-thread runtime and returns how long that
    /// took: a runtime blocked by a store call could not complete them.
    async fn heartbeat(ticks: u32) -> Duration {
        let started = Instant::now();
        tokio::time::timeout(Duration::from_secs(5), async {
            for _ in 0..ticks {
                tokio::time::sleep(Duration::from_millis(5)).await;
            }
        })
        .await
        .expect("the runtime keeps ticking while the store call is parked");
        started.elapsed()
    }

    #[tokio::test]
    async fn save_async_keeps_the_runtime_responsive_while_the_write_blocks() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("cookies.json");
        let (store, gate) = gated(FileStore::new(&path));

        let save = tokio::spawn(save_async(store, Session::from_token("tok")));
        let worker = next(&gate.entered).await;
        assert_ne!(
            worker,
            std::thread::current().id(),
            "the write runs off-runtime"
        );

        assert!(heartbeat(10).await < Duration::from_secs(2));
        assert!(!save.is_finished(), "the write is still parked");

        gate.release.send(()).expect("the worker is parked");
        save.await
            .expect("the save task finishes")
            .expect("save succeeds");
        assert_eq!(token_on_disk(&path).as_deref(), Some("tok"));
    }

    #[tokio::test]
    async fn clear_async_keeps_the_runtime_responsive_while_the_clear_blocks() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("cookies.json");
        FileStore::new(&path)
            .save(&Session::from_token("tok"))
            .expect("seed save succeeds");
        let (store, gate) = gated(FileStore::new(&path));

        let clear = tokio::spawn(clear_async(store));
        let worker = next(&gate.entered).await;
        assert_ne!(
            worker,
            std::thread::current().id(),
            "the clear runs off-runtime"
        );

        assert!(heartbeat(10).await < Duration::from_secs(2));
        assert!(!clear.is_finished(), "the clear is still parked");

        gate.release.send(()).expect("the worker is parked");
        clear
            .await
            .expect("the clear task finishes")
            .expect("clear succeeds");
        assert!(!path.exists());
    }

    #[test]
    fn a_file_store_works_across_two_successive_runtimes() {
        let dir = tempfile::tempdir().expect("tempdir");
        let store: Arc<dyn CredentialStore> =
            Arc::new(FileStore::new(dir.path().join("cookies.json")));

        for token in ["a", "b"] {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("a current-thread runtime builds");
            runtime.block_on(async {
                save_async(Arc::clone(&store), Session::from_token(token))
                    .await
                    .expect("save succeeds");
                let loaded = load_async(Arc::clone(&store)).await.expect("load succeeds");
                assert_eq!(loaded.expose_token(), token);
            });
        }
    }

    /// The adapters' own failures are told apart from an error a store returned, by backend name.
    #[tokio::test]
    async fn only_the_adapters_own_failures_are_task_failures() {
        let unstartable = spawn_guarded::<()>(|| panic!("OS can't spawn worker thread: test"))
            .expect_err("the spawner panicked");
        assert!(super::is_task_failure(&unstartable));

        let store: Arc<dyn CredentialStore> = Arc::new(PanickingStore);
        let panicked = save_async(store, Session::from_token("tok"))
            .await
            .expect_err("the store panicked");
        assert!(super::is_task_failure(&panicked), "{panicked:?}");

        for returned in [
            StorageError::Backend {
                backend: "file".to_owned(),
                message: "disk is on fire".to_owned(),
            },
            StorageError::NotRetained {
                backend: "credential-store-task".to_owned(),
            },
        ] {
            assert!(!super::is_task_failure(&returned), "{returned:?}");
        }
    }

    #[tokio::test]
    async fn a_worker_thread_that_cannot_be_spawned_is_a_handled_storage_error() {
        let err = spawn_guarded::<()>(|| panic!("OS can't spawn worker thread: test"))
            .expect_err("the spawner panicked");

        match &err {
            StorageError::Backend { backend, message } => {
                assert_eq!(backend, "credential-store-task");
                assert_eq!(message, "blocking credential-store task could not start");
            }
            other => panic!("expected a backend error, got {other:?}"),
        }
        assert!(
            !format!("{err} {err:?}").contains("OS can't spawn"),
            "the panic payload never reaches the error"
        );

        // Nothing was left wedged: the next real operation works.
        let store: Arc<dyn CredentialStore> = Arc::new(super::MemoryStore::new());
        save_async(Arc::clone(&store), Session::from_token("tok"))
            .await
            .expect("a following save succeeds");
        assert_eq!(
            load_async(store)
                .await
                .expect("load succeeds")
                .expose_token(),
            "tok"
        );
    }

    /// A store that accepts every write and retains nothing, like a keyring that drops writes.
    #[derive(Debug)]
    struct NullStore;

    impl CredentialStore for NullStore {
        fn load(&self) -> Result<Session, StorageError> {
            Ok(Session::empty())
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            Err(StorageError::NotRetained {
                backend: "null".to_owned(),
            })
        }

        fn clear(&self) -> Result<(), StorageError> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn save_async_returns_a_not_retained_error_unchanged() {
        let err = save_async(Arc::new(NullStore), Session::from_token("tok"))
            .await
            .expect_err("the store did not retain the session");

        assert!(matches!(&err, StorageError::NotRetained { backend } if backend == "null"));
    }

    // ===================== async adapters: the caller's subscriber follows the work =====================

    /// A store that logs a `WARN` from inside every operation and records the span that was
    /// current while it ran.
    #[derive(Debug, Default)]
    struct WarningStore {
        spans: Mutex<Vec<Option<tracing::Id>>>,
    }

    impl WarningStore {
        fn note(&self) {
            tracing::warn!("store warning from the blocking thread");
            self.spans
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .push(tracing::Span::current().id());
        }
    }

    impl CredentialStore for WarningStore {
        fn load(&self) -> Result<Session, StorageError> {
            self.note();
            Ok(Session::empty())
        }

        fn save(&self, _session: &Session) -> Result<(), StorageError> {
            self.note();
            Ok(())
        }

        fn clear(&self) -> Result<(), StorageError> {
            self.note();
            Ok(())
        }
    }

    #[tokio::test]
    async fn a_store_warning_inside_save_async_reaches_the_callers_scoped_subscriber() {
        let (_guard, logs) = capture_logs();
        let store = Arc::new(WarningStore::default());

        save_async(Arc::clone(&store) as _, Session::from_token("tok"))
            .await
            .expect("save succeeds");

        assert_eq!(
            logs.count(Level::WARN, "store warning from the blocking thread"),
            1,
            "the blocking thread logs under the dispatcher that was current when it was spawned"
        );
    }

    #[tokio::test]
    async fn load_and_clear_async_forward_the_callers_subscriber_too() {
        let (_guard, logs) = capture_logs();
        let store = Arc::new(WarningStore::default());

        load_async(Arc::clone(&store) as _)
            .await
            .expect("load succeeds");
        clear_async(Arc::clone(&store) as _)
            .await
            .expect("clear succeeds");

        assert_eq!(
            logs.count(Level::WARN, "store warning from the blocking thread"),
            2
        );
    }

    #[tokio::test]
    async fn the_callers_current_span_is_current_inside_the_store_call() {
        use tracing::Instrument;

        let (_guard, _logs) = capture_logs();
        let store = Arc::new(WarningStore::default());
        let span = tracing::info_span!("caller");
        let expected = span.id();
        assert!(
            expected.is_some(),
            "the capturing subscriber assigns span ids"
        );

        save_async(Arc::clone(&store) as _, Session::from_token("tok"))
            .instrument(span)
            .await
            .expect("save succeeds");

        let seen = store.spans.lock().expect("spans lock").clone();
        assert_eq!(seen, vec![expected]);
    }
}