import { describe, it, beforeEach, expect } from 'bun:test'; import { makeMemDb } from '../../helpers/db'; import { AllowedGroups } from '../../../src/services/allowed-groups'; describe('AllowedGroups service', () => { beforeEach(() => { const memdb = makeMemDb(); (AllowedGroups as any).dbInstance = memdb; AllowedGroups.resetForTests(); }); it('upsertPending inserta pending y es idempotente', () => { const gid = '123@g.us'; AllowedGroups.upsertPending(gid, 'Grupo 123', 'tester'); AllowedGroups.upsertPending(gid, 'Grupo 123', 'tester'); // No se expone la DB aquĆ­; validamos por comportamiento expect(AllowedGroups.isAllowed(gid)).toBe(false); }); it('setStatus cambia a allowed y isAllowed refleja el estado', () => { const gid = '456@g.us'; expect(AllowedGroups.isAllowed(gid)).toBe(false); const changed = AllowedGroups.setStatus(gid, 'allowed', 'Grupo 456'); expect(changed).toBe(true); expect(AllowedGroups.isAllowed(gid)).toBe(true); // Repetir con el mismo estado no debe cambiar const changedAgain = AllowedGroups.setStatus(gid, 'allowed', 'Grupo 456'); expect(changedAgain).toBe(false); }); it('listByStatus devuelve grupos por estado', () => { AllowedGroups.setStatus('a@g.us', 'allowed', 'A'); AllowedGroups.setStatus('b@g.us', 'pending', 'B'); AllowedGroups.setStatus('c@g.us', 'blocked', 'C'); const allowed = AllowedGroups.listByStatus('allowed').map(r => r.group_id); const pending = AllowedGroups.listByStatus('pending').map(r => r.group_id); const blocked = AllowedGroups.listByStatus('blocked').map(r => r.group_id); expect(allowed).toContain('a@g.us'); expect(pending).toContain('b@g.us'); expect(blocked).toContain('c@g.us'); }); it('seedFromEnv marca como allowed los ids provistos', () => { const prev = process.env.ALLOWED_GROUPS; process.env.ALLOWED_GROUPS = 'x@g.us, y@g.us , , z@g.us'; AllowedGroups.seedFromEnv(); expect(AllowedGroups.isAllowed('x@g.us')).toBe(true); expect(AllowedGroups.isAllowed('y@g.us')).toBe(true); expect(AllowedGroups.isAllowed('z@g.us')).toBe(true); process.env.ALLOWED_GROUPS = prev; }); });