Files
FWS-DiscordBot/tests/modules.test.js
T
BlackAngelTV 836abeafd5 feat(voice): support several trigger channels with their own category
A server needs one "create your own room" channel per theme, which a single
trigger id could not express. Triggers are now stored as a list, each with its
own home category, name template and default limit.

Joining a trigger creates the channel at the bottom of that trigger's category,
and a member keeps one channel per trigger instead of one room for the whole
server. Triggers declared by the single-trigger version and
CHANNEL_VOICE_TRIGGER_ID still work as fallbacks, so no server has to be
reconfigured.
2026-09-15 16:09:39 +02:00

81 lines
3.3 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('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()`);
}
});
});