/** * 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, 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 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') }) })