Files
cc-switch/tests
Jason 6f6671c1e8 feat(updater): download updates in the background and ask before installing
Add an opt-in "Auto-download updates" switch to Settings > About (off by
default, stored per device as autoUpdateEnabled). When it is on, CC Switch
checks one minute after launch and then every six hours, downloads and
verifies the new version in the background, and shows a small prompt in the
bottom-right corner of the main content area asking whether to install now.
Installing restarts CC Switch, which cuts requests going through the local
route, so it never happens without the user's click; the prompt says so when
the route is in use. The tray menu and tray panel also get a "Restart to
update to vX" entry.

The downloaded package is kept in a process-level cache in Rust
(app_update::PENDING), not in React state: lightweight mode destroys the
main window, and the download must survive that. It lives in memory only.

- install_update_and_restart checks again first and reuses the cached bytes
  when the version matches; offline it falls back to the cached package; if
  the server no longer offers an update the cache is dropped and the prompt
  and tray entry are withdrawn (app-update-ready with null).
- The cache is copied, never taken, so a failed install can be retried.
- The prompt, About page and tray share one install lock. begin_install uses
  an explicit if: then_some would build the guard eagerly and its Drop would
  release a lock held by another entry point.
- Portable builds and Linux installs other than AppImage cannot self-install;
  the switch is disabled there with the reason. Debug builds skip the
  background loop because dev shares settings.json with the release app.
- "Later" hides the prompt for that version only; the settings dot, the About
  button and the tray entry still offer the install.

Tested: new unit tests for the install lock, the tray item, the prompt and
UpdateContext (a downloaded package counts as an update when the online
check fails; rechecks run once per version). End to end on macOS with a
4.0.30 build in a sandboxed HOME: the switch downloaded and verified 4.0.31
in about 9 s, the prompt, tray entry and "Later" behaved as intended, and
installing from the cache restarted into 4.0.31.
2026-11-14 03:59:30 -08:00
..