#!/usr/bin/env node // Convivo — RPC test runner. // // Runs supabase/tests/rpc_tests.sql against the local Supabase Postgres // container and exits non-zero if any test failed. // // npm run test:rpc // // Requires the Supabase core to be running (`npm run db:start`). const { execFileSync } = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); const SQL_FILE = path.join(__dirname, 'rpc_tests.sql'); const DB_CONTAINER = process.env.SUPABASE_DB_CONTAINER || 'supabase_db_Convivo'; const sql = fs.readFileSync(SQL_FILE, 'utf8'); let output; try { output = execFileSync('docker', ['exec', '-i', DB_CONTAINER, 'psql', '-U', 'postgres'], { input: sql, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'], }); } catch (e) { // psql exits non-zero on a hard error (e.g. container not running). process.stderr.write(e.stdout || ''); process.stderr.write(e.stderr || ''); process.stderr.write('\nFailed to run RPC tests (is the Supabase core running? `npm run db:start`).\n'); process.exit(1); } process.stdout.write(output); // Parse the summary row: " 14 | 14 | 0" (three pipe-separated // numbers, no trailing pipe). Match the last such row in the output. const summaryRows = [...output.matchAll(/(\d+)\s*\|\s*(\d+)\s*\|\s*(\d+)\s*$/gm)]; const summaryMatch = summaryRows.length ? summaryRows[summaryRows.length - 1] : null; if (!summaryMatch) { process.stderr.write('\nCould not parse the test summary from psql output.\n'); process.exit(1); } const [, total, passed, failed] = summaryMatch.map((n) => parseInt(n, 10)); process.stdout.write(`\nRPC tests: ${passed}/${total} passed, ${failed} failed.\n`); process.exit(failed > 0 ? 1 : 0);