/** * 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' 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 { const entries = await readdir(dir) return entries.filter((f) => f.endsWith('.sql')).sort() } export async function listMigrations(): Promise { return listSqlFiles(migrationsDir) } export async function listPgTapTests(): Promise { return listSqlFiles(pgTapTestsDir) } /** Every .sql under supabase/seed, recursively, in path order. */ export async function listSeeds(): Promise { 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 { const db = await PGlite.create({ extensions: { pgtap } }) 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) 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 { 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 } }