test(app): add command and event loading smoke tests
Loading every module catches broken imports and invalid slash command definitions, which unit tests on the services cannot see.
This commit is contained in:
1 file changed
+76
@@ -0,0 +1,76 @@
|
||||
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 () => {
|
||||
for (const command of await loadCommands()) {
|
||||
if (!command.subcommandAccess) continue;
|
||||
const subcommands = (command.data.toJSON().options ?? [])
|
||||
.filter((option) => option.type === 1)
|
||||
.map((option) => option.name);
|
||||
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()`);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user