fix(sidebar): preserve primary key metadata after table loading
This commit is contained in:
4 files changed
+56
-20
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { dataTabOpenModeFromTreeClick, findExistingDataTabCandidate } from "@/lib/sidebar/dataTabOpenPolicy";
|
||||
import { canApplyDataTabMetadata, dataTabOpenModeFromTreeClick, findExistingDataTabCandidate } from "@/lib/sidebar/dataTabOpenPolicy";
|
||||
import type { QueryTab } from "@/types/database";
|
||||
|
||||
function click(modifiers: Partial<Pick<MouseEvent, "metaKey" | "ctrlKey" | "altKey" | "shiftKey">> = {}) {
|
||||
@@ -65,4 +65,27 @@ describe("dataTabOpenPolicy", () => {
|
||||
expect(findExistingDataTabCandidate([otherTable], usersTarget, { openMode: "default", reuseDataTab: true })).toEqual({ tab: otherTable, match: "database" });
|
||||
expect(findExistingDataTabCandidate([otherTable], usersTarget, { openMode: "default", reuseDataTab: false })).toBeUndefined();
|
||||
});
|
||||
|
||||
it("allows metadata to update a tab that still points to the requested table", () => {
|
||||
const tab = dataTab("users", "users");
|
||||
tab.tableMeta = { schema: "public", tableName: "users", columns: [], primaryKeys: [] };
|
||||
|
||||
expect(canApplyDataTabMetadata(tab, usersTarget, new AbortController().signal)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects metadata after its request is cancelled", () => {
|
||||
const tab = dataTab("users", "users");
|
||||
tab.tableMeta = { schema: "public", tableName: "users", columns: [], primaryKeys: [] };
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
||||
expect(canApplyDataTabMetadata(tab, usersTarget, controller.signal)).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects stale metadata after a reusable tab switches to another table", () => {
|
||||
const tab = dataTab("reused", "orders");
|
||||
tab.tableMeta = { schema: "public", tableName: "orders", columns: [], primaryKeys: [] };
|
||||
|
||||
expect(canApplyDataTabMetadata(tab, usersTarget, new AbortController().signal)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -138,4 +138,15 @@ describe("sidebarDataOpenCoordinator", () => {
|
||||
|
||||
expect(signal?.aborted).toBe(true);
|
||||
});
|
||||
|
||||
it("marks a completed request non-current without aborting its signal", async () => {
|
||||
let requestState: { isCurrent: () => boolean; signal: AbortSignal } | undefined;
|
||||
|
||||
runSidebarDataOpenImmediately({ connectionKey: "conn" }, (request) => {
|
||||
requestState = request;
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(requestState?.isCurrent()).toBe(false));
|
||||
expect(requestState?.signal.aborted).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -37,6 +37,10 @@ function isSameTable(tab: DataTabLike, target: DataTabTarget): boolean {
|
||||
return isSameDatabase(tab, target) && (tab.tableMeta?.catalog || "") === (target.catalog || "") && (tab.schema || "") === (target.schema || "") && (tab.tableMeta?.tableName || tab.title) === target.tableName;
|
||||
}
|
||||
|
||||
export function canApplyDataTabMetadata(tab: DataTabLike | undefined, target: DataTabTarget, signal?: AbortSignal): boolean {
|
||||
return signal?.aborted !== true && tab !== undefined && isSameTable(tab, target);
|
||||
}
|
||||
|
||||
export function findExistingDataTabCandidate<T extends DataTabLike>(tabs: T[], target: DataTabTarget, options: { openMode: DataTabOpenMode; reuseDataTab: boolean }): ExistingDataTabCandidate<T> | undefined {
|
||||
if (options.openMode === "new-tab") return undefined;
|
||||
|
||||
|
||||
Reference in new issue
Block a user