feat(update): auto-replace stale downloads and relocate update toggle

Closes #9140
This commit is contained in:
t8y2 committed 2026-09-15 14:58:20 +08:00
1 parent d79dc05cf5
commit 39042f7917
16 files changed
+123 -61

No files matched your search

@@ -101,7 +101,8 @@ describe("silent update lifecycle", () => {
const updater = mount({ prepareForUpdate: prepare });
await updater.initialize();
await updater.installDownloadedUpdate();
expect(mocks.checkForUpdates).not.toHaveBeenCalled();
// Only the startup recheck runs (same latest version keeps the cache); installing never needs fresh metadata or a re-download.
expect(mocks.checkForUpdates).toHaveBeenCalledTimes(1);
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(mocks.installDownloadedUpdate).toHaveBeenCalledWith("cached", "1.1.0");
expect(prepare.mock.invocationCallOrder[0]).toBeLessThan(mocks.installDownloadedUpdate.mock.invocationCallOrder[0]);
@@ -291,4 +292,71 @@ describe("silent update lifecycle", () => {
await vi.waitFor(() => expect(mocks.downloadUpdate).toHaveBeenCalledTimes(2));
expect(updater.updateDownloaded.value).toBe(true);
});
it("replaces a downloaded update when a newer version is released", async () => {
const newer = { ...info, latest_version: "1.2.0", release_name: "v1.2.0" };
const replacement = { ...cache, version: "1.2.0" };
mocks.getDownloadedUpdate.mockResolvedValue(cache);
mocks.checkForUpdates.mockResolvedValue(newer);
mocks.downloadUpdate.mockResolvedValue(replacement);
const updater = mount();
await updater.initialize();
await vi.waitFor(() => expect(mocks.downloadUpdate).toHaveBeenCalled());
expect(mocks.discardDownloadedUpdate).toHaveBeenCalledWith("cached");
expect(mocks.downloadUpdate).toHaveBeenCalledWith("official", "1.2.0", expect.any(String), "Changes");
expect(updater.updateDownloaded.value).toBe(true);
expect(updater.updateInfo.value?.latest_version).toBe("1.2.0");
expect(updater.phase.value).toBe("ready");
});
it("keeps a downloaded update when the released version is not newer", async () => {
mocks.getDownloadedUpdate.mockResolvedValue(cache);
mocks.checkForUpdates.mockResolvedValue({ ...info, latest_version: "1.1.0" });
const updater = mount();
await updater.initialize();
await flush();
expect(mocks.checkForUpdates).toHaveBeenCalled();
expect(mocks.discardDownloadedUpdate).not.toHaveBeenCalled();
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(updater.phase.value).toBe("ready");
expect(updater.updateDownloaded.value).toBe(true);
});
it("keeps a downloaded update when the remote reports an older version", async () => {
const updater = mount();
await updater.checkUpdates({ silent: true });
expect(updater.updateDownloaded.value).toBe(true);
mocks.checkForUpdates.mockResolvedValue({ ...info, latest_version: "1.0.5" });
await updater.checkUpdates({ silent: true });
expect(mocks.discardDownloadedUpdate).not.toHaveBeenCalled();
expect(mocks.downloadUpdate).toHaveBeenCalledOnce();
expect(updater.phase.value).toBe("ready");
});
it("does not disturb an installation that starts while a recheck is in flight", async () => {
mocks.getDownloadedUpdate.mockResolvedValue(cache);
const gate = deferred<typeof info>();
mocks.checkForUpdates.mockReturnValue(gate.promise);
const release = vi.fn();
const updater = mount({ prepareForUpdate: async () => release });
await updater.initialize();
await updater.installDownloadedUpdate();
gate.resolve(info);
await flush();
expect(updater.phase.value).toBe("restart");
expect(mocks.discardDownloadedUpdate).not.toHaveBeenCalled();
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
});
it("keeps the ready package when discarding a superseded update fails", async () => {
mocks.getDownloadedUpdate.mockResolvedValue(cache);
mocks.checkForUpdates.mockResolvedValue({ ...info, latest_version: "1.2.0" });
mocks.discardDownloadedUpdate.mockRejectedValue(new Error("file busy"));
const updater = mount();
await updater.initialize();
await flush();
expect(mocks.downloadUpdate).not.toHaveBeenCalled();
expect(updater.updateDownloaded.value).toBe(true);
expect(updater.updateCheckMessage.value).toContain("file busy");
});
});
+32 -3
View File
@@ -77,6 +77,13 @@ export function isUpdateIgnored(info: api.UpdateInfo | null, ignoredVersion: str
return compareParsedUpdateVersions(parsedLatest, parsedIgnored) <= 0;
}
export function isNewerRemoteVersion(latest: string, cached: string): boolean {
const parsedLatest = parseUpdateVersion(latest);
const parsedCached = parseUpdateVersion(cached);
if (!parsedLatest || !parsedCached) return normalizeUpdateVersion(latest) !== normalizeUpdateVersion(cached);
return compareParsedUpdateVersions(parsedLatest, parsedCached) > 0;
}
export function normalizeUpdateDownloadSource(value: unknown): SettingsUpdateDownloadSource {
// Old persisted AtomGit preferences should retain their mainland mirror behavior.
if (value === "atomgit") return "cnb";
@@ -199,7 +206,9 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
async function checkUpdates(checkOptions: { silent?: boolean } = {}) {
if (disposed || isIgnoringUpdate.value) return;
if (!checkOptions.silent) showUpdateDialog.value = true;
if (phase.value !== "idle" || downloaded.value || (checkOptions.silent && !notificationsEnabled.value)) return;
// A downloaded-but-uninstalled update keeps the app in the ready phase; checks
// continue so a newer release can replace the cached package.
if ((phase.value !== "idle" && phase.value !== "ready") || (checkOptions.silent && !notificationsEnabled.value)) return;
clearRetry();
const token = ++generation;
phase.value = "checking";
@@ -208,18 +217,38 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
const info = await api.checkForUpdates(currentLocale(), normalizeUpdateDownloadSource(settingsStore.editorSettings.updateDownloadSource));
if (token !== generation || disposed) return;
updateInfo.value = info;
phase.value = "idle";
// Installation may have started while the check was in flight; only restore
// the phase this check itself owns.
if (phase.value === "checking") phase.value = downloaded.value ? "ready" : "idle";
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 && !(await discardSupersededUpdate(cached))) return;
await downloadUpdateInBackground();
}
} catch (error) {
if (token !== generation || disposed) return;
phase.value = "idle";
if (phase.value === "checking") phase.value = downloaded.value ? "ready" : "idle";
fail(error);
scheduleRetry();
}
}
async function discardSupersededUpdate(cache: api.DownloadedUpdate): Promise<boolean> {
if (isInstallingUpdate.value || isIgnoringUpdate.value) return false;
try {
await api.discardDownloadedUpdate(cache.cache_id);
if (downloaded.value?.cache_id === cache.cache_id) {
downloaded.value = null;
downloadProgress.value = null;
if (phase.value === "ready" || phase.value === "checking") phase.value = "idle";
}
return true;
} catch (error) {
fail(error);
return false;
}
}
async function downloadUpdateInBackground() {
if (disposed || isIgnoringUpdate.value || phase.value !== "idle" || downloaded.value || !canDownloadAndInstallUpdate(updateInfo.value, isTauriRuntime())) return;
const version = updateInfo.value!.latest_version;