From e2e9a4ccdba34f684a521724419125b00e3f68e8 Mon Sep 17 00:00:00 2001 From: borja Date: Fri, 5 Sep 2025 12:38:23 +0200 Subject: [PATCH] =?UTF-8?q?test:=20usar=20DB=20real=20en=20tests=20de=20va?= =?UTF-8?q?lidaci=C3=B3n=20de=20usuario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) --- tests/unit/server.test.ts | 83 +++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index f5543e5..99f9f09 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -1,13 +1,36 @@ -import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test'; +import { describe, test, expect, beforeEach, afterEach, beforeAll, afterAll } from 'bun:test'; +import { Database } from 'bun:sqlite'; import { WebhookServer } from '../../src/server'; import { ResponseQueue } from '../../src/services/response-queue'; +import { initializeDatabase } from '../../src/db'; // Mock the ResponseQueue let mockAdd: any; +// Test database instance +let testDb: Database; + +beforeAll(() => { + // Create in-memory test database + testDb = new Database(':memory:'); + // Initialize schema + initializeDatabase(testDb); +}); + +afterAll(() => { + // Close the test database + testDb.close(); +}); + beforeEach(() => { mockAdd = mock(() => Promise.resolve()); ResponseQueue.add = mockAdd; + + // Reset database state between tests + testDb.exec('DELETE FROM task_assignments'); + testDb.exec('DELETE FROM tasks'); + testDb.exec('DELETE FROM users'); + testDb.exec('DELETE FROM groups'); }); describe('WebhookServer', () => { @@ -499,6 +522,11 @@ describe('WebhookServer', () => { const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); expect(mockAdd).toHaveBeenCalled(); + + // Verify user was created in real database + const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890'); + expect(user).toBeDefined(); + expect(user.id).toBe('1234567890'); }); test('should ignore messages if user creation fails', async () => { @@ -517,36 +545,15 @@ describe('WebhookServer', () => { const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); expect(mockAdd).not.toHaveBeenCalled(); + + // Verify no user was created + const userCount = testDb.query("SELECT COUNT(*) as count FROM users").get(); + expect(userCount.count).toBe(0); }); - // New tests for user validation + // Integration tests with real database describe('User validation in handleMessageUpsert', () => { - let mockEnsureUserExists: any; - let consoleSpy: any; - let originalConsoleLog: any; - let originalEnsureUserExists: any; - - beforeEach(async () => { - // Import the db module dynamically to mock ensureUserExists locally - const dbModule = await import('../../src/db'); - originalEnsureUserExists = dbModule.ensureUserExists; - mockEnsureUserExists = mock(() => '1234567890'); - dbModule.ensureUserExists = mockEnsureUserExists; - - originalConsoleLog = console.log; - consoleSpy = mock(() => {}); - console.log = consoleSpy; - }); - - afterEach(async () => { - // Restore the original functions manually - const dbModule = await import('../../src/db'); - dbModule.ensureUserExists = originalEnsureUserExists; - console.log = originalConsoleLog; - }); - test('should proceed with valid user', async () => { - mockEnsureUserExists.mockReturnValue('1234567890'); const payload = { event: 'messages.upsert', instance: 'test-instance', @@ -561,12 +568,15 @@ describe('WebhookServer', () => { const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); - expect(mockEnsureUserExists).toHaveBeenCalledWith('1234567890@s.whatsapp.net'); expect(mockAdd).toHaveBeenCalled(); + + // Verify user was created in real database + const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890'); + expect(user).toBeDefined(); + expect(user.id).toBe('1234567890'); }); test('should ignore message if user validation fails', async () => { - mockEnsureUserExists.mockReturnValue(null); const payload = { event: 'messages.upsert', instance: 'test-instance', @@ -581,13 +591,17 @@ describe('WebhookServer', () => { const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); - expect(mockEnsureUserExists).toHaveBeenCalledWith('invalid-user@s.whatsapp.net'); expect(mockAdd).not.toHaveBeenCalled(); - expect(consoleSpy).toHaveBeenCalledWith('⚠️ Failed to ensure user exists, ignoring message'); + + // Verify no user was created + const userCount = testDb.query("SELECT COUNT(*) as count FROM users").get(); + expect(userCount.count).toBe(0); }); test('should handle database errors during user validation', async () => { - mockEnsureUserExists.mockImplementation(() => { throw new Error('DB error'); }); + // Force a database error by corrupting the database state + testDb.exec('DROP TABLE users'); + const payload = { event: 'messages.upsert', instance: 'test-instance', @@ -602,9 +616,10 @@ describe('WebhookServer', () => { const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); - expect(mockEnsureUserExists).toHaveBeenCalledWith('1234567890@s.whatsapp.net'); expect(mockAdd).not.toHaveBeenCalled(); - expect(consoleSpy).toHaveBeenCalledWith('⚠️ Failed to ensure user exists, ignoring message'); + + // Reinitialize database for subsequent tests + initializeDatabase(testDb); }); }); });