Files
HamHam/tests/setup.ts

24 lines
1.2 KiB
TypeScript

import { execFileSync } from 'node:child_process';
import postgres from 'postgres';
import { hash } from '@node-rs/argon2';
import { examples } from './fixtures';
export default async function setup() {
const url = process.env.TEST_DATABASE_URL!;
if (!new URL(url).pathname.endsWith('_test'))
throw new Error('E2E-Datenbank muss auf _test enden.');
execFileSync(process.execPath, ['node_modules/tsx/dist/cli.mjs', 'scripts/migrate.ts'], {
env: { ...process.env, DATABASE_URL: url },
stdio: 'inherit',
});
const sql = postgres(url, { max: 1 });
try {
await sql`TRUNCATE sessions, admins, recipes, media, login_limits CASCADE`;
await sql`INSERT INTO admins (username, password) VALUES ('testadmin', ${await hash(process.env.TEST_PASSWORD!, { algorithm: 2, memoryCost: 65536, timeCost: 3 })})`;
for (const r of examples)
await sql`INSERT INTO recipes (id, title, title_key, published, data) VALUES (${r.id!}, ${r.title}, ${r.title.toLowerCase()}, true, ${sql.json(r)})`;
await sql`INSERT INTO recipes (title, title_key, published, data) VALUES ('Geheimer Entwurf', 'geheimer entwurf', false, ${sql.json({ ...examples[0], id: undefined, title: 'Geheimer Entwurf', published: false })})`;
} finally {
await sql.end();
}
}