Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

31.1 KB
Raw Blame
// @vitest-environment happy-dom

import { createApp, defineComponent, h, nextTick, reactive, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
import UpdateDialog from "@/components/layout/UpdateDialog.vue";

const runtimeState = vi.hoisted(() => ({ tauri: true }));

const updateMocks = vi.hoisted(() => ({
  fetchChangelog: vi.fn(),
  getAppVersion: vi.fn(),
  openExternal: vi.fn(),
}));

vi.mock("@/lib/backend/tauriRuntime", () => ({
  isTauriRuntime: () => runtimeState.tauri,
}));

vi.mock("@/lib/app/changelog", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@/lib/app/changelog")>();
  return { ...actual, fetchChangelog: updateMocks.fetchChangelog };
});

vi.mock("@/lib/backend/api", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@/lib/backend/api")>();
  return { ...actual, getAppVersion: updateMocks.getAppVersion };
});

vi.mock("@tauri-apps/plugin-shell", () => ({ open: updateMocks.openExternal }));

const mountedApps: App[] = [];

interface DialogState {
  open: boolean;
  portableMode: boolean;
  releaseNotes: string;
  manualUpdateOnly: boolean;
  isDownloadingUpdate: boolean;
  downloadProgress: number | null;
  updateDownloaded: boolean;
  isInstallingUpdate: boolean;
  updateReady: boolean;
  isIgnoringUpdate: 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 () => {}), extraProps: Record<string, unknown> = {}) {
  const state = reactive<DialogState>({
    open: true,
    portableMode: false,
    releaseNotes: "",
    manualUpdateOnly: false,
    isDownloadingUpdate: false,
    downloadProgress: 0,
    updateDownloaded: false,
    isInstallingUpdate: false,
    updateReady: false,
    isIgnoringUpdate: false,
    ...initialState,
  });
  const downloadInBackground = vi.fn();
  const cancelDownload = vi.fn();
  const ignoreVersion = 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: state.portableMode,
              manual_update_only: state.manualUpdateOnly,
              release_name: "DBX v0.5.61",
              release_url: "https://github.com/t8y2/dbx/releases/tag/v0.5.61",
              release_notes: state.releaseNotes,
            },
            updateCheckMessage: "",
            checkingUpdates: false,
            updateCheckFailed: false,
            updateDownloadSource: "official",
            isDownloadingUpdate: state.isDownloadingUpdate,
            downloadProgress: state.downloadProgress,
            updateDownloaded: state.updateDownloaded,
            isInstallingUpdate: state.isInstallingUpdate,
            updateReady: state.updateReady,
            isIgnoringUpdate: state.isIgnoringUpdate,
            activeTaskCount,
            ...extraProps,
            "onDownload-in-background": downloadInBackground,
            "onCancel-download": cancelDownload,
            "onInstall-downloaded": handleInstallDownloaded,
            "onIgnore-version": ignoreVersion,
          });
      },
    }),
  );
  mountedApps.push(app);
  app.use(i18n);
  app.mount(container);
  await flushDialog();

  return { state, downloadInBackground, cancelDownload, installDownloaded, ignoreVersion };
}

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 in Background") ?? buttonWithText("Retry Download");
}

function cancelDownloadButton(): HTMLButtonElement | undefined {
  return buttonWithText("Cancel Download");
}

function installDownloadedButton(): HTMLButtonElement | undefined {
  return buttonWithText("Restart & Update");
}

async function pressEscape() {
  document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));
  await flushDialog();
}

async function clickOutside() {
  document.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, cancelable: true }));
  await flushDialog();
}

beforeEach(() => {
  // 默认没有历史版本:只有专门的历史版本用例才提供发布记录
  updateMocks.fetchChangelog.mockResolvedValue({ updatedAt: "", releases: [] });
  updateMocks.getAppVersion.mockResolvedValue("0.5.60");
  updateMocks.openExternal.mockClear();
});

afterEach(() => {
  runtimeState.tauri = true;
  for (const app of mountedApps.splice(0)) app.unmount();
  document.body.innerHTML = "";
});

describe("UpdateDialog web runtime", () => {
  it("keeps web updates as Docker instructions without desktop install controls", async () => {
    runtimeState.tauri = false;

    await mountDialog(0);

    expect(document.body.textContent).toContain("Docker users should run");
    expect(document.body.textContent).toContain("docker compose pull && docker compose up -d");
    expect(buttonWithText("Open Release")).toBeDefined();
    expect(downloadButton()).toBeUndefined();
    expect(installDownloadedButton()).toBeUndefined();
  });
});

describe("UpdateDialog active task guard", () => {
  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(false);
  });

  it("allows installation after all tasks finish", async () => {
    await mountDialog(0);

    expect(document.body.querySelector('[role="alert"]')).toBeNull();
    expect(downloadButton()?.disabled).toBe(false);
  });

  it("offers automatic installation for portable builds", async () => {
    await mountDialog(0, { portableMode: true });

    expect(document.body.textContent).toContain("portable ZIP");
    expect(downloadButton()?.disabled).toBe(false);
  });

  it("routes Windows 7 builds to the dedicated installer", async () => {
    await mountDialog(0, { manualUpdateOnly: true });

    expect(document.body.textContent).toContain("WebView2 109 offline installer");
    expect(downloadButton()).toBeUndefined();
    expect(buttonWithText("Open Release")).toBeDefined();
  });

  it("prevents Windows 7 portable builds from installing the regular x64 portable update", async () => {
    await mountDialog(0, { portableMode: true, manualUpdateOnly: true });

    expect(document.body.textContent).toContain("WebView2 109 offline installer");
    expect(document.body.textContent).not.toContain("signed portable ZIP");
    expect(downloadButton()).toBeUndefined();
    expect(buttonWithText("Open Release")).toBeDefined();
  });

  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 prepared package without a remote update (#13562)", () => {
  const upToDateInfo = {
    current_version: "0.5.61",
    latest_version: "0.5.61",
    update_available: false,
    portable_mode: false,
    manual_update_only: false,
    release_name: "DBX v0.5.61",
    release_url: "https://github.com/t8y2/dbx/releases/tag/v0.5.61",
    release_notes: "",
  };

  it("keeps the install action reachable when the remote reports no update", async () => {
    const installDownloaded = vi.fn(async () => {});
    await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 }, installDownloaded, { updateInfo: upToDateInfo });

    const button = installDownloadedButton();
    expect(button).toBeDefined();
    expect(button?.disabled).toBe(false);
    button?.click();
    await flushDialog();

    expect(installDownloaded).toHaveBeenCalledOnce();
  });

  it("labels the prepared package with its own version", async () => {
    await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 }, undefined, { updateInfo: upToDateInfo, updateDownloadedVersion: "0.5.62" });

    expect(document.body.textContent).toContain("v0.5.62 is downloaded");
    expect(document.body.textContent).not.toContain("DBX is up to date");
    expect(document.body.textContent).not.toContain("v0.5.61 is downloaded");
  });
});

describe("UpdateDialog download progress", () => {
  it("keeps the downloading button at a fixed width as progress changes", async () => {
    const { state } = await mountDialog(0, { isDownloadingUpdate: true, downloadProgress: 9 });

    expect(buttonWithText("Downloading 9%")?.classList.contains("w-52")).toBe(true);

    state.downloadProgress = 100;
    await flushDialog();

    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("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).not.toHaveBeenCalled();
    expect(state.open).toBe(false);
  });

  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(false);
  });

  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(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 () => {
    const { state } = await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 });

    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(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(document.body.querySelector('[data-slot="dialog-close"]')).not.toBeNull();
    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, downloadInBackground } = 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(downloadInBackground).not.toHaveBeenCalled();
  });

  it("allows dismissing after a successful install while restart remains available", async () => {
    const installDownloaded = vi.fn(async () => {});
    const { state, downloadInBackground } = await mountDialog(0, { updateDownloaded: true }, installDownloaded);

    installDownloadedButton()?.click();
    await flushDialog();

    expect(state.updateDownloaded).toBe(false);
    expect(state.updateReady).toBe(true);
    expect(buttonWithText("Restart")).toBeDefined();
    await pressEscape();
    expect(state.open).toBe(false);
    expect(installDownloaded).toHaveBeenCalledOnce();
    expect(downloadInBackground).not.toHaveBeenCalled();
  });
});

describe("UpdateDialog ignore version", () => {
  it("emits ignore-version when the ignore button is clicked", async () => {
    const { ignoreVersion } = await mountDialog(0);

    const ignoreButton = buttonWithText("Ignore this version");
    expect(ignoreButton).toBeDefined();
    ignoreButton?.click();
    await flushDialog();

    expect(ignoreVersion).toHaveBeenCalledOnce();
  });

  it("allows ignoring a downloaded update", async () => {
    await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 });

    expect(buttonWithText("Ignore this version")).toBeDefined();
  });

  it("disables the ignore button while the setting is being persisted", async () => {
    await mountDialog(0, { isIgnoringUpdate: true });

    expect(buttonWithText("Ignore this version")?.disabled).toBe(true);
  });
});

describe("UpdateDialog release notes safety", () => {
  it("renders remote HTML as text and never creates unsafe links or image requests", async () => {
    await mountDialog(0, { releaseNotes: '<img src="https://example.com/tracker" onerror="alert(1)"><script>alert(1)</script> [bad](javascript:alert) ![remote](https://example.com/image) [safe](https://example.com/release)' });
    await vi.waitFor(() => {
      expect(document.body.querySelector('a[href="https://example.com/release"]')).not.toBeNull();
    });
    expect(document.body.querySelector("script, img")).toBeNull();
    expect(Array.from(document.body.querySelectorAll("a")).every((anchor) => anchor.href.startsWith("https://"))).toBe(true);
  });

  it("hides HTML comments such as the CNB mirror marker instead of rendering them as text", async () => {
    await mountDialog(0, { releaseNotes: "### 安装\n- 条目\n\n<!-- dbx-cnb-mirror -->\n> 国内下载:[CNB 镜像](https://cnb.cool/dbxio.com/dbx/-/releases)" });
    await vi.waitFor(() => {
      expect(document.body.querySelector('a[href^="https://cnb.cool"]')).not.toBeNull();
    });
    expect(document.body.textContent).not.toContain("dbx-cnb-mirror");
    expect(document.body.textContent).not.toContain("<!--");
  });
});

describe("UpdateDialog changelog", () => {
  it("shows the shared changelog even when the current version has no update", async () => {
    const checkUpdates = vi.fn();
    updateMocks.fetchChangelog.mockResolvedValue({
      updatedAt: "2026-10-28",
      releases: [{ tag: "v0.5.60", name: "DBX v0.5.60", date: "2026-10-28", sections: [{ type: "fixed", title: "Fixed", items: [{ title: "A useful fix", desc: "Details of the fix" }] }] }],
    });
    await mountDialog(0, {}, undefined, { updateInfo: null, "onCheck-updates": checkUpdates });

    const changelogTab = document.body.querySelector<HTMLButtonElement>('[data-update-tab="changelog"]');
    expect(changelogTab).not.toBeNull();
    changelogTab?.click();
    await flushDialog();
    buttonWithText("0.5.60")?.click();
    await flushDialog();

    expect(document.body.textContent).toContain("A useful fix");
    expect(document.body.textContent).toContain("Details of the fix");
    expect(downloadButton()).toBeUndefined();
    const checkButton = buttonWithText(i18n.global.t("updates.check"));
    expect(checkButton).toBeDefined();
    checkButton?.click();
    expect(checkUpdates).toHaveBeenCalledOnce();
  });

  it("keeps client update actions available after returning from the changelog", async () => {
    const { downloadInBackground } = await mountDialog(0);
    document.body.querySelector<HTMLButtonElement>('[data-update-tab="changelog"]')?.click();
    await flushDialog();
    expect(downloadButton()).toBeUndefined();
    document.body.querySelector<HTMLButtonElement>('[data-update-tab="app"]')?.click();
    await flushDialog();
    downloadButton()?.click();
    expect(downloadInBackground).toHaveBeenCalledOnce();
  });
});

describe("UpdateDialog older versions", () => {
  const release = (tag: string, date: string) => ({ tag, name: `DBX ${tag}`, date, sections: [] });
  const upToDateInfo = {
    current_version: "0.5.60",
    latest_version: "0.5.60",
    update_available: false,
    portable_mode: false,
    manual_update_only: false,
    release_name: "",
    release_url: "",
    release_notes: "",
  };
  const historyRegion = () => document.body.querySelector<HTMLElement>("[data-update-history]");
  const historyButton = (text: string) => Array.from(historyRegion()?.querySelectorAll("button") ?? []).find((button) => button.textContent?.includes(text));

  it("lists only versions older than the current build and opens their release page", async () => {
    updateMocks.fetchChangelog.mockResolvedValue({ updatedAt: "2026-09-10", releases: [release("v0.5.61", "2026-09-10"), release("v0.5.60", "2026-09-01"), release("v0.5.59", "2026-08-20")] });

    await mountDialog(0, {}, undefined, { updateInfo: upToDateInfo, updateCheckMessage: "DBX is up to date (0.5.60)." });
    await flushDialog();

    // 默认折叠:即使没有可用更新也先收起,且列表只保留低于当前版本的版本
    const region = historyRegion();
    expect(region?.textContent).toContain("Older versions");
    expect(region?.textContent).not.toContain("v0.5.59");

    historyButton("Older versions")?.click();
    await flushDialog();

    expect(region?.textContent).toContain("Back up your data before rolling back");
    expect(region?.textContent).toContain("v0.5.59");
    expect(region?.textContent).not.toContain("v0.5.61");

    historyButton("Open Release")?.click();
    await flushDialog();

    expect(updateMocks.openExternal).toHaveBeenCalledWith("https://github.com/t8y2/dbx/releases/tag/v0.5.59");
  });

  it("keeps the history collapsed until the user expands it", async () => {
    updateMocks.fetchChangelog.mockResolvedValue({ updatedAt: "2026-09-10", releases: [release("v0.5.61", "2026-09-10"), release("v0.5.59", "2026-08-20")] });

    await mountDialog(0);
    await flushDialog();

    expect(historyRegion()?.textContent).toContain("Older versions");
    expect(historyRegion()?.textContent).not.toContain("v0.5.59");

    historyButton("Older versions")?.click();
    await flushDialog();

    expect(historyRegion()?.textContent).toContain("v0.5.59");
  });

  it("starts collapsed again after the dialog is reopened", async () => {
    updateMocks.fetchChangelog.mockResolvedValue({ updatedAt: "2026-09-10", releases: [release("v0.5.61", "2026-09-10"), release("v0.5.59", "2026-08-20")] });

    const { state } = await mountDialog(0, {}, undefined, { updateInfo: upToDateInfo, updateCheckMessage: "DBX is up to date (0.5.60)." });
    await flushDialog();

    historyButton("Older versions")?.click();
    await flushDialog();
    expect(historyRegion()?.textContent).toContain("v0.5.59");

    state.open = false;
    await flushDialog();
    state.open = true;
    await flushDialog();

    expect(historyRegion()?.textContent).toContain("Older versions");
    expect(historyRegion()?.textContent).not.toContain("v0.5.59");
  });

  it("hides the history section when no release is older than the current build", async () => {
    updateMocks.fetchChangelog.mockResolvedValue({ updatedAt: "2026-09-10", releases: [release("v0.5.61", "2026-09-10"), release("v0.5.60", "2026-09-01")] });

    await mountDialog(0);
    await flushDialog();

    expect(historyRegion()).toBeNull();
  });

  it("surfaces a changelog load failure instead of hiding it silently", async () => {
    updateMocks.fetchChangelog.mockRejectedValue(new Error("offline"));

    await mountDialog(0, {}, undefined, { updateInfo: upToDateInfo, updateCheckMessage: "DBX is up to date (0.5.60)." });
    await flushDialog();

    expect(historyRegion()?.textContent).toContain("Older versions");

    historyButton("Older versions")?.click();
    await flushDialog();

    expect(historyRegion()?.textContent).toContain("Failed to load older versions");
  });
});

describe("UpdateDialog aggregate update center", () => {
  it("keeps only the client tab when nothing is updatable", async () => {
    await mountDialog(0, {}, undefined, {
      updateInfo: {
        current_version: "0.5.60",
        latest_version: "0.5.60",
        update_available: false,
        portable_mode: false,
        manual_update_only: false,
        release_name: "",
        release_url: "",
        release_notes: "",
      },
      updateCheckMessage: "DBX is up to date (0.5.60).",
    });

    // 客户端页签常驻(承载回退入口),但没有可更新项时不再出现其他页签
    expect(document.body.querySelector('[data-update-tab="app"]')).not.toBeNull();
    expect(document.body.querySelector('[data-update-tab="drivers"]')).toBeNull();
    expect(document.body.textContent).toContain("DBX is up to date (0.5.60).");
  });

  it("keeps the client restart gated while update all is still updating components", async () => {
    await mountDialog(0, { updateDownloaded: true, downloadProgress: 100 }, undefined, { isUpdatingAll: true });

    expect(installDownloadedButton()?.disabled).toBe(true);
    expect(buttonWithText("Ignore this version")).toBeUndefined();
  });

  it("disables component actions while an automatic update is active", async () => {
    await mountDialog(0, {}, undefined, {
      componentUpdatesUpdating: true,
      driverUpdates: [{ db_type: "mysql", label: "MySQL", version: "9.0.0", installed_version: "8.0.0", update_available: true }],
    });

    expect(buttonWithText("Update all")?.disabled).toBe(true);

    const driversTab = document.body.querySelector<HTMLButtonElement>('[data-update-tab="drivers"]');
    driversTab?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, cancelable: true }));
    driversTab?.click();
    await flushDialog();

    expect(buttonWithText("Update Now")).toBeUndefined();
    expect(buttonWithText("Updating…")?.disabled).toBe(true);
  });

  it("allows closing the dialog while component updates continue in the background", async () => {
    const { state } = await mountDialog(0, {}, undefined, {
      updateInfo: null,
      componentUpdatesUpdating: true,
      updatingComponent: "plugins",
      pluginUpdates: [
        {
          key: "official:example",
          status: "update",
          artifact: { target: "universal", url: "https://example.com/plugin.dbxp", sha256: "hash" },
          repository: { id: "official" },
          plugin: { id: "example", latestVersion: "1.1.0" },
          name: "Example plugin",
        },
      ],
    });

    const closeButton = document.body.querySelector<HTMLButtonElement>('[data-slot="dialog-close"]');
    expect(closeButton).not.toBeNull();
    closeButton?.click();
    await flushDialog();
    expect(state.open).toBe(false);

    state.open = true;
    await flushDialog();
    await clickOutside();
    expect(state.open).toBe(false);

    state.open = true;
    await flushDialog();
    await pressEscape();

    expect(state.open).toBe(false);
  });

  it("offers an explicit background action while component updates are running", async () => {
    const { state } = await mountDialog(0, {}, undefined, {
      updateInfo: null,
      componentUpdatesUpdating: true,
      updatingComponent: "plugins",
      pluginUpdates: [
        {
          key: "official:example",
          status: "update",
          artifact: { target: "universal", url: "https://example.com/plugin.dbxp", sha256: "hash" },
          repository: { id: "official" },
          plugin: { id: "example", latestVersion: "1.1.0" },
          name: "Example plugin",
        },
      ],
    });

    const backgroundButton = buttonWithText("Run in Background");
    expect(backgroundButton).toBeDefined();
    expect(buttonWithText("Updating…")?.disabled).toBe(true);

    backgroundButton?.click();
    await flushDialog();

    expect(state.open).toBe(false);
  });

  it("organizes component updates into tabs and installs the selected category", async () => {
    const installComponentUpdates = vi.fn();
    const updateAll = vi.fn();
    await mountDialog(0, {}, undefined, {
      driverUpdates: [{ db_type: "mysql", label: "MySQL", version: "9.0.0", installed_version: "8.0.0", update_available: true }],
      jdbcUpdate: { installed: true, version: "0.1.0", latest_version: "0.2.0", update_available: true, compatible: true, path: "/tmp/jdbc" },
      mcpUpdate: { installed: true, installation_source: "npm", npm_available: true, npm_installed: true, current_version: "1.0.0", latest_version: "1.1.0", update_available: true },
      pluginUpdates: [
        {
          key: "official:example",
          status: "update",
          artifact: { target: "universal", url: "https://example.com/plugin.dbxp", sha256: "hash" },
          repository: { id: "official" },
          plugin: { id: "example", latestVersion: "1.1.0" },
          name: "Example plugin",
        },
      ],
      "onInstall-component-updates": installComponentUpdates,
      "onUpdate-all": updateAll,
    });

    expect(document.body.querySelector('[data-update-tab="app"]')).not.toBeNull();
    expect(document.body.querySelector('[data-update-tab="drivers"]')?.textContent).toContain("Database drivers");
    expect(document.body.querySelector('[data-update-tab="jdbc"]')?.textContent).toContain("JDBC");
    expect(document.body.querySelector('[data-update-tab="mcp"]')?.textContent).toContain("MCP");
    expect(document.body.querySelector('[data-update-tab="plugins"]')?.textContent).toContain("Plugins");
    expect(document.body.querySelector<HTMLElement>("[data-update-scroll-region]")?.classList.contains("overflow-auto")).toBe(true);
    expect(document.body.querySelector<HTMLElement>("[data-update-footer]")?.className).toContain("mx-0");
    expect(document.body.querySelector<HTMLElement>("[data-update-footer]")?.className).toContain("px-[22px]");
    expect(document.body.querySelector<HTMLElement>("[data-update-footer]")?.className).toContain("pb-2.5");
    expect(document.body.querySelector<HTMLElement>("[data-update-footer]")?.className).toContain("pt-2.5");
    expect(buttonWithText("Cancel")).toBeUndefined();

    buttonWithText("Update all")?.click();
    await flushDialog();
    expect(updateAll).toHaveBeenCalledOnce();

    const driversTab = document.body.querySelector<HTMLButtonElement>('[data-update-tab="drivers"]');
    driversTab?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, cancelable: true }));
    driversTab?.click();
    await flushDialog();
    buttonWithText("Update Now")?.click();
    await flushDialog();

    expect(installComponentUpdates).toHaveBeenCalledWith("drivers");
  });

  it("marks only a plugin whose update source changed as needing confirmation in the Plugin Center", async () => {
    const listing = (id: string, provenance: Record<string, string>) => ({
      key: `official:${id}`,
      status: "update",
      artifact: { target: "universal", url: "https://example.com/plugin.dbxp", sha256: "hash", signingKeyId: "key-a" },
      repository: { id: "official" },
      plugin: { id, latestVersion: "1.1.0", publisher: "DBX" },
      installed: { manifest: { id, version: "1.0.0" }, provenance },
      name: `${id} plugin`,
    });
    await mountDialog(0, {}, undefined, {
      updateInfo: null,
      pluginUpdates: [listing("same", { repositoryId: "official" }), listing("moved", { repositoryId: "other-repo" })],
    });

    const pluginsTab = document.body.querySelector<HTMLButtonElement>('[data-update-tab="plugins"]');
    pluginsTab?.click();
    await flushDialog();

    // "Update all" skips the changed-source plugin, so the dialog has to explain why it stays.
    const hint = "Confirm the change in the Plugin Center first";
    const rows = [...document.body.querySelectorAll<HTMLElement>("[data-update-entry]")];
    const rowFor = (name: string) => rows.find((row) => row.textContent?.includes(name));
    expect(rowFor("moved plugin")?.textContent).toContain(hint);
    expect(rowFor("same plugin")?.textContent).not.toContain(hint);
  });

  it("renders driver install progress circle and batch counter during driver update", async () => {
    const driver = {
      db_type: "mysql",
      label: "MySQL",
      version: "0.1.50",
      installed: true,
      installed_version: "0.1.40",
      update_available: true,
    };

    await mountDialog(0, {}, undefined, {
      updateInfo: null,
      driverUpdates: [driver],
      componentUpdatesUpdating: true,
      updatingComponent: "drivers",
      driverBatchCompleted: 1,
      driverBatchTotal: 3,
      driverProgressMap: {
        mysql: {
          step: "driver",
          db_type: "mysql",
          downloaded: 45 * 1024 * 1024,
          total: 100 * 1024 * 1024,
        },
      },
    });

    const driversTab = document.body.querySelector<HTMLButtonElement>('[data-update-tab="drivers"]');
    driversTab?.click();
    await flushDialog();

    // Driver row shows progress circle with percentage 45 and status role
    const progressStatus = document.body.querySelector('[role="status"]');
    expect(progressStatus).not.toBeNull();
    expect(progressStatus?.textContent).toContain("45");
    expect(progressStatus?.getAttribute("title")).toContain("45%");

    // Footer button shows batch progress Upgrading (1/3) or Updating (1/3)
    const footer = document.body.querySelector("[data-update-footer]");
    expect(footer?.textContent).toMatch(/1\/3/);
  });
});