corundum-framework-package 0.6.0

A desktop app as a Velopack package: its start and install hooks, told apart from an update
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
//! Updates: found and downloaded by a thread of the updater's own, applied only when the
//! app asks, after its `before_apply`.

use std::ffi::OsString;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::time::{Duration, Instant};

use corundum_framework_platform::package;
use corundum_framework_update::{PackageState, PackageStatus};
use semver::Version;
use velopack::UpdateInfo;

use crate::backend::{Applied, Backend, Velopack};
use crate::error::Error;
use crate::services::{self, ServiceDriver};
use crate::start::{self, PackageSpec};

/// Where a package's updates are published.
#[derive(Clone, Debug)]
pub enum Source {
    /// A GitHub repository's releases, by its URL: `https://github.com/owner/name`.
    /// Velopack reads only the 10 newest releases.
    Github { repo: String, prerelease: bool },
    /// A feed of the app's own, by its base URL.
    Http(String),
}

/// What happens once an update is applied, and who ends the process for it.
#[derive(Clone, Debug)]
pub enum After {
    /// Start the app again without arguments: the GUI. The caller ends the process.
    RestartApp,
    /// Start the app again with these arguments. The caller ends the process.
    RestartWith(Vec<OsString>),
    /// Exit; something else starts it again (a service manager), or nothing. The process
    /// exits in [`Updater::apply`]: a daemon or a command line has nothing to close.
    Exit,
}

/// A package's updates: one thread checks and downloads, the state is shared, and nothing
/// is applied until [`Updater::apply`]. Clones share the thread and the state; the thread
/// ends with the last clone.
#[derive(Clone)]
pub struct Updater {
    shared: Arc<Shared>,
}

type Listener = Arc<dyn Fn() + Send + Sync>;

struct Shared {
    spec: &'static PackageSpec,
    backend: Arc<dyn Backend>,
    /// The app's services, stopped before an apply and started again when it fails.
    services: Arc<dyn ServiceDriver>,
    current: Version,
    state: Mutex<State>,
    listeners: Mutex<Vec<Listener>>,
    /// The worker's requests, `None` until the first check. Dropping it with the last
    /// clone ends the worker.
    worker: Mutex<Option<Sender<Request>>>,
    /// Whether an apply runs: a status-bar badge and the update panel may both ask at
    /// once. It stays set once an update is handed to Velopack, as the process is ending.
    applying: AtomicBool,
}

/// The backend of [`Updater::for_tests`]: nothing to find, nothing to apply.
#[cfg(feature = "test-support")]
struct Inert;

#[cfg(feature = "test-support")]
impl Backend for Inert {
    fn current(&self) -> Version {
        Version::new(1, 0, 0)
    }

    fn check(&self) -> Result<Option<UpdateInfo>, Error> {
        Ok(None)
    }

    fn download(&self, _info: &UpdateInfo, _progress: &dyn Fn(u8)) -> Result<(), Error> {
        Ok(())
    }

    fn pending(&self) -> Option<Version> {
        None
    }

    fn apply(&self, _applied: Applied, _after: &After) -> Result<(), Error> {
        Ok(())
    }
}

struct State {
    state: PackageState,
    /// What `Ready` stands for.
    downloaded: Option<UpdateInfo>,
}

enum Request {
    Check,
    Every(Duration),
}

impl Updater {
    /// `Error::NotPackaged` outside a package.
    pub fn new(spec: &'static PackageSpec, source: Source) -> Result<Updater, Error> {
        // Told from the files first, as `start` does, so a development build never asks
        // Velopack to find a manifest that is not there.
        if package::current().is_none() {
            return Err(Error::NotPackaged);
        }
        Ok(Updater::with_backend(
            spec,
            Arc::new(Velopack::new(&source)?),
            services::driver(spec),
        ))
    }

    pub(crate) fn with_backend(
        spec: &'static PackageSpec,
        backend: Arc<dyn Backend>,
        services: Arc<dyn ServiceDriver>,
    ) -> Updater {
        Updater {
            shared: Arc::new(Shared {
                spec,
                services,
                current: backend.current(),
                backend,
                state: Mutex::new(State {
                    state: PackageState::Idle,
                    downloaded: None,
                }),
                listeners: Mutex::new(Vec::new()),
                worker: Mutex::new(None),
                applying: AtomicBool::new(false),
            }),
        }
    }

    /// An updater standing in `state`, over a backend that checks nothing and never applies:
    /// [`apply`](Self::apply) returns `Ok` without doing anything when `state` is `Ready`,
    /// and an error (nothing downloaded) otherwise. For a GUI's tests.
    #[cfg(feature = "test-support")]
    #[doc(hidden)]
    pub fn for_tests(state: PackageState) -> Updater {
        static SPEC: PackageSpec = PackageSpec {
            app_name: "test",
            on_installed: None,
            on_uninstalling: None,
            before_apply: None,
            cli: None,
            services: crate::Services::NONE,
        };
        let updater = Updater::with_backend(&SPEC, Arc::new(Inert), Arc::new(()));
        let downloaded = match &state {
            PackageState::Ready { version } => Some(UpdateInfo {
                TargetFullRelease: velopack::VelopackAsset {
                    Version: version.to_string(),
                    ..Default::default()
                },
                ..Default::default()
            }),
            _ => None,
        };
        *lock(&updater.shared.state) = State { state, downloaded };
        updater
    }

    /// Called after every change of state: a GUI asks for a repaint here. Runs on the
    /// updater's thread, or on the thread calling `apply` when `before_apply` refuses.
    pub fn on_change(&self, f: impl Fn() + Send + Sync + 'static) {
        lock(&self.shared.listeners).push(Arc::new(f));
    }

    /// The running version and what the updater is doing. Never waits for a check.
    pub fn status(&self) -> PackageStatus {
        PackageStatus {
            current: Some(self.shared.current.clone()),
            state: lock(&self.shared.state).state.clone(),
        }
    }

    /// Look now (the Check button, `<app> update`), and download what is found.
    pub fn check_now(&self) {
        self.send(Request::Check);
    }

    /// Look at once and then every `every`, downloading what is found. A second call
    /// replaces the interval.
    pub fn run_in_background(&self, every: Duration) {
        self.send(Request::Every(every));
    }

    /// Runs `before_apply`, then hands the downloaded update to Velopack's updater, which
    /// waits for this process to end before it replaces the files.
    ///
    /// Who ends the process: for [`After::Exit`] this call exits it and returns only on an
    /// error. For [`After::RestartApp`] and [`After::RestartWith`] it returns `Ok`, and the
    /// caller must end the process soon (Velopack waits 60 seconds), a GUI by closing its
    /// window so that what it saves on exit is saved.
    ///
    /// Once this returns `Ok`, the update is Velopack's: if the app cancels the close (a
    /// "save your changes?" it answers with Cancel) or otherwise keeps running, a later
    /// call in this process fails with [`Error::Applying`]. Whether the update is applied
    /// then is up to Velopack's updater, which waits for this process to end.
    ///
    /// An error: nothing downloaded, an apply already running on another thread
    /// ([`Error::Applying`]), `before_apply` refused (nothing is applied, and a later call
    /// tries again), or Velopack failed.
    pub fn apply(&self, after: After) -> Result<(), Error> {
        let applying = self.shared.begin_apply()?;
        // Whatever the state shows: after a refused `before_apply` it is `Failed`, and the
        // update is still there to apply.
        let info = lock(&self.shared.state)
            .downloaded
            .clone()
            .ok_or_else(|| Error::Velopack("nothing downloaded".into()))?;
        self.shared.prepare()?;
        self.shared.apply(Applied::Downloaded(info), &after)?;
        applying.keep();
        Ok(())
    }

    /// For a GUI's start only, before its window opens: apply an update downloaded earlier
    /// and never applied, and start the app again; this call then exits the process itself,
    /// as there is no window to close. Looks at the downloaded packages only, never the
    /// network, so the start is not delayed. `Ok` when there is none, and on a start by
    /// Velopack's updater: an update it failed to apply would otherwise be tried at every
    /// start.
    ///
    /// Only the app's single GUI instance may call this: an app that can run two must
    /// prevent the second first (corundum-framework-platform's `instance`, or the gui
    /// crate's `WindowOptions::single_instance`), or the second applies under the first.
    pub fn apply_pending_at_start(&self) -> Result<(), Error> {
        if self.apply_pending(start::restarted())? {
            std::process::exit(0);
        }
        Ok(())
    }

    /// [`apply_pending_at_start`](Self::apply_pending_at_start) but the exit: whether an
    /// update was handed to Velopack.
    fn apply_pending(&self, restarted: bool) -> Result<bool, Error> {
        if restarted {
            return Ok(false);
        }
        match self.shared.backend.pending() {
            Some(pending) if pending > self.shared.current => {
                let applying = self.shared.begin_apply()?;
                self.shared.prepare()?;
                self.shared.apply(Applied::Pending, &After::RestartApp)?;
                applying.keep();
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    /// Hands `request` to the worker, started on the first request.
    fn send(&self, request: Request) {
        let mut worker = lock(&self.shared.worker);
        let sender = worker.get_or_insert_with(|| {
            let (tx, rx) = mpsc::channel();
            let shared = Arc::downgrade(&self.shared);
            let backend = Arc::clone(&self.shared.backend);
            std::thread::Builder::new()
                .name("package-updater".into())
                .spawn(move || work(shared, backend, rx))
                .expect("could not start the updater's thread");
            tx
        });
        // The worker ends only once the sender is dropped, so it is listening.
        let _ = sender.send(request);
    }
}

/// An apply under way: dropped, it lets the next one run, unless [`keep`](Self::keep)
/// says the update was handed to Velopack.
struct ApplyGuard<'a>(Option<&'a AtomicBool>);

impl ApplyGuard<'_> {
    fn keep(mut self) {
        self.0 = None;
    }
}

impl Drop for ApplyGuard<'_> {
    fn drop(&mut self) {
        if let Some(applying) = self.0 {
            applying.store(false, Ordering::SeqCst);
        }
    }
}

impl Shared {
    /// Marks an apply as running, or [`Error::Applying`] when one already is.
    fn begin_apply(&self) -> Result<ApplyGuard<'_>, Error> {
        self.applying
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .map_err(|_| Error::Applying)?;
        Ok(ApplyGuard(Some(&self.applying)))
    }

    /// Runs the app's `before_apply`, then stops its running services; an error from
    /// either is the state and stops the update.
    fn prepare(&self) -> Result<(), Error> {
        let before_apply = self.spec.before_apply.unwrap_or(|| Ok(()));
        before_apply()
            .and_then(|()| self.services.stop_running())
            .map_err(|e| {
                self.set(PackageState::Failed(e.clone()));
                Error::Prepare(e)
            })
    }

    /// Hands the update to the backend; when that fails, the services [`prepare`]
    /// stopped start again, as no restart will.
    ///
    /// [`prepare`]: Self::prepare
    fn apply(&self, applied: Applied, after: &After) -> Result<(), Error> {
        self.backend.apply(applied, after).inspect_err(|_| {
            self.services.restart_stopped();
        })
    }

    /// Sets the state and tells the listeners, if it changed.
    fn set(&self, new: PackageState) {
        {
            let mut state = lock(&self.state);
            if state.state == new {
                return;
            }
            state.state = new;
        }
        self.notify();
    }

    fn notify(&self) {
        // Called without the lock, so a listener may register another.
        let listeners = lock(&self.listeners).clone();
        for listener in listeners {
            listener();
        }
    }

    /// One check, and the download of what it finds. Once an update is downloaded, later
    /// checks run quietly: the state stays `Ready` (what the badge and the button show)
    /// unless one finds a newer update and has downloaded it, and their errors are only
    /// logged. A refused `before_apply` left `Failed`: a check (a GUI's Retry) is `Ready`
    /// again for the update still downloaded.
    fn check(&self, backend: &dyn Backend) {
        let ready = {
            let state = lock(&self.state);
            match (&state.state, &state.downloaded) {
                (PackageState::Failed(_), Some(info)) => {
                    Version::parse(&info.TargetFullRelease.Version).ok()
                }
                _ => None,
            }
        };
        if let Some(version) = ready {
            self.set(PackageState::Ready { version });
        }
        let quiet = lock(&self.state).downloaded.is_some();
        let fail = |msg: String| {
            if quiet {
                tracing::warn!("update check failed, keeping the downloaded update: {msg}");
            } else {
                self.set(PackageState::Failed(msg));
            }
        };
        if !quiet {
            self.set(PackageState::Checking);
        }
        let info = match backend.check() {
            Ok(Some(info)) => info,
            Ok(None) => {
                if !quiet {
                    self.set(PackageState::UpToDate);
                }
                return;
            }
            Err(e) => return fail(e.to_string()),
        };
        let version = match Version::parse(&info.TargetFullRelease.Version) {
            Ok(version) => version,
            Err(e) => {
                return fail(format!(
                    "the update's version {:?} is not a version: {e}",
                    info.TargetFullRelease.Version
                ));
            }
        };
        if !quiet {
            self.set(PackageState::Downloading { percent: 0 });
        }
        let progress = |percent: u8| {
            if !quiet {
                self.set(PackageState::Downloading { percent });
            }
        };
        match backend.download(&info, &progress) {
            Ok(()) => {
                {
                    let mut state = lock(&self.state);
                    state.state = PackageState::Ready { version };
                    state.downloaded = Some(info);
                }
                self.notify();
            }
            Err(e) => fail(e.to_string()),
        }
    }
}

/// The worker: a check per request, and one every interval once asked to. It holds the
/// state only while it works, so it ends when the last [`Updater`] is dropped.
fn work(shared: Weak<Shared>, backend: Arc<dyn Backend>, requests: Receiver<Request>) {
    let mut every: Option<Duration> = None;
    let mut next: Option<Instant> = None;
    loop {
        let request = match next {
            Some(at) => match requests.recv_timeout(at.saturating_duration_since(Instant::now())) {
                Ok(request) => request,
                Err(RecvTimeoutError::Timeout) => Request::Check,
                Err(RecvTimeoutError::Disconnected) => return,
            },
            None => match requests.recv() {
                Ok(request) => request,
                Err(_) => return,
            },
        };
        if let Request::Every(interval) = request {
            every = Some(interval);
        }
        let Some(shared) = shared.upgrade() else {
            return;
        };
        shared.check(&*backend);
        drop(shared);
        next = every.map(|every| Instant::now() + every);
    }
}

/// Locks `mutex`, through a panic elsewhere: the state is replaced whole, never left
/// half-written.
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(|e| e.into_inner())
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::collections::VecDeque;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::{Duration, Instant};

    use corundum_framework_update::{PackageState, PackageStatus};
    use semver::Version;
    use velopack::{UpdateInfo, VelopackAsset};

    use super::*;
    use crate::backend::{Applied, Backend};
    use crate::error::Error;
    use crate::services::ServiceDriver;
    use crate::start::PackageSpec;

    thread_local! {
        /// What ran on the thread that applies, in order: `before_apply` and the fake's
        /// `apply` both run on the caller's thread, so each test sees only its own.
        static APPLY_LOG: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
    }

    fn log(entry: impl Into<String>) {
        APPLY_LOG.with(|l| l.borrow_mut().push(entry.into()));
    }

    fn apply_log() -> Vec<String> {
        APPLY_LOG.with(|l| l.borrow().clone())
    }

    fn v(s: &str) -> Version {
        Version::parse(s).unwrap()
    }

    fn info(version: &str) -> UpdateInfo {
        UpdateInfo {
            TargetFullRelease: VelopackAsset {
                Version: version.to_owned(),
                FileName: format!("app-{version}-full.nupkg"),
                ..Default::default()
            },
            ..Default::default()
        }
    }

    fn spec(before_apply: Option<fn() -> Result<(), String>>) -> &'static PackageSpec {
        Box::leak(Box::new(PackageSpec {
            app_name: "updater-test",
            on_installed: None,
            on_uninstalling: None,
            before_apply,
            cli: None,
            services: crate::Services::NONE,
        }))
    }

    fn stopping() -> Result<(), String> {
        log("before_apply");
        Ok(())
    }

    fn failing_to_stop() -> Result<(), String> {
        log("before_apply");
        Err("stop failed".to_owned())
    }

    /// A Velopack that answers checks from a list, downloads in three steps and records an
    /// apply instead of handing it over.
    struct Fake {
        current: Version,
        checks: Mutex<VecDeque<Result<Option<UpdateInfo>, Error>>>,
        checked: AtomicUsize,
        pending: Option<Version>,
        /// Whether `apply` fails, after recording itself.
        fails_apply: std::sync::atomic::AtomicBool,
    }

    impl Fake {
        fn new(
            current: &str,
            checks: Vec<Result<Option<UpdateInfo>, Error>>,
            pending: Option<&str>,
        ) -> Arc<Fake> {
            Arc::new(Fake {
                current: v(current),
                checks: Mutex::new(checks.into()),
                checked: AtomicUsize::new(0),
                pending: pending.map(v),
                fails_apply: std::sync::atomic::AtomicBool::new(false),
            })
        }
    }

    impl Backend for Fake {
        fn current(&self) -> Version {
            self.current.clone()
        }

        fn check(&self) -> Result<Option<UpdateInfo>, Error> {
            self.checked.fetch_add(1, Ordering::SeqCst);
            self.checks.lock().unwrap().pop_front().unwrap_or(Ok(None))
        }

        fn download(&self, _info: &UpdateInfo, progress: &dyn Fn(u8)) -> Result<(), Error> {
            for percent in [0, 50, 100] {
                progress(percent);
            }
            Ok(())
        }

        fn pending(&self) -> Option<Version> {
            self.pending.clone()
        }

        fn apply(&self, applied: Applied, after: &After) -> Result<(), Error> {
            let what = match applied {
                Applied::Downloaded(info) => info.TargetFullRelease.Version,
                Applied::Pending => "pending".to_owned(),
            };
            log(format!("apply {what} {after:?}"));
            if self.fails_apply.load(Ordering::SeqCst) {
                return Err(Error::Velopack("apply failed".into()));
            }
            Ok(())
        }
    }

    /// Services that record their stop and restart in the apply log, and fail the stop
    /// when asked to.
    struct RecordedServices {
        fails_stop: bool,
    }

    impl ServiceDriver for RecordedServices {
        fn stop_running(&self) -> Result<(), String> {
            log("stop services");
            if self.fails_stop {
                return Err("demo could not be stopped".to_owned());
            }
            Ok(())
        }

        fn restart_stopped(&self) {
            log("restart services");
        }
    }

    fn updater(spec: &'static PackageSpec, fake: Arc<Fake>) -> Updater {
        Updater::with_backend(spec, fake, Arc::new(()))
    }

    fn updater_with_services(
        spec: &'static PackageSpec,
        fake: Arc<Fake>,
        fails_stop: bool,
    ) -> Updater {
        Updater::with_backend(spec, fake, Arc::new(RecordedServices { fails_stop }))
    }

    /// Polls the state until `done` holds, for 5 seconds at most.
    fn wait_for(updater: &Updater, done: impl Fn(&PackageState) -> bool) -> PackageStatus {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let status = updater.status();
            if done(&status.state) {
                return status;
            }
            assert!(
                Instant::now() < deadline,
                "still {:?} after 5 seconds",
                status.state
            );
            std::thread::sleep(Duration::from_millis(5));
        }
    }

    fn settled(state: &PackageState) -> bool {
        matches!(
            state,
            PackageState::UpToDate | PackageState::Ready { .. } | PackageState::Failed(_)
        )
    }

    #[test]
    fn a_found_update_is_downloaded_and_ready() {
        let updater = updater(
            spec(None),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        assert_eq!(
            updater.status(),
            PackageStatus {
                current: Some(v("1.0.0")),
                state: PackageState::Idle
            }
        );
        updater.check_now();
        let status = wait_for(&updater, settled);
        assert_eq!(
            status.state,
            PackageState::Ready {
                version: v("1.1.0")
            }
        );
        assert_eq!(status.current, Some(v("1.0.0")));
    }

    #[test]
    fn nothing_found_is_up_to_date() {
        let updater = updater(spec(None), Fake::new("1.0.0", vec![Ok(None)], None));
        updater.check_now();
        assert_eq!(wait_for(&updater, settled).state, PackageState::UpToDate);
    }

    #[test]
    fn a_check_error_is_failed_and_a_later_check_recovers() {
        let updater = updater(
            spec(None),
            Fake::new(
                "1.0.0",
                vec![Err(Error::Network("offline".into())), Ok(None)],
                None,
            ),
        );
        updater.check_now();
        let failed = wait_for(&updater, settled).state;
        assert!(
            matches!(&failed, PackageState::Failed(msg) if msg.contains("offline")),
            "{failed:?}"
        );
        updater.check_now();
        assert_eq!(
            wait_for(&updater, |s| *s == PackageState::UpToDate).state,
            PackageState::UpToDate
        );
    }

    #[test]
    fn apply_runs_before_apply_first() {
        let updater = updater(
            spec(Some(stopping)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        updater.check_now();
        wait_for(&updater, settled);
        updater.apply(After::RestartApp).unwrap();
        assert_eq!(apply_log(), ["before_apply", "apply 1.1.0 RestartApp"]);
    }

    #[test]
    fn apply_stops_services_after_before_apply() {
        let updater = updater_with_services(
            spec(Some(stopping)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
            false,
        );
        updater.check_now();
        wait_for(&updater, settled);
        updater.apply(After::RestartApp).unwrap();
        assert_eq!(
            apply_log(),
            ["before_apply", "stop services", "apply 1.1.0 RestartApp"]
        );
    }

    #[test]
    fn a_failed_apply_starts_the_stopped_services_again() {
        let fake = Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], Some("1.1.0"));
        fake.fails_apply.store(true, Ordering::SeqCst);
        let updater = updater_with_services(spec(Some(stopping)), fake, false);
        updater.check_now();
        wait_for(&updater, settled);
        let err = updater.apply(After::RestartApp).unwrap_err();
        assert!(matches!(err, Error::Velopack(_)), "{err:?}");
        let err = updater.apply_pending(false).unwrap_err();
        assert!(matches!(err, Error::Velopack(_)), "{err:?}");
        let once = [
            "before_apply",
            "stop services",
            "apply 1.1.0 RestartApp",
            "restart services",
        ];
        let pending = [
            "before_apply",
            "stop services",
            "apply pending RestartApp",
            "restart services",
        ];
        assert_eq!(apply_log(), [once, pending].concat());
    }

    #[test]
    fn a_failed_stop_applies_nothing() {
        let updater = updater_with_services(
            spec(Some(stopping)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
            true,
        );
        updater.check_now();
        wait_for(&updater, settled);
        let err = updater.apply(After::Exit).unwrap_err();
        assert!(
            matches!(err, Error::Prepare(ref m) if m == "demo could not be stopped"),
            "{err:?}"
        );
        assert_eq!(apply_log(), ["before_apply", "stop services"]);
        assert_eq!(
            updater.status().state,
            PackageState::Failed("demo could not be stopped".to_owned())
        );
    }

    #[test]
    fn a_second_apply_while_one_runs_is_refused() {
        static UPDATER: std::sync::OnceLock<Updater> = std::sync::OnceLock::new();
        /// Asks for an apply again while the first is still in `before_apply`: the badge
        /// and the panel, clicked together.
        fn applying_again() -> Result<(), String> {
            let again = UPDATER.get().unwrap().apply(After::RestartApp);
            log(format!("again {again:?}"));
            Ok(())
        }
        let updater = updater(
            spec(Some(applying_again)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        UPDATER.set(updater.clone()).ok().unwrap();
        updater.check_now();
        wait_for(&updater, settled);
        updater.apply(After::RestartApp).unwrap();
        assert_eq!(
            apply_log(),
            ["again Err(Applying)", "apply 1.1.0 RestartApp"]
        );
        // Handed to Velopack: the process is ending, and nothing applies again.
        assert!(matches!(
            updater.apply(After::RestartApp),
            Err(Error::Applying)
        ));
    }

    #[test]
    fn a_failing_before_apply_applies_nothing() {
        let updater = updater(
            spec(Some(failing_to_stop)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        updater.check_now();
        wait_for(&updater, settled);
        let err = updater.apply(After::Exit).unwrap_err();
        assert!(
            matches!(err, Error::Prepare(ref m) if m == "stop failed"),
            "{err:?}"
        );
        assert_eq!(apply_log(), ["before_apply"]);
        assert_eq!(
            updater.status().state,
            PackageState::Failed("stop failed".to_owned())
        );
    }

    #[test]
    fn apply_without_a_download_is_an_error() {
        let updater = updater(spec(Some(stopping)), Fake::new("1.0.0", vec![], None));
        let err = updater.apply(After::RestartApp).unwrap_err();
        assert!(
            matches!(err, Error::Velopack(ref m) if m == "nothing downloaded"),
            "{err:?}"
        );
        assert!(apply_log().is_empty());
    }

    #[test]
    fn a_pending_update_newer_than_current_is_applied_at_start() {
        let updater = updater(
            spec(Some(stopping)),
            Fake::new("1.0.0", vec![], Some("1.1.0")),
        );
        assert!(updater.apply_pending(false).unwrap());
        assert_eq!(apply_log(), ["before_apply", "apply pending RestartApp"]);
    }

    #[test]
    fn a_pending_package_not_newer_is_left_alone() {
        for pending in [Some("1.0.0"), Some("0.9.0"), None] {
            let updater = updater(spec(Some(stopping)), Fake::new("1.0.0", vec![], pending));
            assert!(!updater.apply_pending(false).unwrap());
        }
        assert!(apply_log().is_empty());
    }

    #[test]
    fn a_start_by_velopack_s_updater_applies_no_pending_package() {
        let updater = updater(
            spec(Some(stopping)),
            Fake::new("1.0.0", vec![], Some("1.1.0")),
        );
        assert!(!updater.apply_pending(true).unwrap());
        assert!(apply_log().is_empty());
    }

    #[test]
    fn on_change_is_called_on_every_state_change() {
        let updater = updater(
            spec(None),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        let first = Arc::new(AtomicUsize::new(0));
        let second = Arc::new(AtomicUsize::new(0));
        for counter in [&first, &second] {
            let counter = Arc::clone(counter);
            updater.on_change(move || {
                counter.fetch_add(1, Ordering::SeqCst);
            });
        }
        updater.check_now();
        wait_for(&updater, settled);
        // Checking, Downloading at 0, 50 and 100, Ready. The callbacks run after the state
        // is set, so wait for the last one too.
        let deadline = Instant::now() + Duration::from_secs(5);
        while second.load(Ordering::SeqCst) < 5 && Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(5));
        }
        assert_eq!(first.load(Ordering::SeqCst), 5);
        assert_eq!(second.load(Ordering::SeqCst), 5);
    }

    #[test]
    fn a_failing_check_after_a_download_keeps_it_ready() {
        let fake = Fake::new(
            "1.0.0",
            vec![
                Ok(Some(info("1.1.0"))),
                Err(Error::Network("offline".into())),
            ],
            None,
        );
        let updater = updater(spec(Some(stopping)), Arc::clone(&fake));
        let changes = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&changes);
        updater.on_change(move || {
            counter.fetch_add(1, Ordering::SeqCst);
        });
        updater.check_now();
        wait_for(&updater, settled);
        // A check that fails, then one that finds nothing: once the fake sees the third
        // check, the worker is done with the failing one.
        updater.check_now();
        updater.check_now();
        let deadline = Instant::now() + Duration::from_secs(5);
        while fake.checked.load(Ordering::SeqCst) < 3 {
            assert!(Instant::now() < deadline, "the checks never ran");
            std::thread::sleep(Duration::from_millis(5));
        }
        assert_eq!(
            updater.status().state,
            PackageState::Ready {
                version: v("1.1.0")
            }
        );
        // Checking, Downloading at 0, 50 and 100, Ready; nothing for the later checks.
        assert_eq!(changes.load(Ordering::SeqCst), 5);
        updater.apply(After::RestartApp).unwrap();
        assert_eq!(apply_log(), ["before_apply", "apply 1.1.0 RestartApp"]);
    }

    #[test]
    fn apply_is_retried_after_a_failing_before_apply() {
        static REFUSE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
        fn refusing_once() -> Result<(), String> {
            log("before_apply");
            if REFUSE.swap(false, Ordering::SeqCst) {
                Err("stop failed".to_owned())
            } else {
                Ok(())
            }
        }
        let updater = updater(
            spec(Some(refusing_once)),
            Fake::new("1.0.0", vec![Ok(Some(info("1.1.0")))], None),
        );
        updater.check_now();
        wait_for(&updater, settled);
        let err = updater.apply(After::Exit).unwrap_err();
        assert!(matches!(err, Error::Prepare(_)), "{err:?}");
        assert_eq!(
            updater.status().state,
            PackageState::Failed("stop failed".to_owned())
        );
        updater.apply(After::Exit).unwrap();
        assert_eq!(
            apply_log(),
            ["before_apply", "before_apply", "apply 1.1.0 Exit"]
        );
    }

    #[test]
    fn a_check_after_a_refused_before_apply_is_ready_again() {
        let fake = Fake::new(
            "1.0.0",
            vec![
                Ok(Some(info("1.1.0"))),
                Err(Error::Network("offline".into())),
            ],
            None,
        );
        let updater = updater(spec(Some(failing_to_stop)), Arc::clone(&fake));
        updater.check_now();
        wait_for(&updater, settled);
        updater.apply(After::RestartApp).unwrap_err();
        assert_eq!(
            updater.status().state,
            PackageState::Failed("stop failed".to_owned())
        );
        // The GUI's Retry: a check, which fails here, and the update is still downloaded.
        updater.check_now();
        let status = wait_for(&updater, |s| matches!(s, PackageState::Ready { .. }));
        assert_eq!(
            status.state,
            PackageState::Ready {
                version: v("1.1.0")
            }
        );
    }

    #[test]
    fn run_in_background_checks_at_once_and_stops_with_the_last_updater() {
        let fake = Fake::new("1.0.0", vec![Ok(None)], None);
        let updater = updater(spec(None), Arc::clone(&fake));
        updater.run_in_background(Duration::from_secs(3600));
        assert_eq!(wait_for(&updater, settled).state, PackageState::UpToDate);
        let clone = updater.clone();
        drop(updater);
        drop(clone);
        // The worker holds the fake through the updater only: once it ends, the test holds
        // the last reference.
        let deadline = Instant::now() + Duration::from_secs(5);
        while Arc::strong_count(&fake) > 1 {
            assert!(Instant::now() < deadline, "the worker outlived the updater");
            std::thread::sleep(Duration::from_millis(5));
        }
    }
}