feat(update): download app updates in the background

This commit is contained in:
0xTyson authored and GitHub committed 2026-09-01 11:09:44 +08:00
1 parent d79d357924
commit 8be503bf40
18 files changed
+448 -171

No files matched your search

+36 -17
View File
@@ -16,7 +16,7 @@ interface DialogState {
portableMode: boolean;
manualUpdateOnly: boolean;
isDownloadingUpdate: boolean;
downloadProgress: number;
downloadProgress: number | null;
updateDownloaded: boolean;
isInstallingUpdate: boolean;
updateReady: boolean;
@@ -41,7 +41,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
isIgnoringUpdate: false,
...initialState,
});
const downloadAndInstall = vi.fn();
const downloadInBackground = vi.fn();
const cancelDownload = vi.fn();
const ignoreVersion = vi.fn();
const container = document.createElement("div");
@@ -86,7 +86,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
updateReady: state.updateReady,
isIgnoringUpdate: state.isIgnoringUpdate,
activeTaskCount,
"onDownload-and-install": downloadAndInstall,
"onDownload-in-background": downloadInBackground,
"onCancel-download": cancelDownload,
"onInstall-downloaded": handleInstallDownloaded,
"onIgnore-version": ignoreVersion,
@@ -99,7 +99,7 @@ async function mountDialog(activeTaskCount: number, initialState: Partial<Dialog
app.mount(container);
await flushDialog();
return { state, downloadAndInstall, cancelDownload, installDownloaded, ignoreVersion };
return { state, downloadInBackground, cancelDownload, installDownloaded, ignoreVersion };
}
function buttonWithText(text: string): HTMLButtonElement | undefined {
@@ -107,7 +107,11 @@ function buttonWithText(text: string): HTMLButtonElement | undefined {
}
function downloadButton(): HTMLButtonElement | undefined {
return buttonWithText("Download & Install");
return buttonWithText("Download in Background");
}
function cancelDownloadButton(): HTMLButtonElement | undefined {
return buttonWithText("Cancel Download");
}
function installDownloadedButton(): HTMLButtonElement | undefined {
@@ -130,11 +134,11 @@ afterEach(() => {
});
describe("UpdateDialog active task guard", () => {
it("shows the task warning and disables installation while work is running", async () => {
it("shows the task warning but still allows starting a background download while work is running", async () => {
await mountDialog(2);
expect(document.body.querySelector('[role="alert"]')?.textContent).toContain("2");
expect(downloadButton()?.disabled).toBe(true);
expect(downloadButton()?.disabled).toBe(false);
});
it("allows installation after all tasks finish", async () => {
@@ -193,36 +197,51 @@ describe("UpdateDialog download progress", () => {
expect(buttonWithText("Downloading 100%")?.classList.contains("w-52")).toBe(true);
});
it("falls back to 0% while the download size is unknown", async () => {
await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: null });
expect(buttonWithText("Downloading 0%")).toBeDefined();
});
});
describe("UpdateDialog close protection", () => {
it("cancels the background download when the close button is clicked", async () => {
it("closes without cancelling the background download when the close button is clicked", async () => {
const { state, cancelDownload } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 42 });
const closeButton = document.body.querySelector<HTMLButtonElement>('[data-slot="dialog-close"]');
closeButton?.click();
await flushDialog();
expect(cancelDownload).toHaveBeenCalledOnce();
expect(cancelDownload).not.toHaveBeenCalled();
expect(state.open).toBe(false);
});
it("keeps downloading in the background when the dialog is dismissed by clicking outside", async () => {
it("closes without cancelling the background download when dismissed by clicking outside", async () => {
const { state, cancelDownload } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 42 });
await clickOutside();
expect(cancelDownload).not.toHaveBeenCalled();
expect(state.open).toBe(true);
expect(state.open).toBe(false);
});
it("keeps downloading in the background when the dialog is dismissed with Escape", async () => {
it("closes without cancelling the background download when dismissed with Escape", async () => {
const { state, cancelDownload } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 42 });
await pressEscape();
expect(cancelDownload).not.toHaveBeenCalled();
expect(state.open).toBe(true);
expect(state.open).toBe(false);
});
it("cancels the download only when the explicit Cancel Download button is clicked", async () => {
const { cancelDownload } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 42 });
cancelDownloadButton()?.click();
await flushDialog();
expect(cancelDownload).toHaveBeenCalledOnce();
});
it("allows closing while a downloaded update is idle", async () => {
@@ -272,7 +291,7 @@ describe("UpdateDialog close protection", () => {
const installDownloaded = vi.fn(async () => {
throw new Error("install failed");
});
const { state, downloadAndInstall } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
const { state, downloadInBackground } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
installDownloadedButton()?.click();
await flushDialog();
@@ -283,12 +302,12 @@ describe("UpdateDialog close protection", () => {
await flushDialog();
expect(installDownloaded).toHaveBeenCalledTimes(2);
expect(downloadAndInstall).not.toHaveBeenCalled();
expect(downloadInBackground).not.toHaveBeenCalled();
});
it("allows dismissing after a successful install while restart remains available", async () => {
const installDownloaded = vi.fn(async () => {});
const { state, downloadAndInstall } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
const { state, downloadInBackground } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
installDownloadedButton()?.click();
await flushDialog();
@@ -300,7 +319,7 @@ describe("UpdateDialog close protection", () => {
await pressEscape();
expect(state.open).toBe(false);
expect(installDownloaded).toHaveBeenCalledOnce();
expect(downloadAndInstall).not.toHaveBeenCalled();
expect(downloadInBackground).not.toHaveBeenCalled();
});
});