fix: correct group sync test mocks and assertions

main
borja (aider) 3 months ago
parent 936a864a9e
commit 99b4845a8f

@ -3,18 +3,17 @@ import { GroupSyncService } from '../../../src/services/group-sync';
import { db } from '../../../src/db'; import { db } from '../../../src/db';
// Store original globals // Store original globals
const originalEnv = { ...process.env };
const originalFetch = globalThis.fetch; const originalFetch = globalThis.fetch;
process.env = {
WHATSAPP_COMMUNITY_ID: 'test-community', describe('GroupSyncService', () => {
EVOLUTION_API_URL: 'http://test-api', let fetchMock: any;
EVOLUTION_API_INSTANCE: 'test-instance',
EVOLUTION_API_KEY: 'test-key', beforeEach(() => {
...process.env db.exec('DELETE FROM groups');
}; GroupSyncService['lastSyncAttempt'] = 0;
// Mock fetch // Setup mock fetch
const fetchMock = mock(async () => ({ fetchMock = mock(async () => ({
ok: true, ok: true,
json: async () => [ json: async () => [
{ id: 'group1', subject: 'Group 1', linkedParent: 'test-community' }, { id: 'group1', subject: 'Group 1', linkedParent: 'test-community' },
@ -22,15 +21,9 @@ const fetchMock = mock(async () => ({
{ id: 'group3', subject: 'Group 3' } // No linkedParent { id: 'group3', subject: 'Group 3' } // No linkedParent
] ]
})); }));
// Replace global fetch
globalThis.fetch = fetchMock; globalThis.fetch = fetchMock;
describe('GroupSyncService', () => { // Setup env vars
beforeEach(() => {
db.exec('DELETE FROM groups');
GroupSyncService['lastSyncAttempt'] = 0;
// Reset process.env to mock values
process.env.WHATSAPP_COMMUNITY_ID = 'test-community'; process.env.WHATSAPP_COMMUNITY_ID = 'test-community';
process.env.EVOLUTION_API_URL = 'http://test-api'; process.env.EVOLUTION_API_URL = 'http://test-api';
process.env.EVOLUTION_API_INSTANCE = 'test-instance'; process.env.EVOLUTION_API_INSTANCE = 'test-instance';
@ -38,12 +31,6 @@ describe('GroupSyncService', () => {
}); });
afterAll(() => { afterAll(() => {
// Restore original process.env
process.env.WHATSAPP_COMMUNITY_ID = originalEnv.WHATSAPP_COMMUNITY_ID;
process.env.EVOLUTION_API_URL = originalEnv.EVOLUTION_API_URL;
process.env.EVOLUTION_API_INSTANCE = originalEnv.EVOLUTION_API_INSTANCE;
process.env.EVOLUTION_API_KEY = originalEnv.EVOLUTION_API_KEY;
// Restore original fetch
globalThis.fetch = originalFetch; globalThis.fetch = originalFetch;
}); });
@ -52,11 +39,11 @@ describe('GroupSyncService', () => {
GroupSyncService['lastSyncAttempt'] = Date.now() - 1000; GroupSyncService['lastSyncAttempt'] = Date.now() - 1000;
const result = await GroupSyncService.syncGroups(); const result = await GroupSyncService.syncGroups();
expect(result).toEqual({ added: 0, updated: 0 }); expect(result).toEqual({ added: 0, updated: 0 });
expect(mockFetch).not.toHaveBeenCalled(); expect(fetchMock).not.toHaveBeenCalled();
}); });
it('should throw if WHATSAPP_COMMUNITY_ID is missing', async () => { it('should throw if WHATSAPP_COMMUNITY_ID is missing', async () => {
env.WHATSAPP_COMMUNITY_ID = ''; process.env.WHATSAPP_COMMUNITY_ID = '';
await expect(GroupSyncService.syncGroups()).rejects.toThrow('WHATSAPP_COMMUNITY_ID is not set'); await expect(GroupSyncService.syncGroups()).rejects.toThrow('WHATSAPP_COMMUNITY_ID is not set');
}); });
@ -73,7 +60,7 @@ describe('GroupSyncService', () => {
it('should update existing groups', async () => { it('should update existing groups', async () => {
// Add initial group // Add initial group
db.exec( db.exec(
"INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', TRUE)" "INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', 1)"
); );
const result = await GroupSyncService.syncGroups(); const result = await GroupSyncService.syncGroups();
@ -88,7 +75,7 @@ describe('GroupSyncService', () => {
it('should mark non-matching groups as inactive', async () => { it('should mark non-matching groups as inactive', async () => {
// Add initial group not in current sync // Add initial group not in current sync
db.exec( db.exec(
"INSERT INTO groups (id, community_id, name, active) VALUES ('old-group', 'test-community', 'Old Group', TRUE)" "INSERT INTO groups (id, community_id, name, active) VALUES ('old-group', 'test-community', 'Old Group', 1)"
); );
await GroupSyncService.syncGroups(); await GroupSyncService.syncGroups();

Loading…
Cancel
Save