Some checks failed
Deploy Palico Bot / deploy (push) Failing after 3s
- POST /api/notifications/dm takes a user id and a text, sends it as a direct message with no active mentions, and answers 204, 400, 404 or 422. - The send logic lives in src/features/directMessages.js with its own tests; the route gets its own per-minute rate limit behind the existing API key. - Sokko TCG uses it for its opt-in Expedition alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
2 KiB
JavaScript
46 lines
2 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/);
|
|
});
|