fix: inyectar base de datos de prueba en WebhookServer para tests unitarios

Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent 37f3594117
commit 93bbd923ae

@ -4,7 +4,7 @@ import { GroupSyncService } from './services/group-sync';
import { ResponseQueue } from './services/response-queue';
import { WebhookManager } from './services/webhook-manager';
import { normalizeWhatsAppId } from './utils/whatsapp';
import { ensureUserExists } from './db';
import { ensureUserExists, db } from './db';
// Bun is available globally when running under Bun runtime
declare global {
@ -27,6 +27,8 @@ type WebhookPayload = {
};
export class WebhookServer {
static dbInstance: Database = db;
private static getBaseUrl(request: Request): string {
const proto = request.headers.get('x-forwarded-proto') || 'http';
const host = request.headers.get('x-forwarded-host') || request.headers.get('host');
@ -119,7 +121,7 @@ export class WebhookServer {
}
// Ensure user exists in database
const userId = ensureUserExists(data.key.participant);
const userId = ensureUserExists(data.key.participant, WebhookServer.dbInstance);
if (!userId) {
if (process.env.NODE_ENV !== 'test') {
console.log('⚠️ Failed to ensure user exists, ignoring message');

@ -26,6 +26,9 @@ beforeEach(() => {
mockAdd = mock(() => Promise.resolve());
ResponseQueue.add = mockAdd;
// Inject testDb for WebhookServer to use
WebhookServer.dbInstance = testDb;
// Ensure database is initialized (recreates tables if dropped)
initializeDatabase(testDb);
@ -373,9 +376,7 @@ describe('WebhookServer', () => {
remoteJid: 'group-id@g.us',
participant: 'sender-id@s.whatsapp.net'
},
message: {
conversation: `/tarea nueva Test task ${futureDate1} some text ${futureDate2}`
}
message: { conversation: `/tarea nueva Test task ${futureDate1} some text ${futureDate2}` }
}
};
@ -401,9 +402,7 @@ describe('WebhookServer', () => {
remoteJid: 'group-id@g.us',
participant: 'sender-id@s.whatsapp.net'
},
message: {
conversation: `/tarea nueva Test task ${pastDate} more text ${futureDate}`
}
message: { conversation: `/tarea nueva Test task ${pastDate} more text ${futureDate}` }
}
};
@ -429,9 +428,7 @@ describe('WebhookServer', () => {
remoteJid: 'group-id@g.us',
participant: 'sender-id@s.whatsapp.net'
},
message: {
conversation: `/tarea nueva Test task ${pastDate1} and ${pastDate2}`
}
message: { conversation: `/tarea nueva Test task ${pastDate1} and ${pastDate2}` }
}
};
@ -456,9 +453,7 @@ describe('WebhookServer', () => {
remoteJid: 'group-id@g.us',
participant: 'sender-id@s.whatsapp.net'
},
message: {
conversation: `/tarea nueva Test task 2023-13-01 (invalid) ${futureDate} 25/12/2023 (invalid)`
}
message: { conversation: `/tarea nueva Test task 2023-13-01 (invalid) ${futureDate} 25/12/2023 (invalid)` }
}
};

Loading…
Cancel
Save