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:
bchanot committed 2026-10-25 21:09:45 +01:00
1 parent 2c0531d638
commit 4adbfcb608
9 files changed
+1916 -2

No files matched your search

+136
View File
@@ -308,6 +308,84 @@ export type Database = {
},
]
}
clients: {
Row: {
address_line1: string | null
address_line2: string | null
archived_at: string | null
city: string | null
country: string | null
create_request_digest: string
create_request_id: string
created_at: string
display_name: string
email: string | null
id: string
kind: Database['public']['Enums']['client_kind']
name_key: string | null
phone: string | null
phone_digits: string | null
postal_code: string | null
revision: number
search_key: string | null
updated_at: string
workspace_id: string
}
ComputedFields: never
Insert: {
address_line1?: string | null
address_line2?: string | null
archived_at?: string | null
city?: string | null
country?: string | null
create_request_digest: string
create_request_id: string
created_at?: string
display_name: string
email?: string | null
id?: string
kind: Database['public']['Enums']['client_kind']
name_key?: never
phone?: string | null
phone_digits?: never
postal_code?: string | null
revision?: number
search_key?: never
updated_at?: string
workspace_id: string
}
Update: {
address_line1?: string | null
address_line2?: string | null
archived_at?: string | null
city?: string | null
country?: string | null
create_request_digest?: string
create_request_id?: string
created_at?: string
display_name?: string
email?: string | null
id?: string
kind?: Database['public']['Enums']['client_kind']
name_key?: never
phone?: string | null
phone_digits?: never
postal_code?: string | null
revision?: number
search_key?: never
updated_at?: string
workspace_id?: string
}
Relationships: [
{
foreignKeyName: 'clients_workspace_id_fkey'
columns: ['workspace_id']
isOneToOne: false
referencedRelation: 'workspaces'
referencedColumns: ['id']
},
]
}
profile_roles: {
Row: {
granted_at: string
@@ -576,6 +654,22 @@ export type Database = {
}
Returns: string
}
create_client: {
Args: {
address_line1?: string
address_line2?: string
city?: string
country?: string
create_request_id: string
display_name: string
email?: string
kind: string
phone?: string
postal_code?: string
workspace_id: string
}
Returns: Json
}
create_sheet_draft: {
Args: {
change_note?: string
@@ -636,6 +730,30 @@ export type Database = {
}
Returns: string
}
search_clients: {
Args: {
after_id?: string
after_name_key?: string
archived?: boolean
page_size?: number
query?: string
workspace_id: string
}
Returns: {
archived_at: string
city: string
display_name: string
email: string
id: string
kind: Database['public']['Enums']['client_kind']
name_key: string
phone: string
}[]
}
set_client_archived: {
Args: { archived: boolean; client_id: string }
Returns: Json
}
set_sheet_visibility: {
Args: {
sheet_id: string
@@ -643,6 +761,22 @@ export type Database = {
}
Returns: string
}
update_client: {
Args: {
address_line1: string
address_line2: string
city: string
client_id: string
country: string
display_name: string
email: string
expected_revision: number
kind: string
phone: string
postal_code: string
}
Returns: Json
}
}
Enums: {
asset_status:
@@ -651,6 +785,7 @@ export type Database = {
| 'clean'
| 'rejected'
| 'deleted'
client_kind: 'person' | 'organization'
platform_role: 'owner' | 'admin' | 'moderator' | 'support'
sheet_status: 'active' | 'archived' | 'deleted'
sheet_version_status: 'draft' | 'published' | 'deprecated'
@@ -791,6 +926,7 @@ export const Constants = {
'rejected',
'deleted',
],
client_kind: ['person', 'organization'],
platform_role: ['owner', 'admin', 'moderator', 'support'],
sheet_status: ['active', 'archived', 'deleted'],
sheet_version_status: ['draft', 'published', 'deprecated'],
+45
View File
@@ -0,0 +1,45 @@
/**
* Bounds and patterns of the V1-1 record fields, shared by the forms (Zod
* schemas, D-R7) and proven equal to the SQL CHECK constraints by
* tests/db/field-limits.test.ts (behaviour on the migrated schema, never by
* parsing SQL). Contract: docs/development/V1-1_OWNERS_ANIMALS.md §5, §12.2.
*
* Lengths count code points, as PostgreSQL's char_length does: use
* codePointLength, never String.length (UTF-16 units) or Zod's .max() alone.
*/
export const CLIENT_FIELD_LIMITS = {
displayName: 120,
email: 254,
addressLine: 120,
postalCode: 16,
city: 80,
} as const
/** U+0000 to U+001F, U+007F to U+009F, U+2028, U+2029 (the SQL literal starts at U+0001: text cannot hold U+0000). */
export const CONTROL_CHARACTERS = /[\u0000-\u001F\u007F-\u009F\u2028\u2029]/u
/** Applied to the lower-cased value. JavaScript's \s is wider than SQL's, so the form refuses at least what SQL refuses. */
export const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/u
export const PHONE_E164_PATTERN = /^\+[1-9][0-9]{6,14}$/
export const COUNTRY_CODE_PATTERN = /^[A-Z]{2}$/
export function codePointLength(value: string): number {
return [...value].length
}
/**
* The text rule of contract §5: 1 to max code points, no leading or trailing
* space (U+0020, as SQL btrim checks), no control character.
*/
export function isCleanText(value: string, max: number): boolean {
const length = codePointLength(value)
return (
length >= 1 &&
length <= max &&
value === value.replace(/^ +| +$/g, '') &&
!CONTROL_CHARACTERS.test(value)
)
}
@@ -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),
])
}
})
})
+3 -1
View File
@@ -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>`
@@ -0,0 +1,538 @@
/**
* V1-1.2 clients (M2) on the real stack of `pnpm db:sandbox`: RPCs through
* PostgREST with real sessions per role, identical refusals, search without
* leaks, then concurrent writes (creates, replays, revisions, archive races,
* budget at its limit, lock contention) and the search time on 20 000 clients.
* Contract: docs/development/V1-1_OWNERS_ANIMALS.md §9, §10, §12.
*/
import { randomUUID } from 'node:crypto'
import {
confirmedAccount,
errorCode,
rest,
rpc,
sql,
stack,
type Account,
type RestResult,
} from './stack'
const workspaceOf = (userId: string) =>
sql(`select id from public.workspaces where personal_user_id = '${userId}'`)
const budgetOf = (ws: string) =>
sql(
`select coalesce((select hour_count || '/' || day_count from private.workspace_write_budgets where workspace_id = '${ws}'), 'none')`,
)
function create(
account: Account,
ws: string,
fields: Record<string, unknown>,
requestId = randomUUID(),
) {
return rpc(
'create_client',
{
workspace_id: ws,
create_request_id: requestId,
kind: 'person',
...fields,
},
account.token,
)
}
const idOf = (result: RestResult) => (result.body as { id: string }).id
const fullUpdate = (
clientId: string,
revision: number,
displayName: string,
city: string | null = null,
) => ({
client_id: clientId,
expected_revision: revision,
kind: 'person',
display_name: displayName,
email: null,
phone: null,
address_line1: null,
address_line2: null,
postal_code: null,
city,
country: null,
})
/** The error as the client sees it: status, code, message, details, hint. */
const shape = (result: RestResult) => {
const body = result.body as Record<string, unknown>
return [
result.status,
body.code,
body.message,
body.details ?? null,
body.hint ?? null,
]
}
describe('clients through PostgREST, per role', () => {
let a: Account
let b: Account
let wA: string
let wB: string
let lac: string
beforeAll(async () => {
a = await confirmedAccount('cl-a')
b = await confirmedAccount('cl-b')
wA = await workspaceOf(a.id)
wB = await workspaceOf(b.id)
const created = await create(a, wA, {
kind: 'organization',
display_name: 'Écurie du Lac',
email: 'Contact@Ecurie-Lac.test',
phone: '+33612345678',
city: 'Lyon',
})
expect(created.status).toBe(200)
lac = idOf(created)
})
it('A creates, reads, searches with POST, updates and archives', async () => {
const row = await rest(
`/clients?select=id,kind,display_name,email,revision&id=eq.${lac}`,
a.token,
)
expect(row.body).toEqual([
{
id: lac,
kind: 'organization',
display_name: 'Écurie du Lac',
email: 'contact@ecurie-lac.test',
revision: 1,
},
])
const found = await rpc(
'search_clients',
{ workspace_id: wA, query: 'ecu' },
a.token,
)
expect((found.body as { id: string }[]).map((r) => r.id)).toEqual([lac])
const phone = await rpc(
'search_clients',
{ workspace_id: wA, query: '06 12' },
a.token,
)
expect((phone.body as { id: string }[]).map((r) => r.id)).toEqual([lac])
expect(
(
await rpc(
'update_client',
fullUpdate(lac, 1, 'Écurie du Lac', 'Annecy'),
a.token,
)
).body,
).toEqual({ revision: 2 })
expect(
(
await rpc(
'set_client_archived',
{ client_id: lac, archived: true },
a.token,
)
).body,
).toEqual({ revision: 3 })
expect(
(
await rpc(
'set_client_archived',
{ client_id: lac, archived: false },
a.token,
)
).body,
).toEqual({ revision: 4 })
})
it('B gets nothing of A: reads, counts, search, and the same refusal as for an unknown id', async () => {
expect(
(await rest(`/clients?select=id&id=eq.${lac}`, b.token)).body,
).toEqual([])
const head = await rest(
`/clients?select=id&workspace_id=eq.${wA}`,
b.token,
{ method: 'HEAD', headers: { Prefer: 'count=exact' } },
)
expect(head.headers.get('content-range')).toBe('*/0')
expect(
(await rpc('search_clients', { workspace_id: wA, query: '' }, b.token))
.body,
).toEqual([])
const foreign = await rpc('update_client', fullUpdate(lac, 4, 'X'), b.token)
const unknown = await rpc(
'update_client',
fullUpdate(randomUUID(), 4, 'X'),
b.token,
)
expect(shape(foreign)).toEqual(shape(unknown))
expect(shape(foreign)).toEqual([403, '42501', 'not_found', null, null])
expect(shape(await create(b, wA, { display_name: 'X' }))).toEqual(
shape(await create(b, randomUUID(), { display_name: 'X' })),
)
expect(
shape(
await rpc(
'set_client_archived',
{ client_id: lac, archived: true },
b.token,
),
),
).toEqual([403, '42501', 'not_found', null, null])
})
it('refuses direct writes, the anon key and the service key; error bodies carry no value', async () => {
const attempts: [string, string, RequestInit, number][] = [
[
'/clients',
b.token,
{
method: 'POST',
body: JSON.stringify({
workspace_id: wB,
kind: 'person',
display_name: 'Direct',
}),
},
403,
],
[
`/clients?id=eq.${lac}`,
a.token,
{ method: 'PATCH', body: JSON.stringify({ display_name: 'Direct' }) },
403,
],
[`/clients?id=eq.${lac}`, a.token, { method: 'DELETE' }, 403],
['/clients?select=id', stack.anonKey, {}, 401],
[
'/rpc/search_clients',
stack.anonKey,
{ method: 'POST', body: JSON.stringify({ workspace_id: wA }) },
401,
],
['/clients?select=id', stack.serviceKey, {}, 403],
[
'/rpc/create_client',
stack.serviceKey,
{
method: 'POST',
body: JSON.stringify({
workspace_id: wA,
create_request_id: randomUUID(),
kind: 'person',
display_name: 'S',
}),
},
403,
],
]
for (const [path, token, init, status] of attempts) {
const result = await rest(path, token, init)
expect([path, result.status, errorCode(result)]).toEqual([
path,
status,
'42501',
])
expect(result.text).not.toContain('Lac')
}
const invalid = await create(a, wA, {
display_name: 'Name',
email: 'secret-value-not-an-email',
})
expect(shape(invalid)).toEqual([400, 'P0001', 'invalid_input', null, null])
expect(invalid.text).not.toContain('secret-value')
})
it('cuts an unconfirmed or deactivated account', async () => {
await sql(`update public.profiles set active = false where id = '${a.id}'`)
expect((await rest('/clients?select=id', a.token)).body).toEqual([])
expect(
shape(await rpc('update_client', fullUpdate(lac, 4, 'X'), a.token)),
).toEqual([403, '42501', 'not_found', null, null])
await sql(`update public.profiles set active = true where id = '${a.id}'`)
await sql(
`update auth.users set email_confirmed_at = null where id = '${a.id}'`,
)
expect((await rest('/clients?select=id', a.token)).body).toEqual([])
await sql(
`update auth.users set email_confirmed_at = now() where id = '${a.id}'`,
)
expect((await rest('/clients?select=id', a.token)).body).toEqual([
{ id: lac },
])
})
it('hides client audit rows from the platform owner (N9)', async () => {
expect(
await sql(
`select count(*) from audit.audit_logs where resource_id = '${lac}'`,
),
).toBe('4')
// audit is not exposed through PostgREST; the owner's view is its RLS-filtered SQL view.
const ownerId = await sql(
`select id from auth.users where email = 'owner@faunosteo.local'`,
)
const asOwner = await sql(`begin; set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"${ownerId}","role":"authenticated"}', true);
select count(*) from audit.audit_logs where resource_id = '${lac}'; commit;`)
expect(asOwner.split('\n').pop()).toBe('0')
})
})
describe('concurrent writes', () => {
let a: Account
let wA: string
beforeAll(async () => {
a = await confirmedAccount('cl-race')
wA = await workspaceOf(a.id)
})
it('20 parallel creates in one workspace all land, counted once each', async () => {
const results = await Promise.all(
Array.from({ length: 20 }, (_, i) =>
create(a, wA, { display_name: `Parallel ${i}` }),
),
)
expect(results.map((r) => r.status)).toEqual(Array(20).fill(200))
expect(
await sql(
`select count(*) from public.clients where workspace_id = '${wA}'`,
),
).toBe('20')
expect(await budgetOf(wA)).toBe('20/20')
})
it('the same request id sent 8 times at once creates one client and answers it 8 times', async () => {
const requestId = randomUUID()
const results = await Promise.all(
Array.from({ length: 8 }, () =>
create(a, wA, { display_name: 'Replayed' }, requestId),
),
)
expect(new Set(results.map(idOf)).size).toBe(1)
expect(
await sql(
`select count(*) from public.clients where create_request_id = '${requestId}'`,
),
).toBe('1')
expect(await budgetOf(wA)).toBe('21/21')
})
it('two different contents racing on one request id: one created, the other refused', async () => {
const requestId = randomUUID()
const [x, y] = await Promise.all([
create(a, wA, { display_name: 'Racer X' }, requestId),
create(a, wA, { display_name: 'Racer Y' }, requestId),
])
expect([x.status, y.status].sort()).toEqual([200, 400])
expect(
[x, y]
.map((r) =>
r.status === 400 ? (r.body as { message: string }).message : 'ok',
)
.sort(),
).toEqual(['ok', 'request_conflict'])
expect(
await sql(
`select count(*) from public.clients where create_request_id = '${requestId}'`,
),
).toBe('1')
})
it('two updates from the same revision: one wins, the other gets conflict, nothing lost', async () => {
const id = idOf(await create(a, wA, { display_name: 'Revision base' }))
const [x, y] = await Promise.all([
rpc('update_client', fullUpdate(id, 1, 'Edited in tab X'), a.token),
rpc('update_client', fullUpdate(id, 1, 'Edited in tab Y'), a.token),
])
const winner = x.status === 200 ? 'Edited in tab X' : 'Edited in tab Y'
expect([x.status, y.status].sort()).toEqual([200, 400])
expect([x, y].find((r) => r.status === 400)?.body).toMatchObject({
message: 'conflict',
})
expect(
await sql(
`select display_name || '/' || revision from public.clients where id = '${id}'`,
),
).toBe(`${winner}/2`)
})
it('archive, restore and edits racing on one client end consistent, without deadlock', async () => {
const id = idOf(await create(a, wA, { display_name: 'Archive race' }))
const calls = Array.from({ length: 30 }, (_, i) =>
i % 3 === 0
? rpc('set_client_archived', { client_id: id, archived: true }, a.token)
: i % 3 === 1
? rpc(
'set_client_archived',
{ client_id: id, archived: false },
a.token,
)
: rpc('update_client', fullUpdate(id, 1, `Edit ${i}`), a.token),
)
const results = await Promise.all(calls)
const messages = results.map((r) =>
r.status === 200 ? 'ok' : (r.body as { message: string }).message,
)
expect(
messages.every((m) => ['ok', 'archived', 'conflict'].includes(m)),
).toBe(true)
const [revision, archived] = (
await sql(
`select revision || '|' || (archived_at is not null) from public.clients where id = '${id}'`,
)
).split('|')
const audited = await sql(
`select count(*) from audit.audit_logs where resource_id = '${id}'`,
)
expect(Number(audited)).toBe(Number(revision))
expect(['true', 'false']).toContain(archived)
})
it('at the hourly limit, 20 racing writes: exactly the remaining 10 pass, refused ones consume nothing', async () => {
await sql(
`update private.workspace_write_budgets set hour_count = 990 where workspace_id = '${wA}'`,
)
const results = await Promise.all(
Array.from({ length: 20 }, (_, i) =>
create(a, wA, { display_name: `Limit ${i}` }),
),
)
const statuses = results.map((r) =>
r.status === 200 ? 'ok' : (r.body as { message: string }).message,
)
expect(statuses.filter((s) => s === 'ok')).toHaveLength(10)
expect(statuses.filter((s) => s === 'rate_limited')).toHaveLength(10)
expect((await budgetOf(wA)).split('/')[0]).toBe('1000')
await sql(
`update private.workspace_write_budgets set hour_count = 0, day_count = 0 where workspace_id = '${wA}'`,
)
})
it('locks stay per workspace: a held budget row delays its workspace only', async () => {
const accounts = await Promise.all(
Array.from({ length: 10 }, (_, i) => confirmedAccount(`cl-iso-${i}`)),
)
const spaces = await Promise.all(accounts.map((x) => workspaceOf(x.id)))
expect(
(await create(accounts[0], spaces[0], { display_name: 'Seed' })).status,
).toBe(200)
const holder =
sql(`begin; select 1 from private.workspace_write_budgets where workspace_id = '${spaces[0]}' for update;
select pg_sleep(2); commit;`)
await new Promise((resolve) => setTimeout(resolve, 300))
const started = Date.now()
const blocked = create(accounts[0], spaces[0], {
display_name: 'Waits',
}).then((r) => ({ r, ms: Date.now() - started }))
const others = await Promise.all(
accounts
.slice(1)
.map((x, i) =>
create(x, spaces[i + 1], { display_name: 'Independent' }),
),
)
const othersMs = Date.now() - started
const waited = await blocked
await holder
expect(others.map((r) => r.status)).toEqual(Array(9).fill(200))
expect(othersMs).toBeLessThan(1000)
expect(waited.r.status).toBe(200)
expect(waited.ms).toBeGreaterThan(1200)
console.log(
`[measure] budget row of one workspace held 2 s: 9 creates elsewhere ${othersMs} ms; the create in the held workspace waited ${waited.ms} ms`,
)
})
it('a mixed burst of 60 writes in one workspace raises no deadlock', async () => {
const ids = await Promise.all(
Array.from({ length: 5 }, (_, i) =>
create(a, wA, { display_name: `Burst base ${i}` }).then(idOf),
),
)
const started = Date.now()
const results = await Promise.all(
Array.from({ length: 60 }, (_, i) => {
const id = ids[i % 5]
if (i % 4 === 0)
return create(a, wA, { display_name: `Burst new ${i}` })
if (i % 4 === 1)
return rpc(
'set_client_archived',
{ client_id: id, archived: i % 8 === 1 },
a.token,
)
return rpc(
'update_client',
fullUpdate(id, 1, `Burst edit ${i}`),
a.token,
)
}),
)
const codes = results.map((r) =>
r.status === 200
? 'ok'
: `${errorCode(r)}:${(r.body as { message: string }).message}`,
)
expect(
codes.filter((c) => c.startsWith('40P01') || c.startsWith('40001')),
).toEqual([])
expect(
codes.every(
(c) => c === 'ok' || ['P0001:conflict', 'P0001:archived'].includes(c),
),
).toBe(true)
console.log(
`[measure] 60 mixed writes in one workspace: ${Date.now() - started} ms, ${codes.filter((c) => c === 'ok').length} applied`,
)
})
})
describe('search on 20 000 clients', () => {
it('records the time of name and phone searches (measurement, not a gate)', async () => {
const a = await confirmedAccount('cl-volume')
const ws = await workspaceOf(a.id)
await sql(`insert into public.clients (workspace_id, kind, display_name, city, email, phone, create_request_id, create_request_digest)
select '${ws}', 'person', 'Client ' || g || ' Dupont', 'Ville ' || (g % 500), 'client' || g || '@example.test',
'+336' || lpad(g::text, 8, '0'), gen_random_uuid(), '\\x00'
from generate_series(1, 20000) g; analyze public.clients;`)
const terms = [
'dupont',
'client 1999',
'ville 42',
'example',
'06 00 01',
'zzz-none',
'',
]
const times: number[] = []
for (let round = 0; round < 3; round++) {
for (const query of terms) {
const started = performance.now()
const result = await rpc(
'search_clients',
{ workspace_id: ws, query },
a.token,
)
times.push(performance.now() - started)
expect(result.status).toBe(200)
}
}
times.sort((x, y) => x - y)
const p95 = times[Math.ceil(times.length * 0.95) - 1]
console.log(
`[measure] search_clients over 20 000 clients, ${times.length} HTTP calls: median ${times[times.length >> 1].toFixed(1)} ms, p95 ${p95.toFixed(1)} ms, max ${times[times.length - 1].toFixed(1)} ms`,
)
expect(
await sql(
`select count(*) from public.clients where workspace_id = '${ws}'`,
),
).toBe('20000')
})
})