fix(rules): show the rules field with a save button and edit the message

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.
This commit is contained in:
BlackAngelTV committed 2026-09-25 21:14:00 +02:00
1 parent 22e9723700
commit e6ce594b34
7 files changed
+156 -168

No files matched your search

+45 -30
View File
@@ -7,44 +7,46 @@ import { publishRules, rulesStatus } from '../src/services/rules/rules-service.j
// 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() };
/** 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,
delete: async () => {
state.deleted.push(id);
state.messages.delete(id);
},
});
state.messages.set(id, { id, editable: !notEditable });
return {
id,
url: `https://discord.test/${id}`,
...withEdit({ 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 },
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,
});
publishRules(client, { guildId: 'g', markdown, actorId: 'mod', channelId: RULES_CHANNEL });
const description = (entry) => entry.payload.embeds[0].toJSON().description;
@@ -53,7 +55,7 @@ describe('rules service', () => {
after(() => closeDatabase());
beforeEach(() => getDatabase().exec('DELETE FROM rules_messages;'));
it('publishes the rules and remembers the message', async () => {
it('sends and pins the rules the first time', async () => {
const { state, client } = harness();
const result = await publish(client, '**Pas de contenu illégal.**');
@@ -61,32 +63,45 @@ describe('rules service', () => {
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');
assert.deepEqual({ pinned: result.pinned, edited: result.edited }, { pinned: true, edited: false });
assert.equal(rulesStatus('g', RULES_CHANNEL).messageId, 'message-1');
});
it('deletes the previous message instead of piling copies up', async () => {
it('edits the published message instead of sending a second one', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
await publish(client, 'Version 2');
const result = 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');
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('publishes even when the previous message was deleted by hand', async () => {
it('sends a new one when the previous message is gone', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
state.messages.delete('message-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 });
@@ -94,7 +109,7 @@ describe('rules service', () => {
assert.deepEqual(state.pinned, []);
assert.equal(result.pinned, false);
assert.equal(rulesStatus('g').messageId, 'message-1');
assert.equal(rulesStatus('g', RULES_CHANNEL).messageId, 'message-1');
});
it('refuses an empty or oversized regulation', async () => {