|
| 1 | +/** |
| 2 | + * HTTP API + query schema tests. |
| 3 | + * |
| 4 | + * Part 1: HTTP routing tests using SELF.fetch for stateless endpoints only |
| 5 | + * (health, 404 — no DO interaction, no storage mutation). |
| 6 | + * |
| 7 | + * Part 2: Schema validation tests using parseAndValidateQuery directly |
| 8 | + * (covers orderBy alias, aggregate fns, select alias, groupBy, etc.) |
| 9 | + * |
| 10 | + * Data-dependent query tests are in operators-conformance.test.ts (at scale |
| 11 | + * against DuckDB) and convenience.test.ts (in-memory). |
| 12 | + */ |
| 13 | +import { describe, it, expect } from "vitest"; |
| 14 | +import { SELF } from "cloudflare:test"; |
| 15 | +import { parseAndValidateQuery } from "./query-schema.js"; |
| 16 | + |
| 17 | +// ── HTTP routing (stateless endpoints only) ───────────────────────────────── |
| 18 | + |
| 19 | +describe("HTTP routing", () => { |
| 20 | + it("GET /health returns ok", async () => { |
| 21 | + const res = await SELF.fetch("https://fake-host/health"); |
| 22 | + expect(res.status).toBe(200); |
| 23 | + expect(res.headers.get("content-type")).toBe("application/json"); |
| 24 | + expect(res.headers.get("x-querymode-request-id")).toBeDefined(); |
| 25 | + const body = await res.json() as Record<string, unknown>; |
| 26 | + expect(body.status).toBe("ok"); |
| 27 | + expect(body.service).toBe("querymode"); |
| 28 | + expect(body.timestamp).toBeDefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("GET /nonexistent returns 404", async () => { |
| 32 | + const res = await SELF.fetch("https://fake-host/nonexistent"); |
| 33 | + expect(res.status).toBe(404); |
| 34 | + const text = await res.text(); |
| 35 | + expect(text).toContain("/query"); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +// ── Schema validation ───────────────────────────────────────────────────── |
| 40 | + |
| 41 | +describe("parseAndValidateQuery", () => { |
| 42 | + it("parses minimal query", () => { |
| 43 | + const q = parseAndValidateQuery({ table: "events" }); |
| 44 | + expect(q.table).toBe("events"); |
| 45 | + expect(q.filters).toEqual([]); |
| 46 | + expect(q.projections).toEqual([]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("parses filter with all ops", () => { |
| 50 | + for (const op of ["eq", "neq", "gt", "gte", "lt", "lte", "in"]) { |
| 51 | + const value = op === "in" ? [1, 2] : 42; |
| 52 | + const q = parseAndValidateQuery({ |
| 53 | + table: "t", |
| 54 | + filters: [{ column: "x", op, value }], |
| 55 | + }); |
| 56 | + expect(q.filters[0].op).toBe(op); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it("parses sortColumn + sortDirection", () => { |
| 61 | + const q = parseAndValidateQuery({ |
| 62 | + table: "t", |
| 63 | + sortColumn: "amount", |
| 64 | + sortDirection: "desc", |
| 65 | + }); |
| 66 | + expect(q.sortColumn).toBe("amount"); |
| 67 | + expect(q.sortDirection).toBe("desc"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("parses orderBy alias → sortColumn/sortDirection", () => { |
| 71 | + const q = parseAndValidateQuery({ |
| 72 | + table: "t", |
| 73 | + orderBy: { column: "amount", desc: true }, |
| 74 | + }); |
| 75 | + expect(q.sortColumn).toBe("amount"); |
| 76 | + expect(q.sortDirection).toBe("desc"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("orderBy without desc defaults to asc", () => { |
| 80 | + const q = parseAndValidateQuery({ |
| 81 | + table: "t", |
| 82 | + orderBy: { column: "x" }, |
| 83 | + }); |
| 84 | + expect(q.sortColumn).toBe("x"); |
| 85 | + expect(q.sortDirection).toBe("asc"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("sortColumn takes precedence over orderBy", () => { |
| 89 | + const q = parseAndValidateQuery({ |
| 90 | + table: "t", |
| 91 | + sortColumn: "a", |
| 92 | + sortDirection: "desc", |
| 93 | + orderBy: { column: "b" }, |
| 94 | + }); |
| 95 | + expect(q.sortColumn).toBe("a"); |
| 96 | + expect(q.sortDirection).toBe("desc"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("parses select alias → projections", () => { |
| 100 | + const q = parseAndValidateQuery({ |
| 101 | + table: "t", |
| 102 | + select: ["id", "name"], |
| 103 | + }); |
| 104 | + expect(q.projections).toEqual(["id", "name"]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("projections takes precedence over select", () => { |
| 108 | + const q = parseAndValidateQuery({ |
| 109 | + table: "t", |
| 110 | + projections: ["a"], |
| 111 | + select: ["b"], |
| 112 | + }); |
| 113 | + expect(q.projections).toEqual(["a"]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("parses limit + offset", () => { |
| 117 | + const q = parseAndValidateQuery({ table: "t", limit: 10, offset: 5 }); |
| 118 | + expect(q.limit).toBe(10); |
| 119 | + expect(q.offset).toBe(5); |
| 120 | + }); |
| 121 | + |
| 122 | + it("parses groupBy", () => { |
| 123 | + const q = parseAndValidateQuery({ |
| 124 | + table: "t", |
| 125 | + groupBy: ["region", "category"], |
| 126 | + }); |
| 127 | + expect(q.groupBy).toEqual(["region", "category"]); |
| 128 | + }); |
| 129 | + |
| 130 | + it("parses cacheTTL", () => { |
| 131 | + const q = parseAndValidateQuery({ table: "t", cacheTTL: 60 }); |
| 132 | + expect(q.cacheTTL).toBe(60); |
| 133 | + }); |
| 134 | + |
| 135 | + // Aggregate functions |
| 136 | + for (const fn of ["sum", "avg", "min", "max", "count", "count_distinct", "stddev", "variance", "median", "percentile"]) { |
| 137 | + it(`accepts aggregate fn="${fn}"`, () => { |
| 138 | + const q = parseAndValidateQuery({ |
| 139 | + table: "t", |
| 140 | + aggregates: [{ fn, column: "x" }], |
| 141 | + }); |
| 142 | + expect(q.aggregates).toHaveLength(1); |
| 143 | + expect(q.aggregates![0].fn).toBe(fn); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + it("accepts percentileTarget", () => { |
| 148 | + const q = parseAndValidateQuery({ |
| 149 | + table: "t", |
| 150 | + aggregates: [{ fn: "percentile", column: "x", percentileTarget: 0.95 }], |
| 151 | + }); |
| 152 | + expect(q.aggregates![0].percentileTarget).toBe(0.95); |
| 153 | + }); |
| 154 | + |
| 155 | + it("accepts aggregate alias", () => { |
| 156 | + const q = parseAndValidateQuery({ |
| 157 | + table: "t", |
| 158 | + aggregates: [{ fn: "sum", column: "amount", alias: "total" }], |
| 159 | + }); |
| 160 | + expect(q.aggregates![0].alias).toBe("total"); |
| 161 | + }); |
| 162 | + |
| 163 | + // Rejection cases |
| 164 | + it("rejects empty table name", () => { |
| 165 | + expect(() => parseAndValidateQuery({ table: "" })).toThrow(/table/i); |
| 166 | + }); |
| 167 | + |
| 168 | + it("rejects missing table", () => { |
| 169 | + expect(() => parseAndValidateQuery({})).toThrow(); |
| 170 | + }); |
| 171 | + |
| 172 | + it("rejects invalid filter op", () => { |
| 173 | + expect(() => parseAndValidateQuery({ |
| 174 | + table: "t", |
| 175 | + filters: [{ column: "x", op: "INVALID", value: 1 }], |
| 176 | + })).toThrow(); |
| 177 | + }); |
| 178 | + |
| 179 | + it("rejects invalid aggregate fn", () => { |
| 180 | + expect(() => parseAndValidateQuery({ |
| 181 | + table: "t", |
| 182 | + aggregates: [{ fn: "BOGUS", column: "x" }], |
| 183 | + })).toThrow(); |
| 184 | + }); |
| 185 | + |
| 186 | + it("rejects empty filter column", () => { |
| 187 | + expect(() => parseAndValidateQuery({ |
| 188 | + table: "t", |
| 189 | + filters: [{ column: "", op: "eq", value: 1 }], |
| 190 | + })).toThrow(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("rejects negative limit", () => { |
| 194 | + expect(() => parseAndValidateQuery({ table: "t", limit: -1 })).toThrow(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("rejects negative offset", () => { |
| 198 | + expect(() => parseAndValidateQuery({ table: "t", offset: -5 })).toThrow(); |
| 199 | + }); |
| 200 | + |
| 201 | + it("rejects percentileTarget out of range", () => { |
| 202 | + expect(() => parseAndValidateQuery({ |
| 203 | + table: "t", |
| 204 | + aggregates: [{ fn: "percentile", column: "x", percentileTarget: 1.5 }], |
| 205 | + })).toThrow(); |
| 206 | + }); |
| 207 | +}); |
0 commit comments