Species rows (8 + other), animals with the D6b birth model, relations with composite workspace keys, current/ended/voided states and a partial unique index; definer RPCs for animals and relations (one lock order, shared write budget, caps), invoker reads (search_animals, list_client_animals, client_archive_preview) and search_clients with current_animal_count. pgTAP 014 (254), parity test, sandbox suite, types from the isolated stack.
281 lines
8.2 KiB
TypeScript
281 lines
8.2 KiB
TypeScript
/**
|
|
* D-R7 parity: the constants of src/field-limits.ts behave like the SQL CHECK
|
|
* constraints of public.clients, public.animals and
|
|
* public.animal_client_relations, 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 {
|
|
ANIMAL_FIELD_LIMITS,
|
|
BIRTH_YEAR_MIN,
|
|
CLIENT_FIELD_LIMITS,
|
|
COUNTRY_CODE_PATTERN,
|
|
EMAIL_PATTERN,
|
|
PHONE_E164_PATTERN,
|
|
RECORD_DATE_MIN,
|
|
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],
|
|
]
|
|
|
|
/** One animal row in a rolled-back transaction: does the schema accept these columns? */
|
|
async function animalAccepts(
|
|
columns: Record<string, string>,
|
|
): Promise<boolean> {
|
|
const row = { name: 'Parity', species_id: 'equine', ...columns }
|
|
const names = Object.keys(row)
|
|
await db.exec('begin')
|
|
try {
|
|
await db.query(
|
|
`insert into public.animals (workspace_id, sex, create_request_id, create_request_digest, ${names.join(', ')})
|
|
values ($1, 'unknown', gen_random_uuid(), '\\x00', ${names.map((_, i) => `$${i + 2}`).join(', ')})`,
|
|
[workspaceId, ...Object.values(row)],
|
|
)
|
|
return true
|
|
} catch {
|
|
return false
|
|
} finally {
|
|
await db.exec('rollback')
|
|
}
|
|
}
|
|
|
|
/** One relation row (ended, so both dates are allowed) to a fresh animal and client. */
|
|
async function relationAccepts(
|
|
column: 'started_on' | 'ended_on',
|
|
value: string,
|
|
): Promise<boolean> {
|
|
await db.exec('begin')
|
|
try {
|
|
await db.query(
|
|
`with a as (insert into public.animals (workspace_id, name, species_id, sex, create_request_id, create_request_digest)
|
|
values ($1, 'Parity', 'equine', 'unknown', gen_random_uuid(), '\\x00') returning id),
|
|
c as (insert into public.clients (workspace_id, kind, display_name, create_request_id, create_request_digest)
|
|
values ($1, 'person', 'Parity', gen_random_uuid(), '\\x00') returning id)
|
|
insert into public.animal_client_relations (workspace_id, animal_id, client_id, relation_role, ended_at, ${column},
|
|
create_request_id, create_request_digest)
|
|
select $1, a.id, c.id, 'owner', now(), $2, gen_random_uuid(), '\\x00' from a, c`,
|
|
[workspaceId, value],
|
|
)
|
|
return true
|
|
} catch {
|
|
return false
|
|
} finally {
|
|
await db.exec('rollback')
|
|
}
|
|
}
|
|
|
|
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),
|
|
])
|
|
}
|
|
})
|
|
|
|
it.each([
|
|
['name', ANIMAL_FIELD_LIMITS.name],
|
|
['breed', ANIMAL_FIELD_LIMITS.breed],
|
|
] as const)(
|
|
'animals.%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 animalAccepts({ [column]: value }),
|
|
]).toEqual([
|
|
column,
|
|
JSON.stringify(value).slice(0, 24),
|
|
isCleanText(value, max),
|
|
])
|
|
}
|
|
},
|
|
)
|
|
|
|
it('animals.species_detail: the text rule and its bound agree (with other)', async () => {
|
|
for (const value of textSamples(ANIMAL_FIELD_LIMITS.speciesDetail)) {
|
|
expect([
|
|
JSON.stringify(value).slice(0, 24),
|
|
await animalAccepts({ species_id: 'other', species_detail: value }),
|
|
]).toEqual([
|
|
JSON.stringify(value).slice(0, 24),
|
|
isCleanText(value, ANIMAL_FIELD_LIMITS.speciesDetail),
|
|
])
|
|
}
|
|
})
|
|
|
|
it('birth year: the floor agrees', async () => {
|
|
for (const year of [BIRTH_YEAR_MIN - 1, BIRTH_YEAR_MIN, 2015]) {
|
|
expect([year, await animalAccepts({ birth_year: String(year) })]).toEqual(
|
|
[year, year >= BIRTH_YEAR_MIN],
|
|
)
|
|
}
|
|
})
|
|
|
|
it('record dates: the floor agrees on birth and relation dates', async () => {
|
|
const before = '1899-12-31'
|
|
for (const value of [before, RECORD_DATE_MIN, '2015-04-12']) {
|
|
const expected = value >= RECORD_DATE_MIN
|
|
const year = value.slice(0, 4)
|
|
expect([
|
|
value,
|
|
await animalAccepts({ birth_year: year, birth_date: value }),
|
|
]).toEqual([value, expected])
|
|
expect([value, await relationAccepts('started_on', value)]).toEqual([
|
|
value,
|
|
expected,
|
|
])
|
|
expect([value, await relationAccepts('ended_on', value)]).toEqual([
|
|
value,
|
|
expected,
|
|
])
|
|
}
|
|
})
|
|
})
|