feat(sidebar): batch actions for multi selected nodes

This commit is contained in:
Mo-Moore authored and GitHub committed 2026-09-01 00:22:02 +08:00
1 parent 2f71a1a8aa
commit 69a032ed63
37 files changed
+1302 -159

No files matched your search

@@ -12,4 +12,14 @@ describe("add to AI database context", () => {
expect(addToAiSource).toContain("else if (hasTreeNodeDatabaseContext(node))");
expect(addToAiSource).not.toContain("else if (node.database)");
});
it("adds every selected table as an AI mention", () => {
const addToAiStart = appSource.indexOf("async function addToAi");
const addToAiEnd = appSource.indexOf("function openAiPanel", addToAiStart);
const addToAiSource = appSource.slice(addToAiStart, addToAiEnd);
expect(addToAiSource).toContain("Array.isArray(nodesInput) ? nodesInput : [nodesInput]");
expect(addToAiSource).toContain('entry.type === "table"');
expect(addToAiSource).toContain("for (const mention of tableMentions) handle.addTableMention(mention)");
});
});
@@ -1,5 +1,7 @@
import { describe, expect, it } from "vitest";
import { createColumnReferencePayload, createTableReferencePayload, parseTableReferencePayload, tableReferenceInsertText } from "@/lib/editor/queryEditorTableDrop";
import { createColumnReferencePayload, createMultiTableReferencePayload, createTableReferencePayload, parseTableReferencePayload, tableReferenceInsertText } from "@/lib/editor/queryEditorTableDrop";
const commaNewline = { columnNameSeparator: "comma-newline" as const };
describe("query editor table reference drop", () => {
it("inserts a quoted database name for database references", () => {
@@ -102,7 +104,7 @@ describe("query editor table reference drop", () => {
columnNames: ["id", "order no", "order", "created_at"],
databaseType: "mysql",
})!;
expect(tableReferenceInsertText(mysql)).toBe("id,\n`order no`,\n`order`,\ncreated_at");
expect(tableReferenceInsertText(mysql, undefined, commaNewline)).toBe("id,\n`order no`,\n`order`,\ncreated_at");
const postgres = createColumnReferencePayload({
connectionId: "conn-1",
@@ -110,7 +112,29 @@ describe("query editor table reference drop", () => {
columnNames: ["id", "OrderNo", "select"],
databaseType: "postgres",
})!;
expect(tableReferenceInsertText(postgres)).toBe('id,\n"OrderNo",\n"select"');
expect(tableReferenceInsertText(postgres, undefined, commaNewline)).toBe('id,\n"OrderNo",\n"select"');
});
it("uses columnNameSeparator from the payload for multi-column sidebar drag", () => {
const comma = createColumnReferencePayload({
connectionId: "conn-1",
database: "app-db",
columnNames: ["id", "name"],
databaseType: "mysql",
columnNameSeparator: "comma",
})!;
expect(comma.columnNameSeparator).toBe("comma");
expect(parseTableReferencePayload(JSON.stringify(comma))).toEqual(comma);
expect(tableReferenceInsertText(comma)).toBe("id,name");
const newline = createColumnReferencePayload({
connectionId: "conn-1",
database: "app-db",
columnNames: ["id", "name"],
databaseType: "mysql",
columnNameSeparator: "newline",
})!;
expect(tableReferenceInsertText(newline)).toBe("id\nname");
});
it("keeps smart quoting for dialects without a dedicated keyword table", () => {
@@ -121,7 +145,7 @@ describe("query editor table reference drop", () => {
columnNames: ["id", "order no"],
databaseType: "oracle",
})!;
expect(tableReferenceInsertText(oracle)).toBe('id,\n"order no"');
expect(tableReferenceInsertText(oracle, undefined, commaNewline)).toBe('id,\n"order no"');
});
it("bare-quotes sqlserver columns when safe and brackets reserved words", () => {
@@ -131,7 +155,7 @@ describe("query editor table reference drop", () => {
columnNames: ["id", "order", "order no"],
databaseType: "sqlserver",
})!;
expect(tableReferenceInsertText(payload)).toBe("id,\n[order],\n[order no]");
expect(tableReferenceInsertText(payload, undefined, commaNewline)).toBe("id,\n[order],\n[order no]");
});
it("falls back to the editor database type when the payload omits one", () => {
@@ -140,6 +164,21 @@ describe("query editor table reference drop", () => {
database: "app-db",
columnNames: ["order no"],
})!;
expect(tableReferenceInsertText(payload, "mysql")).toBe("`order no`");
expect(tableReferenceInsertText(payload, "mysql", commaNewline)).toBe("`order no`");
});
it("inserts multiple table references with the configured separator", () => {
const payload = createMultiTableReferencePayload({
connectionId: "conn-1",
database: "app-db",
tableReferences: [
{ schema: "public", tableName: "users" },
{ schema: "public", tableName: "orders" },
],
databaseType: "postgres",
})!;
expect(parseTableReferencePayload(JSON.stringify(payload))).toEqual(payload);
expect(tableReferenceInsertText(payload, undefined, { tableNameSeparator: "comma" })).toBe("users,orders");
expect(tableReferenceInsertText(payload, undefined, { tableNameSeparator: "comma", includeTableSchema: true })).toBe('"public"."users","public"."orders"');
});
});
@@ -0,0 +1,15 @@
import { describe, expect, it } from "vitest";
import type { TreeNode } from "@/types/database";
import { resolveSidebarDdlTargets } from "@/lib/sidebar/sidebarDdlTargets";
describe("sidebarDdlTargets", () => {
it("resolves multi-select ddl targets in the same execution context", () => {
const first: TreeNode = { id: "t1", label: "one", type: "table", connectionId: "c1", database: "db" };
const second: TreeNode = { id: "t2", label: "two", type: "view", connectionId: "c1", database: "db" };
const otherDb: TreeNode = { id: "t3", label: "three", type: "table", connectionId: "c1", database: "other" };
const group: TreeNode = { id: "group", label: "Tables", type: "group-tables", children: [first, second, otherDb] };
expect(resolveSidebarDdlTargets(first, [group], [second.id, first.id])).toEqual([first, second]);
expect(resolveSidebarDdlTargets(first, [group], [otherDb.id, first.id, second.id])).toEqual([first, second]);
});
});
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { TreeNode } from "@/types/database";
import { allDatabasesExportSourceForNode, databaseExportSourceForNode, sidebarStructureExportTargets } from "@/lib/sidebar/sidebarExportRuntime";
import { allDatabasesExportSourceForNode, databaseExportSourceForNode, sidebarSameSchemaStructureTargets, sidebarStructureExportTargets, sidebarTableDataExportTargets } from "@/lib/sidebar/sidebarExportRuntime";
describe("sidebar export runtime", () => {
it("prepares database and table export sources", () => {
@@ -27,4 +27,36 @@ describe("sidebar export runtime", () => {
expect(sidebarStructureExportTargets(first, [group], [third.id, first.id, second.id])).toEqual([first, second, third]);
expect(sidebarStructureExportTargets(first, [group], [second.id])).toEqual([first]);
});
it("limits multi-select diagram and database export prefills to the active schema context", () => {
const publicUsers: TreeNode = { id: "t1", label: "users", type: "table", connectionId: "c1", database: "db", schema: "public" };
const publicOrders: TreeNode = { id: "t2", label: "orders", type: "table", connectionId: "c1", database: "db", schema: "public" };
const salesUsers: TreeNode = { id: "t3", label: "users", type: "table", connectionId: "c1", database: "db", schema: "sales" };
const group: TreeNode = { id: "group", label: "Tables", type: "group-tables", children: [publicUsers, publicOrders, salesUsers] };
expect(sidebarSameSchemaStructureTargets(publicUsers, [group], [publicOrders.id, publicUsers.id, salesUsers.id])).toEqual([publicUsers, publicOrders]);
expect(sidebarSameSchemaStructureTargets(publicUsers, [group], [salesUsers.id, publicUsers.id])).toEqual([publicUsers]);
});
it("counts batch data export targets as tables only", () => {
const table: TreeNode = { id: "t1", label: "users", type: "table", connectionId: "c1", database: "db", schema: "public" };
const view: TreeNode = { id: "v1", label: "active_users", type: "view", connectionId: "c1", database: "db", schema: "public" };
const other: TreeNode = { id: "t2", label: "orders", type: "table", connectionId: "c1", database: "db", schema: "public" };
const group: TreeNode = { id: "group", label: "Objects", type: "group-tables", children: [table, view, other] };
expect(sidebarTableDataExportTargets(table, [group], [other.id, table.id, view.id])).toEqual([table, other]);
expect(sidebarTableDataExportTargets(view, [group], [other.id, table.id, view.id])).toEqual([view]);
expect(sidebarTableDataExportTargets(table, [group], [view.id])).toEqual([table]);
});
it("scopes batch data export targets to the active execution context", () => {
const localUsers: TreeNode = { id: "t1", label: "users", type: "table", connectionId: "c1", database: "db", schema: "public" };
const localOrders: TreeNode = { id: "t2", label: "orders", type: "table", connectionId: "c1", database: "db", schema: "public" };
const otherConnectionOrders: TreeNode = { id: "t3", label: "orders", type: "table", connectionId: "c2", database: "db", schema: "public" };
const otherDatabaseUsers: TreeNode = { id: "t4", label: "users", type: "table", connectionId: "c1", database: "other", schema: "public" };
const group: TreeNode = { id: "group", label: "Tables", type: "group-tables", children: [localUsers, localOrders, otherConnectionOrders, otherDatabaseUsers] };
expect(sidebarTableDataExportTargets(localUsers, [group], [localUsers.id, localOrders.id, otherConnectionOrders.id, otherDatabaseUsers.id])).toEqual([localUsers, localOrders]);
expect(sidebarTableDataExportTargets(localOrders, [group], [localUsers.id, localOrders.id, otherConnectionOrders.id, otherDatabaseUsers.id])).toEqual([localUsers, localOrders]);
});
});
@@ -0,0 +1,69 @@
import { describe, expect, it } from "vitest";
import type { TreeNode } from "@/types/database";
import { columnNameForSidebarDrag, formatSidebarTableNamesForCopy, normalizeSidebarCopyTableNameSeparator, resolveSidebarColumnDragNames, resolveSidebarColumnDragTargets, resolveSidebarTableCopyTargets } from "@/lib/sidebar/sidebarTableNameCopy";
describe("sidebarTableNameCopy", () => {
const table = (id: string, label: string, schema = "public"): TreeNode => ({
id,
label,
type: "table",
connectionId: "c1",
database: "db",
schema,
});
const column = (id: string, label: string, name: string): TreeNode => ({
id,
label,
type: "column",
connectionId: "c1",
database: "db",
schema: "public",
tableName: "users",
meta: { name },
});
it("resolves multi-table copy targets in visible order", () => {
const first = table("t1", "one");
const second = table("t2", "two");
const third = table("t3", "three");
expect(resolveSidebarTableCopyTargets(first, [third, first, second])).toEqual([third, first, second]);
expect(resolveSidebarTableCopyTargets(first, [second])).toEqual([first]);
});
it("falls back to active node when selection is mixed connection", () => {
const first = table("t1", "one");
const other = { ...table("t2", "two"), connectionId: "c2" };
expect(resolveSidebarTableCopyTargets(first, [first, other])).toEqual([first]);
});
it("formats bare and qualified table names", () => {
const targets = [table("t1", "users"), table("t2", "orders", "sales")];
expect(formatSidebarTableNamesForCopy(targets, { separator: "comma", includeSchema: false })).toBe("users,orders");
expect(formatSidebarTableNamesForCopy(targets, { separator: "newline", includeSchema: true, databaseType: "postgres" })).toBe('"public"."users"\n"sales"."orders"');
expect(formatSidebarTableNamesForCopy([table("t1", "users")], { separator: "comma", includeSchema: true, databaseType: "mysql" })).toBe("`users`");
expect(formatSidebarTableNamesForCopy([table("t1", "users", "default")], { separator: "comma", includeSchema: true, databaseType: "hive", schema: "default" })).toBe("`default`.`users`");
});
it("resolves multi-column drag targets for the same table", () => {
const first = column("c1", "id (bigint)", "id");
const second = column("c2", "name (text)", "name");
expect(resolveSidebarColumnDragTargets(first, [second, first])).toEqual([second, first]);
expect(resolveSidebarColumnDragNames(first, [second, first])).toEqual(["name", "id"]);
});
it("falls back to active column when selection spans tables", () => {
const first = column("c1", "id (bigint)", "id");
const other = { ...column("c2", "name (text)", "name"), tableName: "orders" };
expect(resolveSidebarColumnDragTargets(first, [first, other])).toEqual([first]);
});
it("extracts column drag names from labels", () => {
expect(columnNameForSidebarDrag(column("c1", "created_at (timestamp)", "created_at"))).toBe("created_at");
});
it("normalizes separator settings", () => {
expect(normalizeSidebarCopyTableNameSeparator("comma-newline")).toBe("comma-newline");
expect(normalizeSidebarCopyTableNameSeparator("bogus")).toBe("comma");
});
});