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.
This commit is contained in:
1 parent
2c0531d638
commit
4adbfcb608
9 files changed
+1916
-2
No files matched your search
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* 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),
|
||||
])
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -11,6 +11,7 @@ import { resolve, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { PGlite } from '@electric-sql/pglite'
|
||||
import { pgtap } from '@electric-sql/pglite/pgtap'
|
||||
import { unaccent } from '@electric-sql/pglite/contrib/unaccent'
|
||||
|
||||
const here = fileURLToPath(new URL('.', import.meta.url))
|
||||
export const repoRoot = resolve(here, '../../../..')
|
||||
@@ -41,7 +42,8 @@ export async function listSeeds(): Promise<string[]> {
|
||||
|
||||
/** Boots PGlite, installs the shim and applies all migrations. */
|
||||
export async function createMigratedDatabase(): Promise<PGlite> {
|
||||
const db = await PGlite.create({ extensions: { pgtap } })
|
||||
// unaccent: created by the clients migration (search keys), as on the real image.
|
||||
const db = await PGlite.create({ extensions: { pgtap, unaccent } })
|
||||
await db.exec(await readFile(join(here, 'supabase-shim.sql'), 'utf8'))
|
||||
// Same placement as `supabase test db`: pgTAP lives in extensions, out of public.
|
||||
await db.exec('create extension if not exists pgtap with schema extensions;')
|
||||
|
||||
@@ -85,6 +85,28 @@ const expectedColumns = {
|
||||
'status',
|
||||
'updated_at',
|
||||
],
|
||||
clients: [
|
||||
'address_line1',
|
||||
'address_line2',
|
||||
'archived_at',
|
||||
'city',
|
||||
'country',
|
||||
'create_request_digest',
|
||||
'create_request_id',
|
||||
'created_at',
|
||||
'display_name',
|
||||
'email',
|
||||
'id',
|
||||
'kind',
|
||||
'name_key',
|
||||
'phone',
|
||||
'phone_digits',
|
||||
'postal_code',
|
||||
'revision',
|
||||
'search_key',
|
||||
'updated_at',
|
||||
'workspace_id',
|
||||
],
|
||||
profile_roles: [
|
||||
'granted_at',
|
||||
'granted_by',
|
||||
@@ -139,6 +161,19 @@ const expectedFunctionArgs = {
|
||||
complete_avatar_job: ['asset_id', 'byte_size', 'lock_token', 'sha256'],
|
||||
confirm_asset_upload: ['asset_id'],
|
||||
confirm_sheet_asset_upload: ['asset_id'],
|
||||
create_client: [
|
||||
'address_line1',
|
||||
'address_line2',
|
||||
'city',
|
||||
'country',
|
||||
'create_request_id',
|
||||
'display_name',
|
||||
'email',
|
||||
'kind',
|
||||
'phone',
|
||||
'postal_code',
|
||||
'workspace_id',
|
||||
],
|
||||
create_anatomical_sheet: [
|
||||
'description',
|
||||
'document',
|
||||
@@ -163,7 +198,29 @@ const expectedFunctionArgs = {
|
||||
remove_avatar: [],
|
||||
restore_anatomical_sheet: ['sheet_id'],
|
||||
revoke_platform_role: ['reason', 'role', 'target_user_id'],
|
||||
search_clients: [
|
||||
'after_id',
|
||||
'after_name_key',
|
||||
'archived',
|
||||
'page_size',
|
||||
'query',
|
||||
'workspace_id',
|
||||
],
|
||||
set_client_archived: ['archived', 'client_id'],
|
||||
set_sheet_visibility: ['sheet_id', 'visibility'],
|
||||
update_client: [
|
||||
'address_line1',
|
||||
'address_line2',
|
||||
'city',
|
||||
'client_id',
|
||||
'country',
|
||||
'display_name',
|
||||
'email',
|
||||
'expected_revision',
|
||||
'kind',
|
||||
'phone',
|
||||
'postal_code',
|
||||
],
|
||||
} as const satisfies FunctionArgs
|
||||
|
||||
// A function without arguments is generated as `Record<PropertyKey, never>`
|
||||
|
||||
Reference in new issue
Block a user