feat(connection): choose group when creating a connection

This commit is contained in:
zipg authored and GitHub committed 2026-08-28 11:11:00 +08:00
1 parent 4636788f49
commit d8ec9bb8d0
13 files changed
+230 -39

No files matched your search

@@ -0,0 +1,110 @@
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ConnectionConfig, SidebarLayout } from "@/types/database";
function connection(id: string): ConnectionConfig {
return {
id,
name: id,
db_type: "mysql",
host: "127.0.0.1",
port: 3306,
username: "root",
password: "",
connect_timeout_secs: 10,
query_timeout_secs: 30,
connect_timeout_inherit: false,
query_timeout_inherit: false,
};
}
const groupedLayout: SidebarLayout = {
groups: [
{ id: "parent", name: "Parent", collapsed: false },
{ id: "child", name: "Child", collapsed: false },
],
order: [
{
type: "group",
id: "parent",
children: [{ type: "group", id: "child", children: [{ type: "connection", id: "existing" }] }],
},
],
};
describe("connectionStore creating connections in groups", () => {
beforeEach(() => {
vi.resetModules();
vi.stubGlobal("localStorage", {
getItem: vi.fn(() => null),
setItem: vi.fn(),
removeItem: vi.fn(),
});
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
loadEditorSettings: vi.fn().mockResolvedValue(null),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveEditorSettings: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
setActivePinia(createPinia());
});
it("places a new connection in the explicitly selected nested group", async () => {
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.sidebarLayout = structuredClone(groupedLayout);
await store.addConnection(connection("created"), "child");
expect(store.groupIdForConnection("created")).toBe("child");
expect(store.connectionGroupPaths.get("created")).toEqual(["Parent", "Child"]);
});
it("places a new connection at the top level when ungrouped is selected", async () => {
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.sidebarLayout = structuredClone(groupedLayout);
store.startCreatingConnectionInGroup("child");
await store.addConnection(connection("created"), null);
expect(store.groupIdForConnection("created")).toBeNull();
expect(store.sidebarLayout.order.at(-1)).toEqual({ type: "connection", id: "created" });
expect(store.newConnectionGroupId).toBeNull();
});
it("derives the default group from a selected descendant node", async () => {
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.sidebarLayout = structuredClone(groupedLayout);
store.addEphemeralConnection(connection("existing"));
store.treeNodes = [
{
id: "parent",
label: "Parent",
type: "connection-group",
children: [
{
id: "child",
label: "Child",
type: "connection-group",
children: [
{
id: "existing",
label: "existing",
type: "connection",
connectionId: "existing",
children: [{ id: "existing:demo", label: "demo", type: "database", connectionId: "existing" }],
},
],
},
],
},
];
store.selectedTreeNodeId = "existing:demo";
expect(store.selectedConnectionGroupId).toBe("child");
});
});
@@ -58,6 +58,8 @@ import {
reorderEntry as reorderEntryOp,
reorderEntries as reorderEntriesOp,
buildConnectionGroupPathMap,
connectionGroupDestinationRows,
connectionGroupIdForSelection,
connectionSidebarSearchAliases,
type DropPosition,
type ReorderEntriesOptions,
@@ -557,6 +559,12 @@ export const useConnectionStore = defineStore("connection", () => {
} | null>(null);
const sidebarLayout = ref<SidebarLayout>(emptyLayout());
const connectionGroupPaths = computed(() => buildConnectionGroupPathMap(sidebarLayout.value));
const connectionGroupOptions = computed(() => connectionGroupDestinationRows(sidebarLayout.value));
const selectedConnectionGroupId = computed(() => {
const selectedNodeId = selectedTreeNodeId.value;
const selectedNode = selectedNodeId ? findNode(treeNodes.value, selectedNodeId) : null;
return connectionGroupIdForSelection(sidebarLayout.value, selectedNodeId, selectedNode?.connectionId);
});
let layoutPersistTimer: ReturnType<typeof setTimeout> | null = null;
const staleTreeRefreshIds = new Set<string>();
const activeTreeRefreshGenerations = new Map<string, number>();
@@ -8749,6 +8757,8 @@ export const useConnectionStore = defineStore("connection", () => {
recordConnectionLostError,
sidebarLayout,
connectionGroupPaths,
connectionGroupOptions,
selectedConnectionGroupId,
getConfig,
connectionIdentifierQuote,
isTreeNodePinned,