Refactor test database setup and add migrations

- Updated test files to use a separate test database configuration.
- Introduced a new helper module for managing test database operations.
- Added a setup file to configure the environment for tests.
- Created new database migration scripts to optimize message indexing.
- Added a sample environment file for test database configuration.
This commit is contained in:
MythEclipse
2026-05-31 16:32:38 +07:00
parent 8a712cff9b
commit f224be2a66
16 changed files with 1639 additions and 70 deletions
+7 -16
View File
@@ -1,9 +1,10 @@
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { closeDatabase } from "../../src/database/drizzle";
import {
closeDatabase,
getDatabase,
initializeDatabase,
} from "../../src/database/drizzle";
clearTestTables,
getTestDatabase,
initializeTestDatabase,
} from "../helpers/testDatabase";
import { createChildLogger } from "../../src/logger";
import {
decodeCursor,
@@ -18,14 +19,6 @@ import {
} from "../../src/moderation/messageStore";
import type { MessageRecord } from "../../src/moderation/types";
interface TestDatabase {
run(sql: string): Promise<unknown>;
}
function getTestDatabase(): TestDatabase {
return getDatabase() as unknown as TestDatabase;
}
const logger = createChildLogger("messageStoreQueries.test");
describe("message cursor helpers", () => {
@@ -44,7 +37,7 @@ describe("message cursor helpers", () => {
describe("message query integration tests", () => {
beforeAll(async () => {
await initializeDatabase();
await initializeTestDatabase();
// Create tables directly for isolated query integration tests
const db = getTestDatabase();
try {
@@ -110,9 +103,7 @@ describe("message query integration tests", () => {
beforeEach(async () => {
// Clear tables before each test
try {
const db = getTestDatabase();
await db.run(`DELETE FROM "attachments"`);
await db.run(`DELETE FROM "messages"`);
await clearTestTables("attachments", "messages");
} catch (error) {
logger.debug({ error }, "Could not clear tables");
}