25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
// @vitest-environment happy-dom
import { createApp, h, nextTick, reactive, ref, unref, type App, type Ref } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { afterEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
import type { SavedSqlFile } from "@/types/database";
import SqlLibrarySearchSnippet from "../SqlLibrarySearchSnippet.vue";
const mounted: Array<{ app: App; host: HTMLElement }> = [];
function sampleFile(sql: string): SavedSqlFile {
return {
id: "test-file-1",
connectionId: "conn-1",
name: "users_query.sql",
database: "main",
sql,
sqlLoaded: true,
createdAt: "2026-10-01T00:00:00Z",
updatedAt: "2026-10-01T00:00:00Z",
};
}
function mountSnippet(file: SavedSqlFile, query: string | Ref<string>, onSelectMatch?: (match: any) => void) {
setActivePinia(createPinia());
const host = document.createElement("div");
document.body.appendChild(host);
const app = createApp({
render: () => h(SqlLibrarySearchSnippet, { file, query: unref(query), onSelectMatch }),
});
app.use(i18n);
app.mount(host);
mounted.push({ app, host });
return host;
}
afterEach(() => {
for (const { app, host } of mounted.splice(0)) {
app.unmount();
host.remove();
}
});
describe("SqlLibrarySearchSnippet", () => {
it("renders nothing when there are no matches", async () => {
const host = mountSnippet(sampleFile("SELECT 1;"), "orders");
await nextTick();
const snippetContainer = host.querySelector(".dbx-sql-library-snippets");
expect(snippetContainer).toBeNull();
});
it("renders matching line with line number and highlight mark", async () => {
const sql = ["-- comments", "SELECT id, username FROM users", "WHERE active = true;"].join("\n");
const host = mountSnippet(sampleFile(sql), "users");
await nextTick();
const container = host.querySelector(".dbx-sql-library-snippets");
expect(container).not.toBeNull();
const snippetRows = host.querySelectorAll(".group\\/snippet");
expect(snippetRows).toHaveLength(1);
const lineBadge = snippetRows[0].querySelector("span");
expect(lineBadge?.textContent?.trim()).toBe("L2");
const mark = snippetRows[0].querySelector("mark");
expect(mark).not.toBeNull();
expect(mark?.textContent).toBe("users");
// Tooltip should contain multi-line context
const titleAttr = snippetRows[0].getAttribute("title");
expect(titleAttr).toContain("1 | -- comments");
expect(titleAttr).toContain("2 | SELECT id, username FROM users");
expect(titleAttr).toContain("3 | WHERE active = true;");
});
it("emits selectMatch with line and column on click", async () => {
const sql = "SELECT * FROM orders WHERE status = 'pending';";
const onSelectMatch = vi.fn();
const host = mountSnippet(sampleFile(sql), "orders", onSelectMatch);
await nextTick();
const snippetRow = host.querySelector<HTMLElement>(".group\\/snippet");
expect(snippetRow).not.toBeNull();
snippetRow!.click();
await nextTick();
expect(onSelectMatch).toHaveBeenCalledTimes(1);
expect(onSelectMatch).toHaveBeenCalledWith(
expect.objectContaining({
lineNumber: 1,
column: 15,
lineText: "SELECT * FROM orders WHERE status = 'pending';",
}),
);
});
it("limits to 3 matches initially and expands on button click", async () => {
const sql = ["SELECT 1 FROM tbl;", "SELECT 2 FROM tbl;", "SELECT 3 FROM tbl;", "SELECT 4 FROM tbl;", "SELECT 5 FROM tbl;"].join("\n");
const host = mountSnippet(sampleFile(sql), "SELECT");
await nextTick();
let snippetRows = host.querySelectorAll(".group\\/snippet");
expect(snippetRows).toHaveLength(3);
const toggleButton = host.querySelector<HTMLButtonElement>("button");
expect(toggleButton).not.toBeNull();
expect(toggleButton?.textContent).toContain("2");
// Click to expand
toggleButton!.click();
await nextTick();
snippetRows = host.querySelectorAll(".group\\/snippet");
expect(snippetRows).toHaveLength(5);
expect(toggleButton?.textContent?.trim()).toBe(i18n.global.t("sqlLibrary.collapseMatches"));
// Click to collapse
toggleButton!.click();
await nextTick();
snippetRows = host.querySelectorAll(".group\\/snippet");
expect(snippetRows).toHaveLength(3);
});
it("keeps expanded results across refreshes but resets for a different query", async () => {
const file = reactive(sampleFile(Array.from({ length: 5 }, () => "SELECT needle").join("\n")));
const query = ref("needle");
const host = mountSnippet(file, query);
await nextTick();
host.querySelector<HTMLButtonElement>("button")!.click();
await nextTick();
expect(host.querySelectorAll("mark")).toHaveLength(5);
file.sql += "\nSELECT needle";
await nextTick();
expect(host.querySelectorAll("mark")).toHaveLength(6);
query.value = "SELECT";
await nextTick();
expect(host.querySelectorAll("mark")).toHaveLength(3);
});
it("reports the preview cap without understating matching line counts", async () => {
const host = mountSnippet(sampleFile(Array.from({ length: 150 }, () => "SELECT needle").join("\n")), "needle");
await nextTick();
expect(host.querySelector<HTMLButtonElement>("button")?.textContent).toContain("147");
expect(host.textContent).toContain(i18n.global.t("sqlLibrary.matchPreviewLimit", { limit: 100, total: 150 }));
host.querySelector<HTMLButtonElement>("button")!.click();
await nextTick();
expect(host.querySelectorAll("mark")).toHaveLength(100);
});
});