import { Pool } from 'pg'; import { runner } from 'node-pg-migrate'; import path from 'path'; export const testPool = new Pool({ host: process.env.TEST_PGHOST || 'localhost', port: parseInt(process.env.TEST_PGPORT || '55432', 10), user: process.env.TEST_PGUSER || 'redefined_test', password: process.env.TEST_PGPASSWORD || 'redefined_test', database: process.env.TEST_PGDATABASE || 'redefined_test' }); export async function migrate(): Promise { await runner({ databaseUrl: { host: process.env.TEST_PGHOST || 'localhost', port: parseInt(process.env.TEST_PGPORT || '55432', 10), user: process.env.TEST_PGUSER || 'redefined_test', password: process.env.TEST_PGPASSWORD || 'redefined_test', database: process.env.TEST_PGDATABASE || 'redefined_test' }, dir: path.resolve(__dirname, '..', '..', '..', 'migrations'), direction: 'up', migrationsTable: 'pgmigrations', count: Infinity }); } export async function resetDb(): Promise { await testPool.query(` TRUNCATE TABLE item_drafts, upload_links, orders, checkout_items, checkouts, shipping_addresses, cart_items, carts, customer_tokens, customer_sessions, favorites, customers, item_tags, item_images, items, tags, categories RESTART IDENTITY CASCADE `); // admin_settings is not truncated — it holds the seeded cart_expiry_hours // default that other suites read. But the email template rows in it are test // data like any other, and a stored template outliving the suite that wrote // it silently changes the mail every later suite asserts on. That is not // hypothetical: a subject of "Gone" written by the template tests reached the // favorite-alert tests and made five of them fail somewhere else entirely. await testPool.query(`DELETE FROM admin_settings WHERE key LIKE 'email\_%'`); } export async function closeDb(): Promise { await testPool.end(); }