feat(export): support XLSX comment headers
This commit is contained in:
4 files changed
+311
-28
No files matched your search
+162
-2
@@ -1,19 +1,28 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { shallowRef } from "vue";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import type { ColumnInfo, TreeNode } from "@/types/database";
|
||||
|
||||
const source = readFileSync(new URL("../useSidebarTreeExportRuntime.ts", import.meta.url), "utf8");
|
||||
const toastMock = vi.hoisted(() => vi.fn());
|
||||
const addExportTaskMock = vi.hoisted(() => vi.fn());
|
||||
const updateTableExportTaskMock = vi.hoisted(() => vi.fn());
|
||||
const apiMock = vi.hoisted(() => ({
|
||||
buildTableSelectSql: vi.fn(async () => 'SELECT * FROM "main"."users" LIMIT 10000'),
|
||||
executeQuery: vi.fn(),
|
||||
exportQueryResultCsv: vi.fn(),
|
||||
exportQueryResultJson: vi.fn(),
|
||||
exportQueryResultXlsx: vi.fn(),
|
||||
getColumns: vi.fn(),
|
||||
getTableDdl: vi.fn(),
|
||||
startTableExport: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => apiMock);
|
||||
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: toastMock }) }));
|
||||
vi.mock("@/composables/useExportTracker", () => ({ useExportTracker: () => ({ addTask: vi.fn() }) }));
|
||||
vi.mock("@/composables/useExportTracker", () => ({ useExportTracker: () => ({ addTask: addExportTaskMock, updateTableExportTask: updateTableExportTaskMock }) }));
|
||||
vi.mock("@/i18n", () => ({ default: { install() {} } }));
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({
|
||||
t: (key: string, params?: Record<string, unknown>) => {
|
||||
@@ -27,9 +36,64 @@ vi.mock("vue-i18n", () => ({
|
||||
import { useSidebarTreeExportRuntime } from "@/composables/useSidebarTreeExportRuntime";
|
||||
import { showStructurePreviewDialog, structurePreviewSql, structurePreviewTitle } from "@/components/sidebar/sidebarTreeDialogState";
|
||||
|
||||
function functionBody(name: string): string {
|
||||
const signature = new RegExp(`(?:async\\s+)?function\\s+${name}\\s*\\([^)]*\\)\\s*(?::\\s*[^\\{]+)?\\{`, "m").exec(source);
|
||||
if (!signature) throw new Error(`Missing function ${name}`);
|
||||
const bodyStart = signature.index + signature[0].length;
|
||||
let depth = 1;
|
||||
for (let index = bodyStart; index < source.length; index += 1) {
|
||||
if (source[index] === "{") depth += 1;
|
||||
else if (source[index] === "}") depth -= 1;
|
||||
if (depth === 0) return source.slice(bodyStart, index);
|
||||
}
|
||||
throw new Error(`Unclosed function ${name}`);
|
||||
}
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (reason?: unknown) => void;
|
||||
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
||||
resolve = resolvePromise;
|
||||
reject = rejectPromise;
|
||||
});
|
||||
return { promise, reject, resolve };
|
||||
}
|
||||
|
||||
function column(name: string, isPrimaryKey = false): ColumnInfo {
|
||||
return {
|
||||
name,
|
||||
data_type: "text",
|
||||
is_nullable: !isPrimaryKey,
|
||||
column_default: null,
|
||||
is_primary_key: isPrimaryKey,
|
||||
extra: null,
|
||||
comment: null,
|
||||
};
|
||||
}
|
||||
|
||||
function exportSettings() {
|
||||
return {
|
||||
editorSettings: {
|
||||
exportBatchSize: 128,
|
||||
exportRowLimit: 500,
|
||||
exportRowLimitEnabled: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("useSidebarTreeExportRuntime", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
addExportTaskMock.mockImplementation((tableName: string, format: string, filePath: string) => ({
|
||||
exportId: "export-1",
|
||||
tableName,
|
||||
format,
|
||||
filePath,
|
||||
status: "Running",
|
||||
rowsExported: 0,
|
||||
totalRows: null,
|
||||
}));
|
||||
apiMock.startTableExport.mockResolvedValue({});
|
||||
showStructurePreviewDialog.value = false;
|
||||
structurePreviewSql.value = "";
|
||||
structurePreviewTitle.value = "";
|
||||
@@ -86,4 +150,100 @@ describe("useSidebarTreeExportRuntime", () => {
|
||||
expect(structurePreviewTitle.value).toBe("contextMenu.exportStructurePreviewTitleMultiple");
|
||||
expect(showStructurePreviewDialog.value).toBe(true);
|
||||
});
|
||||
|
||||
it("prompts for the header mode before it opens the save dialog when comments exist", () => {
|
||||
const exportDataXlsx = functionBody("exportDataXlsx");
|
||||
|
||||
expect(source).toContain('import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue"');
|
||||
expect(exportDataXlsx).toContain("await api.getColumns(");
|
||||
expect(exportDataXlsx).toContain("hasXlsxHeaderComments(columnInfos?.map((column) => column.comment))");
|
||||
expect(exportDataXlsx.indexOf("await showSidebarTreeXlsxHeaderDialog(")).toBeLessThan(exportDataXlsx.indexOf('await exportTableData(target, "xlsx", columnInfos, headerMode)'));
|
||||
});
|
||||
|
||||
it("falls back to field-name headers when column metadata is unavailable", () => {
|
||||
const exportDataXlsx = functionBody("exportDataXlsx");
|
||||
|
||||
expect(exportDataXlsx).toContain("catch {\n // Export still works with field-name headers when column metadata is unavailable.\n }");
|
||||
expect(exportDataXlsx).toContain('await exportTableData(target, "xlsx", columnInfos, headerMode)');
|
||||
});
|
||||
|
||||
it("sends the selected mode's header overrides to both XLSX export paths", () => {
|
||||
const exportTableData = functionBody("exportTableData");
|
||||
|
||||
expect(exportTableData).toContain("buildXlsxHeaderOverrides(result.columns, comments, headerMode)");
|
||||
expect(exportTableData).toContain("columnComments,");
|
||||
});
|
||||
|
||||
it("keeps the original table and export options when selection changes during XLSX preparation", async () => {
|
||||
const metadata = deferred<ColumnInfo[]>();
|
||||
apiMock.getColumns.mockReturnValueOnce(metadata.promise);
|
||||
const originalNode = { id: "table-1", type: "table", label: "users", connectionId: "conn-1", database: "db-a", schema: "public", catalog: "catalog-a", children: [] } as TreeNode;
|
||||
const activeNode = shallowRef(originalNode);
|
||||
const settingsStore = exportSettings();
|
||||
const connectionStore = {
|
||||
ensureConnected: vi.fn(),
|
||||
getConfig: vi.fn(() => ({ db_type: "postgres" })),
|
||||
connectionIdentifierQuote: vi.fn((connectionId: string) => (connectionId === "conn-1" ? '"' : "`")),
|
||||
treeNodes: [],
|
||||
selectedTreeNodeIds: [],
|
||||
};
|
||||
const runtime = useSidebarTreeExportRuntime({
|
||||
activeNode,
|
||||
connectionStore: connectionStore as never,
|
||||
settingsStore: settingsStore as never,
|
||||
acceptedSelectionIds: () => null,
|
||||
});
|
||||
|
||||
const exportPromise = runtime.exportDataXlsx();
|
||||
await vi.waitFor(() => expect(apiMock.getColumns).toHaveBeenCalledWith("conn-1", "db-a", "public", "users", "catalog-a"));
|
||||
activeNode.value = { id: "table-2", type: "table", label: "orders", connectionId: "conn-2", database: "db-b", schema: "sales", children: [] } as TreeNode;
|
||||
settingsStore.editorSettings.exportBatchSize = 16;
|
||||
settingsStore.editorSettings.exportRowLimit = 10;
|
||||
metadata.resolve([column("name")]);
|
||||
|
||||
await exportPromise;
|
||||
|
||||
expect(apiMock.startTableExport).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
connectionId: "conn-1",
|
||||
database: "db-a",
|
||||
schema: "public",
|
||||
identifierQuote: '"',
|
||||
tableName: "users",
|
||||
filePath: "users.xlsx",
|
||||
columns: ["name"],
|
||||
batchSize: 128,
|
||||
rowLimit: 500,
|
||||
}),
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps explicit column order and primary keys for keyset XLSX export", async () => {
|
||||
apiMock.getColumns.mockResolvedValueOnce([column("display_name"), column("id", true), column("created_at")]);
|
||||
const activeNode = shallowRef({ id: "table-1", type: "table", label: "users", connectionId: "conn-1", database: "db", schema: "public", children: [] } as TreeNode);
|
||||
const connectionStore = {
|
||||
ensureConnected: vi.fn(),
|
||||
getConfig: vi.fn(() => ({ db_type: "postgres" })),
|
||||
connectionIdentifierQuote: vi.fn(() => '"'),
|
||||
treeNodes: [],
|
||||
selectedTreeNodeIds: [],
|
||||
};
|
||||
const runtime = useSidebarTreeExportRuntime({
|
||||
activeNode,
|
||||
connectionStore: connectionStore as never,
|
||||
settingsStore: exportSettings() as never,
|
||||
acceptedSelectionIds: () => null,
|
||||
});
|
||||
|
||||
await runtime.exportDataXlsx();
|
||||
|
||||
expect(apiMock.startTableExport).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
columns: ["display_name", "id", "created_at"],
|
||||
primaryKeys: ["id"],
|
||||
}),
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user