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