refactor(desktop): organize lib modules by domain

This commit is contained in:
t8y2 committed 2026-07-04 13:53:52 +08:00
1 parent 90f0594e93
commit 7e7c4944e9
700 files changed
+1292 -1264

No files matched your search

@@ -0,0 +1,52 @@
import { Text } from "@codemirror/state";
import { describe, expect, it, vi } from "vitest";
import { executableStatementRangeCacheForDoc, executableStatementRangeStartingAt, type ExecutableStatementRangeParser } from "@/lib/sql/executableStatementRangeCache";
describe("executableStatementRangeCacheForDoc", () => {
it("reuses parsed executable statement ranges for the same document and database type", () => {
const doc = Text.of(["SELECT 1;", "SELECT 2;"]);
const parse = vi.fn<ExecutableStatementRangeParser>(() => [
{ from: 0, to: 8, sql: "SELECT 1" },
{ from: 10, to: 18, sql: "SELECT 2" },
]);
const first = executableStatementRangeCacheForDoc(null, doc, "mysql", parse);
const second = executableStatementRangeCacheForDoc(first, doc, "mysql", parse);
expect(second).toBe(first);
expect(parse).toHaveBeenCalledTimes(1);
expect(executableStatementRangeStartingAt(second, 10)?.sql).toBe("SELECT 2");
});
it("resolves the exact multi-line statement for a gutter run button", () => {
const doc = Text.of(["SELECT *", "FROM apis AS ap", "LIMIT 100;", "", "SELECT *", "FROM menus AS mn", "LIMIT 100;"]);
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
const secondStatementLine = doc.line(5);
expect(executableStatementRangeStartingAt(cache, secondStatementLine.from)?.sql).toBe("SELECT *\nFROM menus AS mn\nLIMIT 100");
});
it("rebuilds the cache when the document instance changes", () => {
const firstDoc = Text.of(["SELECT 1;"]);
const secondDoc = Text.of(["SELECT 1;"]);
const parse = vi.fn<ExecutableStatementRangeParser>(() => [{ from: 0, to: 8, sql: "SELECT 1" }]);
const first = executableStatementRangeCacheForDoc(null, firstDoc, "mysql", parse);
const second = executableStatementRangeCacheForDoc(first, secondDoc, "mysql", parse);
expect(second).not.toBe(first);
expect(parse).toHaveBeenCalledTimes(2);
});
it("rebuilds the cache when the database type changes", () => {
const doc = Text.of(["SELECT 1;"]);
const parse = vi.fn<ExecutableStatementRangeParser>(() => [{ from: 0, to: 8, sql: "SELECT 1" }]);
const mysql = executableStatementRangeCacheForDoc(null, doc, "mysql", parse);
const postgres = executableStatementRangeCacheForDoc(mysql, doc, "postgres", parse);
expect(postgres).not.toBe(mysql);
expect(parse).toHaveBeenCalledTimes(2);
});
});