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
@@ -7,11 +7,12 @@ import {
it,
vi,
} from "vitest";
import { closeDatabase } from "../../src/database/drizzle";
import {
closeDatabase,
getDatabase,
initializeDatabase,
} from "../../src/database/drizzle";
clearTestTables,
getTestDatabase,
initializeTestDatabase,
} from "../helpers/testDatabase";
import { captureMessage } from "../../src/moderation/messageCapture";
import type { ModerationBroadcaster } from "../../src/moderation/types";
@@ -22,14 +23,6 @@ type ModerationTestGlobal = typeof globalThis & {
moderationBroadcaster?: Partial<ModerationBroadcaster>;
};
interface TestDatabase {
run(sql: string): Promise<unknown>;
}
function getTestDatabase(): TestDatabase {
return getDatabase() as unknown as TestDatabase;
}
vi.mock("../../src/moderation/aiAnalyzer", () => ({
queueMessageAnalysis: (id: string) => queueMessageAnalysis(id),
}));
@@ -118,15 +111,13 @@ async function createTables() {
describe("captureMessage", () => {
beforeAll(async () => {
await initializeDatabase();
await initializeTestDatabase();
await createTables();
});
beforeEach(async () => {
queueMessageAnalysis.mockClear();
const db = getTestDatabase();
await db.run(`DELETE FROM "attachments"`);
await db.run(`DELETE FROM "messages"`);
await clearTestTables("attachments", "messages");
delete (globalThis as ModerationTestGlobal).moderationBroadcaster;
});