feat(updater): decouple update notice from auto download

Closes #9329
This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-16 23:50:18 +08:00
1 parent b891bf80fd
commit 2c97e44d82
20 files changed
+187 -18

No files matched your search

@@ -21,7 +21,7 @@ vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => true }));
vi.mock("@tauri-apps/api/event", () => ({ listen: mocks.listen }));
vi.mock("@tauri-apps/plugin-process", () => ({ relaunch: mocks.relaunch }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
const settings = reactive({ updateDownloadSource: "official", ignoredUpdateVersion: "", updateNotificationsEnabled: true });
const settings = reactive({ updateDownloadSource: "official", ignoredUpdateVersion: "", updateNotificationsEnabled: true, autoDownloadUpdates: true });
vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: settings, updateEditorSettingsAndPersist: mocks.persist }) }));
const info = { current_version: "1.0.0", latest_version: "1.1.0", update_available: true, portable_mode: false, manual_update_only: false, release_name: "v1.1.0", release_url: "https://example.com", release_notes: "Changes" };
const cache = { cache_id: "cached", version: "1.1.0", portable_mode: false, release_url: "https://example.com", release_notes: "Changes", downloaded_at: 1 };
@@ -58,6 +58,7 @@ beforeEach(() => {
settings.updateDownloadSource = "official";
settings.ignoredUpdateVersion = "";
settings.updateNotificationsEnabled = true;
settings.autoDownloadUpdates = true;
mocks.checkForUpdates.mockResolvedValue(info);
mocks.downloadUpdate.mockResolvedValue(cache);
mocks.getDownloadedUpdate.mockResolvedValue(null);
@@ -70,6 +71,96 @@ afterEach(() => {
vi.useRealTimers();
});
describe("silent update lifecycle", () => {
it("checks and shows a badge without downloading when automatic downloads are disabled", async () => {
settings.autoDownloadUpdates = false;
const updater = mount();
await updater.initialize();
await flush();
expect(mocks.checkForUpdates).toHaveBeenCalled();
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(updater.hasUpdateAvailable.value).toBe(true);
expect(updater.showUpdateDialog.value).toBe(false);
await updater.checkUpdates();
expect(updater.showUpdateDialog.value).toBe(true);
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
await updater.downloadUpdateInBackground();
expect(mocks.downloadUpdate).toHaveBeenCalledOnce();
expect(updater.updateDownloaded.value).toBe(true);
expect(mocks.installDownloadedUpdate).not.toHaveBeenCalled();
});
it("surfaces a newer release instead of a stale package with automatic downloads disabled", async () => {
settings.autoDownloadUpdates = false;
mocks.getDownloadedUpdate.mockResolvedValue(cache);
mocks.checkForUpdates.mockResolvedValue({ ...info, latest_version: "1.2.0", release_name: "v1.2.0" });
const updater = mount();
await updater.initialize();
await flush();
expect(updater.updateInfo.value?.latest_version).toBe("1.2.0");
expect(mocks.discardDownloadedUpdate).toHaveBeenCalledWith(cache.cache_id);
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(updater.updateDownloaded.value).toBe(false);
expect(updater.hasUpdateAvailable.value).toBe(true);
await updater.installDownloadedUpdate();
expect(mocks.installDownloadedUpdate).not.toHaveBeenCalled();
mocks.downloadUpdate.mockResolvedValue({ ...cache, version: "1.2.0" });
await updater.downloadUpdateInBackground();
expect(mocks.downloadUpdate).toHaveBeenCalledWith("official", "1.2.0", expect.any(String), "Changes");
expect(updater.updateDownloaded.value).toBe(true);
});
it("preserves a prepared package with automatic downloads disabled", async () => {
settings.autoDownloadUpdates = false;
mocks.getDownloadedUpdate.mockResolvedValue(cache);
const updater = mount();
await updater.initialize();
await flush();
expect(mocks.discardDownloadedUpdate).not.toHaveBeenCalled();
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(updater.updateInfo.value?.latest_version).toBe(cache.version);
expect(updater.hasUpdateAvailable.value).toBe(true);
await updater.installDownloadedUpdate();
expect(mocks.installDownloadedUpdate).toHaveBeenCalledWith(cache.cache_id, cache.version);
});
it("starts a silent download when the preference is enabled", async () => {
settings.autoDownloadUpdates = false;
const updater = mount();
await updater.initialize();
await flush();
settings.autoDownloadUpdates = true;
await vi.waitFor(() => expect(updater.updateDownloaded.value).toBe(true));
expect(mocks.downloadUpdate).toHaveBeenCalledOnce();
expect(updater.showUpdateDialog.value).toBe(false);
});
it("cancels an automatic download when the preference is disabled", async () => {
const pending = deferred<typeof cache>();
mocks.downloadUpdate.mockReturnValue(pending.promise);
mocks.cancelUpdateDownload.mockImplementation(async () => pending.reject(new Error("cancelled")));
const updater = mount();
const checking = updater.checkUpdates({ silent: true });
await vi.waitFor(() => expect(mocks.downloadUpdate).toHaveBeenCalledOnce());
settings.autoDownloadUpdates = false;
await vi.waitFor(() => expect(updater.phase.value).toBe("idle"));
await checking;
expect(mocks.cancelUpdateDownload).toHaveBeenCalledOnce();
expect(updater.hasUpdateAvailable.value).toBe(true);
});
it("does not cancel a manually started background download when the preference changes", async () => {
settings.autoDownloadUpdates = false;
const updater = mount();
await updater.checkUpdates();
const pending = deferred<typeof cache>();
mocks.downloadUpdate.mockReturnValue(pending.promise);
const downloading = updater.downloadUpdateInBackground();
await flush();
settings.autoDownloadUpdates = true;
await nextTick();
settings.autoDownloadUpdates = false;
await flush();
expect(mocks.cancelUpdateDownload).not.toHaveBeenCalled();
updater.showUpdateDialog.value = false;
pending.resolve(cache);
await downloading;
expect(updater.updateDownloaded.value).toBe(true);
});
it("automatically downloads without surfacing or installing, even while idle", async () => {
const updater = mount();
await updater.checkUpdates({ silent: true });
+33 -4
View File
@@ -134,12 +134,17 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
const updateReady = computed(() => phase.value === "restart");
const activeTaskCount = computed(() => Math.max(0, Math.trunc(options.getActiveTaskCount?.() ?? 0)));
const notificationsEnabled = computed(() => settingsStore.editorSettings.updateNotificationsEnabled !== false);
const autoDownloadEnabled = computed(() => settingsStore.editorSettings.autoDownloadUpdates === true);
const hasUpdateAvailable = computed(
() => notificationsEnabled.value && (updateDownloaded.value || updateReady.value || (updateInfo.value?.update_available === true && (!isTauriRuntime() || updateInfo.value.manual_update_only))) && !isUpdateIgnored(updateInfo.value, settingsStore.editorSettings.ignoredUpdateVersion),
() =>
notificationsEnabled.value &&
(updateDownloaded.value || updateReady.value || (updateInfo.value?.update_available === true && (!autoDownloadEnabled.value || !isTauriRuntime() || updateInfo.value.manual_update_only))) &&
!isUpdateIgnored(updateInfo.value, settingsStore.editorSettings.ignoredUpdateVersion),
);
const latestReleaseUrl = "https://github.com/t8y2/dbx/releases/latest";
let generation = 0;
let activeDownload: Promise<void> | undefined;
let automaticDownload = false;
let cancellation: Promise<void> | undefined;
let cancelOperation: Promise<void> | undefined;
let retryTimer: ReturnType<typeof setTimeout> | undefined;
@@ -223,9 +228,15 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
if (!info.update_available) updateCheckMessage.value = t("updates.upToDate", { version: info.current_version });
if (canDownloadAndInstallUpdate(info, isTauriRuntime()) && !isUpdateIgnored(info, settingsStore.editorSettings.ignoredUpdateVersion)) {
const cached = downloaded.value;
if (cached && !isNewerRemoteVersion(info.latest_version, cached.version)) return;
if (cached && !isNewerRemoteVersion(info.latest_version, cached.version)) {
// Keep a prepared package installable without replacing it automatically.
if (!autoDownloadEnabled.value || !notificationsEnabled.value) setDownloaded(cached);
return;
}
// A cached package older than the remote release must not mask the newer version.
if (cached && !(await discardSupersededUpdate(cached))) return;
await downloadUpdateInBackground();
if (!autoDownloadEnabled.value || !notificationsEnabled.value) return;
await downloadUpdateInBackground(true);
}
} catch (error) {
if (token !== generation || disposed) return;
@@ -249,8 +260,10 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
return false;
}
}
async function downloadUpdateInBackground() {
async function downloadUpdateInBackground(automatic = false) {
if (disposed || isIgnoringUpdate.value || phase.value !== "idle" || downloaded.value || !canDownloadAndInstallUpdate(updateInfo.value, isTauriRuntime())) return;
if (automatic && (!autoDownloadEnabled.value || !notificationsEnabled.value)) return;
automaticDownload = automatic;
const version = updateInfo.value!.latest_version;
const token = ++generation;
const attemptId = crypto.randomUUID();
@@ -292,6 +305,7 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
activeDownload = run();
await activeDownload;
activeDownload = undefined;
automaticDownload = false;
}
function cancelDownload(): Promise<void> {
if (cancelOperation) return cancelOperation;
@@ -444,11 +458,26 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
})
.catch(fail);
});
const stopAutoDownloadWatch = watch(autoDownloadEnabled, (enabled) => {
if (disposed) return;
if (!enabled) {
clearRetry();
if (automaticDownload) void cancelDownload().catch(fail);
return;
}
if (!initialized || !notificationsEnabled.value) return;
void (cancelOperation ?? Promise.resolve())
.then(() => {
if (autoDownloadEnabled.value && notificationsEnabled.value && !disposed) void checkUpdates({ silent: true });
})
.catch(fail);
});
function dispose() {
disposed = true;
clearRetry();
clearInterval(hourlyTimer);
stopSettingsWatch();
stopAutoDownloadWatch();
updatePreparationRelease?.();
updatePreparationRelease = undefined;
void cancelDownload().catch(() => {});