You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB
TypeScript

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;
});
});