feat(updater): allow switching download source after failure

This commit is contained in:
zipg authored and GitHub committed 2026-09-02 17:35:24 +08:00
1 parent 0333b3b389
commit 1706081802
14 files changed
+137 -3

No files matched your search

@@ -267,6 +267,70 @@ describe("useAppUpdater reopening the dialog from the toolbar", () => {
});
});
describe("useAppUpdater failure state handling", () => {
interface UpdateInfo {
current_version: string;
latest_version: string;
update_available: boolean;
release_name: string;
release_url: string;
release_notes: string;
}
it("keeps the failed state visible while a retry is in flight and clears it on success", async () => {
apiMock.checkForUpdates.mockRejectedValueOnce(new Error("boom"));
const updater = mountUpdater();
await updater.checkUpdates();
expect(updater.updateCheckFailed.value).toBe(true);
expect(updater.updateCheckMessage.value).not.toBe("");
const retry = deferred<UpdateInfo>();
apiMock.checkForUpdates.mockReturnValueOnce(retry.promise);
const pending = updater.checkUpdates();
await vi.waitFor(() => expect(updater.checkingUpdates.value).toBe(true));
// Mid-retry: the source switcher stays mounted (updateCheckFailed) and
// the dialog must not fall through to "up to date" with an empty version.
expect(updater.updateCheckFailed.value).toBe(true);
expect(updater.updateCheckMessage.value).not.toBe("");
retry.resolve({
current_version: "0.5.69",
latest_version: "0.5.69",
update_available: false,
release_name: "",
release_url: "",
release_notes: "",
});
await pending;
expect(updater.updateCheckFailed.value).toBe(false);
expect(updater.updateCheckMessage.value).toContain("0.5.69");
});
it("keeps the last update info when a silent background check fails", async () => {
apiMock.checkForUpdates.mockResolvedValueOnce({
current_version: "0.5.69",
latest_version: "0.5.70",
update_available: true,
release_name: "DBX v0.5.70",
release_url: "https://github.com/t8y2/dbx/releases/tag/v0.5.70",
release_notes: "",
});
const updater = mountUpdater();
await updater.checkUpdates({ silent: true });
expect(updater.updateInfo.value?.update_available).toBe(true);
apiMock.checkForUpdates.mockRejectedValueOnce(new Error("network down"));
await updater.checkUpdates({ silent: true });
expect(updater.updateInfo.value?.update_available).toBe(true);
expect(updater.updateCheckFailed.value).toBe(false);
expect(updater.showUpdateDialog.value).toBe(false);
});
});
describe("useAppUpdater ignore version", () => {
it("persists the ignored latest version and closes the update dialog", async () => {
const updater = mountUpdater();
+20 -1
View File
@@ -94,6 +94,7 @@ export function resolveUpdateReleaseUrl(info: api.UpdateInfo | null, source: unk
if (normalizedSource === "cnb" && info?.latest_version) {
return `https://cnb.cool/dbxio.com/dbx/-/releases/tag/${tagVersion(info.latest_version)}`;
}
if (normalizedSource === "cnb") return "https://cnb.cool/dbxio.com/dbx/-/releases";
return info?.release_url || fallbackUrl;
}
@@ -115,6 +116,7 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
const checkingUpdates = ref(false);
const updateInfo = ref<api.UpdateInfo | null>(null);
const updateCheckMessage = ref("");
const updateCheckFailed = ref(false);
const showUpdateDialog = ref(false);
const isDownloadingUpdate = ref(false);
// null while the download size is unknown (backend reported no total) — the UI shows an indeterminate state.
@@ -146,10 +148,14 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
return;
}
checkingUpdates.value = true;
updateCheckMessage.value = "";
// Keep the previous message/failed state visible while the check is in
// flight: pre-clearing would flip the dialog to "up to date" with an empty
// version and unmount the source switcher until the request settles.
try {
const info = await api.checkForUpdates(currentLocale(), normalizeUpdateDownloadSource(settingsStore.editorSettings.updateDownloadSource));
updateInfo.value = info;
updateCheckMessage.value = "";
updateCheckFailed.value = false;
if (info.update_available) {
if (shouldOpenUpdateDialog({ silent: options.silent })) {
showUpdateDialog.value = true;
@@ -160,7 +166,13 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
}
} catch (e: any) {
if (!options.silent) {
// Drop the stale update info only on a surfaced failure; silent
// background checks keep the last known state so the toolbar
// indicator and an in-flight download's portable-mode flag survive
// a single failed poll.
updateInfo.value = null;
updateCheckMessage.value = formatUpdateError(String(e));
updateCheckFailed.value = true;
showUpdateDialog.value = true;
}
} finally {
@@ -187,6 +199,11 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
openUrl(url);
}
async function changeUpdateDownloadSource(source: SettingsUpdateDownloadSource) {
await settingsStore.updateEditorSettingsAndPersist({ updateDownloadSource: source });
await checkUpdates();
}
async function ignoreCurrentVersion() {
const latest = updateInfo.value?.latest_version;
if (!latest || isIgnoringUpdate.value) return;
@@ -342,6 +359,7 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
checkingUpdates,
updateInfo,
updateCheckMessage,
updateCheckFailed,
showUpdateDialog,
isDownloadingUpdate,
downloadProgress,
@@ -356,6 +374,7 @@ export function useAppUpdater(options: UseAppUpdaterOptions = {}) {
checkUpdates,
formatUpdateError,
openLatestRelease,
changeUpdateDownloadSource,
ignoreCurrentVersion,
downloadUpdateInBackground,
cancelDownload,