Files
FWS-DiscordBot/tests/modules.test.js

109 lines
4.6 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('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()`);
}
});
});