fix(sidebar): centralize tree action handling
This commit is contained in:
1 parent
7f096653db
commit
ee85033068
28 files changed
+2705
-1032
No files matched your search
+48
-3
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { updatePinnedTreeNodeInPlace } from "@/lib/app/pinnedItems";
|
||||
import { migrateLegacyPinnedTreeNodeIds, syncPinnedTreeNodeStateInPlace, treeNodePinKey, updatePinnedTreeNodeInPlace } from "@/lib/app/pinnedItems";
|
||||
import { buildTreeNodesFromLayout } from "@/lib/sidebar/sidebarLayout";
|
||||
import type { ConnectionConfig, SidebarLayout, TreeNode } from "@/types/database";
|
||||
|
||||
@@ -17,7 +17,7 @@ describe("sidebar pinned tree nodes", () => {
|
||||
},
|
||||
];
|
||||
|
||||
expect(updatePinnedTreeNodeInPlace(tree, "conn:db:b", true)).toBe("siblings");
|
||||
expect(updatePinnedTreeNodeInPlace(tree, tree[0].children![1], true)).toBe("siblings");
|
||||
|
||||
expect(tree[0].children?.map((node) => node.id)).toEqual(["conn:db:b", "conn:db:a"]);
|
||||
expect(tree[0].children?.[0].pinned).toBe(true);
|
||||
@@ -29,12 +29,57 @@ describe("sidebar pinned tree nodes", () => {
|
||||
{ id: "group-b", label: "B", type: "connection-group" },
|
||||
];
|
||||
|
||||
expect(updatePinnedTreeNodeInPlace(tree, "group-b", true)).toBe("root");
|
||||
expect(updatePinnedTreeNodeInPlace(tree, tree[1], true)).toBe("root");
|
||||
|
||||
expect(tree.map((node) => node.id)).toEqual(["group-b", "group-a"]);
|
||||
expect(tree[0].pinned).toBe(true);
|
||||
});
|
||||
|
||||
it("scopes duplicate node ids by database when pinning", () => {
|
||||
const databaseA: TreeNode = {
|
||||
id: "conn:a",
|
||||
label: "A",
|
||||
type: "database",
|
||||
children: [{ id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "a" }],
|
||||
};
|
||||
const databaseB: TreeNode = {
|
||||
id: "conn:b",
|
||||
label: "B",
|
||||
type: "database",
|
||||
children: [{ id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "b" }],
|
||||
};
|
||||
const tree: TreeNode[] = [{ id: "conn", label: "Connection", type: "connection", children: [databaseA, databaseB] }];
|
||||
|
||||
expect(updatePinnedTreeNodeInPlace(tree, databaseA.children![0], true)).toBe("siblings");
|
||||
|
||||
expect(databaseA.children![0].pinned).toBe(true);
|
||||
expect(databaseB.children![0].pinned).not.toBe(true);
|
||||
});
|
||||
|
||||
it("clears stale legacy duplicate pins after switching to scoped keys", () => {
|
||||
const tableA: TreeNode = { id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "a", pinned: true };
|
||||
const tableB: TreeNode = { id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "b", pinned: true };
|
||||
const tree: TreeNode[] = [
|
||||
{ id: "conn:a", label: "A", type: "database", children: [tableA] },
|
||||
{ id: "conn:b", label: "B", type: "database", children: [tableB] },
|
||||
];
|
||||
|
||||
syncPinnedTreeNodeStateInPlace(tree, new Set([treeNodePinKey(tableA)]));
|
||||
|
||||
expect(tableA.pinned).toBe(true);
|
||||
expect(tableB.pinned).toBe(false);
|
||||
});
|
||||
|
||||
it("migrates a legacy id once instead of pinning every colliding node", () => {
|
||||
const tableA: TreeNode = { id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "a" };
|
||||
const tableB: TreeNode = { id: "duplicate-table-id", label: "users", type: "table", connectionId: "conn", database: "b" };
|
||||
|
||||
const migrated = migrateLegacyPinnedTreeNodeIds([tableA, tableB], new Set(["duplicate-table-id"]));
|
||||
|
||||
expect(migrated.changed).toBe(true);
|
||||
expect(migrated.ids).toEqual(new Set([treeNodePinKey(tableA)]));
|
||||
});
|
||||
|
||||
it("applies pinned state to connection groups when rebuilding from layout", () => {
|
||||
const layout: SidebarLayout = {
|
||||
groups: [
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import { createSidebarActionRequest, createSidebarActionTarget, findSidebarActionTarget } from "@/lib/sidebar/sidebarActionTarget";
|
||||
|
||||
function tableNode(): TreeNode {
|
||||
return {
|
||||
id: "table-1",
|
||||
label: "users",
|
||||
type: "table",
|
||||
connectionId: "connection-1",
|
||||
database: "app",
|
||||
schema: "public",
|
||||
children: [{ id: "column-1", label: "id", type: "column" }],
|
||||
};
|
||||
}
|
||||
|
||||
describe("sidebar action targets", () => {
|
||||
it("captures a frozen shallow snapshot without retaining children", () => {
|
||||
const node = tableNode();
|
||||
const target = createSidebarActionTarget(node);
|
||||
|
||||
node.label = "recycled-row";
|
||||
node.connectionId = "connection-2";
|
||||
|
||||
expect(target).toMatchObject({ label: "users", connectionId: "connection-1", children: undefined, hiddenChildren: undefined });
|
||||
expect(Object.isFrozen(target)).toBe(true);
|
||||
});
|
||||
|
||||
it("captures selection independently from later selection changes", () => {
|
||||
const selectedNodeIds = ["table-1", "table-2"];
|
||||
const request = createSidebarActionRequest(tableNode(), selectedNodeIds, { initialEditing: true });
|
||||
|
||||
selectedNodeIds.splice(0, selectedNodeIds.length, "table-3");
|
||||
|
||||
expect(request.selectedNodeIds).toEqual(["table-1", "table-2"]);
|
||||
expect(request.payload).toEqual({ initialEditing: true });
|
||||
expect(Object.isFrozen(request.selectedNodeIds)).toBe(true);
|
||||
expect(Object.isFrozen(request)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not resolve a recycled row with the same local id", () => {
|
||||
const target = createSidebarActionTarget(tableNode());
|
||||
const recycled = tableNode();
|
||||
recycled.connectionId = "connection-2";
|
||||
|
||||
expect(findSidebarActionTarget([recycled], target)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when the action target was removed", () => {
|
||||
const target = createSidebarActionTarget(tableNode());
|
||||
|
||||
expect(findSidebarActionTarget([], target)).toBeNull();
|
||||
});
|
||||
|
||||
it("resolves targets stored only in hidden children", () => {
|
||||
const node = tableNode();
|
||||
const target = createSidebarActionTarget(node);
|
||||
const root: TreeNode = { id: "root", label: "root", type: "database", hiddenChildren: [node] };
|
||||
|
||||
expect(findSidebarActionTarget([root], target)).toBe(node);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cancelPendingSidebarDataOpen, runSidebarDataOpenImmediately } from "@/lib/sidebar/sidebarDataOpenCoordinator";
|
||||
|
||||
describe("sidebarDataOpenCoordinator", () => {
|
||||
afterEach(() => {
|
||||
cancelPendingSidebarDataOpen();
|
||||
});
|
||||
|
||||
it("cancels the active request when a newer open starts", async () => {
|
||||
const cancel = vi.fn();
|
||||
let firstIsCurrent = true;
|
||||
|
||||
runSidebarDataOpenImmediately(async (request) => {
|
||||
request.registerCancel(cancel);
|
||||
await Promise.resolve();
|
||||
firstIsCurrent = request.isCurrent();
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
expect(firstIsCurrent).toBe(false);
|
||||
});
|
||||
|
||||
it("runs registered cancellation immediately for an already stale request", async () => {
|
||||
const cancel = vi.fn();
|
||||
let registerLateCancel: (() => void) | undefined;
|
||||
|
||||
runSidebarDataOpenImmediately((request) => {
|
||||
registerLateCancel = () => request.registerCancel(cancel);
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
registerLateCancel?.();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("recovers after a runner throws synchronously", async () => {
|
||||
const nextRunner = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately(() => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
await Promise.resolve();
|
||||
runSidebarDataOpenImmediately(nextRunner);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(nextRunner).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps only the newest rapid navigation request current", async () => {
|
||||
const states: boolean[] = [];
|
||||
const captures: Array<() => void> = [];
|
||||
|
||||
for (let index = 0; index < 3; index += 1) {
|
||||
runSidebarDataOpenImmediately((request) => {
|
||||
captures.push(() => states.push(request.isCurrent()));
|
||||
});
|
||||
}
|
||||
captures.forEach((capture) => capture());
|
||||
|
||||
expect(states).toEqual([false, false, true]);
|
||||
});
|
||||
|
||||
it("does not cancel work that was not registered as sidebar-owned", async () => {
|
||||
const sidebarCancel = vi.fn();
|
||||
const unrelatedEditorCancel = vi.fn();
|
||||
|
||||
runSidebarDataOpenImmediately((request) => request.registerCancel(sidebarCancel));
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(sidebarCancel).toHaveBeenCalledOnce();
|
||||
expect(unrelatedEditorCancel).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("prevents a superseded connection completion from becoming current", async () => {
|
||||
let finishConnection: (() => void) | undefined;
|
||||
let completionWasCurrent = true;
|
||||
|
||||
runSidebarDataOpenImmediately(async (request) => {
|
||||
await new Promise<void>((resolve) => {
|
||||
finishConnection = resolve;
|
||||
});
|
||||
completionWasCurrent = request.isCurrent();
|
||||
});
|
||||
runSidebarDataOpenImmediately(() => undefined);
|
||||
finishConnection?.();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(completionWasCurrent).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user