|
| 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 { unqualifiedColumnCompletionSource } from "../column-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 = unqualifiedColumnCompletionSource( |
| 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("unqualifiedColumnCompletionSource", () => { |
| 48 | + it("offers the FROM table's columns for a bare prefix", async () => { |
| 49 | + const result = await complete("SELECT e FROM users", { |
| 50 | + schema, |
| 51 | + pos: "SELECT e".length, |
| 52 | + }); |
| 53 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 54 | + expect(result?.from).toBe("SELECT ".length); |
| 55 | + expect(result?.options.every((option) => option.type === "property")).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it("completes in ORDER BY position", async () => { |
| 59 | + const doc = "SELECT id FROM users ORDER BY user"; |
| 60 | + const result = await complete(doc, { schema, pos: doc.length }); |
| 61 | + expect(labels(result)).toContain("username"); |
| 62 | + expect(result?.from).toBe(doc.indexOf("user", doc.indexOf("ORDER BY"))); |
| 63 | + }); |
| 64 | + |
| 65 | + it("only offers columns of the referenced table when the schema has many", async () => { |
| 66 | + const result = await complete("SELECT e FROM users", { |
| 67 | + schema, |
| 68 | + pos: "SELECT e".length, |
| 69 | + }); |
| 70 | + expect(labels(result)).not.toContain("total"); |
| 71 | + expect(labels(result)).not.toContain("order_id"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("offers the union of all joined tables' columns with per-table details", async () => { |
| 75 | + const doc = "SELECT FROM users u JOIN orders o ON u.id = o.user_id"; |
| 76 | + const result = await complete(doc, { |
| 77 | + schema, |
| 78 | + pos: "SELECT ".length, |
| 79 | + explicit: true, |
| 80 | + }); |
| 81 | + expect(labels(result)).toEqual(["id", "username", "email", "order_id", "total"]); |
| 82 | + expect(result?.options.find((option) => option.label === "email")?.detail).toBe( |
| 83 | + "column of users", |
| 84 | + ); |
| 85 | + expect(result?.options.find((option) => option.label === "total")?.detail).toBe( |
| 86 | + "column of orders", |
| 87 | + ); |
| 88 | + // Schema-provided details are preserved |
| 89 | + expect(result?.options.find((option) => option.label === "order_id")?.detail).toBe( |
| 90 | + "Order ID", |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("offers a duplicate column name once, from the first table", async () => { |
| 95 | + const dupSchema: SQLNamespace = { a: ["id", "x"], b: ["id", "y"] }; |
| 96 | + const doc = "SELECT FROM a JOIN b ON a.id = b.id"; |
| 97 | + const result = await complete(doc, { schema: dupSchema, pos: "SELECT ".length, explicit: true }); |
| 98 | + expect(labels(result)).toEqual(["id", "x", "y"]); |
| 99 | + expect(result?.options.find((option) => option.label === "id")?.detail).toBe("column of a"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns null after a dot", async () => { |
| 103 | + const result = await complete("SELECT u. FROM users u", { |
| 104 | + schema, |
| 105 | + pos: "SELECT u.".length, |
| 106 | + explicit: true, |
| 107 | + }); |
| 108 | + expect(result).toBeNull(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("returns null in table-name position", async () => { |
| 112 | + expect(await complete("SELECT id FROM use", { schema, pos: "SELECT id FROM use".length })).toBeNull(); |
| 113 | + const doc = "SELECT id FROM users JOIN ord"; |
| 114 | + expect(await complete(doc, { schema, pos: doc.length })).toBeNull(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("returns null in a comma-continued FROM table list", async () => { |
| 118 | + expect(await complete("SELECT id FROM users, ord", { schema })).toBeNull(); |
| 119 | + expect(await complete("SELECT id FROM users u, ord", { schema })).toBeNull(); |
| 120 | + expect(await complete("SELECT id FROM users AS u, orders o, ord", { schema })).toBeNull(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("still completes after commas in non-FROM clauses", async () => { |
| 124 | + const groupBy = "SELECT id FROM users GROUP BY id, user"; |
| 125 | + expect(labels(await complete(groupBy, { schema }))).toContain("username"); |
| 126 | + const selectList = "SELECT id, e FROM users"; |
| 127 | + expect(labels(await complete(selectList, { schema, pos: "SELECT id, e".length }))).toContain( |
| 128 | + "email", |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("returns null inside string literals and comments", async () => { |
| 133 | + const inString = "SELECT id FROM users WHERE name = 'em"; |
| 134 | + expect(await complete(inString, { schema })).toBeNull(); |
| 135 | + const inLineComment = "SELECT id FROM users -- em"; |
| 136 | + expect(await complete(inLineComment, { schema })).toBeNull(); |
| 137 | + const inBlockComment = "SELECT id FROM users /* em"; |
| 138 | + expect(await complete(inBlockComment, { schema })).toBeNull(); |
| 139 | + }); |
| 140 | + |
| 141 | + it("requires a prefix unless the request is explicit", async () => { |
| 142 | + const doc = "SELECT FROM users"; |
| 143 | + expect(await complete(doc, { schema, pos: "SELECT ".length })).toBeNull(); |
| 144 | + expect(labels(await complete(doc, { schema, pos: "SELECT ".length, explicit: true }))).toEqual([ |
| 145 | + "id", |
| 146 | + "username", |
| 147 | + "email", |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("returns null when the statement has no FROM clause yet", async () => { |
| 152 | + const result = await complete("SELECT em", { schema, pos: "SELECT em".length }); |
| 153 | + expect(result).toBeNull(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("still completes while the statement is mid-edit (unparsable)", async () => { |
| 157 | + const result = await complete("SELECT em FROM users WHERE", { |
| 158 | + schema, |
| 159 | + pos: "SELECT em".length, |
| 160 | + }); |
| 161 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("scopes tables to the statement containing the cursor", async () => { |
| 165 | + const doc = "SELECT id FROM users; SELECT t FROM orders"; |
| 166 | + const result = await complete(doc, { schema, pos: doc.indexOf("t FROM orders") + 1 }); |
| 167 | + expect(labels(result)).toEqual(["order_id", "total"]); |
| 168 | + }); |
| 169 | + |
| 170 | + it("completes columns of an aliased table", async () => { |
| 171 | + const result = await complete("SELECT e FROM users u", { |
| 172 | + schema, |
| 173 | + pos: "SELECT e".length, |
| 174 | + }); |
| 175 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 176 | + }); |
| 177 | + |
| 178 | + it("resolves tables nested in the schema", async () => { |
| 179 | + const qualified = await complete("SELECT user FROM mydb.users", { |
| 180 | + schema: nestedSchema, |
| 181 | + pos: "SELECT user".length, |
| 182 | + }); |
| 183 | + expect(labels(qualified)).toEqual(["id", "username"]); |
| 184 | + |
| 185 | + const underQualified = await complete("SELECT user FROM users", { |
| 186 | + schema: nestedSchema, |
| 187 | + pos: "SELECT user".length, |
| 188 | + }); |
| 189 | + expect(labels(underQualified)).toEqual(["id", "username"]); |
| 190 | + }); |
| 191 | + |
| 192 | + it("returns null when an under-qualified table is ambiguous across schemas", async () => { |
| 193 | + const ambiguousSchema: SQLNamespace = { |
| 194 | + db1: { users: ["id"] }, |
| 195 | + db2: { users: ["email"] }, |
| 196 | + }; |
| 197 | + const result = await complete("SELECT i FROM users", { |
| 198 | + schema: ambiguousSchema, |
| 199 | + pos: "SELECT i".length, |
| 200 | + }); |
| 201 | + expect(result).toBeNull(); |
| 202 | + }); |
| 203 | + |
| 204 | + it("offers CTE columns when the FROM table is a CTE", async () => { |
| 205 | + const doc = "WITH recent AS (SELECT x, y FROM logs) SELECT x FROM recent"; |
| 206 | + const result = await complete(doc, { schema, pos: doc.indexOf("x FROM recent") + 1 }); |
| 207 | + const resultLabels = labels(result); |
| 208 | + expect(resultLabels).toContain("x"); |
| 209 | + expect(resultLabels).toContain("y"); |
| 210 | + expect(result?.options.find((option) => option.label === "x")?.detail).toBe( |
| 211 | + "column of recent", |
| 212 | + ); |
| 213 | + }); |
| 214 | + |
| 215 | + it("completes columns of a quoted table name", async () => { |
| 216 | + const quotedSchema: SQLNamespace = { "User Table": ["id", "full_name"] }; |
| 217 | + const doc = 'SELECT full FROM "User Table"'; |
| 218 | + const result = await complete(doc, { schema: quotedSchema, pos: "SELECT full".length }); |
| 219 | + expect(labels(result)).toEqual(["id", "full_name"]); |
| 220 | + }); |
| 221 | + |
| 222 | + it("completes columns of a quoted table name containing a dot", async () => { |
| 223 | + const dottedSchema: SQLNamespace = { "my.table": ["id", "email"] }; |
| 224 | + const doc = 'SELECT e FROM "my.table"'; |
| 225 | + const result = await complete(doc, { schema: dottedSchema, pos: "SELECT e".length }); |
| 226 | + expect(labels(result)).toEqual(["id", "email"]); |
| 227 | + }); |
| 228 | + |
| 229 | + it("falls back to the sqlSchemaFacet when no schema is configured", async () => { |
| 230 | + const result = await complete("SELECT e FROM users", { |
| 231 | + facetSchema: schema, |
| 232 | + pos: "SELECT e".length, |
| 233 | + }); |
| 234 | + expect(labels(result)).toEqual(["id", "username", "email"]); |
| 235 | + }); |
| 236 | + |
| 237 | + it("returns null when the table is not in the schema", async () => { |
| 238 | + const result = await complete("SELECT x FROM missing", { |
| 239 | + schema, |
| 240 | + pos: "SELECT x".length, |
| 241 | + }); |
| 242 | + expect(result).toBeNull(); |
| 243 | + }); |
| 244 | + |
| 245 | + it("boosts columns while preserving schema-provided boosts", async () => { |
| 246 | + const boostedSchema: SQLNamespace = { |
| 247 | + users: ["id", { label: "email", boost: 5 }], |
| 248 | + }; |
| 249 | + const result = await complete("SELECT e FROM users", { |
| 250 | + schema: boostedSchema, |
| 251 | + pos: "SELECT e".length, |
| 252 | + }); |
| 253 | + expect(result?.options.find((option) => option.label === "id")?.boost).toBe(1); |
| 254 | + expect(result?.options.find((option) => option.label === "email")?.boost).toBe(5); |
| 255 | + }); |
| 256 | +}); |
0 commit comments