Files
faunosteo/packages/supabase/tests/db/field-limits.test.ts
T
bchanot 4adbfcb608 feat(supabase): clients M2 with RPCs, search and per-workspace budget
V1-1.2 (contract V1-1_OWNERS_ANIMALS.md §5, §9, §10). public.clients (person or organization, display name only, optional contact, folded search keys), create/update/set_archived as definer RPCs (identical not_found, idempotent create with divergent-replay refusal, revisions, archive read-only), search_clients invoker with keyset pages and phone search, write budget per workspace in UTC hour/day windows under a row lock, 20 000-client cap, ids-only audit hidden from platform staff (N9), nothing for anon or service_role. pgTAP 013 (130), field-limits parity test, sandbox suite (concurrency, budget races, 20 000-client search), types regenerated.
2026-10-25 21:09:45 +01:00

171 lines
4.5 KiB
TypeScript

/**
* D-R7 parity: the constants of src/field-limits.ts behave like the SQL CHECK
* constraints of public.clients, proven by inserting values at and past each
* bound into the migrated PGlite schema (contract §12.2). The form may refuse
* more than SQL (never less): a value the TypeScript rule accepts must be
* accepted by SQL, and the listed samples must agree both ways.
*/
import type { PGlite } from '@electric-sql/pglite'
import {
CLIENT_FIELD_LIMITS,
COUNTRY_CODE_PATTERN,
EMAIL_PATTERN,
PHONE_E164_PATTERN,
codePointLength,
isCleanText,
} from '../../src/field-limits'
import { createMigratedDatabase } from './harness'
type Column =
| 'display_name'
| 'email'
| 'phone'
| 'address_line1'
| 'postal_code'
| 'city'
| 'country'
let db: PGlite
let workspaceId: string
async function sqlAccepts(column: Column, value: string): Promise<boolean> {
const name = column === 'display_name' ? value : 'Parity'
const extra = column === 'display_name' ? '' : `, ${column}`
await db.exec('begin')
try {
await db.query(
`insert into public.clients (workspace_id, kind, display_name, create_request_id, create_request_digest${extra})
values ($1, 'person', $2, gen_random_uuid(), '\\x00'${column === 'display_name' ? '' : ', $3'})`,
column === 'display_name'
? [workspaceId, name]
: [workspaceId, name, value],
)
return true
} catch {
return false
} finally {
await db.exec('rollback')
}
}
const textSamples = (max: number): string[] => [
'a'.repeat(max),
'a'.repeat(max + 1),
'é'.repeat(max),
'é'.repeat(max + 1),
'𝄞'.repeat(max),
'𝄞'.repeat(max + 1),
'',
' a',
'a ',
'a\tb',
'a\u007Fb',
'a\u0085b',
'a\u2028b',
'a\u2029b',
'\u00A0a',
]
const textFields: [Column, number][] = [
['display_name', CLIENT_FIELD_LIMITS.displayName],
['address_line1', CLIENT_FIELD_LIMITS.addressLine],
['postal_code', CLIENT_FIELD_LIMITS.postalCode],
['city', CLIENT_FIELD_LIMITS.city],
]
const tsEmail = (v: string) =>
isCleanText(v, CLIENT_FIELD_LIMITS.email) &&
v === v.toLowerCase() &&
EMAIL_PATTERN.test(v)
const emailSamples = [
'a@b.co',
'A@b.co',
'a b@c.de',
'a@b',
'@b.co',
'a@@b.co',
'a'.repeat(246) + '@ex.test',
'a'.repeat(247) + '@ex.test',
'é@exemple.fr',
]
/** JavaScript's \s covers U+00A0, PostgreSQL's does not: the form refuses, SQL would accept. */
const emailTsStricter = ['a\u00A0b@c.de']
describe('field limits match the SQL constraints (D-R7)', () => {
beforeAll(async () => {
db = await createMigratedDatabase()
await db.exec(`insert into auth.users (id, email, email_confirmed_at)
values ('7a000000-0000-0000-0000-000000000001', 'parity@test.local', now())`)
const { rows } = await db.query<{ id: string }>(
`select id from public.workspaces where personal_user_id = '7a000000-0000-0000-0000-000000000001'`,
)
workspaceId = rows[0].id
})
afterAll(async () => {
await db?.close()
})
it('counts code points like char_length', () => {
expect(codePointLength('𝄞é')).toBe(2)
expect('𝄞é'.length).toBe(3)
})
it.each(textFields)(
'%s: the text rule and its bound %i agree',
async (column, max) => {
for (const value of textSamples(max)) {
expect([
column,
JSON.stringify(value).slice(0, 24),
await sqlAccepts(column, value),
]).toEqual([
column,
JSON.stringify(value).slice(0, 24),
isCleanText(value, max),
])
}
},
)
it('email: bound, lower case and pattern agree', async () => {
for (const value of emailSamples) {
expect([value.slice(0, 24), await sqlAccepts('email', value)]).toEqual([
value.slice(0, 24),
tsEmail(value),
])
}
for (const value of emailTsStricter) {
expect(tsEmail(value)).toBe(false)
}
})
it('phone: E.164 agrees', async () => {
const samples = [
'+33612345678',
'+1234567',
'+123456',
'+123456789012345',
'+1234567890123456',
'0612345678',
'+0612345678',
'+33 612345678',
]
for (const value of samples) {
expect([value, await sqlAccepts('phone', value)]).toEqual([
value,
PHONE_E164_PATTERN.test(value),
])
}
})
it('country: ISO alpha-2 upper case agrees', async () => {
for (const value of ['FR', 'fr', 'FRA', 'F1', 'F']) {
expect([value, await sqlAccepts('country', value)]).toEqual([
value,
COUNTRY_CODE_PATTERN.test(value),
])
}
})
})