import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; import { makeMemDb } from '../../helpers/db'; import { GroupSyncService } from '../../../src/services/group-sync'; import { AllowedGroups } from '../../../src/services/allowed-groups'; describe('GroupSyncService - gating en syncMembersForGroup (enforce)', () => { const envBackup = process.env; beforeEach(() => { process.env = { ...envBackup, NODE_ENV: 'test', GROUP_GATING_MODE: 'enforce' }; const memdb = makeMemDb(); (GroupSyncService as any).dbInstance = memdb; (AllowedGroups as any).dbInstance = memdb; // Stub fetchGroupMembersFromAPI para no hacer red (GroupSyncService as any).fetchGroupMembersFromAPI = async (_groupId: string) => { return [{ userId: '34600111111', isAdmin: false }]; }; }); afterEach(() => { process.env = envBackup; }); it('no sincroniza miembros para grupo no allowed', async () => { const res = await GroupSyncService.syncMembersForGroup('na@g.us'); expect(res).toEqual({ added: 0, updated: 0, deactivated: 0 }); const db = (GroupSyncService as any).dbInstance; const row = db.query(`SELECT COUNT(*) AS c FROM group_members WHERE group_id = 'na@g.us'`).get() as any; expect(Number(row?.c || 0)).toBe(0); }); it('sincroniza miembros para grupo allowed', async () => { AllowedGroups.setStatus('ok@g.us', 'allowed', 'OK'); const res = await GroupSyncService.syncMembersForGroup('ok@g.us'); expect(res.added + res.updated + res.deactivated).toBeGreaterThan(0); const db = (GroupSyncService as any).dbInstance; const row = db.query(`SELECT COUNT(*) AS c FROM group_members WHERE group_id = 'ok@g.us'`).get() as any; expect(Number(row?.c || 0)).toBeGreaterThan(0); }); });