/** * Group validation tests. * * Ensures WebhookServer respects the active/inactive status of groups, * supports the t command alias, and enforces the DM‑only response policy. */ import { describe, test, expect } from 'bun:test'; import { WebhookServer } from '../../src/server'; import { SimulatedResponseQueue } from '../helpers/queue'; import { createTestRequest, registerServerTestLifecycle } from '../helpers/server-test-harness'; const testDb = registerServerTestLifecycle(); // ── Tests ────────────────────────────────────────────────────────────── describe('Group validation in handleMessageUpsert', () => { test('should ignore messages from inactive groups', async () => { testDb.exec(` INSERT OR REPLACE INTO groups (id, community_id, name, active) VALUES ('inactive-group@g.us', 'test-community', 'Inactive Group', 0) `); const payload = { event: 'messages.upsert', instance: 'test-instance', data: { key: { remoteJid: 'inactive-group@g.us', participant: '1234567890@s.whatsapp.net', }, message: { conversation: 'tarea nueva Test' }, }, }; const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); expect(SimulatedResponseQueue.get().length).toBe(0); }); test('should proceed with messages from active groups', async () => { const payload = { event: 'messages.upsert', instance: 'test-instance', data: { key: { remoteJid: 'group-id@g.us', participant: '1234567890@s.whatsapp.net', }, message: { conversation: 'tarea nueva Test' }, }, }; const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); expect(SimulatedResponseQueue.get().length).toBeGreaterThan(0); }); test('should accept t alias and process command', async () => { const payload = { event: 'messages.upsert', instance: 'test-instance', data: { key: { remoteJid: 'group-id@g.us', participant: '1234567890@s.whatsapp.net', }, message: { conversation: 't n Tarea alias hoy' }, }, }; const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); expect(SimulatedResponseQueue.get().length).toBeGreaterThan(0); }); test('should never send responses to the group (DM only policy)', async () => { const payload = { event: 'messages.upsert', instance: 'test-instance', data: { key: { remoteJid: 'group-id@g.us', participant: '1234567890@s.whatsapp.net', }, message: { conversation: 't n Probar silencio grupo mañana' }, }, }; const request = createTestRequest(payload); await WebhookServer.handleRequest(request); const out = SimulatedResponseQueue.get(); expect(out.length).toBeGreaterThan(0); for (const r of out) { expect(r.recipient.endsWith('@g.us')).toBe(false); } }); });