feat(supabase): @faunosteo/supabase package with PGlite harness and typed client
- tests/db: PGlite (PostgreSQL 17 WASM) applies the Supabase shim + migrations and runs supabase/tests/*.sql through pgTAP without Docker - schema-types test pins tables, columns, enums, RPC args, RLS-on, no using(true) - database.types.ts in supabase gen types format; createBrowserClient (PKCE) - Supabase CLI pinned as root devDependency; db:* root scripts
This commit is contained in:
1 parent
2d5ff0c33e
commit
ce76c6597a
15 files changed
+1020
-2
No files matched your search
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* PGlite harness: a real PostgreSQL (WASM) that applies the Supabase shim,
|
||||
* then every migration under supabase/migrations, in filename order.
|
||||
* 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')
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/** Boots PGlite, installs the shim and applies all migrations. */
|
||||
export async function createMigratedDatabase(): Promise<PGlite> {
|
||||
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'))
|
||||
}
|
||||
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 }
|
||||
}
|
||||
Reference in new issue
Block a user