fix(editor): move large SQL analysis off the input path
This commit is contained in:
30 files changed
+1241
-211
No files matched your search
@@ -0,0 +1,124 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { createSqlAnalysisWorker } from "@/lib/sql/sqlAnalysisWorker";
|
||||
|
||||
class ControlledWorker {
|
||||
onmessage: Worker["onmessage"] = null;
|
||||
onerror: Worker["onerror"] = null;
|
||||
onmessageerror: Worker["onmessageerror"] = null;
|
||||
postMessage = vi.fn();
|
||||
terminate = vi.fn();
|
||||
|
||||
reply(id: number, result: string) {
|
||||
this.onmessage?.call(this as unknown as Worker, { data: { id, result } } as MessageEvent);
|
||||
}
|
||||
|
||||
fail() {
|
||||
this.onerror?.call(this as unknown as Worker, {} as ErrorEvent);
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
function harness() {
|
||||
vi.stubGlobal("Worker", ControlledWorker);
|
||||
const worker = new ControlledWorker();
|
||||
const create = vi.fn(() => worker as unknown as Worker);
|
||||
const fallback = vi.fn((input: string) => `fallback:${input}`);
|
||||
const service = createSqlAnalysisWorker(create, fallback);
|
||||
return { worker, create, fallback, service };
|
||||
}
|
||||
|
||||
describe("SQL background analysis scheduling", () => {
|
||||
it("does not run analysis on the UI thread and reuses the worker", async () => {
|
||||
const { worker, create, fallback, service } = harness();
|
||||
const first = service.analyze("first");
|
||||
expect(fallback).not.toHaveBeenCalled();
|
||||
worker.reply(1, "first result");
|
||||
await expect(first).resolves.toBe("first result");
|
||||
const second = service.analyze("second");
|
||||
worker.reply(2, "second result");
|
||||
await expect(second).resolves.toBe("second result");
|
||||
expect(create).toHaveBeenCalledTimes(1);
|
||||
service.dispose();
|
||||
expect(worker.terminate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps at most the running request and the newest queued edit", async () => {
|
||||
const { worker, fallback, service } = harness();
|
||||
const first = service.analyze("first");
|
||||
const second = service.analyze("second");
|
||||
const third = service.analyze("third");
|
||||
await expect(first).resolves.toBeNull();
|
||||
await expect(second).resolves.toBeNull();
|
||||
expect(worker.postMessage).toHaveBeenCalledTimes(1);
|
||||
worker.reply(1, "obsolete");
|
||||
expect(worker.postMessage).toHaveBeenLastCalledWith({ id: 3, input: "third" });
|
||||
worker.reply(2, "out of order");
|
||||
worker.reply(3, "current");
|
||||
await expect(third).resolves.toBe("current");
|
||||
expect(fallback).not.toHaveBeenCalled();
|
||||
service.dispose();
|
||||
});
|
||||
|
||||
it("settles both requests when cancelled without losing the warm worker", async () => {
|
||||
const { worker, create, service } = harness();
|
||||
const first = service.analyze("first");
|
||||
const second = service.analyze("second");
|
||||
service.cancel();
|
||||
await expect(first).resolves.toBeNull();
|
||||
await expect(second).resolves.toBeNull();
|
||||
worker.reply(1, "obsolete");
|
||||
expect(worker.postMessage).toHaveBeenCalledTimes(1);
|
||||
const next = service.analyze("next");
|
||||
worker.reply(3, "next result");
|
||||
await expect(next).resolves.toBe("next result");
|
||||
expect(create).toHaveBeenCalledTimes(1);
|
||||
service.dispose();
|
||||
});
|
||||
|
||||
it("ignores late events after disposal", async () => {
|
||||
const { worker, service, fallback } = harness();
|
||||
const pending = service.analyze("pending");
|
||||
service.dispose();
|
||||
worker.reply(1, "late");
|
||||
worker.fail();
|
||||
await expect(pending).resolves.toBeNull();
|
||||
expect(fallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back only for the newest request when a worker fails", async () => {
|
||||
const { worker, service, fallback } = harness();
|
||||
const first = service.analyze("first");
|
||||
const latest = service.analyze("latest");
|
||||
worker.fail();
|
||||
await expect(first).resolves.toBeNull();
|
||||
await expect(latest).resolves.toBe("fallback:latest");
|
||||
expect(fallback.mock.calls).toEqual([["latest"]]);
|
||||
await expect(service.analyze("next")).resolves.toBe("fallback:next");
|
||||
});
|
||||
|
||||
it("preserves functionality when worker construction or message cloning fails", async () => {
|
||||
const { worker, service, fallback } = harness();
|
||||
worker.postMessage.mockImplementation(() => {
|
||||
throw new Error("clone failed");
|
||||
});
|
||||
await expect(service.analyze("clone")).resolves.toBe("fallback:clone");
|
||||
const unavailable = createSqlAnalysisWorker(() => {
|
||||
throw new Error("blocked by CSP");
|
||||
}, fallback);
|
||||
await expect(unavailable.analyze("CSP")).resolves.toBe("fallback:CSP");
|
||||
});
|
||||
|
||||
it("uses the caller's editor-aware fallback and handles its exceptions", async () => {
|
||||
vi.stubGlobal("Worker", undefined);
|
||||
const create = vi.fn();
|
||||
const service = createSqlAnalysisWorker<string, string>(create, () => "default");
|
||||
await expect(service.analyze("SQL", () => "editor state")).resolves.toBe("editor state");
|
||||
await expect(
|
||||
service.analyze("invalid", () => {
|
||||
throw new Error("invalid");
|
||||
}),
|
||||
).resolves.toBeNull();
|
||||
expect(create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import * as langSql from "@codemirror/lang-sql";
|
||||
import { ensureSyntaxTree } from "@codemirror/language";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect";
|
||||
import { analyzeSqlCompletion, SqlCompletionAnalysisEngine, type SqlCompletionAnalysisRequest } from "@/lib/sql/sqlCompletionAnalysis";
|
||||
import { analyzeSqlStatements } from "@/lib/sql/sqlStatementAnalysis";
|
||||
import { executableStatementRangeCacheForDoc } from "@/lib/sql/executableStatementRangeCache";
|
||||
import { analyzeSqlDiagnostics } from "@/lib/sql/sqlDiagnosticAnalysis";
|
||||
import { sqlSemanticDiagnosticRangesForViewport, sqlServerRoutineDefinitionRangesForViewport } from "@/lib/sql/semantic/diagnostics";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
import { computeBlockFoldRanges } from "@/lib/editor/codemirrorSqlBlockFolding";
|
||||
|
||||
describe("background SQL completion parity", () => {
|
||||
it.each([
|
||||
["mysql", "WITH recent(id) AS (SELECT id FROM orders) SELECT recent. FROM recent"],
|
||||
["postgres", "SELECT nextval('orders_id_seq')"],
|
||||
["oracle", "SELECT * FROM orders@remote"],
|
||||
["sqlserver", "USE reporting; SELECT * FROM dbo.orders WHERE "],
|
||||
["mysql", "SELECT 'literal; still literal' -- comment"],
|
||||
] as const)("preserves %s context and trigger policy", (databaseType, sql) => {
|
||||
const dialect = databaseType === "oracle" ? "mysql" : databaseType;
|
||||
const request: SqlCompletionAnalysisRequest = { sql, cursor: sql.indexOf("recent.") >= 0 ? sql.indexOf("recent.") + 7 : sql.length, databaseType, dialect, semanticCompletionEnabled: true };
|
||||
const state = EditorState.create({ doc: sql, extensions: [langSql.sql({ dialect: createDbxCodeMirrorSqlDialect(langSql, dialect, databaseType) })] });
|
||||
ensureSyntaxTree(state, sql.length, 1000);
|
||||
const expected = analyzeSqlCompletion(request, state);
|
||||
expect(new SqlCompletionAnalysisEngine().analyze(request)).toEqual(expected);
|
||||
expect(structuredClone(expected)).toEqual(expected);
|
||||
});
|
||||
|
||||
it("preserves distant CTE columns in an oversized statement", () => {
|
||||
const sql = `WITH recent(id, total) AS (SELECT id, total FROM orders WHERE 1=1\n${"AND 1=1\n".repeat(5_000)}) SELECT recent. FROM recent`;
|
||||
const engine = new SqlCompletionAnalysisEngine();
|
||||
const request: SqlCompletionAnalysisRequest = { sql, cursor: sql.lastIndexOf("recent.") + 7, databaseType: "mysql", dialect: "mysql", semanticCompletionEnabled: true };
|
||||
const result = engine.analyze(request);
|
||||
expect(result.completionContext.qualifier).toBe("recent");
|
||||
expect(result.cteDefinitions).toContainEqual({ name: "recent", columns: ["id", "total"] });
|
||||
const edited = { ...request, sql: sql.slice(0, request.cursor) + "i" + sql.slice(request.cursor), cursor: request.cursor + 1 };
|
||||
expect(engine.analyze(edited)).toEqual(new SqlCompletionAnalysisEngine().analyze(edited));
|
||||
const replaced = { ...request, sql: "SELECT o. FROM orders o", cursor: 9 };
|
||||
expect(engine.analyze(replaced)).toEqual(new SqlCompletionAnalysisEngine().analyze(replaced));
|
||||
});
|
||||
});
|
||||
|
||||
describe("background statement and diagnostic parity", () => {
|
||||
it.each([
|
||||
["postgres", "DO $body$ BEGIN PERFORM 1; PERFORM 2; END $body$;\nSELECT 3;"],
|
||||
["mysql", "DELIMITER $$\nCREATE PROCEDURE p() BEGIN SELECT 1; SELECT 2; END$$\nDELIMITER ;\nSELECT 3;"],
|
||||
["oracle", "BEGIN\n NULL;\n NULL;\nEND;\n/\nSELECT 3 FROM dual;"],
|
||||
["sqlserver", "CREATE PROCEDURE p AS\nBEGIN\n SELECT 1;\n SELECT 2;\nEND\nGO\nSELECT 3;"],
|
||||
["mysql", "/*+ optimizer hint */ SELECT #{value};\nSELECT 'escaped;literal';"],
|
||||
] as const)("keeps exact %s execution boundaries and viewport diagnostics", (databaseType, sql) => {
|
||||
const request = { sql, databaseType: databaseType as DatabaseType, parameterOptions: { enabledSyntaxes: ["mybatis" as const] } };
|
||||
const { doc: _doc, ...expected } = executableStatementRangeCacheForDoc(null, EditorState.create({ doc: sql }).doc, databaseType, request.parameterOptions);
|
||||
const result = structuredClone(analyzeSqlStatements(request));
|
||||
expect(result).toEqual(expected);
|
||||
const dialect = databaseType === "oracle" ? "mysql" : databaseType;
|
||||
const state = EditorState.create({ doc: sql, extensions: [langSql.sql({ dialect: createDbxCodeMirrorSqlDialect(langSql, dialect, databaseType) })] });
|
||||
ensureSyntaxTree(state, sql.length, 1000);
|
||||
expect(analyzeSqlStatements({ ...request, includeFolds: true, syntaxDialect: dialect }).folds).toEqual(computeBlockFoldRanges(state, databaseType));
|
||||
const visibleRanges = [{ from: 0, to: sql.length }];
|
||||
const diagnostics = analyzeSqlDiagnostics({ ...request, cursor: 0, visibleRanges }).document!;
|
||||
expect(diagnostics.shouldRun).toBe(true);
|
||||
expect(diagnostics.diagnosticRanges).toEqual(sqlSemanticDiagnosticRangesForViewport(sql, visibleRanges, databaseType, undefined, request.parameterOptions));
|
||||
expect(diagnostics.sqlServerRoutineRanges).toEqual(databaseType === "sqlserver" ? sqlServerRoutineDefinitionRangesForViewport(sql, visibleRanges) : []);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user