Files
FWS-DiscordBot/tests/rules-service.test.js
T
BlackAngelTV 4643e9300d 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:39:52 +02:00

126 lines
4.0 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 { publishRules, rulesStatus } from '../src/services/rules/rules-service.js';
// The dashboard relies on `CHANNEL_RULES_ID`, which no CI run declares, so the
// channel is passed explicitly here rather than read from the environment.
const RULES_CHANNEL = '1234567890123456789';
/** A Discord client reduced to what publishing the rules touches. */
function harness({ pinFails = false } = {}) {
const state = { sent: [], deleted: [], pinned: [], messages: new Map() };
const channel = {
isSendable: () => true,
async send(payload) {
const id = `message-${state.sent.length + 1}`;
state.sent.push({ id, payload });
state.messages.set(id, {
id,
delete: async () => {
state.deleted.push(id);
state.messages.delete(id);
},
});
return {
id,
url: `https://discord.test/${id}`,
pin: async () => {
if (pinFails) throw new Error('Missing Permissions');
state.pinned.push(id);
},
};
},
messages: { fetch: async (id) => state.messages.get(id) ?? null },
};
return { state, client: { channels: { fetch: async () => channel } } };
}
const publish = (client, markdown = '## Règlement\n\n1. Reste.bg') =>
publishRules(client, {
guildId: 'g',
markdown,
actorId: 'mod',
channelId: RULES_CHANNEL,
});
const description = (entry) => entry.payload.embeds[0].toJSON().description;
describe('rules service', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(() => getDatabase().exec('DELETE FROM rules_messages;'));
it('publishes the rules and remembers the message', async () => {
const { state, client } = harness();
const result = await publish(client, '**Pas de contenu illégal.**');
assert.equal(state.sent.length, 1);
assert.equal(description(state.sent[0]), '**Pas de contenu illégal.**');
assert.deepEqual(state.pinned, ['message-1']);
assert.equal(result.pinned, true);
assert.equal(rulesStatus('g').messageId, 'message-1');
});
it('deletes the previous message instead of piling copies up', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
await publish(client, 'Version 2');
assert.deepEqual(state.deleted, ['message-1']);
assert.deepEqual(state.sent.map(description), ['Version 1', 'Version 2']);
assert.equal(rulesStatus('g').messageId, 'message-2');
});
it('publishes even when the previous message was deleted by hand', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
state.messages.delete('message-1');
const result = await publish(client, 'Version 2');
assert.equal(result.messageId, 'message-2');
});
it('publishes even when the bot cannot pin', async () => {
const { state, client } = harness({ pinFails: true });
const result = await publish(client);
assert.deepEqual(state.pinned, []);
assert.equal(result.pinned, false);
assert.equal(rulesStatus('g').messageId, 'message-1');
});
it('refuses an empty or oversized regulation', async () => {
const { client } = harness();
await assert.rejects(() => publish(client, ' '), /vide/);
await assert.rejects(() => publish(client, 'a'.repeat(4001)), /4000 caractères/);
});
it('refuses to publish without a rules channel', async () => {
const { client } = harness();
await assert.rejects(
() => publishRules(client, { guildId: 'g', markdown: 'Règlement', channelId: '' }),
/CHANNEL_RULES_ID/,
);
});
it('reports the configured channel to the dashboard', () => {
assert.deepEqual(rulesStatus('g', RULES_CHANNEL), {
channelId: RULES_CHANNEL,
configured: true,
messageId: null,
publishedAt: null,
publishedBy: null,
});
});
});