Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 resolveConfiguredShape,
4 type ConfiguredTable,
5} from '../../packages/server/src/definition.js'
6const tables: ConfiguredTable[] = [
7 {
8 namespace: 'public',
9 table: 'items',
10 primaryKey: ['id'],
11 columns: {
12 id: { type: 'int8' },
13 label: { type: 'text' },
14 enabled: { type: 'bool' },
15 },
16 },
17]
18async function resolve(params: Record<string, string | undefined>) {
19 const url = new URL('https://proxy.test/shapes?table=items')
20 for (const [key, value] of Object.entries(params))
21 if (value !== undefined) url.searchParams.set(key, value)
22 return resolveConfiguredShape(url, tables)
23}
24describe('configured root-table definitions with WHERE passthrough', () => {
25 it('resolves only configured identifiers and keeps hidden primary keys in source projection', async () => {
26 const shape = await resolve({
27 table: '"public"."items"',
28 columns: '"label",enabled',
29 log: 'full',
30 replica: 'default',
31 })
32 expect(shape.query.sql).toBe(
33 'SELECT "label", "enabled", "id" FROM "public"."items"'
34 )
35 expect(shape.schema).toEqual({
36 label: { type: 'text' },
37 enabled: { type: 'bool' },
38 })
39 expect(shape.projection).toEqual(['label', 'enabled'])
40 })
41 it('preserves the original expression and sends scalar parameters separately in positional order', async () => {
42 const where =
43 "id > $2 AND label = 'a''b' AND enabled = $1 OR id > 9007199254740993"
44 const shape = await resolve({
45 where,
46 params: JSON.stringify({ '1': true, '2': '9007199254740993' }),
47 })
48 expect(shape.query).toEqual({
49 sql: `SELECT "id", "label", "enabled" FROM "public"."items" WHERE (\n${where}\n)`,
50 parameters: [true, '9007199254740993'],
51 })
52 expect(
53 (
54 await resolve({
55 where: 'id >= 9007199254740993 AND id < $1',
56 params: '["9999999999999999"]',
57 })
58 ).query.parameters
59 ).toEqual(['9999999999999999'])
60 })
61 it('preserves bracketed positional parameters used by existing trusted proxies', async () => {
62 const shape = await resolve({
63 where: 'label = $1 AND id > $2',
64 'params[1]': "a'b + %",
65 'params[2]': '9007199254740993',
66 })
67 expect(shape.query.parameters).toEqual(["a'b + %", '9007199254740993'])
68 expect(shape.query.sql).toContain('WHERE (\nlabel = $1 AND id > $2\n)')
69 expect(() =>
70 resolveConfiguredShape(
71 new URL(
72 'http://local/?table=items&where=id%3D%241¶ms[1]=1¶ms[1]=2'
73 ),
74 tables
75 )
76 ).toThrow('repeated')
77 })
78 it.each([
79 'id = 1 OR id = 2',
80 'id IN (SELECT item_id FROM memberships WHERE tenant_id = $1)',
81 'EXISTS (SELECT 1 FROM memberships m WHERE m.item_id = items.id AND m.tenant_id = $1)',
82 'NOT EXISTS (SELECT 1 FROM memberships m WHERE m.item_id = items.id)',
83 'id IN (SELECT item_id FROM a UNION SELECT item_id FROM b)',
84 'CASE WHEN enabled THEN id > $1 ELSE label IS NOT NULL END',
85 "label ILIKE '%a%' AND id BETWEEN 1 AND 10",
86 "id = ANY ('{1,2}'::bigint[])",
87 'id = $1 OR id = $1 /* repeated bound position */',
88 "label = '$5' OR label = $$text with $6$$ -- not parameters",
89 'id = $1 -- closing wrapper remains on its own line',
90 'EXISTS (WITH candidates AS (SELECT id FROM a) SELECT 1 FROM candidates)',
91 ])('passes through a complete WHERE expression: %s', async (where) => {
92 const shape = await resolve({ where, 'params[1]': '4' })
93 expect(shape.query.sql).toContain(`WHERE (\n${where}\n)`)
94 expect(shape.query.parameters).toEqual(['4'])
95 expect(shape.namespace).toBe('public')
96 expect(shape.table).toBe('items')
97 })
98 it('accepts changes-only logs and both replica modes without interpolating mode values into SQL', async () => {
99 expect(
100 await resolve({ log: 'changes_only', replica: 'full' })
101 ).toMatchObject({
102 logMode: 'changes_only',
103 replica: 'full',
104 })
105 expect((await resolve({ log: 'full', replica: 'default' })).query).toEqual(
106 (await resolve({})).query
107 )
108 })
109 it.each([
110 { where: 'id=$1', 'params[1]': '1', params: '[2]' },
111 { where: 'id=$1', 'params[0]': '1' },
112 { table: 'unknown' },
113 { table: 'items; select 1' },
114 { columns: 'id,label,' },
115 { columns: 'label,label' },
116 { columns: 'unknown' },
117 { where: 'id = $1', params: 'not json' },
118 { where: 'id = $1', params: '[{}]' },
119 { where: 'id = $1', params: '[9007199254740993]' },
120 { where: 'id = $2', 'params[2]': '2' },
121 { where: '' },
122 { log: 'unknown' },
123 { replica: 'unknown' },
124 ])('rejects invalid or ambiguous definition metadata: %j', async (params) => {
125 await expect(resolve(params)).rejects.toThrow()
126 })
127 it('passes text, comments and dollar-quoted strings through without interpreting them', async () => {
128 const where = "label = $$); SELECT 1; --$$ OR label = 'ORDER BY x' /* ; */"
129 expect((await resolve({ where })).query.sql).toContain(where)
130 })
131 it('rejects repeated shape-defining parameters rather than selecting a different authorization input', async () => {
132 for (const key of [
133 'where',
134 'table',
135 'params',
136 'columns',
137 'replica',
138 'log',
139 ]) {
140 const url = new URL('https://proxy.test/shape?table=items')
141 url.searchParams.append(key, key === 'table' ? 'items' : 'first')
142 url.searchParams.append(key, 'second')
143 expect(() => resolveConfiguredShape(url, tables)).toThrow('Repeated')
144 }
145 })
146})