/** * The PGlite shim's stand-ins for `auth.sessions` and `auth.mfa_factors` * (packages/supabase/tests/db/supabase-shim.sql) against the pinned GoTrue * image of the sandbox (contract ยง15: "columns checked against the pinned * image"). Every shim column exists on the image with the same type and * nullability, every NOT NULL column of the image without a default is in the * shim, and the three enums carry the same labels. So the aal2 checks that * pgTAP runs on PGlite read the same shape as on the real stack. */ import { readFile } from 'node:fs/promises' import { fileURLToPath } from 'node:url' import { PGlite } from '@electric-sql/pglite' import { sql } from './stack' const TABLES = ['sessions', 'mfa_factors'] const ENUMS = ['aal_level', 'factor_type', 'factor_status'] const COLUMNS = `select table_name || '.' || column_name || ':' || udt_name || ':' || is_nullable || ':' || (column_default is not null) as line from information_schema.columns where table_schema = 'auth' and table_name in ('sessions', 'mfa_factors') order by table_name, column_name` const LABELS = `select t.typname || ':' || string_agg(e.enumlabel, ',' order by e.enumsortorder) as line from pg_type t join pg_enum e on e.enumtypid = t.oid join pg_namespace n on n.oid = t.typnamespace where n.nspname = 'auth' and t.typname in ('aal_level', 'factor_type', 'factor_status') group by t.typname order by t.typname` type Column = { key: string; type: string; nullable: string; hasDefault: string } const parse = (line: string): Column => { const [key, type, nullable, hasDefault] = line.split(':') return { key, type, nullable, hasDefault } } describe('PGlite shim against the pinned image', () => { let shim: PGlite beforeAll(async () => { shim = await PGlite.create() await shim.exec(await readFile(fileURLToPath(new URL('../db/supabase-shim.sql', import.meta.url)), 'utf8')) }, 60_000) afterAll(async () => { await shim?.close() }) const shimLines = async (query: string) => (await shim.query<{ line: string }>(query)).rows.map((r) => r.line) it('every shim column matches the image, and the image needs no column the shim lacks', async () => { const image = (await sql(COLUMNS)).split('\n').filter(Boolean).map(parse) const own = (await shimLines(COLUMNS)).map(parse) expect(own.length).toBeGreaterThan(0) const imageByKey = new Map(image.map((c) => [c.key, c])) for (const c of own) { expect(imageByKey.get(c.key), `${c.key} on the image`).toBeDefined() expect([c.key, imageByKey.get(c.key)!.type, imageByKey.get(c.key)!.nullable]).toEqual([c.key, c.type, c.nullable]) } const ownKeys = new Set(own.map((c) => c.key)) const required = image.filter((c) => c.nullable === 'NO' && c.hasDefault === 'false').map((c) => c.key) expect(required.filter((key) => !ownKeys.has(key))).toEqual([]) expect(TABLES.every((t) => own.some((c) => c.key.startsWith(`${t}.`)))).toBe(true) }) it('the three enums carry the image labels', async () => { const image = (await sql(LABELS)).split('\n').filter(Boolean) const own = await shimLines(LABELS) expect(own).toEqual(image) expect(own.map((line) => line.split(':')[0])).toEqual([...ENUMS].sort()) }) })