import assert from 'node:assert/strict'; import { after, before, beforeEach, describe, it } from 'node:test'; import { GUILD_SETTINGS, SETTING_SECTIONS, allSettings, readSetting, validateSetting, writeSetting, } from '../src/config/guild-settings.js'; import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js'; const GUILD = 'guild-1'; describe('settings schema', () => { it('places every setting in a declared section', () => { const sections = new Set(SETTING_SECTIONS.map((section) => section.id)); for (const setting of GUILD_SETTINGS) { assert.ok(sections.has(setting.section), `${setting.key} points at an unknown section`); } }); it('documents every setting and gives it a default', () => { for (const setting of GUILD_SETTINGS) { assert.ok(setting.label?.length > 0, `${setting.key} has no label`); assert.ok(setting.help?.length > 0, `${setting.key} has no help`); assert.notEqual(setting.default, undefined, `${setting.key} has no default`); } }); it('uses unique keys', () => { const keys = GUILD_SETTINGS.map((setting) => setting.key); assert.equal(new Set(keys).size, keys.length); }); /** * A switch that nothing reads is worse than no switch: it promises a * behaviour the bot does not have. */ it('is read by the code for every key it declares', async () => { const { readFile, readdir } = await import('node:fs/promises'); const path = await import('node:path'); const files = []; const walk = async (directory) => { for (const entry of await readdir(directory, { withFileTypes: true })) { const target = path.join(directory, entry.name); if (entry.isDirectory()) await walk(target); else if (entry.name.endsWith('.js')) files.push(target); } }; await walk('src'); const sources = (await Promise.all(files.map((file) => readFile(file, 'utf8')))).join('\n'); for (const setting of GUILD_SETTINGS) { assert.ok( sources.includes(`'${setting.key}'`), `${setting.key} is declared but never read by the code`, ); } }); }); describe('settings validation', () => { it('accepts a boolean and refuses anything else', () => { assert.equal(validateSetting('welcome.enabled', true), null); assert.match(validateSetting('welcome.enabled', 'oui'), /vrai ou faux/); }); it('enforces the numeric bounds', () => { assert.equal(validateSetting('moderation.defaultMuteMinutes', 30), null); assert.match(validateSetting('moderation.defaultMuteMinutes', 0), /Minimum/); assert.match(validateSetting('moderation.banDeleteMessageHours', 200), /Maximum/); assert.match(validateSetting('moderation.defaultMuteMinutes', 'beaucoup'), /entier/); }); it('restricts an enum to its options', () => { assert.equal(validateSetting('web.defaultMode', 'advanced'), null); assert.match(validateSetting('web.defaultMode', 'expert'), /Valeurs possibles/); }); it('accepts an empty snowflake, meaning « none »', () => { assert.equal(validateSetting('notifications.mentionRoleId', ''), null); assert.equal(validateSetting('notifications.mentionRoleId', '123456789012345678'), null); assert.match(validateSetting('notifications.mentionRoleId', 'abc'), /17 à 20 chiffres/); }); it('caps the length of a free text', () => { assert.equal(validateSetting('welcome.message', 'Bonjour'), null); assert.match(validateSetting('welcome.message', 'x'.repeat(501)), /500 caractères/); }); it('rejects an unknown key', () => { assert.match(validateSetting('nope', true), /inconnu/); }); }); describe('settings storage', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => getDatabase().exec('DELETE FROM guild_settings;')); it('falls back to the declared default', () => { assert.equal(readSetting('welcome.enabled', GUILD), true); assert.equal(readSetting('moderation.defaultMuteMinutes', GUILD), 60); assert.equal(readSetting('notifications.mentionRoleId', GUILD), ''); }); it('stores then reads a value back with its type', () => { writeSetting('welcome.enabled', false, { guildId: GUILD }); writeSetting('moderation.defaultMuteMinutes', 15, { guildId: GUILD }); assert.equal(readSetting('welcome.enabled', GUILD), false); assert.equal(readSetting('moderation.defaultMuteMinutes', GUILD), 15); }); it('refuses to store an invalid value', () => { assert.throws(() => writeSetting('web.defaultMode', 'expert', { guildId: GUILD }), /Valeurs possibles/); assert.equal(readSetting('web.defaultMode', GUILD), 'simple'); }); it('keeps each guild apart', () => { writeSetting('welcome.enabled', false, { guildId: GUILD }); assert.equal(readSetting('welcome.enabled', 'other-guild'), true); }); it('coerces a value stored out of bounds back into range', () => { getDatabase() .prepare('INSERT INTO guild_settings (guild_id, key, value) VALUES (?, ?, ?)') .run(GUILD, 'moderation.defaultMuteMinutes', '999999'); assert.equal(readSetting('moderation.defaultMuteMinutes', GUILD), 40_320); }); it('returns every setting at once', () => { const values = allSettings(GUILD); assert.equal(Object.keys(values).length, GUILD_SETTINGS.length); assert.equal(values['welcome.enabled'], true); }); it('rejects an unknown key rather than storing it', () => { assert.throws(() => writeSetting('nope', true, { guildId: GUILD }), /Unknown guild setting/); }); });