feat(editor): add sql editor split panes with shared results

This commit is contained in:
Lan-zk authored and GitHub committed 2026-09-04 14:44:03 +08:00
1 parent 24a274bd89
commit 5a3a6e2bb0
76 files changed
+7997 -3343

No files matched your search

@@ -155,7 +155,7 @@ describe("useDataGridActions", () => {
const tab = tableDataTab();
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(
expect.objectContaining({
@@ -191,7 +191,9 @@ describe("useDataGridActions", () => {
const actions = useDataGridActions(computed(() => tab));
const { whereInput, orderBy } = restoredDataTabReloadFilters(tab);
await actions.onReloadData(undefined, undefined, whereInput, orderBy);
// ContentArea's empty-placeholder refresh emits the owning tabId first
// (tabId-first contract), so the filters land in the whereInput/orderBy slots.
await actions.onReloadData(tab.id, undefined, undefined, whereInput, orderBy);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ whereInput: "status = 'active'", orderBy: '"name" DESC' }));
expect(tab.whereInput).toBe("status = 'active'");
@@ -205,7 +207,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", undefined, undefined, undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", undefined, undefined, undefined, undefined, "refresh");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ whereInput: undefined, orderBy: undefined }));
expect(tab.whereInput).toBe("");
@@ -219,7 +221,7 @@ describe("useDataGridActions", () => {
mocks.buildTableSelectSql.mockResolvedValueOnce("SELECT * FROM public.users LIMIT 25 OFFSET 50");
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", 25, 50, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", 25, 50, "refresh");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ limit: 25, offset: 50 }));
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT * FROM public.users LIMIT 25 OFFSET 50", expect.objectContaining({ pagination: { limit: 25, offset: 50 } }));
@@ -238,7 +240,7 @@ describe("useDataGridActions", () => {
mocks.buildTableSelectSql.mockResolvedValueOnce("SELECT * FROM public.users LIMIT 100 OFFSET 100");
const actions = useDataGridActions(computed(() => tab));
await actions.onPaginate(100, 100);
await actions.onPaginate(tab.id, 100, 100);
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT * FROM public.users LIMIT 100 OFFSET 100", expect.objectContaining({ appendResult: { maxRows: 10_000 } }));
});
@@ -264,7 +266,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", '"old_name" ASC', undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", '"old_name" ASC', undefined, undefined, "refresh");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy: undefined }));
expect(tab.resultSortColumn).toBeUndefined();
@@ -289,7 +291,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", '"old_name" ASC', undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", '"old_name" ASC', undefined, undefined, "refresh");
expect(mocks.clearInvalidDataTabSort).toHaveReturnedWith(false);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy: undefined }));
@@ -317,7 +319,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "LOWER(new_name) ASC", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "LOWER(new_name) ASC", undefined, undefined, "refresh");
expect(mocks.clearInvalidDataTabSort).toHaveReturnedWith(true);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy: "LOWER(new_name) ASC" }));
@@ -341,7 +343,7 @@ describe("useDataGridActions", () => {
} as QueryTab;
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
expect(mocks.buildTableSelectSql).not.toHaveBeenCalled();
expect(mocks.executeTabSql).toHaveBeenCalledWith(
@@ -366,7 +368,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
// 合成的 ["Error"] 结果列不得进入 SQL 投影:真实列缺失时省略 columns(SELECT *)
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ columns: undefined }));
@@ -387,7 +389,7 @@ describe("useDataGridActions", () => {
// 第一轮:stale-tab 早退(tabs 里找不到匹配项时 refreshDataTabTableMeta 直接返回)
mocks.tabs.length = 0;
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
await vi.waitFor(() => expect(mocks.getColumns).toHaveBeenCalledTimes(1));
expect(mocks.setTableMeta).not.toHaveBeenCalled();
expect(tab.tableMetaPending).toBe(true);
@@ -395,7 +397,7 @@ describe("useDataGridActions", () => {
// 第二轮:真实 columns 仍为空,新的消费者加入同一在途请求;共享缓存应
// 去重后端调用,但本轮仍要在目标恢复后落地结果
mocks.tabs.push(tab);
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
await vi.waitFor(() => {
expect(mocks.getColumns).toHaveBeenCalledTimes(1);
expect(mocks.setTableMeta).toHaveBeenCalledWith("tab-1", expect.objectContaining({ primaryKeys: ["id"] }));
@@ -422,7 +424,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
// Dameng 元数据必须排在数据查询之后(串行约束,同 useSidebarDataOpenRuntime);
// 真实元数据落地后结束行标识等待
@@ -445,7 +447,7 @@ describe("useDataGridActions", () => {
const actions = useDataGridActions(computed(() => tab));
// 查询 reject 仍会重新抛出,但元数据刷新必须已启动,标签页可恢复
await expect(actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh")).rejects.toThrow("query failed");
await expect(actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh")).rejects.toThrow("query failed");
await vi.waitFor(() => {
expect(mocks.getColumns).toHaveBeenCalled();
expect(tab.tableMetaPending).toBe(false);
@@ -473,7 +475,7 @@ describe("useDataGridActions", () => {
mocks.listIndexes.mockResolvedValueOnce([]);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
expect(mocks.getColumns).toHaveBeenCalledTimes(1);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ columns: ["id", "age"] }));
@@ -501,7 +503,7 @@ describe("useDataGridActions", () => {
mocks.getColumns.mockResolvedValueOnce([{ name: "new_name", data_type: "text", is_nullable: true, column_default: null, is_primary_key: false, extra: null }]);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
expect(mocks.ensureConnected).toHaveBeenCalled();
expect(mocks.getColumns).toHaveBeenCalledTimes(1);
@@ -514,7 +516,7 @@ describe("useDataGridActions", () => {
mocks.tabs.push(tab);
const actions = useDataGridActions(computed(() => tab));
await actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
await actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
expect(mocks.getColumns).not.toHaveBeenCalled();
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ columns: ["id"] }));
@@ -535,7 +537,7 @@ describe("useDataGridActions", () => {
}),
);
const actions = useDataGridActions(computed(() => tab));
const reload = actions.onReloadData(tab.sql, "", "", "", undefined, undefined, "refresh");
const reload = actions.onReloadData(tab.id, tab.sql, "", "", "", undefined, undefined, "refresh");
await vi.waitFor(() => expect(mocks.getColumns).toHaveBeenCalledTimes(1));
mocks.metadataGeneration = 1;
@@ -574,7 +576,7 @@ describe("useDataGridActions", () => {
} as QueryTab;
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("email", 2, "asc");
await actions.onSort(tab.id, "email", 2, "asc");
expect(mocks.executeTabSql).toHaveBeenCalledWith(
"tab-1",
@@ -644,7 +646,7 @@ describe("useDataGridActions", () => {
});
const actions = useDataGridActions(computed(() => tab));
await actions.onPaginate(300, 100);
await actions.onPaginate(tab.id, 300, 100);
expect(mocks.executeTabSql.mock.calls.map((call) => call[2].pagination)).toEqual([
{ offset: 100, limit: 100, sessionId: "cursor-1", clientSessionId: "client-1" },
@@ -677,7 +679,7 @@ describe("useDataGridActions", () => {
mocks.getConfig.mockImplementation((id: string) => ({ id, db_type: "postgres" }));
const actions = useDataGridActions(computed(() => tab));
await actions.onPaginate(100, 100);
await actions.onPaginate(tab.id, 100, 100);
expect(mocks.executeTabSql).toHaveBeenCalledWith(
"tab-1",
@@ -719,7 +721,7 @@ describe("useDataGridActions", () => {
});
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("sort_value", 0, "asc", "status = 'active'");
await actions.onSort(tab.id, "sort_value", 0, "asc", "status = 'active'");
expect(tab.result?.rows.map((row) => row[0])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(tab.orderByInput).toBe('"sort_value" ASC');
@@ -753,7 +755,7 @@ describe("useDataGridActions", () => {
mocks.buildTableSelectSql.mockResolvedValueOnce("SELECT sorted");
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("sort_value", 0, direction, "status = 'active'", "database");
await actions.onSort(tab.id, "sort_value", 0, direction, "status = 'active'", "database");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy, whereInput: "status = 'active'", limit: 7, offset: 0 }));
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT sorted", expect.objectContaining({ pagination: { limit: 7, offset: 0 }, preserveTotalRowCountDuringExecution: true }));
@@ -781,7 +783,7 @@ describe("useDataGridActions", () => {
mocks.buildTableSelectSql.mockResolvedValueOnce("SELECT unsorted");
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("sort_value", 0, null, "status = 'active'", "database");
await actions.onSort(tab.id, "sort_value", 0, null, "status = 'active'", "database");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy: undefined, whereInput: "status = 'active'", limit: 10, offset: 0 }));
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT unsorted", expect.objectContaining({ pagination: { limit: 10, offset: 0 }, preserveTotalRowCountDuringExecution: true }));
@@ -813,7 +815,7 @@ describe("useDataGridActions", () => {
mocks.buildTableSelectSql.mockResolvedValueOnce("SELECT sorted page 2");
const actions = useDataGridActions(computed(() => tab));
await actions.onPaginate(10, 10, "status = 'active'");
await actions.onPaginate(tab.id, 10, 10, "status = 'active'");
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ orderBy: '"sort_value" ASC', whereInput: "status = 'active'", limit: 10, offset: 10 }));
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT sorted page 2", expect.objectContaining({ pagination: { offset: 10, limit: 10 }, preserveTotalRowCountDuringExecution: true }));
@@ -834,7 +836,7 @@ describe("useDataGridActions", () => {
});
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("sort_value", 0, "asc", "status = 'active'", "local");
await actions.onSort(tab.id, "sort_value", 0, "asc", "status = 'active'", "local");
expect(mocks.sortTabResultLocally).toHaveBeenCalledWith("tab-1", "sort_value", 0, "asc");
expect(mocks.buildTableSelectSql).not.toHaveBeenCalled();
@@ -862,7 +864,7 @@ describe("useDataGridActions", () => {
mocks.buildSortedQuerySql.mockResolvedValueOnce({ ok: true, sql: "SELECT sorted" });
const actions = useDataGridActions(computed(() => tab));
await actions.onSort("sort_value", 0, "desc", undefined, "database");
await actions.onSort(tab.id, "sort_value", 0, "desc", undefined, "database");
expect(mocks.executeTabSql).toHaveBeenCalledWith("tab-1", "SELECT sorted", expect.objectContaining({ pagination: { limit: 10, offset: 0 }, preserveTotalRowCountDuringExecution: true }));
});
@@ -219,7 +219,7 @@ describe("useSqlExecution", () => {
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it("passes the selected statement's editor offset to the query store", async () => {
@@ -245,7 +245,7 @@ describe("useSqlExecution", () => {
await execution.tryExecute({ fullSql, selectedSql, cursorPos: selectionFrom, selectionFrom, selectionTo: fullSql.length - 1 });
expect(executeCurrentSql).toHaveBeenCalledWith(selectedSql, { sourceOffset: selectionFrom });
expect(executeCurrentSql).toHaveBeenCalledWith(selectedSql, { tabId: "tab-1", sourceOffset: selectionFrom });
});
it("expands preceding @set values in a selected shell-style statement", async () => {
@@ -273,7 +273,7 @@ describe("useSqlExecution", () => {
await execution.tryExecute({ fullSql, selectedSql, cursorPos: selectionFrom, selectionFrom, selectionTo });
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(executeCurrentSql).toHaveBeenCalledWith("SELECT * FROM patrol WHERE post_id = '224';", {});
expect(executeCurrentSql).toHaveBeenCalledWith("SELECT * FROM patrol WHERE post_id = '224';", { tabId: "tab-1" });
});
it("opens the result table for a multi-statement batch by default", async () => {
@@ -671,7 +671,7 @@ SELECT @value AS Message;`;
await execution.tryExecuteInNewResultTab();
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { openInNewResultTab: true });
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1", openInNewResultTab: true });
});
it("does not record or refresh when a new-result execution restores the prior run", async () => {
@@ -723,7 +723,7 @@ SELECT @value AS Message;`;
await execution.onSqlParametersConfirm(resolvedSql);
expect(executeCurrentSql).toHaveBeenCalledWith(resolvedSql, { openInNewResultTab: true });
expect(executeCurrentSql).toHaveBeenCalledWith(resolvedSql, { tabId: "tab-1", openInNewResultTab: true });
});
it("executes the PostgreSQL ARRAY date-format query without opening the parameter dialog", async () => {
@@ -783,7 +783,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it("executes Oracle database-link queries without opening the parameter dialog", async () => {
@@ -808,7 +808,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it("sends native SET variables without client-side expansion", async () => {
@@ -872,7 +872,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it("executes MySQL cursor procedures with compact labels without opening the parameter dialog", async () => {
@@ -912,7 +912,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it.each([
@@ -975,7 +975,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { sourceOffset: 0, onExecutionStarted: expect.any(Function) });
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1", sourceOffset: 0, onExecutionStarted: expect.any(Function) });
});
it("sends Doris STRUCT DDL unchanged without opening the parameter dialog", async () => {
@@ -1008,7 +1008,7 @@ SELECT @value AS Message;`;
expect(execution.showSqlParameterDialog.value).toBe(false);
expect(execution.sqlParameterNames.value).toEqual([]);
expect(executeCurrentSql).toHaveBeenCalledWith(sql, {});
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1" });
});
it("records a later MySQL batch error and skips metadata refresh", async () => {
@@ -1287,7 +1287,7 @@ SELECT @value AS Message;`;
await execution.onDangerConfirm();
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { openInNewResultTab: true });
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1", openInNewResultTab: true });
});
it("keeps the active retained result when a new-result dangerous prompt is cancelled", async () => {
@@ -1493,7 +1493,7 @@ SELECT @value AS Message;`;
await execution.onDangerConfirm();
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { skipRedisSafetyCheck: false, openInNewResultTab: true });
expect(executeCurrentSql).toHaveBeenCalledWith(sql, { tabId: "tab-1", skipRedisSafetyCheck: false, openInNewResultTab: true });
});
it("distinguishes read-only and mutating Meilisearch REST requests", () => {
@@ -1,233 +0,0 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import { TAB_DRAG_HORIZONTAL_THRESHOLD, useTabDrag } from "@/composables/useTabDrag";
function createDragHarness(onDrop = vi.fn(() => true)) {
const drag = useTabDrag(onDrop);
const source = document.createElement("div");
source.innerHTML = '<span class="truncate">Source</span>';
source.addEventListener("pointerdown", (event) => drag.startDrag(event as PointerEvent, "source"));
document.body.appendChild(source);
return { drag, source, onDrop };
}
function beginDrag(source: HTMLElement, x = 100, y = 20, pointerType = "mouse", buttons = 1, pointerId = 1) {
source.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, button: 0, buttons, clientX: x, clientY: y, pointerId, pointerType }));
}
function movePointer(x: number, y = 20, pointerType = "mouse", buttons = 1, pointerId = 1) {
document.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, buttons, clientX: x, clientY: y, pointerId, pointerType }));
}
function releasePointer(x = 0, y = 0, pointerId = 1) {
document.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, clientX: x, clientY: y, pointerId, screenX: x, screenY: y }));
}
function cancelPointer(pointerId = 1) {
document.dispatchEvent(new PointerEvent("pointercancel", { bubbles: true, pointerId }));
}
function enterDropTarget(drag: ReturnType<typeof useTabDrag>, tabId = "target", x = 10) {
const target = document.createElement("div");
target.getBoundingClientRect = () => ({ x: 0, y: 0, left: 0, top: 0, right: 100, bottom: 28, width: 100, height: 28, toJSON: () => ({}) });
target.addEventListener("mousemove", (event) => drag.updateTarget(event, tabId));
document.body.appendChild(target);
target.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, clientX: x, clientY: 20 }));
}
afterEach(() => {
releasePointer();
document.body.innerHTML = "";
document.body.style.cursor = "";
document.body.style.userSelect = "";
});
describe("useTabDrag", () => {
it("ignores sub-threshold horizontal movement and vertical pointer jitter", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD - 1, 120);
expect(drag.state.active).toBe(false);
expect(drag.state.suppressClick).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
});
it("does not suppress the click when a drag gesture has no valid drop target", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
expect(drag.state.active).toBe(true);
releasePointer();
expect(onDrop).not.toHaveBeenCalled();
expect(drag.state.suppressClick).toBe(false);
});
it("does not suppress the click when the drop callback reports no reorder", () => {
const onDrop = vi.fn(() => false);
const { drag, source } = createDragHarness(onDrop);
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
enterDropTarget(drag);
releasePointer();
expect(onDrop).toHaveBeenCalledWith("source", "target", "before");
expect(drag.state.suppressClick).toBe(false);
});
it("suppresses the click only after a completed reorder", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
enterDropTarget(drag, "target", 90);
releasePointer();
expect(onDrop).toHaveBeenCalledWith("source", "target", "after");
expect(drag.state.suppressClick).toBe(true);
beginDrag(source);
expect(drag.state.suppressClick).toBe(false);
});
it("clears completed-drag click suppression on a later trackpad tap", () => {
const { drag, source } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
enterDropTarget(drag, "target", 90);
releasePointer();
expect(drag.state.suppressClick).toBe(true);
beginDrag(source, 100, 20, "mouse", 0);
expect(drag.state.active).toBe(false);
expect(drag.state.suppressClick).toBe(false);
});
it("ignores 18px of jitter, matching a typical touchscreen tap reported as generic mouse input", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 - 18, 24);
expect(drag.state.active).toBe(false);
expect(drag.state.suppressClick).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
});
it("does not arm a drag when the primary button is not held", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source, 100, 20, "mouse", 0);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD, 20, "mouse", 0);
releasePointer();
expect(drag.state.active).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
});
it("cancels a pending drag when the primary button is released before movement", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD, 20, "mouse", 0);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD + 10);
expect(drag.state.active).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
});
it("cancels an active drag when the primary button is no longer held", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
expect(drag.state.active).toBe(true);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD + 10, 20, "mouse", 0);
expect(drag.state.active).toBe(false);
expect(drag.state.draggedId).toBe(null);
expect(document.body.style.cursor).toBe("");
expect(document.body.style.userSelect).toBe("");
expect(onDrop).not.toHaveBeenCalled();
});
it("ignores movement from a different pointer", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source, 100, 20, "mouse", 1, 7);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD, 20, "mouse", 1, 8);
expect(drag.state.active).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
releasePointer(0, 0, 7);
});
it("never arms a drag for touch input, even past the horizontal threshold", () => {
const { drag, source, onDrop } = createDragHarness();
beginDrag(source, 100, 20, "touch");
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD + 20, 20, "touch");
expect(drag.state.active).toBe(false);
expect(drag.state.suppressClick).toBe(false);
expect(onDrop).not.toHaveBeenCalled();
});
it("detaches only when an armed drag is released outside the tab strip", () => {
const onDetach = vi.fn(() => true);
const drag = useTabDrag(
vi.fn(() => true),
onDetach,
);
drag.setDetachBoundsProvider(() => ({ left: 0, top: 0, right: 300, bottom: 40 }) as DOMRect);
const source = document.createElement("div");
source.innerHTML = '<span class="truncate">Source</span>';
source.addEventListener("pointerdown", (event) => drag.startDrag(event as PointerEvent, "source"));
document.body.appendChild(source);
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
releasePointer(500, 500);
expect(onDetach).toHaveBeenCalledWith("source", { x: 500, y: 500 });
expect(drag.state.active).toBe(false);
expect(drag.state.suppressClick).toBe(true);
drag.setDetachBoundsProvider(null);
});
it("clears an armed drag when the pointer is cancelled", () => {
const { drag, source } = createDragHarness();
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
expect(drag.state.active).toBe(true);
cancelPointer();
expect(drag.state.active).toBe(false);
expect(drag.state.draggedId).toBe(null);
expect(drag.state.suppressClick).toBe(false);
});
it("does not detach when the pointer is released inside the tab strip", () => {
const onDetach = vi.fn(() => true);
const onDrop = vi.fn(() => false);
const drag = useTabDrag(onDrop, onDetach);
drag.setDetachBoundsProvider(() => ({ left: 0, top: 0, right: 300, bottom: 40 }) as DOMRect);
const source = document.createElement("div");
source.innerHTML = '<span class="truncate">Source</span>';
source.addEventListener("pointerdown", (event) => drag.startDrag(event as PointerEvent, "source"));
document.body.appendChild(source);
beginDrag(source);
movePointer(100 + TAB_DRAG_HORIZONTAL_THRESHOLD);
releasePointer();
expect(onDetach).not.toHaveBeenCalled();
expect(onDrop).not.toHaveBeenCalled();
drag.setDetachBoundsProvider(null);
});
});
@@ -0,0 +1,133 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, ref } from "vue";
import type { Ref, WatchSource } from "vue";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { useToolbarOverflow } from "../useToolbarOverflow";
import type { EditorToolbarTier } from "@/lib/tabs/editorToolbarLayout";
function createHarness(revalidateSources: WatchSource[] = []) {
const rootRef = ref<HTMLElement | null>(null);
let tierRef: Ref<EditorToolbarTier> | undefined;
let measureFn: () => void = () => {};
const app = createApp(
defineComponent({
setup() {
const { tier, measure } = useToolbarOverflow(rootRef, revalidateSources);
tierRef = tier;
measureFn = measure;
return () => h("div");
},
}),
);
app.mount(document.createElement("div"));
return { rootRef, tierRef: tierRef!, measure: () => measureFn() };
}
/**
* Fake toolbar row whose scrollWidth shrinks once a tier is condensed —
* mirroring real rows where controls move into the overflow menu, so the
* measure loop can converge instead of running to the max tier.
*/
function fakeRow(clientWidth: number, contentWidthAtTier: (tier: EditorToolbarTier) => number, tierSource: Ref<EditorToolbarTier>): HTMLElement {
const element = document.createElement("div");
Object.defineProperty(element, "clientWidth", { value: clientWidth, configurable: true });
Object.defineProperty(element, "scrollWidth", { configurable: true, get: () => contentWidthAtTier(tierSource.value) });
return element;
}
async function flush(ticks = 5) {
for (let i = 0; i < ticks; i += 1) {
await nextTick();
}
}
describe("useToolbarOverflow", () => {
beforeEach(() => {
document.body.innerHTML = "";
});
afterEach(() => {
document.body.innerHTML = "";
});
it("steps up while the row content overflows and keeps the tier when it merely fits again", async () => {
const { rootRef, tierRef, measure } = createHarness();
rootRef.value = fakeRow(200, (tier) => (tier >= 1 ? 180 : 260), tierRef);
await flush();
expect(tierRef.value).toBe(1);
// Widening without enough growth keeps the condensed tier: the row fits
// (slack 20px < 48px) and the pane grew 0px since the condensation. The
// explicit measure() stands in for the ResizeObserver event that a real
// browser fires after the box resize.
Object.defineProperty(rootRef.value!, "clientWidth", { value: 220, configurable: true });
measure();
expect(tierRef.value).toBe(1);
});
it("condenses tier by tier and caps at the max tier while still overflowing", async () => {
const { rootRef, tierRef } = createHarness();
rootRef.value = fakeRow(160, () => 400, tierRef);
await flush();
expect(tierRef.value).toBe(3);
});
it("steps down only with real slack and meaningful pane growth", async () => {
const { rootRef, tierRef, measure } = createHarness();
rootRef.value = fakeRow(200, (tier) => (tier >= 1 ? 180 : 260), tierRef);
await flush();
expect(tierRef.value).toBe(1);
// Grew by 10px only: below the 64px growth hysteresis, no step down even
// though the condensed row now has slack.
Object.defineProperty(rootRef.value!, "clientWidth", { value: 210, configurable: true });
measure();
expect(tierRef.value).toBe(1);
// Grew by 100px with 70px slack: both step-down thresholds pass.
Object.defineProperty(rootRef.value!, "clientWidth", { value: 300, configurable: true });
measure();
expect(tierRef.value).toBe(0);
});
it("re-measures when a revalidate source changes the control set", async () => {
const flag = ref(0);
const { rootRef, tierRef } = createHarness([() => flag.value]);
let contentGrew = false;
rootRef.value = fakeRow(200, (tier) => (tier >= 1 ? 180 : contentGrew ? 260 : 180), tierRef);
await flush();
expect(tierRef.value).toBe(0);
// A conditional control appeared: the content extent grew without the
// toolbar box resizing (no ResizeObserver event) — only the source watch
// re-measures this.
contentGrew = true;
flag.value += 1;
await flush();
expect(tierRef.value).toBe(1);
});
it("starts from the full layout when a row re-attaches instead of inheriting a stale tier", async () => {
const { rootRef, tierRef } = createHarness();
rootRef.value = fakeRow(200, () => 400, tierRef);
await flush();
expect(tierRef.value).toBe(3);
rootRef.value = fakeRow(300, () => 230, tierRef);
await flush();
expect(tierRef.value).toBe(0);
});
it("keeps the current tier while the row is unmeasured", async () => {
const { rootRef, tierRef } = createHarness();
rootRef.value = fakeRow(0, () => 0, tierRef);
await flush();
expect(tierRef.value).toBe(0);
});
});