|
| 1 | +import { CompletionContext, type CompletionResult } from "@codemirror/autocomplete"; |
| 2 | +import type { SQLNamespace } from "@codemirror/lang-sql"; |
| 3 | +import { EditorState } from "@codemirror/state"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { aliasColumnCompletionSource } from "../alias-completion-source.js"; |
| 6 | +import { sqlSchemaFacet } from "../schema-facet.js"; |
| 7 | + |
| 8 | +const schema: SQLNamespace = { |
| 9 | + users: ["id", "username", "email"], |
| 10 | + orders: [ |
| 11 | + { label: "order_id", detail: "Order ID", type: "property" }, |
| 12 | + "total", |
| 13 | + ], |
| 14 | +}; |
| 15 | + |
| 16 | +const nestedSchema: SQLNamespace = { |
| 17 | + mydb: { |
| 18 | + users: ["id", "username"], |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +async function complete( |
| 23 | + doc: string, |
| 24 | + opts: { |
| 25 | + schema?: SQLNamespace; |
| 26 | + pos?: number; |
| 27 | + explicit?: boolean; |
| 28 | + facetSchema?: SQLNamespace; |
| 29 | + } = {}, |
| 30 | +): Promise<CompletionResult | null> { |
| 31 | + const source = aliasColumnCompletionSource( |
| 32 | + opts.schema === undefined && opts.facetSchema !== undefined ? {} : { schema: opts.schema }, |
| 33 | + ); |
| 34 | + const state = EditorState.create({ |
| 35 | + doc, |
| 36 | + extensions: opts.facetSchema !== undefined ? [sqlSchemaFacet.of(opts.facetSchema)] : [], |
| 37 | + }); |
| 38 | + const pos = opts.pos ?? doc.length; |
| 39 | + const context = new CompletionContext(state, pos, opts.explicit ?? false); |
| 40 | + return (await source(context)) as CompletionResult | null; |
| 41 | +} |
| 42 | + |
| 43 | +function labels(result: CompletionResult | null): string[] { |
| 44 | + return (result?.options ?? []).map((option) => option.label); |
| 45 | +} |
| 46 | + |
| 47 | +describe("aliasColumnCompletionSource", () => { |
| 48 | + it("offers the aliased table's columns after `alias.`", async () => { |
| 49 | + const result = await complete("SELECT u. FROM users u", { |
| 50 | + schema, |
| 51 | + pos: "SELECT u.".length, |
| 52 | + }); |
| 53 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 54 | + expect(result?.from).toBe("SELECT u.".length); |
| 55 | + expect(result?.options.every((option) => option.type === "property")).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it("completes with a typed prefix and keeps `from` after the dot", async () => { |
| 59 | + const doc = "SELECT u.user FROM users u"; |
| 60 | + const result = await complete(doc, { schema, pos: "SELECT u.user".length }); |
| 61 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 62 | + expect(result?.from).toBe("SELECT u.".length); |
| 63 | + expect(result?.validFor).toBeTruthy(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("works on explicit completion requests", async () => { |
| 67 | + const result = await complete("SELECT u. FROM users u", { |
| 68 | + schema, |
| 69 | + pos: "SELECT u.".length, |
| 70 | + explicit: true, |
| 71 | + }); |
| 72 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("resolves each alias in a join to its own table", async () => { |
| 76 | + const doc = "SELECT o. FROM users u JOIN orders o ON u.id = o.user_id"; |
| 77 | + const result = await complete(doc, { schema, pos: "SELECT o.".length }); |
| 78 | + expect(labels(result)).toEqual(["order_id", "total"]); |
| 79 | + // Completion objects from the schema are preserved |
| 80 | + expect(result?.options.find((option) => option.label === "order_id")?.detail).toBe( |
| 81 | + "Order ID", |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it("resolves aliases of tables nested in the schema", async () => { |
| 86 | + const result = await complete("SELECT u. FROM mydb.users u", { |
| 87 | + schema: nestedSchema, |
| 88 | + pos: "SELECT u.".length, |
| 89 | + }); |
| 90 | + expect(labels(result)).toEqual(["id", "username"]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("offers CTE columns for a CTE alias", async () => { |
| 94 | + const doc = "WITH recent AS (SELECT x, y FROM logs) SELECT r. FROM recent r"; |
| 95 | + const result = await complete(doc, { schema, pos: doc.indexOf("r. FROM") + 2 }); |
| 96 | + expect(labels(result)).toEqual(["x", "y"]); |
| 97 | + }); |
| 98 | + |
| 99 | + it("falls back to the sqlSchemaFacet when no schema is configured", async () => { |
| 100 | + const result = await complete("SELECT u. FROM users u", { |
| 101 | + facetSchema: schema, |
| 102 | + pos: "SELECT u.".length, |
| 103 | + }); |
| 104 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("returns null when the qualifier is not an alias", async () => { |
| 108 | + const result = await complete("SELECT x. FROM users u", { |
| 109 | + schema, |
| 110 | + pos: "SELECT x.".length, |
| 111 | + }); |
| 112 | + expect(result).toBeNull(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("returns null without a dot before the cursor", async () => { |
| 116 | + const result = await complete("SELECT u FROM users u", { |
| 117 | + schema, |
| 118 | + pos: "SELECT u".length, |
| 119 | + }); |
| 120 | + expect(result).toBeNull(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("ignores multi-segment qualifiers like `db.table.`", async () => { |
| 124 | + const doc = "SELECT mydb.users. FROM mydb.users u"; |
| 125 | + const result = await complete(doc, { schema: nestedSchema, pos: "SELECT mydb.users.".length }); |
| 126 | + expect(result).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("scopes aliases to the statement containing the cursor", async () => { |
| 130 | + const doc = "SELECT id FROM users u; SELECT u. FROM orders o"; |
| 131 | + const result = await complete(doc, { schema, pos: doc.indexOf("u. FROM orders") + 2 }); |
| 132 | + // `u` is aliased in statement 1, not statement 2 |
| 133 | + expect(result).toBeNull(); |
| 134 | + }); |
| 135 | + |
| 136 | + it("still completes while the statement is mid-edit (unparsable)", async () => { |
| 137 | + const doc = "SELECT u. FROM users u WHERE"; |
| 138 | + const result = await complete(doc, { schema, pos: "SELECT u.".length }); |
| 139 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("completes after a quoted alias qualifier", async () => { |
| 143 | + const quotedSchema: SQLNamespace = { "User Table": ["id", "full_name"] }; |
| 144 | + const doc = 'SELECT "ut". FROM "User Table" ut'; |
| 145 | + const result = await complete(doc, { schema: quotedSchema, pos: 'SELECT "ut".'.length }); |
| 146 | + expect(labels(result)).toEqual(["id", "full_name"]); |
| 147 | + }); |
| 148 | + |
| 149 | + it("forces the property type but keeps schema-provided details", async () => { |
| 150 | + const typedSchema: SQLNamespace = { |
| 151 | + users: [{ label: "id", type: "keyword", detail: "Primary key" }], |
| 152 | + }; |
| 153 | + const result = await complete("SELECT u. FROM users u", { |
| 154 | + schema: typedSchema, |
| 155 | + pos: "SELECT u.".length, |
| 156 | + }); |
| 157 | + expect(result?.options).toEqual([ |
| 158 | + { label: "id", type: "property", detail: "Primary key" }, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it("returns null when the aliased table is not in the schema", async () => { |
| 163 | + const result = await complete("SELECT m. FROM missing m", { |
| 164 | + schema, |
| 165 | + pos: "SELECT m.".length, |
| 166 | + }); |
| 167 | + expect(result).toBeNull(); |
| 168 | + }); |
| 169 | +}); |
0 commit comments