All checks were successful
PR Checks / lint-test-and-docker-build (pull_request) Successful in 2m27s
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
// Bun script — runs with `bun run e2e/setup/seed-db.ts`.
|
|
// Uses bun:sqlite and argon2 directly; must NOT be imported from Node context.
|
|
import { Database as BunSqlite } from 'bun:sqlite';
|
|
import { existsSync, unlinkSync } from 'fs';
|
|
import { resolve } from 'path';
|
|
import argon2 from 'argon2';
|
|
import { randomUUID } from 'crypto';
|
|
import { config as dotenvConfig } from 'dotenv';
|
|
import { TEST_USERS } from './test-users.js';
|
|
|
|
dotenvConfig({ path: resolve(process.cwd(), '.env.test'), override: true });
|
|
|
|
const DB_PATH = resolve(process.cwd(), 'trips.test.db');
|
|
|
|
// Delete any existing test DB so we start clean each run
|
|
for (const ext of ['', '-shm', '-wal']) {
|
|
const p = DB_PATH + ext;
|
|
if (existsSync(p)) {
|
|
unlinkSync(p);
|
|
}
|
|
}
|
|
|
|
const db = new BunSqlite(DB_PATH);
|
|
db.exec('PRAGMA foreign_keys = ON');
|
|
|
|
// Wrap bun:sqlite to match the Database interface expected by runMigrations
|
|
const dbWrapper = {
|
|
run(sql: string, params: unknown[] = []) {
|
|
db.query(sql).run(...(params as [unknown?, ...unknown[]]));
|
|
},
|
|
get<T = Record<string, unknown>>(sql: string, params: unknown[] = []): T | undefined {
|
|
const row = db.query(sql).get(...(params as [unknown?, ...unknown[]]));
|
|
return (row === null ? undefined : row) as T | undefined;
|
|
},
|
|
all<T = Record<string, unknown>>(sql: string, params: unknown[] = []): T[] {
|
|
return db.query(sql).all(...(params as [unknown?, ...unknown[]])) as T[];
|
|
},
|
|
close() {
|
|
db.close();
|
|
}
|
|
};
|
|
|
|
const { runMigrations } = await import('../../src/lib/server/db/migrations.js');
|
|
runMigrations(dbWrapper);
|
|
console.log('[e2e] Migrations complete.');
|
|
|
|
async function hashPassword(password: string): Promise<string> {
|
|
return argon2.hash(password, {
|
|
type: argon2.argon2id,
|
|
memoryCost: 8192,
|
|
timeCost: 2,
|
|
parallelism: 1
|
|
});
|
|
}
|
|
|
|
const regularId = randomUUID();
|
|
dbWrapper.run(
|
|
`INSERT INTO users (id, username, full_name, email, auth_source) VALUES (?, ?, ?, ?, ?)`,
|
|
[regularId, TEST_USERS.regular.username, TEST_USERS.regular.fullName, TEST_USERS.regular.email, 'Local']
|
|
);
|
|
dbWrapper.run(`INSERT INTO local_credentials (user_id, password_hash) VALUES (?, ?)`, [
|
|
regularId,
|
|
await hashPassword(TEST_USERS.regular.password)
|
|
]);
|
|
|
|
const adminId = randomUUID();
|
|
dbWrapper.run(
|
|
`INSERT INTO users (id, username, full_name, email, auth_source) VALUES (?, ?, ?, ?, ?)`,
|
|
[adminId, TEST_USERS.admin.username, TEST_USERS.admin.fullName, TEST_USERS.admin.email, 'Local']
|
|
);
|
|
dbWrapper.run(`INSERT INTO local_credentials (user_id, password_hash) VALUES (?, ?)`, [
|
|
adminId,
|
|
await hashPassword(TEST_USERS.admin.password)
|
|
]);
|
|
|
|
db.close();
|
|
console.log(`[e2e] Seeded users: ${TEST_USERS.regular.username}, ${TEST_USERS.admin.username}`);
|