fix(updater): guard installs during active tasks
This commit is contained in:
19 files changed
+554
-26
No files matched your search
@@ -0,0 +1,222 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, reactive, type App } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import UpdateDialog from "@/components/layout/UpdateDialog.vue";
|
||||
|
||||
vi.mock("@/lib/backend/tauriRuntime", () => ({
|
||||
isTauriRuntime: () => true,
|
||||
}));
|
||||
|
||||
const mountedApps: App[] = [];
|
||||
|
||||
interface DialogState {
|
||||
open: boolean;
|
||||
isDownloadingUpdate: boolean;
|
||||
downloadProgress: number;
|
||||
updateDownloaded: boolean;
|
||||
isInstallingUpdate: boolean;
|
||||
updateReady: boolean;
|
||||
}
|
||||
|
||||
async function flushDialog() {
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
async function mountDialog(activeTaskCount: number, initialState: Partial<DialogState> = {}, installDownloaded = vi.fn(async () => {})) {
|
||||
const state = reactive<DialogState>({
|
||||
open: true,
|
||||
isDownloadingUpdate: false,
|
||||
downloadProgress: 0,
|
||||
updateDownloaded: false,
|
||||
isInstallingUpdate: false,
|
||||
updateReady: false,
|
||||
...initialState,
|
||||
});
|
||||
const downloadAndInstall = vi.fn();
|
||||
const container = document.createElement("div");
|
||||
document.body.append(container);
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
async function handleInstallDownloaded() {
|
||||
state.isInstallingUpdate = true;
|
||||
try {
|
||||
await installDownloaded();
|
||||
state.updateDownloaded = false;
|
||||
state.updateReady = true;
|
||||
} catch {
|
||||
// The real updater reports the error but retains the downloaded package for retry.
|
||||
} finally {
|
||||
state.isInstallingUpdate = false;
|
||||
}
|
||||
}
|
||||
|
||||
return () =>
|
||||
h(UpdateDialog, {
|
||||
open: state.open,
|
||||
"onUpdate:open": (value: boolean) => {
|
||||
state.open = value;
|
||||
},
|
||||
updateInfo: {
|
||||
current_version: "0.5.60",
|
||||
latest_version: "0.5.61",
|
||||
update_available: true,
|
||||
portable_mode: false,
|
||||
release_name: "DBX v0.5.61",
|
||||
release_url: "https://github.com/t8y2/dbx/releases/tag/v0.5.61",
|
||||
release_notes: "",
|
||||
},
|
||||
updateCheckMessage: "",
|
||||
isDownloadingUpdate: state.isDownloadingUpdate,
|
||||
downloadProgress: state.downloadProgress,
|
||||
updateDownloaded: state.updateDownloaded,
|
||||
isInstallingUpdate: state.isInstallingUpdate,
|
||||
updateReady: state.updateReady,
|
||||
activeTaskCount,
|
||||
"onDownload-and-install": downloadAndInstall,
|
||||
"onInstall-downloaded": handleInstallDownloaded,
|
||||
});
|
||||
},
|
||||
}),
|
||||
);
|
||||
mountedApps.push(app);
|
||||
app.use(i18n);
|
||||
app.mount(container);
|
||||
await flushDialog();
|
||||
|
||||
return { state, downloadAndInstall, installDownloaded };
|
||||
}
|
||||
|
||||
function buttonWithText(text: string): HTMLButtonElement | undefined {
|
||||
return Array.from(document.body.querySelectorAll("button")).find((button) => button.textContent?.includes(text));
|
||||
}
|
||||
|
||||
function downloadButton(): HTMLButtonElement | undefined {
|
||||
return buttonWithText("Download & Install");
|
||||
}
|
||||
|
||||
function installDownloadedButton(): HTMLButtonElement | undefined {
|
||||
return buttonWithText("Exit & Update");
|
||||
}
|
||||
|
||||
async function pressEscape() {
|
||||
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));
|
||||
await flushDialog();
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const app of mountedApps.splice(0)) app.unmount();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
describe("UpdateDialog active task guard", () => {
|
||||
it("shows the task warning and disables installation while work is running", async () => {
|
||||
await mountDialog(2);
|
||||
|
||||
expect(document.body.querySelector('[role="alert"]')?.textContent).toContain("2");
|
||||
expect(downloadButton()?.disabled).toBe(true);
|
||||
});
|
||||
|
||||
it("allows installation after all tasks finish", async () => {
|
||||
await mountDialog(0);
|
||||
|
||||
expect(document.body.querySelector('[role="alert"]')).toBeNull();
|
||||
expect(downloadButton()?.disabled).toBe(false);
|
||||
});
|
||||
|
||||
it("retains the downloaded update and enables installation only after tasks finish", async () => {
|
||||
await mountDialog(1, { updateDownloaded: true, downloadProgress: 100 });
|
||||
|
||||
expect(downloadButton()).toBeUndefined();
|
||||
expect(installDownloadedButton()?.disabled).toBe(true);
|
||||
|
||||
for (const app of mountedApps.splice(0)) app.unmount();
|
||||
document.body.innerHTML = "";
|
||||
await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 });
|
||||
|
||||
expect(installDownloadedButton()?.disabled).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("UpdateDialog close protection", () => {
|
||||
it("allows closing while a downloaded update is idle", async () => {
|
||||
const { state } = await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 });
|
||||
|
||||
expect(buttonWithText("Cancel")).toBeDefined();
|
||||
expect(document.body.querySelector('[data-slot="dialog-close"]')).not.toBeNull();
|
||||
await pressEscape();
|
||||
|
||||
expect(state.open).toBe(false);
|
||||
});
|
||||
|
||||
it("prevents closing while installation is in progress", async () => {
|
||||
const { state } = await mountDialog(0, { updateDownloaded: true, isInstallingUpdate: true });
|
||||
|
||||
expect(buttonWithText("Cancel")).toBeUndefined();
|
||||
expect(document.body.querySelector('[data-slot="dialog-close"]')).toBeNull();
|
||||
await pressEscape();
|
||||
|
||||
expect(state.open).toBe(true);
|
||||
});
|
||||
|
||||
it("allows closing after installation rejects", async () => {
|
||||
let rejectInstall!: (error: Error) => void;
|
||||
const installDownloaded = vi.fn(
|
||||
() =>
|
||||
new Promise<void>((_resolve, reject) => {
|
||||
rejectInstall = reject;
|
||||
}),
|
||||
);
|
||||
const { state } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
|
||||
|
||||
installDownloadedButton()?.click();
|
||||
await flushDialog();
|
||||
await pressEscape();
|
||||
expect(state.open).toBe(true);
|
||||
|
||||
rejectInstall(new Error("install failed"));
|
||||
await flushDialog();
|
||||
expect(buttonWithText("Cancel")).toBeDefined();
|
||||
await pressEscape();
|
||||
|
||||
expect(state.open).toBe(false);
|
||||
});
|
||||
|
||||
it("retries the retained download after reopening without downloading again", async () => {
|
||||
const installDownloaded = vi.fn(async () => {
|
||||
throw new Error("install failed");
|
||||
});
|
||||
const { state, downloadAndInstall } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
|
||||
|
||||
installDownloadedButton()?.click();
|
||||
await flushDialog();
|
||||
await pressEscape();
|
||||
state.open = true;
|
||||
await flushDialog();
|
||||
installDownloadedButton()?.click();
|
||||
await flushDialog();
|
||||
|
||||
expect(installDownloaded).toHaveBeenCalledTimes(2);
|
||||
expect(downloadAndInstall).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the successful install flow protected", async () => {
|
||||
const installDownloaded = vi.fn(async () => {});
|
||||
const { state, downloadAndInstall } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);
|
||||
|
||||
installDownloadedButton()?.click();
|
||||
await flushDialog();
|
||||
|
||||
expect(state.updateDownloaded).toBe(false);
|
||||
expect(state.updateReady).toBe(true);
|
||||
expect(buttonWithText("Restart")).toBeDefined();
|
||||
expect(buttonWithText("Cancel")).toBeUndefined();
|
||||
await pressEscape();
|
||||
expect(state.open).toBe(true);
|
||||
expect(installDownloaded).toHaveBeenCalledOnce();
|
||||
expect(downloadAndInstall).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from "vue";
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { Loader2 } from "@lucide/vue";
|
||||
import { AlertTriangle, Loader2 } from "@lucide/vue";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import type { UpdateInfo } from "@/lib/backend/api";
|
||||
@@ -15,12 +15,16 @@ const props = defineProps<{
|
||||
updateCheckMessage: string;
|
||||
isDownloadingUpdate: boolean;
|
||||
downloadProgress: number;
|
||||
updateDownloaded: boolean;
|
||||
isInstallingUpdate: boolean;
|
||||
updateReady: boolean;
|
||||
activeTaskCount: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
"open-latest-release": [];
|
||||
"download-and-install": [];
|
||||
"install-downloaded": [];
|
||||
restart: [];
|
||||
}>();
|
||||
|
||||
@@ -28,6 +32,8 @@ const { t } = useI18n();
|
||||
const isDesktop = isTauriRuntime();
|
||||
|
||||
const renderedNotes = ref("");
|
||||
// Keep a downloaded package retryable after install errors; only active transitions must trap the dialog.
|
||||
const isCloseBlocked = computed(() => props.isDownloadingUpdate || props.isInstallingUpdate || props.updateReady);
|
||||
|
||||
function handleReleaseNotesClick(event: MouseEvent) {
|
||||
const target = event.target as HTMLElement;
|
||||
@@ -62,14 +68,15 @@ watch(
|
||||
<Dialog v-model:open="open">
|
||||
<DialogContent
|
||||
class="sm:max-w-[520px]"
|
||||
:show-close-button="!isCloseBlocked"
|
||||
@interact-outside="
|
||||
(e: Event) => {
|
||||
if (isDownloadingUpdate || updateReady) e.preventDefault();
|
||||
if (isCloseBlocked) e.preventDefault();
|
||||
}
|
||||
"
|
||||
@escape-key-down="
|
||||
(e: Event) => {
|
||||
if (isDownloadingUpdate || updateReady) e.preventDefault();
|
||||
if (isCloseBlocked) e.preventDefault();
|
||||
}
|
||||
"
|
||||
>
|
||||
@@ -102,18 +109,27 @@ watch(
|
||||
<p v-if="isDesktop && updateInfo?.update_available && updateInfo.portable_mode" class="text-xs text-muted-foreground">
|
||||
{{ t("updates.portableManualUpdate") }}
|
||||
</p>
|
||||
<div v-if="canDownloadAndInstallUpdate(updateInfo, isDesktop) && activeTaskCount > 0" role="alert" class="flex items-start gap-2 rounded-md border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-xs text-amber-700 dark:text-amber-300">
|
||||
<AlertTriangle class="mt-0.5 h-4 w-4 shrink-0" />
|
||||
<span>{{ t("updates.activeTasksBlockUpdate", { count: activeTaskCount }) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button v-if="!isDownloadingUpdate && !updateReady" variant="outline" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
||||
<Button v-if="!isCloseBlocked" variant="outline" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
||||
<template v-if="updateInfo?.update_available">
|
||||
<Button variant="outline" @click="emit('open-latest-release')">{{ t("updates.openRelease") }}</Button>
|
||||
<template v-if="canDownloadAndInstallUpdate(updateInfo, isDesktop)">
|
||||
<Button v-if="updateReady" @click="emit('restart')">{{ t("updates.restart") }}</Button>
|
||||
<Button v-if="updateReady" :disabled="activeTaskCount > 0" @click="emit('restart')">{{ t("updates.restart") }}</Button>
|
||||
<Button v-else-if="isInstallingUpdate" disabled>
|
||||
<Loader2 class="h-4 w-4 animate-spin" />
|
||||
{{ t("updates.installing") }}
|
||||
</Button>
|
||||
<Button v-else-if="isDownloadingUpdate" disabled>
|
||||
<Loader2 class="h-4 w-4 animate-spin" />
|
||||
{{ t("updates.downloading", { progress: downloadProgress }) }}
|
||||
</Button>
|
||||
<Button v-else @click="emit('download-and-install')">{{ t("updates.downloadAndInstall") }}</Button>
|
||||
<Button v-else-if="updateDownloaded" :disabled="activeTaskCount > 0" @click="emit('install-downloaded')">{{ t("updates.exitAndUpdate") }}</Button>
|
||||
<Button v-else :disabled="activeTaskCount > 0" @click="emit('download-and-install')">{{ t("updates.downloadAndInstall") }}</Button>
|
||||
</template>
|
||||
</template>
|
||||
<Button v-else-if="updateCheckMessage" @click="emit('open-latest-release')">{{ t("updates.openRelease") }}</Button>
|
||||
|
||||
Reference in new issue
Block a user