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.7 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
  EDITOR_SETTINGS_DRAFT_KEYS,
  editorSettingsDraftFromSettings,
  editorSettingsDraftChanged,
  editorSettingsDraftPatchFromSettings,
  editorSettingsPatchFromDraft,
  normalizeQueryResultMaxRowsDraft,
  normalizeTableOpenPageSizeDraft,
  shouldConfirmEditorSettingsDialogClose,
} from "../editorSettingsDraft";
import type { EditorSettings } from "@/stores/settingsStore";

function makeSettings(overrides: Partial<EditorSettings> = {}): EditorSettings {
  return {
    autoCalculateTotalRows: false,
    pageSize: 100,
    tableOpenPageSize: 100,
    queryResultMaxRowsEnabled: true,
    queryResultMaxRows: 100000,
    sqlEngine: "desktop",
    tabSize: 2,
    keywordCase: "upper",
    indentStyle: "standard",
    lineWidth: 80,
    commaPosition: "end",
    editorFontFamily: "",
    editorFontSize: 0,
    editorLineHeight: 0,
    maxRowsPerPage: 50000,
    showHiddenFiles: false,
    confirmDangerousSqlExecution: true,
    continueOnErrorOnBatch: false,
    confirmUnsavedSqlClose: true,
    savedSqlOpenTargetMode: "saved",
    ddlOpenMode: "dialog",
    objectBrowserViewMode: "list",
    sqlVariableSubstitutionEnabled: true,
    sqlVariableSyntaxOverrides: {},
    tabLayout: "scroll",
    ...overrides,
  };
}

describe("EDITOR_SETTINGS_DRAFT_KEYS", () => {
  it("tracks the result-tab click setting in the draft without changing saved settings", () => {
    const settings = makeSettings({ pinResultTabOnClick: true });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.pinResultTabOnClick = false;
    expect(settings.pinResultTabOnClick).toBe(true);
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
    expect(editorSettingsPatchFromDraft(draft, base)).toMatchObject({ pinResultTabOnClick: false });
  });

  it("captures and saves the column comment placement preference", () => {
    const base = editorSettingsDraftFromSettings(makeSettings({ dataGridColumnCommentsInInfoArea: false, dataGridTransposeMetadataInInfoColumn: false }));
    const draft = editorSettingsDraftFromSettings(makeSettings({ dataGridColumnCommentsInInfoArea: true, dataGridTransposeMetadataInInfoColumn: true }));
    expect(draft.dataGridColumnCommentsInInfoArea).toBe(true);
    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ dataGridColumnCommentsInInfoArea: true, dataGridTransposeMetadataInInfoColumn: true });
  });
  it("exposes the global connect/query timeouts in the settings draft (#13233)", () => {
    // 设置页现在直接展示并允许调整全局超时(issue #13233),草稿键必须包含这两个字段,
    // 且落盘口径与连接编辑对话框一致:连接超时 1~300 秒,查询超时 0~3600 秒(0=不限)。
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("globalConnectTimeoutSecs");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("globalQueryTimeoutSecs");
    const settings = makeSettings({ globalConnectTimeoutSecs: 10, globalQueryTimeoutSecs: 60 } as Partial<EditorSettings>);
    const draft = editorSettingsDraftFromSettings(settings);
    draft.globalConnectTimeoutSecs = 9999;
    draft.globalQueryTimeoutSecs = 0;
    expect(editorSettingsPatchFromDraft(draft, editorSettingsDraftFromSettings(settings))).toEqual({ globalConnectTimeoutSecs: 300, globalQueryTimeoutSecs: 0 });
  });

  it("includes showLineNumbers", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("showLineNumbers");
  });

  it("includes the DDL open mode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("ddlOpenMode");
  });

  it("includes caretBrowsingEnabled (#11827)", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("caretBrowsingEnabled");
    const draft = editorSettingsDraftFromSettings(makeSettings({ caretBrowsingEnabled: true } as Partial<EditorSettings>));
    expect(draft.caretBrowsingEnabled).toBe(true);
  });

  it("includes queryResultExportFileNameSource (#12092)", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("queryResultExportFileNameSource");
    const draft = editorSettingsDraftFromSettings(makeSettings({ queryResultExportFileNameSource: "tab-title" } as Partial<EditorSettings>));
    expect(draft.queryResultExportFileNameSource).toBe("tab-title");
  });

  it("includes colorizeConnectionTabsScope (#12955)", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("colorizeConnectionTabsScope");
    const draft = editorSettingsDraftFromSettings(makeSettings({ colorizeConnectionTabsScope: "active" } as Partial<EditorSettings>));
    expect(draft.colorizeConnectionTabsScope).toBe("active");
  });

  it("includes continueOnErrorOnBatch", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("continueOnErrorOnBatch");
  });

  it("includes the table-open page size", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("pageSize");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tableOpenPageSize");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("queryResultMaxRowsEnabled");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("queryResultMaxRows");
  });

  it("includes the saved SQL open target mode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("savedSqlOpenTargetMode");
  });

  it("includes aiSqlApplyTargetMode (#12438)", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("aiSqlApplyTargetMode");
    const draft = editorSettingsDraftFromSettings(makeSettings({ aiSqlApplyTargetMode: "new" } as Partial<EditorSettings>));
    expect(draft.aiSqlApplyTargetMode).toBe("new");
  });

  it("includes the privacy-safe welcome page mode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("welcomePageMode");
    expect(editorSettingsDraftFromSettings(makeSettings({ welcomePageMode: "intro" })).welcomePageMode).toBe("intro");
    expect(editorSettingsDraftFromSettings(makeSettings({ welcomePageMode: "workspace" })).welcomePageMode).toBe("workspace");
  });

  it("includes the regular expression match limit", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("regexMaxMatchCount");
  });

  it("includes the data-tab reuse mode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataTabReuseMode");
  });

  it("includes the data grid toolbar layout", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridToolbarLayout");

    const draft = editorSettingsDraftFromSettings(makeSettings({ dataGridToolbarLayout: "single" }));
    expect(draft.dataGridToolbarLayout).toBe("single");
  });

  it("includes generated SQL identifier quote preference", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("generateSqlQuoteIdentifiers");
  });

  it("includes adjacent data-tab opening", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("openDataTabsNextToActive");
  });

  it("includes snippet trigger key setting in draft keys and draft conversion", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("snippetTriggerKey");
    const draft = editorSettingsDraftFromSettings(makeSettings({ snippetTriggerKey: "space" }));
    expect(draft.snippetTriggerKey).toBe("space");
  });

  it("includes data grid type colors", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("colorizeDataGridCellTypes");
  });

  it("includes the type color scheme in the apply-footer draft", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridTypeColorSchemes");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("activeDataGridTypeColorSchemeId");

    const base = editorSettingsDraftFromSettings(makeSettings({ dataGridTypeColorSchemes: [], activeDataGridTypeColorSchemeId: "auto" }));
    const withScheme = editorSettingsDraftFromSettings(
      makeSettings({
        dataGridTypeColorSchemes: [{ id: "type-colors-1", name: "配色方案 1", colors: { integer: "#254fce", numeric: "#0e7490", string: "#fdc9c9", boolean: "#100cc2", temporal: "#7e22ce", structured: "#be185d", identifier: "#92400e", binary: "#b91c1c", spatial: "#047857" } }],
        activeDataGridTypeColorSchemeId: "type-colors-1",
      }),
    );

    expect(editorSettingsDraftChanged(withScheme, base)).toBe(true);
    expect(editorSettingsPatchFromDraft(withScheme, base)).toEqual({
      dataGridTypeColorSchemes: [{ id: "type-colors-1", name: "配色方案 1", colors: { integer: "#254fce", numeric: "#0e7490", string: "#fdc9c9", boolean: "#100cc2", temporal: "#7e22ce", structured: "#be185d", identifier: "#92400e", binary: "#b91c1c", spatial: "#047857" } }],
      activeDataGridTypeColorSchemeId: "type-colors-1",
    });
  });

  it("includes the data grid filter view", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridFilterEditorView");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridToolbarLayout");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridKeepFilterEditorExpanded");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridTextFilterPanelHeight");
  });

  it("includes the multi-statement default view", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("multiStatementDefaultView");
  });

  it("includes the default explain view", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("defaultExplainView");
  });

  it("includes the cell detail button visibility", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCellDetailButtonVisible");
  });

  it("includes zebra row background in draft keys", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridZebraRowBg");
  });

  it("includes crosshair row and column backgrounds in draft keys", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCrosshairRowBg");
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCrosshairColBg");
  });

  it("includes dataGridCellDetailDialogDefault", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCellDetailDialogDefault");
  });

  it("includes completionTriggerMode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("completionTriggerMode");
  });

  it("includes tableCompletionSchemaQualification", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tableCompletionSchemaQualification");
  });

  it("includes tableHoverLookupMode", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tableHoverLookupMode");
  });

  it("includes the SQL variable substitution master switch", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("sqlVariableSubstitutionEnabled");
  });

  it("includes functionCompletionIncludeParams in draft keys, draft conversion, and patch", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("functionCompletionIncludeParams");
    const draft = editorSettingsDraftFromSettings(makeSettings({ functionCompletionIncludeParams: false }));
    const base = editorSettingsDraftFromSettings(makeSettings({ functionCompletionIncludeParams: true }));
    expect(draft.functionCompletionIncludeParams).toBe(false);
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ functionCompletionIncludeParams: false });
  });

  it("includes sequenceCompletionEnabled in draft keys, draft conversion, and patch", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("sequenceCompletionEnabled");
    const draft = editorSettingsDraftFromSettings(makeSettings({ sequenceCompletionEnabled: false }));
    const base = editorSettingsDraftFromSettings(makeSettings({ sequenceCompletionEnabled: true }));
    expect(draft.sequenceCompletionEnabled).toBe(false);
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ sequenceCompletionEnabled: false });
  });
});

describe("editorSettingsDraftFromSettings", () => {
  it("round-trips a hidden line-number preference through the draft patch", () => {
    const settings = makeSettings({ showLineNumbers: true });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);

    draft.showLineNumbers = false;

    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ showLineNumbers: false });
  });

  it("toggles substitution without discarding per-database overrides", () => {
    const base = editorSettingsDraftFromSettings(
      makeSettings({
        sqlVariableSubstitutionEnabled: true,
        sqlVariableSyntaxOverrides: { mysql: { shell: false } },
      }),
    );
    const draft = editorSettingsDraftFromSettings(
      makeSettings({
        sqlVariableSubstitutionEnabled: true,
        sqlVariableSyntaxOverrides: { mysql: { shell: false } },
      }),
    );

    draft.sqlVariableSubstitutionEnabled = false;

    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ sqlVariableSubstitutionEnabled: false });
    expect(draft.sqlVariableSyntaxOverrides).toEqual({ mysql: { shell: false } });
    expect(base.sqlVariableSyntaxOverrides).toEqual({ mysql: { shell: false } });
  });

  it("includes persisted global timeout values in editor drafts (#13233)", () => {
    // 设置页直接展示全局超时后(issue #13233),草稿必须携带落盘值且走同一 normalize 口径;
    // 未修改时不应产生这两个字段的 patch,避免 Apply 误覆盖。
    const settings = makeSettings({ globalConnectTimeoutSecs: 17, globalQueryTimeoutSecs: 43 });
    const draft = editorSettingsDraftFromSettings(settings);

    expect(draft.globalConnectTimeoutSecs).toBe(17);
    expect(draft.globalQueryTimeoutSecs).toBe(43);
    expect(editorSettingsPatchFromDraft(draft, draft)).not.toHaveProperty("globalConnectTimeoutSecs");
    expect(editorSettingsPatchFromDraft(draft, draft)).not.toHaveProperty("globalQueryTimeoutSecs");
  });

  it("maps continueOnErrorOnBatch from settings", () => {
    const draft = editorSettingsDraftFromSettings(makeSettings({ continueOnErrorOnBatch: true }));
    expect(draft.continueOnErrorOnBatch).toBe(true);
  });

  it("maps continueOnErrorOnBatch=false from settings", () => {
    const draft = editorSettingsDraftFromSettings(makeSettings({ continueOnErrorOnBatch: false }));
    expect(draft.continueOnErrorOnBatch).toBe(false);
  });

  it("maps the data grid type color preference from settings", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ colorizeDataGridCellTypes: false })).colorizeDataGridCellTypes).toBe(false);
  });

  it("maps the data grid filter view and persistent expansion from settings", () => {
    const draft = editorSettingsDraftFromSettings(makeSettings({ dataGridFilterEditorView: "text", dataGridKeepFilterEditorExpanded: true, dataGridTextFilterPanelHeight: 224 }));
    expect(draft.dataGridFilterEditorView).toBe("text");
    expect(draft.dataGridKeepFilterEditorExpanded).toBe(true);
    expect(draft.dataGridTextFilterPanelHeight).toBe(224);
  });

  it("maps the multi-statement default view from settings", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ multiStatementDefaultView: "summary" })).multiStatementDefaultView).toBe("summary");
  });

  it("maps the default explain view from settings", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ defaultExplainView: "table" })).defaultExplainView).toBe("table");
  });

  it("preserves the table-open default for legacy settings", () => {
    const settings = makeSettings();
    delete (settings as Partial<EditorSettings>).tableOpenPageSize;
    expect(editorSettingsDraftFromSettings(settings).tableOpenPageSize).toBe(100);
  });

  it("maps the saved SQL open target mode", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ savedSqlOpenTargetMode: "current" })).savedSqlOpenTargetMode).toBe("current");
  });

  it("maps completionTriggerMode from settings", () => {
    const draft = editorSettingsDraftFromSettings(makeSettings({ completionTriggerMode: "require-prefix" } as Partial<EditorSettings>));
    expect(draft.completionTriggerMode).toBe("require-prefix");
  });

  it("normalizes invalid completionTriggerMode to positional", () => {
    const draft = editorSettingsDraftFromSettings(makeSettings({ completionTriggerMode: "always" as unknown } as Partial<EditorSettings>));
    expect(draft.completionTriggerMode).toBe("positional");
  });

  it("maps csvNullMode and normalizes an unknown value back to the marker", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ csvNullMode: "empty" })).csvNullMode).toBe("empty");
    expect(editorSettingsDraftFromSettings(makeSettings({ csvNullMode: "bogus" as unknown } as Partial<EditorSettings>)).csvNullMode).toBe("marker");
  });

  it("maps and normalizes tableCompletionSchemaQualification", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ tableCompletionSchemaQualification: "always" })).tableCompletionSchemaQualification).toBe("always");
    expect(editorSettingsDraftFromSettings(makeSettings({ tableCompletionSchemaQualification: "invalid" } as any)).tableCompletionSchemaQualification).toBe("collision");
  });
});

describe("normalizeTableOpenPageSizeDraft", () => {
  it.each([
    [200000, 200000],
    [2000000, 1000000],
    [0, 100],
    [-1, 100],
    ["123.9", 123],
    [Number.NaN, 100],
    [Number.POSITIVE_INFINITY, 100],
    ["not-a-number", 100],
    [500, 500],
  ])("normalizes %s to %s", (value, expected) => {
    expect(normalizeTableOpenPageSizeDraft(value)).toBe(expected);
  });
});

describe("normalizeQueryResultMaxRowsDraft", () => {
  it.each([
    [250000, 250000],
    [0, 1],
    [2147483648, 2147483647],
    [Number.NaN, 100000],
  ])("normalizes %s to %s", (value, expected) => {
    expect(normalizeQueryResultMaxRowsDraft(value)).toBe(expected);
  });
});

describe("editorSettingsDraftChanged", () => {
  it("detects change in continueOnErrorOnBatch", () => {
    const settings = makeSettings({ continueOnErrorOnBatch: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.continueOnErrorOnBatch = true;
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects no change when continueOnErrorOnBatch matches", () => {
    const settings = makeSettings({ continueOnErrorOnBatch: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    expect(editorSettingsDraftChanged(draft, base)).toBe(false);
  });

  it("compares the normalized table-open page size", () => {
    const settings = makeSettings({ tableOpenPageSize: 100 });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tableOpenPageSize = Number.NaN;
    expect(editorSettingsDraftChanged(draft, base)).toBe(false);
  });

  it("detects a saved SQL open target change", () => {
    const settings = makeSettings({ savedSqlOpenTargetMode: "saved" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.savedSqlOpenTargetMode = "current";
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects a data-tab reuse mode change", () => {
    const settings = makeSettings({ dataTabReuseMode: "same-table" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.dataTabReuseMode = "active-tab";
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects adjacent data-tab opening changes", () => {
    const settings = makeSettings({ openDataTabsNextToActive: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.openDataTabsNextToActive = true;
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects completionTriggerMode change", () => {
    const settings = makeSettings({ completionTriggerMode: "positional" } as Partial<EditorSettings>);
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.completionTriggerMode = "manual";
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects no change when completionTriggerMode matches", () => {
    const settings = makeSettings({ completionTriggerMode: "require-prefix" } as Partial<EditorSettings>);
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    expect(editorSettingsDraftChanged(draft, base)).toBe(false);
  });
});

describe("editorSettingsPatchFromDraft", () => {
  it("applies, cancels, and re-enables the cell detail button visibility", () => {
    const visible = editorSettingsDraftFromSettings(makeSettings({ dataGridCellDetailButtonVisible: true }));
    const hidden = editorSettingsDraftFromSettings(makeSettings({ dataGridCellDetailButtonVisible: false }));

    expect(editorSettingsPatchFromDraft(hidden, visible)).toEqual({ dataGridCellDetailButtonVisible: false });
    expect(editorSettingsPatchFromDraft(visible, visible)).toEqual({});
    expect(editorSettingsPatchFromDraft(visible, hidden)).toEqual({ dataGridCellDetailButtonVisible: true });
  });

  it("applies, cancels, and re-enables cell detail dialog default", () => {
    const enabled = editorSettingsDraftFromSettings(makeSettings({ dataGridCellDetailDialogDefault: true }));
    const disabled = editorSettingsDraftFromSettings(makeSettings({ dataGridCellDetailDialogDefault: false }));

    expect(editorSettingsPatchFromDraft(enabled, disabled)).toEqual({ dataGridCellDetailDialogDefault: true });
    expect(editorSettingsPatchFromDraft(enabled, enabled)).toEqual({});
    expect(editorSettingsPatchFromDraft(disabled, enabled)).toEqual({ dataGridCellDetailDialogDefault: false });
  });

  it("includes the multi-statement default view when changed", () => {
    const result = editorSettingsDraftFromSettings(makeSettings({ multiStatementDefaultView: "result" }));
    const summary = editorSettingsDraftFromSettings(makeSettings({ multiStatementDefaultView: "summary" }));

    expect(editorSettingsPatchFromDraft(summary, result)).toEqual({ multiStatementDefaultView: "summary" });
  });

  it("includes the default explain view when changed", () => {
    const canvas = editorSettingsDraftFromSettings(makeSettings({ defaultExplainView: "canvas" }));
    const table = editorSettingsDraftFromSettings(makeSettings({ defaultExplainView: "table" }));

    expect(editorSettingsPatchFromDraft(table, canvas)).toEqual({ defaultExplainView: "table" });
  });

  it("includes continueOnErrorOnBatch in patch when changed", () => {
    const settings = makeSettings({ continueOnErrorOnBatch: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.continueOnErrorOnBatch = true;
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.continueOnErrorOnBatch).toBe(true);
  });

  it("omits continueOnErrorOnBatch when unchanged", () => {
    const settings = makeSettings({ continueOnErrorOnBatch: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.continueOnErrorOnBatch).toBeUndefined();
  });

  it("writes the normalized table-open page size", () => {
    const settings = makeSettings({ tableOpenPageSize: 100 });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tableOpenPageSize = 200000.9;
    expect(editorSettingsPatchFromDraft(draft, base).tableOpenPageSize).toBe(200000);
  });

  it("includes the saved SQL open target when changed", () => {
    const settings = makeSettings({ savedSqlOpenTargetMode: "saved" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.savedSqlOpenTargetMode = "current";
    expect(editorSettingsPatchFromDraft(draft, base).savedSqlOpenTargetMode).toBe("current");
  });

  it("includes the data-tab reuse mode when changed", () => {
    const settings = makeSettings({ dataTabReuseMode: "same-table" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.dataTabReuseMode = "always-new";
    expect(editorSettingsPatchFromDraft(draft, base).dataTabReuseMode).toBe("always-new");
  });

  it("includes adjacent data-tab opening when changed", () => {
    const settings = makeSettings({ openDataTabsNextToActive: false });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.openDataTabsNextToActive = true;
    expect(editorSettingsPatchFromDraft(draft, base).openDataTabsNextToActive).toBe(true);
  });

  it("includes completionTriggerMode in patch when changed", () => {
    const settings = makeSettings({ completionTriggerMode: "positional" } as Partial<EditorSettings>);
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.completionTriggerMode = "manual";
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.completionTriggerMode).toBe("manual");
  });

  it("omits completionTriggerMode when unchanged", () => {
    const settings = makeSettings({ completionTriggerMode: "require-prefix" } as Partial<EditorSettings>);
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.completionTriggerMode).toBeUndefined();
  });

  it("includes tableCompletionSchemaQualification only when changed", () => {
    const settings = makeSettings({ tableCompletionSchemaQualification: "collision" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);

    expect(editorSettingsPatchFromDraft(draft, base).tableCompletionSchemaQualification).toBeUndefined();
    draft.tableCompletionSchemaQualification = "never";
    expect(editorSettingsPatchFromDraft(draft, base).tableCompletionSchemaQualification).toBe("never");
  });
});

describe("EDITOR_SETTINGS_DRAFT_KEYS - tabLayout", () => {
  it("includes tabLayout", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tabLayout");
  });
});

describe("editorSettingsDraftFromSettings - tabLayout", () => {
  it("maps tabLayout from settings", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ tabLayout: "wrap" })).tabLayout).toBe("wrap");
    expect(editorSettingsDraftFromSettings(makeSettings({ tabLayout: "scroll" })).tabLayout).toBe("scroll");
  });
});

describe("editorSettingsDraftFromSettings - ddlOpenMode", () => {
  it("maps and tracks the selected DDL open mode", () => {
    const settings = makeSettings({ ddlOpenMode: "dialog" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);

    expect(draft.ddlOpenMode).toBe("dialog");
    draft.ddlOpenMode = "tab";
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
    expect(editorSettingsPatchFromDraft(draft, base)).toEqual({ ddlOpenMode: "tab" });
  });
});

describe("editorSettingsDraftChanged - tabLayout", () => {
  it("detects change in tabLayout", () => {
    const settings = makeSettings({ tabLayout: "scroll" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tabLayout = "wrap";
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects no change when tabLayout matches", () => {
    const settings = makeSettings({ tabLayout: "wrap" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    expect(editorSettingsDraftChanged(draft, base)).toBe(false);
  });
});

describe("shouldConfirmEditorSettingsDialogClose", () => {
  // Regression for https://github.com/t8y2/dbx/issues/5905: customizing a
  // shortcut or the sidebar activation mode and then dismissing the dialog
  // via Escape/outside-click/the "Close" button (anything other than Apply)
  // must not silently drop the draft.
  it("requests confirmation when the dialog is closing with an unsaved shortcut/sidebarActivation edit", () => {
    const settings = makeSettings({ sidebarActivation: "single" } as Partial<EditorSettings>);
    const base = editorSettingsDraftFromSettings(settings);
    const draft = editorSettingsDraftFromSettings(settings);
    draft.sidebarActivation = "double";

    expect(shouldConfirmEditorSettingsDialogClose(false, editorSettingsDraftChanged(draft, base))).toBe(true);
  });

  it("does not block closing when there is no unsaved draft", () => {
    const settings = makeSettings();
    const base = editorSettingsDraftFromSettings(settings);
    const draft = editorSettingsDraftFromSettings(settings);

    expect(shouldConfirmEditorSettingsDialogClose(false, editorSettingsDraftChanged(draft, base))).toBe(false);
  });

  it("never blocks opening the dialog, even with a stale dirty flag", () => {
    expect(shouldConfirmEditorSettingsDialogClose(true, true)).toBe(false);
  });
});

describe("editorSettingsPatchFromDraft - tabLayout", () => {
  it("includes tabLayout in patch when changed", () => {
    const settings = makeSettings({ tabLayout: "scroll" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tabLayout = "wrap";
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.tabLayout).toBe("wrap");
  });

  it("omits tabLayout when unchanged", () => {
    const settings = makeSettings({ tabLayout: "scroll" });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.tabLayout).toBeUndefined();
  });
});

describe("editorSettingsDraftPatchFromSettings", () => {
  it("contains exactly the keys present in the input", () => {
    const patch = editorSettingsDraftPatchFromSettings({ wordWrap: true, pageSize: 200 } as Partial<EditorSettings>);
    expect(Object.keys(patch).sort()).toEqual(["pageSize", "wordWrap"]);
    expect(patch.wordWrap).toBe(true);
    expect(patch.pageSize).toBe(200);
  });

  it("normalizes imported values per key", () => {
    const patch = editorSettingsDraftPatchFromSettings({ pageSize: 999999 } as Partial<EditorSettings>);
    expect(patch.pageSize).toBe(normalizeTableOpenPageSizeDraft(999999));
  });
});

describe("editorSettingsDraft - tabMaxWidth", () => {
  it("includes tabMaxWidth in EDITOR_SETTINGS_DRAFT_KEYS", () => {
    expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("tabMaxWidth");
  });

  it("maps tabMaxWidth from settings", () => {
    expect(editorSettingsDraftFromSettings(makeSettings({ tabMaxWidth: 240 })).tabMaxWidth).toBe(240);
    expect(editorSettingsDraftFromSettings(makeSettings({ tabMaxWidth: 0 })).tabMaxWidth).toBe(0);
  });

  it("detects change in tabMaxWidth", () => {
    const settings = makeSettings({ tabMaxWidth: 0 });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tabMaxWidth = 240;
    expect(editorSettingsDraftChanged(draft, base)).toBe(true);
  });

  it("detects no change when tabMaxWidth matches", () => {
    const settings = makeSettings({ tabMaxWidth: 240 });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    expect(editorSettingsDraftChanged(draft, base)).toBe(false);
  });

  it("includes tabMaxWidth in patch when changed", () => {
    const settings = makeSettings({ tabMaxWidth: 0 });
    const draft = editorSettingsDraftFromSettings(settings);
    const base = editorSettingsDraftFromSettings(settings);
    draft.tabMaxWidth = 240;
    const patch = editorSettingsPatchFromDraft(draft, base);
    expect(patch.tabMaxWidth).toBe(240);
  });
});