Files
FWS-DiscordBot/tests/database.test.js
T
BlackAngelTV 9f8ac0d29e feat(moderation): add the unmute and unban commands
A sanction could be applied but never lifted from Discord, so undoing a
mistake meant leaving the bot for the server settings. Both lifts are
recorded next to the sanctions they lift, which means widening the
moderation history CHECK constraint through a table rebuild.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-25 20:11:00 +02:00

48 lines
1.9 KiB
JavaScript

import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
import { listActions, recordAction } from '../src/database/repositories/moderation-repository.js';
import { getSetting, setSetting } from '../src/database/repositories/settings-repository.js';
describe('database', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
it('applies the initial migration', () => {
const tables = getDatabase()
.prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
.all()
.map((row) => row.name);
assert.ok(tables.includes('tickets'));
assert.ok(tables.includes('guild_settings'));
});
it('isolates settings per guild', () => {
setSetting('guild-a', 'welcome', { enabled: true });
assert.deepEqual(getSetting('guild-a', 'welcome'), { enabled: true });
assert.equal(getSetting('guild-b', 'welcome'), null);
});
it('records a lifted sanction next to the sanction it lifts', () => {
// `/unmute` and `/unban` go through the same CHECK constraint as `/mute`
// and `/ban`, so a migration that forgets them breaks the history.
for (const action of ['unban', 'unmute']) {
recordAction({ guildId: 'g', action, moderatorId: 'mod', targetId: 'u', reason: 'Erreur' });
}
const rows = listActions({ guildId: 'g', targetId: 'u' });
assert.deepEqual(
rows.map((row) => row.action).sort(),
['unban', 'unmute'],
);
});
it('prevents two open tickets for the same author and category', () => {
const insert = getDatabase().prepare(
"INSERT INTO tickets (guild_id, category_id, author_id) VALUES ('g', 'bug', 'u')",
);
insert.run();
assert.throws(() => insert.run(), /UNIQUE/);
});
});