palico-bot/test/directMessages.test.js
Shinuwa 3795ee06cb Add file headers, tidy the stylesheet and document the conventions
Headers:
- Every JavaScript file (sources, front, scripts, tests) starts with a
  /** @file … */ header, in the three levels used by sokko-g: a short
  description for simple files; Utilisé par / Attention / @see for
  shared modules; a full header (context, entry points, callers,
  dependencies, pitfalls) for index.js, the music queue, the web
  server, the storage manager and the jukebox page script.
- No @author, @version or @since: git already knows.
- test/fileHeaders.test.js fails `npm run check` when a header is
  missing or a central file's header is incomplete.

Stylesheet (no visual change, checked by comparing every element's
computed styles in Chromium at 1280, 800 and 400 px, hover included):
- Role header and section comments.
- Transparent shades derive from the colour variables with
  color-mix() instead of copied rgba() values; new tokens for repeated
  values (text, text on accent, accent gradient, white veils, shadow).
- Native nesting for buttons, the live indicator, upload rows and
  home cards; drop the unused .hero__visual--large and redundant rules.
- Utility classes move to the end so they win over components. The
  library list's u-mt-1, which never applied (.list reset it), is
  removed to keep the layout as it was.

Docs: CONTRIBUTING.md describes the language, header and stylesheet
conventions; CLAUDE.md points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 21:05:59 +01:00

98 lines
4.7 KiB
JavaScript

/**
* @file Vérifie l'envoi des messages privés : mentions désactivées, contrôle du texte, des embeds et
* des liens, erreurs Discord traduites en erreurs pour l'application.
*/
import test from "node:test";
import assert from "node:assert/strict";
import { DirectMessageError, sendDirectMessage } from "../src/features/directMessages.js";
const USER_ID = "123456789012345678";
function fakeClient({ fetchError = null, sendError = null } = {}) {
const sent = [];
return {
sent,
users: {
async fetch(userId) {
if (fetchError) throw fetchError;
return {
id: userId,
async send(message) {
if (sendError) throw sendError;
sent.push({ userId, ...message });
},
};
},
},
};
}
test("sendDirectMessage sends the text to the user without active mentions", async () => {
const client = fakeClient();
await sendDirectMessage(client, { userId: USER_ID, content: "Votre expédition est de retour !" });
assert.deepEqual(client.sent, [
{ userId: USER_ID, content: "Votre expédition est de retour !", allowedMentions: { parse: [] } },
]);
});
test("sendDirectMessage rejects invalid users and contents before calling Discord", async () => {
const client = fakeClient();
for (const body of [{ userId: "dev-admin", content: "x" }, { userId: USER_ID, content: " " }, { userId: USER_ID, content: "x".repeat(2001) }, undefined]) {
await assert.rejects(sendDirectMessage(client, body), (error) => error instanceof DirectMessageError && error.status === 400);
}
assert.equal(client.sent.length, 0);
});
test("sendDirectMessage maps unknown users and closed messages to client errors", async () => {
await assert.rejects(sendDirectMessage(fakeClient({ fetchError: { code: 10013 } }), { userId: USER_ID, content: "x" }), (error) => error.status === 404 && error.code === "UNKNOWN_USER");
await assert.rejects(sendDirectMessage(fakeClient({ sendError: { code: 50007 } }), { userId: USER_ID, content: "x" }), (error) => error.status === 422 && error.code === "DM_CLOSED");
await assert.rejects(sendDirectMessage(fakeClient({ sendError: new Error("network") }), { userId: USER_ID, content: "x" }), /network/);
});
test("sendDirectMessage adds an embed and a link button when given", async () => {
const client = fakeClient();
await sendDirectMessage(client, {
userId: USER_ID,
content: "Votre expédition est de retour !",
embed: {
title: "Votre expédition est de retour !",
description: "Les Glaces de la Longue Nuit · ★★★ · 6 h",
url: "https://sokko.example/#/cards/expeditions/abc",
color: 0xf6c453,
thumbnail: "https://sokko.example/static/img/cards/x.webp",
fields: [{ name: "Butin", value: "+16 Éclats" }],
footer: "Sokko TCG",
timestamp: 1_800_000_000_000,
},
link: { label: "Voir l'expédition", url: "https://sokko.example/#/cards/expeditions/abc" },
});
const [message] = client.sent;
assert.equal(message.embeds[0].title, "Votre expédition est de retour !");
assert.deepEqual(message.embeds[0].thumbnail, { url: "https://sokko.example/static/img/cards/x.webp" });
assert.deepEqual(message.embeds[0].fields, [{ name: "Butin", value: "+16 Éclats", inline: false }]);
assert.deepEqual(message.embeds[0].footer, { text: "Sokko TCG" });
assert.equal(message.embeds[0].timestamp, new Date(1_800_000_000_000).toISOString());
assert.deepEqual(message.components, [{ type: 1, components: [{ type: 2, style: 5, label: "Voir l'expédition", url: "https://sokko.example/#/cards/expeditions/abc" }] }]);
assert.deepEqual(message.allowedMentions, { parse: [] });
});
test("sendDirectMessage refuses embeds and links that Discord would reject", async () => {
const client = fakeClient();
const send = (extra) => sendDirectMessage(client, { userId: USER_ID, content: "x", ...extra });
for (const extra of [
{ embed: { description: "sans titre" } },
{ embed: { title: "x".repeat(257) } },
{ embed: { title: "t", url: "http://sokko.example" } },
{ embed: { title: "t", thumbnail: "javascript:alert(1)" } },
{ embed: { title: "t", color: 0x1000000 } },
{ embed: { title: "t", fields: Array.from({ length: 26 }, () => ({ name: "n", value: "v" })) } },
{ embed: { title: "t", fields: [{ name: "n", value: "" }] } },
{ embed: { title: "t", description: "x".repeat(4096), fields: Array.from({ length: 2 }, () => ({ name: "n", value: "x".repeat(1024) })) } },
{ link: { label: "Voir", url: "ftp://sokko.example" } },
{ link: { url: "https://sokko.example" } },
]) {
await assert.rejects(send(extra), (error) => error instanceof DirectMessageError && error.status === 400 && error.code === "INVALID_EMBED");
}
assert.equal(client.sent.length, 0);
});