feat(sidebar): add configurable object metadata display

This commit is contained in:
zipg authored and GitHub committed 2026-07-23 00:23:19 +08:00
1 parent 8adf802444
commit 57f2d27313
29 files changed
+889 -143

No files matched your search

@@ -0,0 +1,65 @@
import { describe, expect, it } from "vitest";
import { applySidebarDatabaseStorage, applySidebarTableStorage, formatSidebarObjectStorage, sidebarDatabaseNames, sidebarTableStorageScopes, supportsSidebarDatabaseStorage, supportsSidebarTableStorage } from "@/lib/sidebar/sidebarDatabaseStorage";
import type { ConnectionConfig, TreeNode } from "@/types/database";
function config(dbType: ConnectionConfig["db_type"]): ConnectionConfig {
return { id: "connection", name: "connection", db_type: dbType } as ConnectionConfig;
}
describe("sidebar database storage", () => {
it("keeps database totals PostgreSQL-specific while reusing supported table statistics", () => {
expect(supportsSidebarDatabaseStorage(config("postgres"))).toBe(true);
expect(supportsSidebarDatabaseStorage(config("mysql"))).toBe(false);
expect(supportsSidebarDatabaseStorage({ ...config("postgres"), driver_profile: "cockroachdb" })).toBe(false);
for (const dbType of ["mysql", "postgres", "sqlserver", "oracle", "clickhouse", "dameng", "gaussdb", "kingbase", "gbase"] as const) {
expect(supportsSidebarTableStorage(config(dbType))).toBe(true);
}
expect(supportsSidebarTableStorage(config("jdbc"))).toBe(false);
expect(supportsSidebarTableStorage({ ...config("postgres"), driver_profile: "cockroachdb" })).toBe(false);
expect(supportsSidebarTableStorage({ ...config("gbase"), driver_profile: "gbase8s" })).toBe(false);
});
it("requests and applies only visible database nodes", () => {
const nodes: TreeNode[] = [
{ id: "a", label: "app", type: "database", connectionId: "connection", database: "app" },
{ id: "b", label: "hidden", type: "database", connectionId: "connection", database: "hidden" },
{ id: "utility", label: "users", type: "user-admin", connectionId: "connection" },
];
expect(sidebarDatabaseNames(nodes)).toEqual(["app", "hidden"]);
expect(applySidebarDatabaseStorage(nodes, [{ name: "app", size_bytes: 2048 }])).toBe(true);
expect(nodes[0].sizeBytes).toBe(2048);
expect(nodes[1].sizeBytes).toBeUndefined();
});
it("keeps unavailable values blank and formats known sizes compactly", () => {
expect(formatSidebarObjectStorage(null)).toBe("");
expect(formatSidebarObjectStorage(0)).toBe("0 B");
expect(formatSidebarObjectStorage(1536)).toBe("1.5 KB");
expect(formatSidebarObjectStorage(15 * 1024 * 1024)).toBe("15 MB");
expect(formatSidebarObjectStorage(15.25 * 1024 * 1024)).toBe("15.3 MB");
});
it("collects table scopes and applies PostgreSQL table sizes without crossing schemas", () => {
const publicTable: TreeNode = { id: "public-users", label: "users", type: "table", connectionId: "connection", database: "app", schema: "public" };
const auditTable: TreeNode = { id: "audit-users", label: "users", type: "table", connectionId: "connection", database: "app", schema: "audit" };
const nodes: TreeNode[] = [
{ id: "public", label: "public", type: "schema", connectionId: "connection", database: "app", schema: "public", children: [publicTable] },
{ id: "audit", label: "audit", type: "schema", connectionId: "connection", database: "app", schema: "audit", children: [auditTable] },
];
expect(sidebarTableStorageScopes([publicTable, auditTable])).toEqual([
{ connectionId: "connection", database: "app", schema: "public" },
{ connectionId: "connection", database: "app", schema: "audit" },
]);
expect(applySidebarTableStorage(nodes, { connectionId: "connection", database: "app", schema: "public" }, [{ name: "users", schema: "public", total_bytes: 8192 }])).toBe(true);
expect(publicTable.sizeBytes).toBe(8192);
expect(auditTable.sizeBytes).toBeUndefined();
});
it("applies database-scoped MySQL statistics to tree nodes without a schema", () => {
const table: TreeNode = { id: "products", label: "products", type: "table", connectionId: "connection", database: "shop" };
expect(applySidebarTableStorage([table], { connectionId: "connection", database: "shop", schema: "" }, [{ name: "products", schema: "shop", total_bytes: 49152 }])).toBe(true);
expect(table.sizeBytes).toBe(49152);
});
});
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { trailingCommentAvailableWidth, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout";
import { alignedSidebarCommentLabelWidths, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout";
describe("sidebar tree item layout", () => {
it("keeps a table row constrained when it displays a comment", () => {
@@ -11,10 +11,17 @@ describe("sidebar tree item layout", () => {
expect(treeLabelWidthClass({ fullWidth: false, hasTrailingComment: true })).toBe("min-w-0 flex-1 truncate");
});
it("gives the comment only the width left after the full table name and gap", () => {
expect(trailingCommentAvailableWidth(260, 100)).toBe(152);
expect(trailingCommentAvailableWidth(108, 100)).toBe(0);
expect(trailingCommentAvailableWidth(100, 100)).toBe(0);
expect(trailingCommentAvailableWidth(99, 100)).toBe(0);
it("aligns comments to the longest sibling name without crossing parent groups", () => {
const widths = alignedSidebarCommentLabelWidths([
{ id: "tables", depth: 1, alignable: false, hasComment: false, labelWidth: 0 },
{ id: "short", depth: 2, alignable: true, hasComment: true, labelWidth: 48 },
{ id: "long", depth: 2, alignable: true, hasComment: false, labelWidth: 136 },
{ id: "views", depth: 1, alignable: false, hasComment: false, labelWidth: 0 },
{ id: "view", depth: 2, alignable: true, hasComment: true, labelWidth: 72 },
]);
expect(widths.get("short")).toBe(136);
expect(widths.has("long")).toBe(false);
expect(widths.get("view")).toBe(72);
});
});