Files
FWS-DiscordBot/tests/repositories.test.js
BlackAngelTV 31c0fc861a feat(rules): write the rules from the dashboard and republish them
The rules lived in a channel nobody but a staff member with a shell could
edit, and the welcome message pointed at it. They are now written in
markdown under Settings and published as a single pinned embed, and a new
publication deletes the message the bot posted last time instead of
piling copies up in the channel.
2026-09-25 20:44:29 +02:00

217 lines
7.1 KiB
JavaScript

import assert from 'node:assert/strict';
import { after, before, beforeEach, describe, it } from 'node:test';
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
import {
countActions,
listActions,
recordAction,
} from '../src/database/repositories/moderation-repository.js';
import {
getNotificationChannel,
removeNotificationChannel,
setNotificationChannel,
} from '../src/database/repositories/notification-repository.js';
import {
clearRulesMessage,
getRulesMessage,
setRulesMessage,
} from '../src/database/repositories/rules-repository.js';
import {
castVote,
closePoll,
countVotes,
createPoll,
listDuePolls,
} from '../src/database/repositories/poll-repository.js';
import {
claimTicket,
closeTicket,
countOpenTickets,
createTicket,
getOpenTicket,
getTicketByChannel,
} from '../src/database/repositories/ticket-repository.js';
const reset = () =>
getDatabase().exec(
'DELETE FROM tickets; DELETE FROM moderation_actions; DELETE FROM polls; DELETE FROM poll_votes; DELETE FROM notification_channels; DELETE FROM rules_messages;',
);
describe('ticket repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
const openTicket = () => createTicket({ guildId: 'g', channelId: 'c1', categoryId: 'bug', authorId: 'u1' });
it('creates a ticket and finds it by channel', () => {
const id = openTicket();
const ticket = getTicketByChannel('c1');
assert.equal(ticket.id, id);
assert.equal(ticket.status, 'open');
assert.equal(ticket.claimed_by, null);
});
it('refuses a second open ticket for the same author and category', () => {
openTicket();
assert.throws(
() => createTicket({ guildId: 'g', channelId: 'c2', categoryId: 'bug', authorId: 'u1' }),
/UNIQUE/,
);
assert.equal(getOpenTicket({ guildId: 'g', authorId: 'u1', categoryId: 'bug' }).channel_id, 'c1');
});
it('allows a new ticket once the previous one is closed', () => {
const id = openTicket();
closeTicket(id, { closedBy: 'staff', reason: 'résolu' });
const newId = createTicket({ guildId: 'g', channelId: 'c3', categoryId: 'bug', authorId: 'u1' });
assert.notEqual(newId, id);
assert.equal(countOpenTickets('g'), 1);
});
it('records who claimed and closed a ticket', () => {
const id = openTicket();
assert.equal(claimTicket(id, 'staff'), 1);
assert.equal(getTicketByChannel('c1').claimed_by, 'staff');
assert.equal(closeTicket(id, { closedBy: 'staff', reason: 'ok' }), 1);
const closed = getTicketByChannel('c1');
assert.equal(closed.status, 'closed');
assert.equal(closed.close_reason, 'ok');
assert.ok(closed.closed_at);
});
it('cannot close a ticket twice', () => {
const id = openTicket();
closeTicket(id, { closedBy: 'staff' });
assert.equal(closeTicket(id, { closedBy: 'staff' }), 0);
});
});
describe('moderation repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
it('records and lists actions of a target', () => {
recordAction({ guildId: 'g', action: 'warn', moderatorId: 'm1', targetId: 'u1', reason: 'spam' });
recordAction({ guildId: 'g', action: 'warn', moderatorId: 'm1', targetId: 'u1', reason: 'again' });
recordAction({ guildId: 'g', action: 'kick', moderatorId: 'm2', targetId: 'u2' });
assert.equal(countActions({ guildId: 'g', targetId: 'u1', action: 'warn' }), 2);
const history = listActions({ guildId: 'g', targetId: 'u1' });
assert.equal(history.length, 2);
assert.equal(history[0].reason, 'again');
assert.equal(listActions({ guildId: 'g', action: 'kick' }).length, 1);
});
it('keeps the duration of a mute', () => {
recordAction({
guildId: 'g',
action: 'mute',
moderatorId: 'm1',
targetId: 'u1',
durationMs: 60_000,
});
assert.equal(listActions({ guildId: 'g', targetId: 'u1' })[0].duration_ms, 60_000);
});
});
describe('poll repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
const poll = () =>
createPoll({
guildId: 'g',
channelId: 'c',
creatorId: 'u1',
question: 'Question ?',
choices: ['a', 'b'],
endsAt: null,
});
it('counts one vote per user and allows a change of mind', () => {
const id = poll();
assert.equal(castVote(id, 'u1', 0), 'created');
assert.equal(castVote(id, 'u1', 0), 'unchanged');
assert.equal(castVote(id, 'u1', 1), 'changed');
assert.equal(castVote(id, 'u2', 1), 'created');
const counts = countVotes(id);
assert.equal(counts.length, 1);
assert.deepEqual({ ...counts[0] }, { choice_index: 1, count: 2 });
});
it('lists only the polls whose end date has passed', () => {
const due = createPoll({
guildId: 'g',
channelId: 'c',
creatorId: 'u1',
question: 'Bientôt fini',
choices: ['a', 'b'],
endsAt: '2000-01-01 00:00:00',
});
createPoll({
guildId: 'g',
channelId: 'c',
creatorId: 'u1',
question: 'En cours',
choices: ['a', 'b'],
endsAt: '2999-01-01 00:00:00',
});
const ids = listDuePolls().map((row) => row.id);
assert.deepEqual(ids, [due]);
});
it('closes a poll only once', () => {
const id = poll();
assert.equal(closePoll(id), 1);
assert.equal(closePoll(id), 0);
});
});
describe('notification repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
it('stores one channel per type and replaces it', () => {
setNotificationChannel({ guildId: 'g', type: 'issues', channelId: 'c1' });
setNotificationChannel({ guildId: 'g', type: 'issues', channelId: 'c2' });
assert.equal(getNotificationChannel('g', 'issues').channel_id, 'c2');
assert.equal(getNotificationChannel('other', 'issues'), null);
});
it('removes an override and reports it', () => {
setNotificationChannel({ guildId: 'g', type: 'release', channelId: 'c1' });
assert.equal(removeNotificationChannel('g', 'release'), 1);
assert.equal(removeNotificationChannel('g', 'release'), 0);
});
});
describe('rules repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
it('keeps one rules message per guild and replaces it', () => {
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm1', updatedBy: 'mod' });
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm2', updatedBy: 'mod' });
const stored = getRulesMessage('g');
assert.equal(stored.message_id, 'm2');
assert.equal(stored.updated_by, 'mod');
assert.equal(getRulesMessage('other'), null);
});
it('forgets the message when it is cleared', () => {
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm1' });
assert.equal(clearRulesMessage('g'), 1);
assert.equal(getRulesMessage('g'), null);
});
});