feat(config): support optional plaintext connection exports

This commit is contained in:
jearhe authored and GitHub committed 2026-08-22 19:19:10 +08:00
1 parent 904efff6b3
commit 3118e0d617
19 files changed
+546 -49

No files matched your search

@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, type App } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
import type { ConnectionConfig } from "@/types/database";
@@ -35,6 +35,11 @@ import { useDialogSources } from "@/composables/useDialogSources";
const mountedApps: App[] = [];
beforeEach(() => {
mocks.store.connections = [];
mocks.store.exportConnectionsToFile.mockReset().mockResolvedValue("saved");
});
afterEach(() => {
for (const app of mountedApps.splice(0)) app.unmount();
vi.clearAllMocks();
@@ -84,4 +89,93 @@ describe("useDialogSources", () => {
await applyPromise;
await nextTick();
});
it("requires confirmation before an unencrypted export and preserves the flow when cancelled", async () => {
mocks.store.connections = [conn("selected")];
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
expect(dialogs.showConfigPassphraseDialog.value).toBe(true);
dialogs.onRequestUnencryptedExport();
expect(dialogs.showConfigPassphraseDialog.value).toBe(false);
expect(dialogs.showConfigUnencryptedExportConfirm.value).toBe(true);
expect(mocks.store.exportConnectionsToFile).not.toHaveBeenCalled();
dialogs.onConfigUnencryptedExportOpenChange(false);
expect(dialogs.showConfigUnencryptedExportConfirm.value).toBe(false);
expect(dialogs.showConfigPassphraseDialog.value).toBe(true);
expect(mocks.store.exportConnectionsToFile).not.toHaveBeenCalled();
dialogs.onRequestUnencryptedExport();
await dialogs.onConfigUnencryptedExportConfirm();
expect(mocks.store.exportConnectionsToFile).toHaveBeenCalledWith({ mode: "plaintext" }, ["selected"]);
});
it("keeps encrypted export state when the native save dialog is cancelled", async () => {
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockResolvedValueOnce("cancelled").mockResolvedValueOnce("saved");
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
await dialogs.onExportConfirm("passphrase");
expect(dialogs.showConfigPassphraseDialog.value).toBe(true);
expect(mocks.toast).not.toHaveBeenCalled();
await dialogs.onExportConfirm("passphrase");
expect(mocks.store.exportConnectionsToFile.mock.calls).toEqual([
[{ mode: "encrypted", passphrase: "passphrase" }, ["selected"]],
[{ mode: "encrypted", passphrase: "passphrase" }, ["selected"]],
]);
expect(dialogs.showConfigPassphraseDialog.value).toBe(false);
expect(mocks.toast).toHaveBeenCalledOnce();
});
it("keeps plaintext export state when the native save dialog is cancelled", async () => {
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockResolvedValueOnce("cancelled").mockResolvedValueOnce("saved");
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
dialogs.onRequestUnencryptedExport();
await dialogs.onConfigUnencryptedExportConfirm();
expect(dialogs.showConfigUnencryptedExportConfirm.value).toBe(true);
expect(mocks.toast).not.toHaveBeenCalled();
await dialogs.onConfigUnencryptedExportConfirm();
expect(mocks.store.exportConnectionsToFile.mock.calls).toEqual([
[{ mode: "plaintext" }, ["selected"]],
[{ mode: "plaintext" }, ["selected"]],
]);
expect(dialogs.showConfigUnencryptedExportConfirm.value).toBe(false);
expect(mocks.toast).toHaveBeenCalledOnce();
});
it("prevents duplicate unencrypted exports while the file is being written", async () => {
let resolveExport!: () => void;
const exportPromise = new Promise<void>((resolve) => {
resolveExport = resolve;
});
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockReturnValue(exportPromise);
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
dialogs.onRequestUnencryptedExport();
const first = dialogs.onConfigUnencryptedExportConfirm();
const second = dialogs.onConfigUnencryptedExportConfirm();
expect(dialogs.configExportBusy.value).toBe(true);
expect(mocks.store.exportConnectionsToFile).toHaveBeenCalledTimes(1);
resolveExport();
await first;
await second;
expect(dialogs.configExportBusy.value).toBe(false);
});
});
@@ -22,6 +22,8 @@ const pendingImportLayout = ref<SidebarLayout | null>(null);
const showConfigPassphraseDialog = ref(false);
const configPassphraseMode = ref<"export" | "import">("export");
const configPassphraseError = ref("");
const showConfigUnencryptedExportConfirm = ref(false);
const configExportBusy = ref(false);
const pendingImportContent = ref("");
const showConfigConnectionSelectDialog = ref(false);
const configConnectionSelectMode = ref<"export" | "import">("export");
@@ -330,16 +332,57 @@ export function useDialogSources() {
}
async function onExportConfirm(passphrase: string) {
if (configExportBusy.value) return;
configExportBusy.value = true;
try {
await connectionStore.exportConnectionsToFile(passphrase, pendingExportConnectionIds.value);
const result = await connectionStore.exportConnectionsToFile({ mode: "encrypted", passphrase }, pendingExportConnectionIds.value);
if (result === "cancelled") return;
showConfigPassphraseDialog.value = false;
clearPendingExportState();
toast(t("configExport.exportSuccess"), 2000);
} catch (e: any) {
configPassphraseError.value = e?.message === "crypto_unavailable" ? t("configExport.cryptoUnavailable") : e?.message || String(e);
configPassphraseError.value = e?.message === "crypto_unavailable" ? t("configExport.cryptoUnavailable") : e?.message === "passphrase_required" ? t("configExport.passphraseRequired") : t("configExport.exportFailed");
} finally {
configExportBusy.value = false;
}
}
function onRequestUnencryptedExport() {
if (configExportBusy.value) return;
showConfigPassphraseDialog.value = false;
showConfigUnencryptedExportConfirm.value = true;
}
async function onConfigUnencryptedExportConfirm() {
if (configExportBusy.value) return;
configExportBusy.value = true;
try {
const result = await connectionStore.exportConnectionsToFile({ mode: "plaintext" }, pendingExportConnectionIds.value);
if (result === "cancelled") return;
showConfigUnencryptedExportConfirm.value = false;
clearPendingExportState();
toast(t("configExport.exportSuccess"), 2000);
} catch {
showConfigUnencryptedExportConfirm.value = false;
configPassphraseError.value = t("configExport.exportFailed");
showConfigPassphraseDialog.value = true;
} finally {
configExportBusy.value = false;
}
}
function onConfigUnencryptedExportCancel() {
if (configExportBusy.value) return;
showConfigUnencryptedExportConfirm.value = false;
showConfigPassphraseDialog.value = true;
}
function onConfigUnencryptedExportOpenChange(open: boolean) {
if (configExportBusy.value) return;
showConfigUnencryptedExportConfirm.value = open;
if (!open) showConfigPassphraseDialog.value = true;
}
async function onImportClick(source: "dbx" | "navicat" | "dbeaver" | "datagrip" = "dbx") {
try {
const result = await connectionStore.readImportFile(source);
@@ -414,8 +457,9 @@ export function useDialogSources() {
function onConfigPassphraseOpenChange(open: boolean) {
showConfigPassphraseDialog.value = open;
if (open) return;
if (configPassphraseMode.value === "export") clearPendingExportState();
else if (!pendingImportPreview.value) clearPendingImportState();
if (configPassphraseMode.value === "export") {
if (!showConfigUnencryptedExportConfirm.value) clearPendingExportState();
} else if (!pendingImportPreview.value) clearPendingImportState();
}
return {
@@ -435,6 +479,8 @@ export function useDialogSources() {
showConfigPassphraseDialog,
configPassphraseMode,
configPassphraseError,
showConfigUnencryptedExportConfirm,
configExportBusy,
pendingImportContent,
showConfigConnectionSelectDialog,
applyingImportSelection,
@@ -489,6 +535,10 @@ export function useDialogSources() {
databaseExportAllDatabases,
onExportClick,
onExportConfirm,
onRequestUnencryptedExport,
onConfigUnencryptedExportConfirm,
onConfigUnencryptedExportCancel,
onConfigUnencryptedExportOpenChange,
onImportClick,
onImportConfirm,
onConfigConnectionSelectConfirm,