Files
BlackAngelTV 99e1fd2723 fix(moderation): tell a mute apart from a kick in the logs
A timeout was filed as "Exclusion temporaire" right next to "Expulsion",
so a sanction could not be read back from the log channel, the direct
message or `/mod logs`. Each action now carries its own emoji and wording
behind a single label, used by every surface.
2026-09-25 20:29:19 +02:00

133 lines
5.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { createHmac } from 'node:crypto';
import { readdir } from 'node:fs/promises';
import { describe, it } from 'node:test';
import { ACCESS_LEVELS } from '../src/config/constants.js';
import { loadCommands } from '../src/core/command-loader.js';
const eventsDir = new URL('../src/events/', import.meta.url);
describe('command modules', () => {
it('exposes data, a valid access level and an execute function', async () => {
const commands = await loadCommands();
assert.ok(commands.length >= 16, `only ${commands.length} commands loaded`);
for (const command of commands) {
const json = command.data.toJSON();
assert.ok(json.name, 'a command has no name');
assert.ok(json.description, `/${json.name} has no description`);
assert.equal(typeof command.execute, 'function', `/${json.name} has no execute()`);
assert.ok(
Object.values(ACCESS_LEVELS).includes(command.access),
`/${json.name} has an unknown access level`,
);
}
});
it('uses unique command names', async () => {
const names = (await loadCommands()).map((command) => command.data.name);
assert.equal(new Set(names).size, names.length);
});
it('only overrides the access of declared subcommands', async () => {
/** Top-level subcommands and those nested in a subcommand group (M14). */
const subcommandNames = (options = []) =>
options.flatMap((option) =>
option.type === 1 ? [option.name] : option.type === 2 ? subcommandNames(option.options) : [],
);
for (const command of await loadCommands()) {
if (!command.subcommandAccess) continue;
const subcommands = subcommandNames(command.data.toJSON().options);
for (const name of Object.keys(command.subcommandAccess)) {
assert.ok(subcommands.includes(name), `/${command.data.name} ${name} is not a subcommand`);
}
}
});
});
describe('message tone', () => {
it('builds every embed with the shared titles, marker and hint', async () => {
const { TONE, bullets, errorEmbed, successEmbed, warningEmbed, withHint } =
await import('../src/ui/embeds.js');
assert.equal(successEmbed('x').data.title, TONE.success);
assert.equal(errorEmbed('x').data.title, TONE.error);
assert.equal(warningEmbed('x').data.title, TONE.warning);
assert.equal(bullets(['a', 'b', '']), '• a\n• b');
assert.equal(withHint('fait', 'ensuite'), 'fait\n\nensuite');
assert.equal(withHint('fait', null), 'fait');
});
it('keeps command descriptions free of gendered wording', async () => {
/** Words that would address or describe a person with a gender. */
const gendered =
/\b(utilisateur|utilisatrice|cher|chère|ravi|ravie|content|contente|bienvenu|bienvenue à (lui|elle))/i;
for (const command of await loadCommands()) {
const json = command.data.toJSON();
const texts = [json.description, ...(json.options ?? []).map((option) => option.description ?? '')];
for (const text of texts) {
assert.ok(!gendered.test(text), `/${json.name} uses gendered wording: ${text}`);
}
}
});
});
describe('webhook entry points', () => {
it('exposes the server and the dispatch functions', async () => {
const server = await import('../src/webhooks/server.js');
const webhook = await import('../src/webhooks/gitea-webhook.js');
assert.equal(typeof server.startWebhookServer, 'function');
assert.equal(typeof webhook.handleGiteaWebhook, 'function');
});
it('accepts only a valid HMAC signature and refuses an empty secret', async () => {
const { isValidSignature } = await import('../src/webhooks/gitea-webhook.js');
const body = Buffer.from('{"action":"opened"}');
const signature = createHmac('sha256', 'secret').update(body).digest('hex');
assert.equal(isValidSignature(body, signature, 'secret'), true);
assert.equal(isValidSignature(body, 'deadbeef', 'secret'), false);
assert.equal(isValidSignature(body, signature, ''), false);
assert.equal(isValidSignature(body, undefined, 'secret'), false);
});
});
describe('moderation labels', () => {
it('tells every action apart by wording and emoji', async () => {
const { MODERATION_ACTIONS } = await import('../src/config/constants.js');
const { moderationLabel } = await import('../src/services/moderation/moderation-service.js');
const emojis = Object.values(MODERATION_ACTIONS).map((meta) => meta.emoji);
assert.ok(emojis.every(Boolean), 'an action has no emoji');
assert.equal(new Set(emojis).size, emojis.length, 'two actions share an emoji');
const labels = Object.keys(MODERATION_ACTIONS).map(moderationLabel);
assert.equal(new Set(labels).size, labels.length, 'two actions read the same');
assert.equal(moderationLabel('inconnu'), 'inconnu', 'an unknown action must stay readable');
});
it('never reads a mute like a kick', async () => {
// Reported from the moderation log channel: a timeout was filed as a
// temporary kick because both labels sat next to each other.
const { moderationLabel } = await import('../src/services/moderation/moderation-service.js');
assert.equal(moderationLabel('mute'), '🔇 Mise en sourdine');
assert.equal(moderationLabel('kick'), '👢 Expulsion');
});
});
describe('event modules', () => {
it('exports a name and a handler', async () => {
const files = (await readdir(eventsDir)).filter((file) => file.endsWith('.js'));
assert.ok(files.length >= 6, `only ${files.length} event files found`);
for (const file of files) {
const event = await import(new URL(file, eventsDir).href);
assert.equal(typeof event.name, 'string', `${file} has no name`);
assert.equal(typeof event.execute, 'function', `${file} has no execute()`);
}
});
});