feat: añadir alias para comandos de admin y actualizar tests

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
main
brobert 1 month ago
parent 99c68bf105
commit abfa9b73a7

@ -38,11 +38,11 @@ export class AdminService {
private static help(): string { private static help(): string {
return [ return [
'Comandos de administración:', 'Comandos de administración:',
'- /admin pendientes', '- /admin pendientes (alias: pending, pend)',
'- /admin habilitar-aquí', '- /admin habilitar-aquí (alias: enable)',
'- /admin deshabilitar-aquí', '- /admin deshabilitar-aquí (alias: disable)',
'- /admin allow-group <group_id@g.us>', '- /admin allow-group <group_id@g.us> (alias: allow)',
'- /admin block-group <group_id@g.us>', '- /admin block-group <group_id@g.us> (alias: block)',
].join('\n'); ].join('\n');
} }
@ -66,7 +66,7 @@ export class AdminService {
const rest = lower.slice('/admin'.length).trim(); const rest = lower.slice('/admin'.length).trim();
// /admin pendientes // /admin pendientes
if (rest === 'pendientes') { if (rest === 'pendientes' || rest === 'pending' || rest === 'pend') {
const rows = AllowedGroups.listByStatus('pending'); const rows = AllowedGroups.listByStatus('pending');
if (!rows || rows.length === 0) { if (!rows || rows.length === 0) {
return [{ recipient: sender, message: '✅ No hay grupos pendientes.' }]; return [{ recipient: sender, message: '✅ No hay grupos pendientes.' }];
@ -79,7 +79,7 @@ export class AdminService {
} }
// /admin habilitar-aquí // /admin habilitar-aquí
if (rest === 'habilitar-aquí' || rest === 'habilitar-aqui') { if (rest === 'habilitar-aquí' || rest === 'habilitar-aqui' || rest === 'enable') {
if (!isGroupId(ctx.groupId)) { if (!isGroupId(ctx.groupId)) {
return [{ recipient: sender, message: ' Este comando se debe usar dentro de un grupo.' }]; return [{ recipient: sender, message: ' Este comando se debe usar dentro de un grupo.' }];
} }
@ -89,7 +89,7 @@ export class AdminService {
} }
// /admin deshabilitar-aquí // /admin deshabilitar-aquí
if (rest === 'deshabilitar-aquí' || rest === 'deshabilitar-aqui') { if (rest === 'deshabilitar-aquí' || rest === 'deshabilitar-aqui' || rest === 'disable') {
if (!isGroupId(ctx.groupId)) { if (!isGroupId(ctx.groupId)) {
return [{ recipient: sender, message: ' Este comando se debe usar dentro de un grupo.' }]; return [{ recipient: sender, message: ' Este comando se debe usar dentro de un grupo.' }];
} }
@ -99,8 +99,8 @@ export class AdminService {
} }
// /admin allow-group <jid> // /admin allow-group <jid>
if (rest.startsWith('allow-group ')) { if (rest.startsWith('allow-group ') || rest.startsWith('allow ')) {
const arg = rest.slice('allow-group '.length).trim(); const arg = (rest.startsWith('allow-group ') ? rest.slice('allow-group '.length) : rest.slice('allow '.length)).trim();
if (!isGroupId(arg)) { if (!isGroupId(arg)) {
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }]; return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
} }
@ -110,8 +110,8 @@ export class AdminService {
} }
// /admin block-group <jid> // /admin block-group <jid>
if (rest.startsWith('block-group ')) { if (rest.startsWith('block-group ') || rest.startsWith('block ')) {
const arg = rest.slice('block-group '.length).trim(); const arg = (rest.startsWith('block-group ') ? rest.slice('block-group '.length) : rest.slice('block '.length)).trim();
if (!isGroupId(arg)) { if (!isGroupId(arg)) {
return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }]; return [{ recipient: sender, message: '⚠️ Debes indicar un group_id válido terminado en @g.us' }];
} }

@ -108,4 +108,111 @@ describe('WebhookServer - /admin aprobación en modo enforce', () => {
const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'another-group@g.us'`).get() as any; const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'another-group@g.us'`).get() as any;
expect(row == null).toBe(true); expect(row == null).toBe(true);
}); });
test('admin puede habilitar el grupo actual con alias /admin enable', async () => {
const payload = {
event: 'messages.upsert',
instance: 'test-instance',
data: {
key: {
remoteJid: 'new-group-alias@g.us',
participant: '1234567890@s.whatsapp.net'
},
message: { conversation: '/admin enable' }
}
};
const res = await WebhookServer.handleRequest(createTestRequest(payload));
expect(res.status).toBe(200);
// Debe haber respuesta de confirmación encolada
expect(SimulatedResponseQueue.get().length).toBeGreaterThan(0);
// El grupo debe figurar como allowed
const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'new-group-alias@g.us'`).get() as any;
expect(row && String(row.status)).toBe('allowed');
});
test('admin puede deshabilitar el grupo actual con alias /admin disable', async () => {
const payload = {
event: 'messages.upsert',
instance: 'test-instance',
data: {
key: {
remoteJid: 'disable-group@g.us',
participant: '1234567890@s.whatsapp.net'
},
message: { conversation: '/admin disable' }
}
};
const res = await WebhookServer.handleRequest(createTestRequest(payload));
expect(res.status).toBe(200);
expect(SimulatedResponseQueue.get().length).toBeGreaterThan(0);
const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'disable-group@g.us'`).get() as any;
expect(row && String(row.status)).toBe('blocked');
});
test('admin puede permitir un grupo por JID con alias /admin allow <jid>', async () => {
const payload = {
event: 'messages.upsert',
instance: 'test-instance',
data: {
key: {
remoteJid: 'some-group@g.us',
participant: '1234567890@s.whatsapp.net'
},
message: { conversation: '/admin allow target-allow@g.us' }
}
};
const res = await WebhookServer.handleRequest(createTestRequest(payload));
expect(res.status).toBe(200);
const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'target-allow@g.us'`).get() as any;
expect(row && String(row.status)).toBe('allowed');
});
test('admin puede bloquear un grupo por JID con alias /admin block <jid>', async () => {
const payload = {
event: 'messages.upsert',
instance: 'test-instance',
data: {
key: {
remoteJid: 'some-group@g.us',
participant: '1234567890@s.whatsapp.net'
},
message: { conversation: '/admin block target-block@g.us' }
}
};
const res = await WebhookServer.handleRequest(createTestRequest(payload));
expect(res.status).toBe(200);
const row = testDb.query(`SELECT status FROM allowed_groups WHERE group_id = 'target-block@g.us'`).get() as any;
expect(row && String(row.status)).toBe('blocked');
});
test('admin puede ver pendientes con alias /admin pending', async () => {
const payload = {
event: 'messages.upsert',
instance: 'test-instance',
data: {
key: {
remoteJid: 'some-group@g.us',
participant: '1234567890@s.whatsapp.net'
},
message: { conversation: '/admin pending' }
}
};
const res = await WebhookServer.handleRequest(createTestRequest(payload));
expect(res.status).toBe(200);
const out = SimulatedResponseQueue.get();
expect(out.length).toBe(1);
expect(String(out[0].message).toLowerCase()).toContain('no hay grupos pendientes');
});
}); });

Loading…
Cancel
Save