mbr-markdown-browser 0.5.1-rc2

A fast, featureful markdown viewer, browser, and (optional) static site generator
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
//! HLS transcode cache module for caching playlists and segments.
//!
//! Provides a thread-safe, size-bounded cache for HLS playlists and transcoded
//! video segments to avoid redundant transcoding operations. Handles concurrent
//! requests by ensuring only one transcode runs per segment.
//!
//! ## Cancellation safety
//!
//! An in-progress marker that is never settled would wedge its key forever:
//! every later request for it would wait out [`HLS_WAIT_TIMEOUT`] and fail, with
//! no work running. That is not hypothetical — Safari's HLS loader routinely
//! abandons segment prefetches, and axum drops the request future when a client
//! disconnects, so any completion bookkeeping living in the request path gets
//! skipped.
//!
//! [`HlsCache::spawn_generation`] is therefore the only supported way to produce
//! content: it runs the work in a detached task so a disconnect cannot stop it,
//! and holds an [`InFlightGuard`] whose `Drop` withdraws the marker if the result
//! was never recorded. No client behaviour can leave a key in-progress.

use crate::cache::{Claim, SizeBoundedMap};
use crate::video_remux::RemuxPart;
use crate::video_transcode::{TranscodeError, TranscodeTarget};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
#[cfg(test)]
use std::time::Instant;
use tokio::sync::Notify;

/// Maximum time a request will wait for an in-progress generation to complete
/// before giving up. Guards against a lost wakeup degrading into a permanent
/// hang: on timeout the caller gets `None` (retryable) rather than blocking
/// forever.
pub const HLS_WAIT_TIMEOUT: Duration = Duration::from_secs(60);

/// How long a `Failed` cache entry is honored before it is treated as expired
/// and a retry is allowed. Prevents a single transient error from poisoning a
/// playlist until the process restarts.
const FAILED_ENTRY_TTL: Duration = Duration::from_secs(60);

/// Cache key for HLS content (playlists and segments).
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum HlsCacheKey {
    /// Key for an HLS playlist (.m3u8)
    Playlist {
        path: PathBuf,
        target: TranscodeTarget,
    },
    /// Key for an HLS segment (.ts)
    Segment {
        path: PathBuf,
        target: TranscodeTarget,
        segment_index: u32,
    },
    /// Key for one part of the stream-copy (remux) fMP4 variant.
    ///
    /// Unlike the transcode variants this is keyed by a *mtime-scoped* string
    /// (see [`crate::video_metadata_cache::cache_key_with_mtime`]) rather than
    /// the bare path, so editing the source file yields fresh keys and the
    /// playlist, init segment and media segments are re-derived together. That
    /// matters more here than for the transcode ladder because a remux
    /// segment's byte layout depends on the source's keyframe positions.
    Remux { source_key: String, part: RemuxPart },
}

impl HlsCacheKey {
    /// Creates a new playlist cache key.
    pub fn playlist(path: PathBuf, target: TranscodeTarget) -> Self {
        Self::Playlist { path, target }
    }

    /// Creates a new segment cache key.
    pub fn segment(path: PathBuf, target: TranscodeTarget, segment_index: u32) -> Self {
        Self::Segment {
            path,
            target,
            segment_index,
        }
    }

    /// Creates a cache key for one part of the remux variant.
    pub fn remux(source_key: String, part: RemuxPart) -> Self {
        Self::Remux { source_key, part }
    }

    /// Whether this entry is a small, frequently needed index rather than bulk
    /// media, and so should be evicted last.
    ///
    /// Playlists and the fMP4 init segment are both tiny and required for every
    /// playback attempt; media segments are large and cheap to regenerate.
    fn is_index_entry(&self) -> bool {
        match self {
            Self::Playlist { .. } => true,
            Self::Segment { .. } => false,
            Self::Remux { part, .. } => {
                matches!(part, RemuxPart::Playlist | RemuxPart::Init)
            }
        }
    }
}

/// State of a cache entry.
#[derive(Clone)]
pub enum HlsCacheState {
    /// Content generation is in progress - wait on the notify signal
    InProgress(Arc<Notify>),
    /// Content is ready (playlist text or segment binary data)
    Complete(Arc<Vec<u8>>),
    /// Content generation failed with an error message
    Failed(String),
}

/// Thread-safe cache for HLS playlists and segments.
///
/// A state machine (in-progress / complete / failed with TTL) layered on the
/// shared [`SizeBoundedMap`] core, which provides lock-free reads, overwrite
/// accounting, and size-based eviction. Only `Complete` entries carry weight.
pub struct HlsCache {
    /// The shared size-bounded concurrent cache core
    cache: SizeBoundedMap<HlsCacheKey, HlsCacheState>,
}

impl HlsCache {
    /// Creates a new cache with the specified maximum size in bytes.
    ///
    /// # Arguments
    ///
    /// * `max_size_bytes` - Maximum memory to use for cached content.
    ///   Set to 0 to disable caching entirely.
    pub fn new(max_size_bytes: usize) -> Self {
        Self {
            cache: SizeBoundedMap::new(max_size_bytes),
        }
    }

    /// Gets the current state of a cache entry.
    ///
    /// Returns `None` if no entry exists for this key.
    pub fn get_state(&self, key: &HlsCacheKey) -> Option<HlsCacheState> {
        self.cache.with_entry(key, |entry| entry.value.clone())
    }

    /// Marks content generation as in-progress and returns the Notify to signal on completion.
    ///
    /// If generation is already in progress or complete, returns the existing state.
    /// This ensures only one generation runs per key.
    ///
    /// The slot is claimed with a single compare-and-swap
    /// ([`SizeBoundedMap::claim_or`]). A `get` followed by an `insert` would
    /// not hold: papaya's pin guard is an epoch guard, not a lock, so racing
    /// callers could all observe a vacant slot, all be told to `Started`, and
    /// all transcode the same segment — with every loser's `Notify` orphaned
    /// when the winner later overwrites the entry.
    pub fn start_generation(&self, key: HlsCacheKey) -> HlsCacheStartResult {
        if self.cache.is_disabled() {
            return HlsCacheStartResult::CacheDisabled;
        }

        // Weightless: only Complete entries are counted against the budget.
        let notify = Arc::new(Notify::new());
        let claim = self.cache.claim_or(
            key.clone(),
            HlsCacheState::InProgress(notify.clone()),
            0,
            |entry| match &entry.value {
                HlsCacheState::InProgress(existing) => {
                    Some(HlsCacheStartResult::AlreadyInProgress(existing.clone()))
                }
                HlsCacheState::Complete(data) => {
                    Some(HlsCacheStartResult::AlreadyComplete(data.clone()))
                }
                // Only honor a failure while it is still fresh. Once the TTL
                // elapses the entry is replaced so a transient error can be
                // retried — by exactly one caller.
                HlsCacheState::Failed(msg) if entry.inserted_at.elapsed() < FAILED_ENTRY_TTL => {
                    Some(HlsCacheStartResult::PreviouslyFailed(msg.clone()))
                }
                HlsCacheState::Failed(_) => None,
            },
        );

        match claim {
            Claim::Retained(existing) => existing,
            Claim::Claimed => {
                tracing::debug!("Started generation for {:?}", key);
                HlsCacheStartResult::Started(notify)
            }
        }
    }

    /// Marks content generation as complete and stores the result.
    ///
    /// Notifies any waiters and may trigger eviction if cache is over limit.
    ///
    /// Returns the stored payload so the producer can serve it directly. Reading
    /// it back out of the cache instead would be a race: a large entry can be
    /// evicted by the very insertion that added it, and the caller would then
    /// have to 404 a segment it had just built successfully.
    pub fn complete_generation(&self, key: HlsCacheKey, data: Vec<u8>) -> Arc<Vec<u8>> {
        let size_bytes = data.len();
        let stored = Arc::new(data);
        if self.cache.is_disabled() {
            return stored;
        }

        // Store the completed content. The core subtracts any replaced
        // entry's accounted size so `current_size` does not ratchet up on
        // rewrite, and hands back the replaced state so waiters registered on
        // an in-progress generation can be notified.
        let (replaced, new_size) = self.cache.insert_weighted(
            key.clone(),
            HlsCacheState::Complete(stored.clone()),
            size_bytes,
        );

        tracing::debug!(
            "Generation complete for {:?} ({} bytes, cache size: {} bytes)",
            key,
            size_bytes,
            new_size
        );

        // Notify waiters
        if let Some(HlsCacheState::InProgress(n)) = replaced {
            n.notify_waiters();
        }

        // Evict if over limit
        if new_size > self.cache.max_size() {
            self.evict_oldest(new_size - self.cache.max_size());
        }

        stored
    }

    /// Marks content generation as failed with an error message.
    pub fn fail_generation(&self, key: HlsCacheKey, error: &TranscodeError) {
        if self.cache.is_disabled() {
            return;
        }

        let (replaced, _) =
            self.cache
                .insert_weighted(key.clone(), HlsCacheState::Failed(error.to_string()), 0);
        tracing::warn!("Generation failed for {:?}: {}", key, error);

        // Notify waiters (so they know to check the state)
        if let Some(HlsCacheState::InProgress(n)) = replaced {
            n.notify_waiters();
        }
    }

    /// Withdraws an in-progress marker that will never be settled.
    ///
    /// Only removes the entry while it is still `InProgress`, so a generation
    /// that completed concurrently is never clobbered. Returns whether a marker
    /// was withdrawn.
    ///
    /// Leaving the marker in place would be the worst possible failure mode: the
    /// key looks busy forever, so every later request waits out
    /// [`HLS_WAIT_TIMEOUT`] while no work is running. Removing it instead lets
    /// the next request start cleanly.
    fn abandon_generation(&self, key: &HlsCacheKey) -> bool {
        let withdrawn = self.cache.remove_if(key.clone(), |entry| {
            matches!(entry.value, HlsCacheState::InProgress(_))
        });
        if withdrawn {
            tracing::debug!("Withdrew unsettled in-progress marker for {:?}", key);
        }
        withdrawn
    }

    /// Runs `generate` for a key already claimed via [`Self::start_generation`],
    /// settling the cache entry no matter what happens to the caller.
    ///
    /// The work runs in a **detached** task. That is the fix for a real and
    /// frequent failure: when a client disconnects mid-request (Safari's HLS
    /// loader abandons segment prefetches constantly), axum drops the request
    /// future. Bookkeeping awaited in that future would simply never run, and the
    /// key would stay in-progress forever. Dropping the returned `JoinHandle`
    /// does not abort a tokio task, so the generation still finishes and still
    /// populates the cache — the abandoned work is not even wasted, since the
    /// client's inevitable retry is then served from cache.
    ///
    /// An [`InFlightGuard`] covers the remaining gap: if the task itself is
    /// dropped (runtime shutdown) or panics before recording a result, the marker
    /// is withdrawn and waiters are woken on unwind.
    ///
    /// # Panics
    ///
    /// Must be called from within a tokio runtime.
    pub fn spawn_generation<F>(
        cache: &Arc<Self>,
        key: HlsCacheKey,
        notify: Arc<Notify>,
        generate: F,
    ) -> tokio::task::JoinHandle<Result<Arc<Vec<u8>>, TranscodeError>>
    where
        F: FnOnce() -> Result<Vec<u8>, TranscodeError> + Send + 'static,
    {
        let cache = Arc::clone(cache);
        tokio::spawn(async move {
            let mut guard = InFlightGuard {
                cache: Arc::clone(&cache),
                key: key.clone(),
                notify: Arc::clone(&notify),
                settled: false,
            };

            // ffmpeg demuxing and muxing is blocking CPU/IO work and must not
            // occupy a tokio worker thread.
            let outcome = tokio::task::spawn_blocking(generate).await;

            let result = match outcome {
                Ok(Ok(data)) => Ok(cache.complete_generation(key.clone(), data)),
                Ok(Err(error)) => {
                    cache.fail_generation(key.clone(), &error);
                    Err(error)
                }
                Err(join_error) => {
                    // The blocking closure panicked. Record it as a failure so
                    // waiters get an answer instead of timing out.
                    let error = TranscodeError::TranscodeFailed(format!(
                        "generation task did not finish: {join_error}"
                    ));
                    cache.fail_generation(key.clone(), &error);
                    Err(error)
                }
            };

            // A result is recorded, so the guard has nothing left to clean up.
            guard.settled = true;
            // `complete_generation`/`fail_generation` already wake waiters via
            // the state they replaced; this also covers the case where the
            // in-progress entry was evicted or overwritten in the meantime, so a
            // waiter can never be left asleep with no one to wake it.
            notify.notify_waiters();

            result
        })
    }

    /// Waits for an in-progress generation (identified by `notify`) to complete,
    /// returning the completed data if it becomes available within `timeout`.
    ///
    /// This uses the race-free tokio `Notify` pattern: interest is registered
    /// (`enable`) *before* re-checking cache state, so a completion that lands
    /// between `start_generation` and this call is never missed. A bounded
    /// timeout guards against a genuinely lost signal, degrading a hang into a
    /// retryable `None`.
    pub async fn wait_for_completion(
        &self,
        key: &HlsCacheKey,
        notify: Arc<Notify>,
        timeout: Duration,
    ) -> Option<Arc<Vec<u8>>> {
        // Register interest before re-checking so no wakeup can be lost.
        let notified = notify.notified();
        tokio::pin!(notified);
        notified.as_mut().enable();

        // The generation may have finished before we registered above.
        if let Some(HlsCacheState::Complete(data)) = self.get_state(key) {
            return Some(data);
        }

        match tokio::time::timeout(timeout, notified).await {
            Ok(()) => match self.get_state(key) {
                Some(HlsCacheState::Complete(data)) => Some(data),
                _ => None,
            },
            Err(_) => {
                tracing::warn!("Timed out waiting for in-progress generation of {:?}", key);
                None
            }
        }
    }

    /// Evicts oldest completed entries until at least `target_bytes` have been freed.
    ///
    /// Only `Complete` entries are evictable; media segments are preferred over
    /// playlists and init segments (segments are larger), oldest first within
    /// each group.
    fn evict_oldest(&self, target_bytes: usize) {
        let stats = self.cache.evict_until_freed(target_bytes, |key, entry| {
            if matches!(entry.value, HlsCacheState::Complete(_)) && entry.size_bytes > 0 {
                // Sort key: bulk media first (is_index_entry=false), then oldest first
                Some((key.is_index_entry(), entry.inserted_at))
            } else {
                None
            }
        });

        if stats.evicted > 0 {
            tracing::info!(
                "HLS cache evicted {} entries ({} bytes freed)",
                stats.evicted,
                stats.freed
            );
        }
    }

    /// Returns the current approximate size of the cache in bytes.
    #[cfg(test)]
    pub fn current_size(&self) -> usize {
        self.cache.current_size()
    }

    /// Returns the number of entries in the cache.
    #[cfg(test)]
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    /// Returns true if the cache is empty.
    #[cfg(test)]
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }

    /// Inserts a `Failed` entry with an explicit `created_at` timestamp.
    ///
    /// Test-only helper used to exercise TTL expiry without waiting in real
    /// time (std `Instant` cannot be paused like tokio's clock).
    #[cfg(test)]
    pub fn insert_failed_for_test(&self, key: HlsCacheKey, message: &str, created_at: Instant) {
        self.cache.insert_weighted_at(
            key,
            HlsCacheState::Failed(message.to_string()),
            0,
            created_at,
        );
    }
}

/// Withdraws an unsettled in-progress marker when dropped.
///
/// The invariant it enforces: a claimed key is always either settled with a
/// result or released. Without that, an interrupted generation leaves the key
/// looking permanently busy, and every later request for it waits out
/// [`HLS_WAIT_TIMEOUT`] with no work running — a silent, permanent stall rather
/// than an error anyone can see.
///
/// `Notify::notify_waiters` is synchronous, so waking waiters from `Drop` is
/// sound; nothing here awaits.
struct InFlightGuard {
    cache: Arc<HlsCache>,
    key: HlsCacheKey,
    notify: Arc<Notify>,
    /// Set once a result has been recorded, which makes the drop a no-op.
    settled: bool,
}

impl Drop for InFlightGuard {
    fn drop(&mut self) {
        if self.settled {
            return;
        }
        tracing::warn!(
            "Generation of {:?} ended without a result; releasing the claim",
            self.key
        );
        self.cache.abandon_generation(&self.key);
        // Wake anyone already waiting so they retry immediately instead of
        // blocking on a claim that no longer exists.
        self.notify.notify_waiters();
    }
}

/// Result of attempting to start content generation.
pub enum HlsCacheStartResult {
    /// Generation was started - the caller should generate content and call complete/fail
    Started(Arc<Notify>),
    /// Another generation is already in progress - wait on the Notify then re-fetch state
    AlreadyInProgress(Arc<Notify>),
    /// Content already generated - use the cached data
    AlreadyComplete(Arc<Vec<u8>>),
    /// A previous generation failed - caller can retry or serve original
    PreviouslyFailed(String),
    /// Cache is disabled (max_size = 0)
    CacheDisabled,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_playlist_key(path: &str, target: TranscodeTarget) -> HlsCacheKey {
        HlsCacheKey::playlist(PathBuf::from(path), target)
    }

    fn make_segment_key(path: &str, target: TranscodeTarget, index: u32) -> HlsCacheKey {
        HlsCacheKey::segment(PathBuf::from(path), target, index)
    }

    #[test]
    fn test_start_and_complete_playlist() {
        let cache = HlsCache::new(1024 * 1024); // 1MB

        let key = make_playlist_key("/videos/test.mp4", TranscodeTarget::Resolution720p);

        // Start generation
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));

        // Should be in progress
        let state = cache.get_state(&key);
        assert!(matches!(state, Some(HlsCacheState::InProgress(_))));

        // Complete it
        let data = b"#EXTM3U\n#EXT-X-VERSION:3\n".to_vec();
        cache.complete_generation(key.clone(), data);

        // Should be complete
        let state = cache.get_state(&key);
        assert!(matches!(state, Some(HlsCacheState::Complete(_))));
    }

    #[test]
    fn test_start_and_complete_segment() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 5);

        // Start generation
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));

        // Complete it
        let data = vec![0u8; 1000]; // Fake MPEG-TS data
        cache.complete_generation(key.clone(), data);

        // Should be complete
        let state = cache.get_state(&key);
        assert!(matches!(state, Some(HlsCacheState::Complete(_))));
    }

    #[test]
    fn test_concurrent_start_returns_in_progress() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        // First start
        let result1 = cache.start_generation(key.clone());
        assert!(matches!(result1, HlsCacheStartResult::Started(_)));

        // Second start should return AlreadyInProgress
        let result2 = cache.start_generation(key.clone());
        assert!(matches!(result2, HlsCacheStartResult::AlreadyInProgress(_)));
    }

    #[test]
    fn test_concurrent_start_generation_admits_one_producer() {
        // Regression: `start_generation` used to `get` then `insert`, two
        // independent lock-free operations. Racing callers could all observe a
        // vacant slot and all become producers, so the same segment was
        // transcoded several times and every loser's `Notify` was orphaned.
        use std::sync::Barrier;
        use std::sync::atomic::{AtomicUsize, Ordering};

        const THREADS: usize = 8;
        const ROUNDS: usize = 64;

        for round in 0..ROUNDS {
            let cache = HlsCache::new(1024 * 1024);
            let key = make_segment_key(
                &format!("/videos/race{round}.mp4"),
                TranscodeTarget::Resolution720p,
                0,
            );
            let barrier = Barrier::new(THREADS);
            let started = AtomicUsize::new(0);

            std::thread::scope(|scope| {
                for _ in 0..THREADS {
                    scope.spawn(|| {
                        barrier.wait();
                        if matches!(
                            cache.start_generation(key.clone()),
                            HlsCacheStartResult::Started(_)
                        ) {
                            started.fetch_add(1, Ordering::Relaxed);
                        }
                    });
                }
            });

            assert_eq!(
                started.load(Ordering::Relaxed),
                1,
                "exactly one caller may be told to generate a given key"
            );
        }
    }

    #[test]
    fn test_concurrent_start_generation_after_expired_failure_admits_one_producer() {
        // The expired-`Failed` path also replaces the entry, so it needs the
        // same atomicity: only one retry may become the producer.
        use std::sync::Barrier;
        use std::sync::atomic::{AtomicUsize, Ordering};

        const THREADS: usize = 8;
        const ROUNDS: usize = 64;

        for round in 0..ROUNDS {
            let cache = HlsCache::new(1024 * 1024);
            let key = make_segment_key(
                &format!("/videos/retry{round}.mp4"),
                TranscodeTarget::Resolution720p,
                0,
            );
            let stale = Instant::now()
                .checked_sub(FAILED_ENTRY_TTL + Duration::from_secs(1))
                .expect("clock far enough from epoch");
            cache.insert_failed_for_test(key.clone(), "stale failure", stale);

            let barrier = Barrier::new(THREADS);
            let started = AtomicUsize::new(0);

            std::thread::scope(|scope| {
                for _ in 0..THREADS {
                    scope.spawn(|| {
                        barrier.wait();
                        if matches!(
                            cache.start_generation(key.clone()),
                            HlsCacheStartResult::Started(_)
                        ) {
                            started.fetch_add(1, Ordering::Relaxed);
                        }
                    });
                }
            });

            assert_eq!(
                started.load(Ordering::Relaxed),
                1,
                "an expired failure may be retried by exactly one caller"
            );
        }
    }

    #[test]
    fn test_complete_returns_already_complete() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        // Start and complete
        cache.start_generation(key.clone());
        cache.complete_generation(key.clone(), vec![0u8; 100]);

        // Another start should return AlreadyComplete
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::AlreadyComplete(_)));
    }

    #[test]
    fn test_failed_generation() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        cache.start_generation(key.clone());
        cache.fail_generation(
            key.clone(),
            &TranscodeError::TranscodeFailed("Test failure".to_string()),
        );

        // Should be failed
        let state = cache.get_state(&key);
        assert!(matches!(state, Some(HlsCacheState::Failed(_))));

        // Start again should return PreviouslyFailed
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::PreviouslyFailed(_)));
    }

    #[test]
    fn test_failed_entry_within_ttl_blocks_retry() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        cache.start_generation(key.clone());
        cache.fail_generation(
            key.clone(),
            &TranscodeError::TranscodeFailed("Test failure".to_string()),
        );

        // A fresh failure should still be honored (not retried).
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::PreviouslyFailed(_)));
    }

    #[test]
    fn test_failed_entry_expires_and_allows_retry() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        // Simulate a failure that happened longer ago than the TTL.
        let stale = Instant::now()
            .checked_sub(FAILED_ENTRY_TTL + Duration::from_secs(1))
            .expect("clock far enough from epoch");
        cache.insert_failed_for_test(key.clone(), "stale failure", stale);

        // Sanity: it is stored as Failed.
        assert!(matches!(
            cache.get_state(&key),
            Some(HlsCacheState::Failed(_))
        ));

        // Expired failure should allow a fresh generation.
        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));
    }

    #[test]
    fn test_disabled_cache() {
        let cache = HlsCache::new(0); // Disabled

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        let result = cache.start_generation(key.clone());
        assert!(matches!(result, HlsCacheStartResult::CacheDisabled));

        assert!(cache.get_state(&key).is_none());
    }

    #[test]
    fn test_different_resolutions_are_separate() {
        let cache = HlsCache::new(1024 * 1024);

        let key_720 = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);
        let key_480 = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution480p, 0);

        cache.start_generation(key_720.clone());
        cache.complete_generation(key_720.clone(), vec![0u8; 100]);

        // 480p should still be startable
        let result = cache.start_generation(key_480.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));
    }

    #[test]
    fn test_different_segments_are_separate() {
        let cache = HlsCache::new(1024 * 1024);

        let key_0 = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);
        let key_1 = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 1);

        cache.start_generation(key_0.clone());
        cache.complete_generation(key_0.clone(), vec![0u8; 100]);

        // Segment 1 should still be startable
        let result = cache.start_generation(key_1.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));
    }

    #[test]
    fn test_playlist_and_segment_are_separate() {
        let cache = HlsCache::new(1024 * 1024);

        let playlist_key = make_playlist_key("/videos/test.mp4", TranscodeTarget::Resolution720p);
        let segment_key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        cache.start_generation(playlist_key.clone());
        cache.complete_generation(playlist_key.clone(), b"#EXTM3U\n".to_vec());

        // Segment should still be startable
        let result = cache.start_generation(segment_key.clone());
        assert!(matches!(result, HlsCacheStartResult::Started(_)));
    }

    #[test]
    fn test_size_tracking() {
        let cache = HlsCache::new(1024 * 1024);

        assert_eq!(cache.current_size(), 0);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);
        cache.start_generation(key.clone());
        cache.complete_generation(key.clone(), vec![0u8; 500]);

        assert_eq!(cache.current_size(), 500);
    }

    #[test]
    fn test_eviction_prefers_segments_over_playlists() {
        // Small cache
        let cache = HlsCache::new(500);

        // Add a playlist (small)
        let playlist_key = make_playlist_key("/videos/test.mp4", TranscodeTarget::Resolution720p);
        cache.start_generation(playlist_key.clone());
        cache.complete_generation(playlist_key.clone(), vec![0u8; 50]);

        // Add segments until eviction is triggered
        for i in 0..10 {
            let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, i);
            cache.start_generation(key.clone());
            cache.complete_generation(key, vec![0u8; 100]);
        }

        // Playlist should still exist (segments evicted first)
        assert!(matches!(
            cache.get_state(&playlist_key),
            Some(HlsCacheState::Complete(_))
        ));
    }

    #[test]
    fn test_eviction_on_size_limit() {
        // Small cache that can only hold ~1000 bytes
        let cache = HlsCache::new(1000);

        // Insert several segments to trigger eviction
        for i in 0..10 {
            let key = make_segment_key(
                &format!("/videos/test{}.mp4", i),
                TranscodeTarget::Resolution720p,
                0,
            );
            cache.start_generation(key.clone());
            cache.complete_generation(key, vec![0u8; 200]);
        }

        // Cache should have evicted some entries to stay within bounds
        assert!(cache.current_size() <= 1200); // Allow some slack
    }

    /// The producer must always get its payload back, even when the entry it
    /// just inserted is immediately evicted for being over the size limit.
    /// Otherwise a large segment could 404 right after generating successfully.
    #[test]
    fn test_complete_generation_returns_payload_even_when_evicted() {
        // Cache far smaller than the payload, so the insert immediately evicts.
        let cache = HlsCache::new(64);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);
        cache.start_generation(key.clone());
        let stored = cache.complete_generation(key.clone(), vec![9u8; 4096]);

        assert_eq!(stored.len(), 4096);
        assert!(stored.iter().all(|byte| *byte == 9));
        // It really was evicted, so reading the cache back would have failed.
        assert!(!matches!(
            cache.get_state(&key),
            Some(HlsCacheState::Complete(_))
        ));
    }

    /// A disabled cache stores nothing but must still hand the payload back.
    #[test]
    fn test_complete_generation_returns_payload_when_cache_disabled() {
        let cache = HlsCache::new(0);
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        let stored = cache.complete_generation(key.clone(), vec![1u8; 32]);
        assert_eq!(stored.len(), 32);
        assert!(cache.get_state(&key).is_none());
    }

    /// Remux parts are keyed independently of the transcode ladder and of each
    /// other, and a new mtime yields new keys so an edited file re-segments.
    #[test]
    fn test_remux_keys_are_distinct() {
        use crate::video_remux::RemuxPart;

        let cache = HlsCache::new(1024 * 1024);
        let playlist = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Playlist);
        let init = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Init);
        let segment = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Segment(0));
        let edited = HlsCacheKey::remux("clip::mtime=2".to_string(), RemuxPart::Segment(0));

        cache.start_generation(playlist.clone());
        cache.complete_generation(playlist, b"#EXTM3U\n".to_vec());

        for key in [init, segment, edited] {
            assert!(
                matches!(
                    cache.start_generation(key.clone()),
                    HlsCacheStartResult::Started(_)
                ),
                "{key:?} must not collide with the playlist entry"
            );
        }
    }

    /// Media segments are evicted before playlists and init segments, which are
    /// tiny and needed for every playback attempt.
    #[test]
    fn test_eviction_prefers_remux_segments_over_playlist_and_init() {
        use crate::video_remux::RemuxPart;

        let cache = HlsCache::new(500);

        let playlist = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Playlist);
        let init = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Init);
        cache.start_generation(playlist.clone());
        cache.complete_generation(playlist.clone(), vec![0u8; 40]);
        cache.start_generation(init.clone());
        cache.complete_generation(init.clone(), vec![0u8; 40]);

        for index in 0..10 {
            let key = HlsCacheKey::remux("clip::mtime=1".to_string(), RemuxPart::Segment(index));
            cache.start_generation(key.clone());
            cache.complete_generation(key, vec![0u8; 100]);
        }

        for key in [playlist, init] {
            assert!(
                matches!(cache.get_state(&key), Some(HlsCacheState::Complete(_))),
                "{key:?} must survive eviction of media segments"
            );
        }
    }

    #[test]
    fn test_size_accounting_on_overwrite() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        cache.start_generation(key.clone());
        cache.complete_generation(key.clone(), vec![0u8; 500]);
        assert_eq!(cache.current_size(), 500);

        // Overwriting the same key must not ratchet the size up; it should
        // reflect only the latest entry, not 500 + 200.
        cache.complete_generation(key.clone(), vec![0u8; 200]);
        assert_eq!(cache.current_size(), 200);

        // Overwriting with a larger payload updates accounting upward correctly.
        cache.complete_generation(key.clone(), vec![0u8; 900]);
        assert_eq!(cache.current_size(), 900);
    }

    #[tokio::test]
    async fn test_wait_for_completion_already_done_no_lost_wakeup() {
        // Reproduces the lost-wakeup race: the generation completes (and the
        // notify fires with no registered waiters) *before* the waiter calls
        // wait_for_completion. The state re-check must return the data rather
        // than blocking forever.
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };

        // Complete BEFORE anyone waits — notify_waiters here reaches no one.
        cache.complete_generation(key.clone(), vec![7u8; 128]);

        // Even so, waiting must resolve immediately from the re-checked state.
        let data = cache
            .wait_for_completion(&key, notify, Duration::from_secs(5))
            .await;
        assert_eq!(data.map(|d| d.len()), Some(128));
    }

    #[tokio::test]
    async fn test_wait_for_completion_concurrent_waiters() {
        use std::sync::Arc as StdArc;

        let cache = StdArc::new(HlsCache::new(1024 * 1024));

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        // The generating task holds the primary notify.
        let gen_notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };

        // Spawn several concurrent waiters that arrive while in progress.
        let waiters: Vec<_> = (0..8)
            .map(|_| {
                let cache = cache.clone();
                let key = key.clone();
                let notify = match cache.start_generation(key.clone()) {
                    HlsCacheStartResult::AlreadyInProgress(n) => n,
                    _ => panic!("expected AlreadyInProgress"),
                };
                tokio::spawn(async move {
                    cache
                        .wait_for_completion(&key, notify, Duration::from_secs(5))
                        .await
                })
            })
            .collect();

        // Let the waiters register, then complete the generation.
        tokio::time::sleep(Duration::from_millis(20)).await;
        cache.complete_generation(key.clone(), vec![3u8; 256]);
        gen_notify.notify_waiters();

        for w in waiters {
            let data = w.await.expect("waiter task panicked");
            assert_eq!(data.map(|d| d.len()), Some(256));
        }
    }

    // -----------------------------------------------------------------
    // Cancellation safety
    // -----------------------------------------------------------------
    //
    // The invariant: no caller behaviour may leave a key in the in-progress
    // state. A leaked marker is the worst failure mode available — the key looks
    // busy forever, so every later request waits out `HLS_WAIT_TIMEOUT` while no
    // work is running, and the client sees a stall with no error to report.

    /// Dropping the handle returned by `spawn_generation` is exactly what happens
    /// when a client disconnects mid-request (axum drops the request future). The
    /// work must still finish and still populate the cache.
    #[tokio::test]
    async fn test_dropped_handle_still_completes_generation() {
        let cache = Arc::new(HlsCache::new(1024 * 1024));
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 5);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };

        // Abandon the handle immediately, before the work can finish.
        drop(HlsCache::spawn_generation(
            &cache,
            key.clone(),
            notify,
            || {
                std::thread::sleep(Duration::from_millis(50));
                Ok(vec![1u8; 64])
            },
        ));

        // A later request must be able to obtain the content promptly rather than
        // block on a marker nobody will ever settle.
        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::AlreadyInProgress(n) => n,
            HlsCacheStartResult::AlreadyComplete(data) => {
                assert_eq!(data.len(), 64);
                return;
            }
            other => panic!(
                "expected AlreadyInProgress or AlreadyComplete, got {}",
                describe(&other)
            ),
        };
        let data = cache
            .wait_for_completion(&key, notify, Duration::from_secs(5))
            .await;
        assert_eq!(
            data.map(|d| d.len()),
            Some(64),
            "an abandoned request's work must still land in the cache"
        );
    }

    /// The same, but for a generation that fails: the key must end up `Failed`,
    /// never stuck in-progress.
    #[tokio::test]
    async fn test_dropped_handle_still_records_failure() {
        let cache = Arc::new(HlsCache::new(1024 * 1024));
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 1);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };
        drop(HlsCache::spawn_generation(
            &cache,
            key.clone(),
            notify,
            || {
                std::thread::sleep(Duration::from_millis(20));
                Err(TranscodeError::TranscodeFailed("nope".to_string()))
            },
        ));

        // Give the detached task time to settle the entry.
        for _ in 0..100 {
            if matches!(cache.get_state(&key), Some(HlsCacheState::Failed(_))) {
                return;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        panic!(
            "failure was never recorded; state is {}",
            cache
                .get_state(&key)
                .map_or("absent", |s| describe_state(&s))
        );
    }

    /// A generation whose closure panics must still settle the key, so waiters get
    /// an answer instead of timing out.
    #[tokio::test]
    async fn test_panicking_generation_settles_the_key() {
        let cache = Arc::new(HlsCache::new(1024 * 1024));
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 2);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };
        let handle = HlsCache::spawn_generation(&cache, key.clone(), notify, || {
            panic!("generation exploded");
        });

        let result = handle
            .await
            .expect("outer task must not propagate the panic");
        assert!(
            result.is_err(),
            "a panicking generation must report an error"
        );
        assert!(
            matches!(cache.get_state(&key), Some(HlsCacheState::Failed(_))),
            "the key must be settled, not left in-progress"
        );
    }

    /// The guard's own contract, exercised directly: an unsettled claim is
    /// withdrawn on drop so the next `start_generation` gets a clean `Started`.
    #[tokio::test]
    async fn test_unsettled_guard_releases_the_claim() {
        let cache = Arc::new(HlsCache::new(1024 * 1024));
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 3);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };
        assert!(matches!(
            cache.get_state(&key),
            Some(HlsCacheState::InProgress(_))
        ));

        drop(InFlightGuard {
            cache: Arc::clone(&cache),
            key: key.clone(),
            notify,
            settled: false,
        });

        assert!(
            cache.get_state(&key).is_none(),
            "an unsettled claim must be withdrawn"
        );
        assert!(
            matches!(
                cache.start_generation(key.clone()),
                HlsCacheStartResult::Started(_)
            ),
            "the next request must be able to claim the key"
        );
    }

    /// A settled guard must not touch the completed entry on drop.
    #[tokio::test]
    async fn test_settled_guard_leaves_content_alone() {
        let cache = Arc::new(HlsCache::new(1024 * 1024));
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 4);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };
        cache.complete_generation(key.clone(), vec![7u8; 16]);

        drop(InFlightGuard {
            cache: Arc::clone(&cache),
            key: key.clone(),
            notify,
            settled: true,
        });

        assert!(matches!(
            cache.get_state(&key),
            Some(HlsCacheState::Complete(_))
        ));
    }

    /// Withdrawing a claim must never clobber content another writer already
    /// stored for the same key.
    #[test]
    fn test_abandon_only_removes_an_in_progress_marker() {
        let cache = HlsCache::new(1024 * 1024);
        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        cache.start_generation(key.clone());
        cache.complete_generation(key.clone(), vec![1u8; 32]);

        assert!(
            !cache.abandon_generation(&key),
            "a completed entry must not be withdrawn"
        );
        assert!(matches!(
            cache.get_state(&key),
            Some(HlsCacheState::Complete(_))
        ));

        // And an in-progress marker *is* withdrawn.
        let other = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 9);
        cache.start_generation(other.clone());
        assert!(cache.abandon_generation(&other));
        assert!(cache.get_state(&other).is_none());
    }

    fn describe(result: &HlsCacheStartResult) -> &'static str {
        match result {
            HlsCacheStartResult::Started(_) => "Started",
            HlsCacheStartResult::AlreadyInProgress(_) => "AlreadyInProgress",
            HlsCacheStartResult::AlreadyComplete(_) => "AlreadyComplete",
            HlsCacheStartResult::PreviouslyFailed(_) => "PreviouslyFailed",
            HlsCacheStartResult::CacheDisabled => "CacheDisabled",
        }
    }

    fn describe_state(state: &HlsCacheState) -> &'static str {
        match state {
            HlsCacheState::InProgress(_) => "InProgress",
            HlsCacheState::Complete(_) => "Complete",
            HlsCacheState::Failed(_) => "Failed",
        }
    }

    #[tokio::test]
    async fn test_wait_for_completion_times_out() {
        let cache = HlsCache::new(1024 * 1024);

        let key = make_segment_key("/videos/test.mp4", TranscodeTarget::Resolution720p, 0);

        let notify = match cache.start_generation(key.clone()) {
            HlsCacheStartResult::Started(n) => n,
            _ => panic!("expected Started"),
        };

        // Never complete — the bounded wait must degrade to None, not hang.
        let data = cache
            .wait_for_completion(&key, notify, Duration::from_millis(50))
            .await;
        assert!(data.is_none());
    }
}