The rules lived in a card of their own that never showed up next to the settings, so the section read as an empty toggle nobody could fill in. The markdown now renders as a real field with an "Enregistrer" button, and saving edits the message already pinned instead of deleting it and posting a new one: same link, same place, pin kept.
141 lines
4.8 KiB
JavaScript
141 lines
4.8 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 saving the rules touches. */
|
|
function harness({ pinFails = false, notEditable = false } = {}) {
|
|
const state = { sent: [], edited: [], pinned: [], messages: new Map() };
|
|
|
|
// Discord edits a message in place: the id, the link and the pin stay.
|
|
const withEdit = (message) => ({
|
|
...message,
|
|
edit: async ({ embeds }) => {
|
|
state.edited.push({ id: message.id, description: embeds[0].toJSON().description });
|
|
return message;
|
|
},
|
|
});
|
|
|
|
const channel = {
|
|
isSendable: () => true,
|
|
async send(payload) {
|
|
const id = `message-${state.sent.length + 1}`;
|
|
state.sent.push({ id, payload });
|
|
state.messages.set(id, { id, editable: !notEditable });
|
|
return {
|
|
...withEdit({ id, url: `https://discord.test/${id}` }),
|
|
pin: async () => {
|
|
if (pinFails) throw new Error('Missing Permissions');
|
|
state.pinned.push(id);
|
|
},
|
|
};
|
|
},
|
|
messages: {
|
|
fetch: async (id) => {
|
|
const message = state.messages.get(id);
|
|
return message ? withEdit(message) : 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('sends and pins the rules the first time', 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.deepEqual({ pinned: result.pinned, edited: result.edited }, { pinned: true, edited: false });
|
|
assert.equal(rulesStatus('g', RULES_CHANNEL).messageId, 'message-1');
|
|
});
|
|
|
|
it('edits the published message instead of sending a second one', async () => {
|
|
const { state, client } = harness();
|
|
|
|
await publish(client, 'Version 1');
|
|
const result = await publish(client, 'Version 2');
|
|
|
|
assert.equal(state.sent.length, 1, 'a second message was sent');
|
|
assert.deepEqual(state.edited, [{ id: 'message-1', description: 'Version 2' }]);
|
|
assert.equal(result.messageId, 'message-1', 'the message id must not change');
|
|
assert.equal(result.edited, true);
|
|
});
|
|
|
|
it('sends a new one when the previous message is gone', async () => {
|
|
const { state, client } = harness();
|
|
|
|
await publish(client, 'Version 1');
|
|
state.messages.clear();
|
|
|
|
const result = await publish(client, 'Version 2');
|
|
|
|
assert.equal(state.sent.length, 2);
|
|
assert.equal(result.edited, false);
|
|
assert.equal(result.messageId, 'message-2');
|
|
});
|
|
|
|
it('sends a new one when the bot may no longer edit it', async () => {
|
|
const { state, client } = harness({ notEditable: true });
|
|
|
|
await publish(client, 'Version 1');
|
|
const result = await publish(client, 'Version 2');
|
|
|
|
assert.equal(state.sent.length, 2);
|
|
assert.equal(result.edited, false);
|
|
});
|
|
|
|
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', RULES_CHANNEL).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,
|
|
});
|
|
});
|
|
});
|