mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-20 01:55:26 -05:00
Fix test infrastructure and stabilize e2e tests (#2836)
This commit is contained in:
65
scripts/check-test-db-migrations.ts
Normal file
65
scripts/check-test-db-migrations.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/** biome-ignore-all lint/suspicious/noConsole: Biome v2 migration */
|
||||
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import Database from "better-sqlite3";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const ROOT_DIR = path.join(__dirname, "..");
|
||||
|
||||
const MIGRATIONS_DIR = path.join(ROOT_DIR, "migrations");
|
||||
const DB_FILES = [
|
||||
path.join(ROOT_DIR, "db-test.sqlite3"),
|
||||
...fs
|
||||
.readdirSync(path.join(ROOT_DIR, "e2e", "seeds"))
|
||||
.filter((f) => f.startsWith("db-seed-") && f.endsWith(".sqlite3"))
|
||||
.map((f) => path.join(ROOT_DIR, "e2e", "seeds", f)),
|
||||
];
|
||||
|
||||
const migrationFilesOnDisk = fs
|
||||
.readdirSync(MIGRATIONS_DIR)
|
||||
.filter((f) => f.endsWith(".js"))
|
||||
.sort();
|
||||
|
||||
let hasErrors = false;
|
||||
|
||||
for (const dbPath of DB_FILES) {
|
||||
const relativePath = path.relative(ROOT_DIR, dbPath);
|
||||
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
console.warn(`Warning: ${relativePath} does not exist, skipping`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const db = new Database(dbPath, { readonly: true });
|
||||
const rows = db
|
||||
.prepare("SELECT name FROM migrations ORDER BY id ASC")
|
||||
.all() as Array<{ name: string }>;
|
||||
db.close();
|
||||
|
||||
const migrationsInDb = new Set(rows.map((r) => r.name));
|
||||
const missingMigrations = migrationFilesOnDisk.filter(
|
||||
(name) => !migrationsInDb.has(name),
|
||||
);
|
||||
|
||||
if (missingMigrations.length > 0) {
|
||||
hasErrors = true;
|
||||
console.error(
|
||||
`\n${relativePath} is missing ${missingMigrations.length} migration(s):`,
|
||||
);
|
||||
for (const name of missingMigrations) {
|
||||
console.error(` - ${name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasErrors) {
|
||||
console.error(
|
||||
"\nRun `npm run test:e2e:generate-seeds` to regenerate test databases.",
|
||||
);
|
||||
process.exit(1);
|
||||
} else {
|
||||
console.log("All test databases have the latest migrations.");
|
||||
}
|
||||
@@ -7,10 +7,20 @@ import { SEED_VARIATIONS } from "../app/features/api-private/constants";
|
||||
const E2E_SEEDS_DIR = "e2e/seeds";
|
||||
const BASE_TEST_DB = "db-test.sqlite3";
|
||||
|
||||
const E2E_WORKER_DB_PATTERN = /^db-test-e2e-\d+\.sqlite3$/;
|
||||
|
||||
async function generatePreSeededDatabases() {
|
||||
// biome-ignore lint/suspicious/noConsole: CLI script output
|
||||
console.log("Generating pre-seeded databases for e2e tests...\n");
|
||||
|
||||
for (const file of fs.readdirSync(".")) {
|
||||
if (E2E_WORKER_DB_PATTERN.test(file)) {
|
||||
fs.unlinkSync(file);
|
||||
// biome-ignore lint/suspicious/noConsole: CLI script output
|
||||
console.log(`Deleted stale worker db: ${file}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fs.existsSync(E2E_SEEDS_DIR)) {
|
||||
fs.mkdirSync(E2E_SEEDS_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user