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
+6
View File
@@ -0,0 +1,6 @@
NODE_ENV=test
# Use a separate database/data area for tests. It may be on the same PostgreSQL host,
# but the database name must clearly be a test database so destructive test setup
# cannot touch production data.
TEST_DATABASE_URL=postgres://root:root@100.108.1.124:5432/hub_test
DATABASE_URL=postgres://root:root@100.108.1.124:5432/hub_test
@@ -0,0 +1,3 @@
CREATE INDEX IF NOT EXISTS "idx_messages_guild_created_deleted" ON "messages" USING btree ("guild_id","created_at","deleted_at","id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_messages_channel_ai_status_created" ON "messages" USING btree ("channel_id","ai_status","created_at","id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_messages_thread_ai_status_created" ON "messages" USING btree ("thread_id","ai_status","created_at","id");
File diff suppressed because it is too large Load Diff
+7
View File
@@ -29,6 +29,13 @@
"when": 1780079000000,
"tag": "0003_ai_moderation_review_guardrails",
"breakpoints": true
},
{
"idx": 5,
"version": "7",
"when": 1780218363790,
"tag": "0005_optimize-message-index",
"breakpoints": true
}
]
}
+12
View File
@@ -106,6 +106,18 @@ export const pgMessagesTable = pgTable(
table.created_at,
table.id,
),
guildCreatedDeletedIdx: pgIndex("idx_messages_guild_created_deleted").on(
table.guild_id,
table.created_at,
table.deleted_at,
table.id,
),
channelAiStatusCreatedIdx: pgIndex(
"idx_messages_channel_ai_status_created",
).on(table.channel_id, table.ai_status, table.created_at, table.id),
threadAiStatusCreatedIdx: pgIndex(
"idx_messages_thread_ai_status_created",
).on(table.thread_id, table.ai_status, table.created_at, table.id),
}),
);
+5 -3
View File
@@ -56,12 +56,14 @@ export async function buildConversationContext(
0,
);
const contextLines = await Promise.all(
contextBefore.map((msg) => formatMessageForPrompt(msg, "context")),
);
const selectedContextLines: string[] = [];
// Go backwards through context, taking most recent first
for (let i = contextBefore.length - 1; i >= 0; i--) {
const msg = contextBefore[i];
const line = await formatMessageForPrompt(msg, "context");
for (let i = contextLines.length - 1; i >= 0; i--) {
const line = contextLines[i];
const lineTokens = estimateTokens(line);
if (usedTokens + lineTokens <= maxTokens) {
+15 -2
View File
@@ -178,14 +178,27 @@ export async function captureMessage(
// Queue analysis after attachment uploads settle so AI uses stable tele URLs.
if (!isBacklog) {
if (attachmentUploadTasks.length > 0) {
setTimeout(() => queueMessageAnalysis(message.id), 30000);
let analysisQueued = false;
let fallbackTimer: NodeJS.Timeout | null = null;
const queueAnalysisOnce = () => {
if (analysisQueued) return;
analysisQueued = true;
if (fallbackTimer) {
clearTimeout(fallbackTimer);
fallbackTimer = null;
}
queueMessageAnalysis(message.id);
};
fallbackTimer = setTimeout(queueAnalysisOnce, 30000);
Promise.allSettled(attachmentUploadTasks)
.then(() => queueMessageAnalysis(message.id))
.then(queueAnalysisOnce)
.catch((err) => {
logger.error(
{ messageId: message.id, error: err },
"Failed to queue message analysis after attachment upload",
);
queueAnalysisOnce();
});
} else {
queueMessageAnalysis(message.id);
+20 -19
View File
@@ -48,6 +48,7 @@ interface MessageDatabase {
selectDistinct<T = unknown[]>(...args: unknown[]): QueryBuilder<T>;
insert<T = unknown>(...args: unknown[]): QueryBuilder<T>;
update(...args: unknown[]): QueryBuilder<unknown>;
transaction<T>(callback: (tx: MessageDatabase) => Promise<T>): Promise<T>;
}
function db(): MessageDatabase {
@@ -457,28 +458,28 @@ export async function updateMessagesAIAnalysisBulk(
): Promise<MessageRecord[]> {
if (updates.length === 0) return [];
try {
// Use raw SQL batch UPDATE instead of Promise.all per-message queries
// (P2: reduce N*2 queries → 2 queries total)
const database = db();
const now = Date.now();
for (const { messageId, result } of updates) {
await database
.update(messagesTable)
.set({
ai_status: result.status,
ai_moderation_flags: result.flags ?? null,
ai_moderation_score: result.score ?? null,
ai_analysis: result.analysis ?? null,
ai_categories: stringifyAIList(result.categories),
ai_severity: result.severity ?? null,
ai_confidence: result.confidence ?? result.score ?? null,
ai_recommended_action: result.recommendedAction ?? null,
ai_analyzed_at: result.analyzedAt ?? now,
ai_error: result.error ?? null,
})
.where(eq(messagesTable.id, messageId));
}
await database.transaction(async (tx) => {
for (const { messageId, result } of updates) {
await tx
.update(messagesTable)
.set({
ai_status: result.status,
ai_moderation_flags: result.flags ?? null,
ai_moderation_score: result.score ?? null,
ai_analysis: result.analysis ?? null,
ai_categories: stringifyAIList(result.categories),
ai_severity: result.severity ?? null,
ai_confidence: result.confidence ?? result.score ?? null,
ai_recommended_action: result.recommendedAction ?? null,
ai_analyzed_at: result.analyzedAt ?? now,
ai_error: result.error ?? null,
})
.where(eq(messagesTable.id, messageId));
}
});
// Fetch all updated messages in a single query
const ids = updates.map(({ messageId }) => messageId);
+2 -2
View File
@@ -38,7 +38,7 @@ export async function uploadRecordingSegment(input: {
try {
// 1. Get file size and insert initial pending state to DB
const stats = fs.statSync(oggPath);
const stats = await fs.promises.stat(oggPath);
await insertVoiceRecording({
id,
user_id: userId,
@@ -54,7 +54,7 @@ export async function uploadRecordingSegment(input: {
});
// 2. Perform async upload with retry logic
const fileBuffer = fs.readFileSync(oggPath);
const fileBuffer = await fs.promises.readFile(oggPath);
const uploadResult = await uploadToTele({
buffer: fileBuffer,
filename: fileName,
+5
View File
@@ -1,5 +1,6 @@
import process from "node:process";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { assertSafeTestDatabaseUrl } from "./helpers/testDatabase";
const originalEnv = process.env;
@@ -14,6 +15,10 @@ describe("Drizzle ORM Database", () => {
DISCORD_TOKEN: "test-token",
NODE_ENV: "test",
};
if (originalEnv.TEST_DATABASE_URL) {
process.env.DATABASE_URL = originalEnv.TEST_DATABASE_URL;
}
assertSafeTestDatabaseUrl();
// Reset modules to pick up new environment
vi.resetModules();
+64
View File
@@ -0,0 +1,64 @@
import process from "node:process";
import {
getDatabase,
initializeDatabase,
} from "../../src/database/drizzle";
interface RunnableDatabase {
run(sql: string): Promise<unknown>;
}
const SAFE_TEST_DATABASE_NAME = /(^|[_-])(test|testing)([_-]|$)|gmw_test/i;
function getDatabaseNameFromUrl(databaseUrl: string): string {
try {
const parsed = new URL(databaseUrl);
return parsed.pathname.replace(/^\//, "");
} catch {
return "";
}
}
function getConfiguredDatabaseName(): string {
if (process.env.DATABASE_URL) {
return getDatabaseNameFromUrl(process.env.DATABASE_URL);
}
return process.env.POSTGRES_DB ?? "";
}
export function assertSafeTestDatabaseUrl(): void {
if (process.env.NODE_ENV !== "test") {
throw new Error(
`Refusing to run destructive database test outside NODE_ENV=test (got ${process.env.NODE_ENV ?? "unset"})`,
);
}
if (process.env.TEST_DATABASE_URL) {
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
}
const databaseName = getConfiguredDatabaseName();
if (!SAFE_TEST_DATABASE_NAME.test(databaseName)) {
throw new Error(
`Refusing to run destructive database test against non-test database "${databaseName || "unknown"}". Set TEST_DATABASE_URL or DATABASE_URL to a database whose name contains "test" (for example hub_test).`,
);
}
}
export async function initializeTestDatabase() {
assertSafeTestDatabaseUrl();
return initializeDatabase();
}
export function getTestDatabase(): RunnableDatabase {
assertSafeTestDatabaseUrl();
return getDatabase() as unknown as RunnableDatabase;
}
export async function clearTestTables(...tableNames: string[]): Promise<void> {
assertSafeTestDatabaseUrl();
const db = getTestDatabase();
for (const tableName of tableNames) {
await db.run(`DELETE FROM "${tableName}"`);
}
}
+22 -12
View File
@@ -686,9 +686,7 @@ describe("runModerationAnalysis", () => {
expect(secondRequestBody.messages[0].content).toContain(
"RESPON SEBELUMNYA GAGAL VALIDASI",
);
expect(secondRequestBody.messages[0].content).toContain(
"Invalid option",
);
expect(secondRequestBody.messages[0].content).toContain("Invalid option");
expect(secondRequestBody.messages[0].content).toContain(
"Coba lagi dengan output JSON yang benar",
);
@@ -753,8 +751,8 @@ describe("runModerationAnalysis", () => {
arrayBuffer: async () => {
// Minimal valid PNG bytes (8-byte signature)
const png = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00,
0x0d, 0x49, 0x48, 0x44, 0x52,
]);
return png.buffer.slice(
png.byteOffset,
@@ -932,7 +930,8 @@ describe("runModerationAnalysis", () => {
status: "warn",
flags: ["harassment"],
score: 0.65,
analysis: "Teks mengandung unsur harassment dan memerlukan tindakan lebih lanjut.",
analysis:
"Teks mengandung unsur harassment dan memerlukan tindakan lebih lanjut.",
},
],
}),
@@ -941,7 +940,22 @@ describe("runModerationAnalysis", () => {
],
};
const imageBytes = Buffer.from("realistic-image-bytes");
const imageBytes = Buffer.from([
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
]);
const toBody = (buffer: Buffer) => {
let done = false;
return {
getReader: () => ({
read: async () => {
if (done) return { done: true, value: undefined };
done = true;
return { done: false, value: new Uint8Array(buffer) };
},
cancel: vi.fn(),
}),
};
};
global.fetch = vi.fn().mockImplementation((url: string) => {
if (
url === "https://httpbin.org/image/png" ||
@@ -949,11 +963,7 @@ describe("runModerationAnalysis", () => {
) {
return Promise.resolve({
ok: true,
arrayBuffer: async () =>
imageBytes.buffer.slice(
imageBytes.byteOffset,
imageBytes.byteOffset + imageBytes.byteLength,
),
body: toBody(imageBytes),
});
}
+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;
});
+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");
}
+7
View File
@@ -0,0 +1,7 @@
import process from "node:process";
process.env.NODE_ENV = "test";
if (process.env.TEST_DATABASE_URL) {
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL;
}
+1
View File
@@ -8,5 +8,6 @@ export default defineConfig({
environment: "node",
fileParallelism: false,
include: ["tests/**/*.test.ts"],
setupFiles: ["tests/setup.ts"],
},
});