palico-bot/test/directMessages.test.js
Shinuwa 6ca17264e0
All checks were successful
Deploy Palico Bot / deploy (push) Successful in 7s
Let trusted apps add an embed and a link button to Discord direct messages
- POST /api/notifications/dm accepts optional embed (title, description, url, color, thumbnail, fields, footer, timestamp) and link fields; plain text stays required for phone notifications.
- Every value is checked against Discord's limits, and urls and thumbnails must use https; a refused embed answers 400 INVALID_EMBED.
- Sokko TCG uses it to show the destination, the result and the loot of its expedition alerts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 11:24:20 +01:00

93 lines
4.5 KiB
JavaScript

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);
});