Files
faunosteo/packages/supabase/tests/db/harness.ts
T
bchanot 4adbfcb608 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.
2026-10-25 21:09:45 +01:00

99 lines
3.8 KiB
TypeScript

/**
* PGlite harness: a real PostgreSQL (WASM) that applies the Supabase shim,
* then every migration under supabase/migrations, in filename order, then
* the versioned seeds under supabase/seed (same files `supabase db reset`
* applies through config.toml sql_paths).
* Lets the pgTAP suite and the schema checks run without Docker. The same
* SQL runs unchanged under `supabase test db` on the real stack.
*/
import { readdir, readFile } from 'node:fs/promises'
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, '../../../..')
export const supabaseDir = join(repoRoot, 'supabase')
export const migrationsDir = join(supabaseDir, 'migrations')
export const pgTapTestsDir = join(supabaseDir, 'tests')
/** Versioned seeds (reference data such as the official sheets); seed.sql itself needs the real auth schema and stays out. The CLI reads literal paths from config.toml: the seeds test checks every file here is listed. */
export const seedDir = join(supabaseDir, 'seed')
async function listSqlFiles(dir: string): Promise<string[]> {
const entries = await readdir(dir)
return entries.filter((f) => f.endsWith('.sql')).sort()
}
export async function listMigrations(): Promise<string[]> {
return listSqlFiles(migrationsDir)
}
export async function listPgTapTests(): Promise<string[]> {
return listSqlFiles(pgTapTestsDir)
}
/** Every .sql under supabase/seed, recursively, in path order. */
export async function listSeeds(): Promise<string[]> {
const entries = await readdir(seedDir, { recursive: true }).catch(() => [])
return entries.filter((f) => f.endsWith('.sql')).sort()
}
/** Boots PGlite, installs the shim and applies all migrations. */
export async function createMigratedDatabase(): Promise<PGlite> {
// 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;')
await db.exec('set search_path to public, extensions;')
for (const file of await listMigrations()) {
await db.exec(await readFile(join(migrationsDir, file), 'utf8'))
}
for (const file of await listSeeds()) {
await db.exec(await readFile(join(seedDir, file), 'utf8'))
}
return db
}
export interface TapReport {
lines: string[]
failures: string[]
}
function collectTapLines(results: Array<{ rows: unknown[] }>): string[] {
const lines: string[] = []
for (const result of results) {
for (const row of result.rows) {
const values = Object.values(row as Record<string, unknown>)
for (const value of values) {
if (typeof value === 'string') lines.push(...value.split('\n'))
}
}
}
return lines
}
/**
* Runs one pgTAP file (which must wrap itself in begin/rollback) and reports
* every `not ok` line plus plan mismatches announced by finish().
*/
export async function runPgTapFile(
db: PGlite,
file: string,
): Promise<TapReport> {
const sql = await readFile(join(pgTapTestsDir, file), 'utf8')
let lines: string[]
try {
lines = collectTapLines(await db.exec(sql))
} finally {
// A failing statement leaves the transaction aborted: always clean up.
await db.exec('rollback;').catch(() => undefined)
}
const failures = lines.filter(
(line) => /^not ok\b/.test(line) || /^# Looks like/.test(line),
)
return { lines, failures }
}