import test from "node:test"; import assert from "node:assert/strict"; import { DiscordjsErrorCodes } from "discord.js"; import { InvitationError, buildInvitationMessage, checkInvitation, parseInviteCode, } from "../src/features/invitation.js"; const GUILD_ID = "111111111111111111"; function fakeGuild({ invite = null, fetchError = null } = {}) { return { id: GUILD_ID, invites: { async fetch() { if (fetchError) throw fetchError; if (!invite) throw Object.assign(new Error("not found"), { code: DiscordjsErrorCodes.InviteNotFound }); return invite; }, }, }; } function fakeClient(publicInvite) { return { async fetchInvite() { if (!publicInvite) throw Object.assign(new Error("Unknown Invite"), { code: 10006 }); return publicInvite; }, }; } test("parseInviteCode accepts invite links and bare codes only", () => { for (const input of ["https://discord.gg/AbC-12", "discord.gg/AbC-12", "https://discord.com/invite/AbC-12", " AbC-12 "]) { assert.equal(parseInviteCode(input), "AbC-12", input); } for (const input of ["https://evil.example/AbC-12", "https://discord.gg/", "a b", "", undefined]) { assert.equal(parseInviteCode(input), null, String(input)); } }); test("a temporary invite without limits is published as is", async () => { const guild = fakeGuild({ invite: { code: "AbC-12", temporary: true, maxUses: 0, expiresTimestamp: null } }); assert.deepEqual(await checkInvitation(guild, fakeClient(), "discord.gg/AbC-12"), { link: "https://discord.gg/AbC-12", warnings: [], }); }); test("permanent and unknown invites are refused", async () => { const permanent = fakeGuild({ invite: { code: "AbC-12", temporary: false } }); await assert.rejects(checkInvitation(permanent, fakeClient(), "AbC-12"), InvitationError); await assert.rejects(checkInvitation(fakeGuild(), fakeClient(), "AbC-12"), InvitationError); await assert.rejects(checkInvitation(fakeGuild(), fakeClient(), "pas un lien"), InvitationError); }); test("limited or expiring invites are published with warnings", async () => { const guild = fakeGuild({ invite: { code: "AbC-12", temporary: true, maxUses: 5, uses: 2, expiresTimestamp: 1_800_000_000_000 }, }); const { warnings } = await checkInvitation(guild, fakeClient(), "AbC-12"); assert.equal(warnings.length, 2); }); test("without Manage Server the bot checks only the guild", async () => { const missingPermissions = Object.assign(new Error("Missing Permissions"), { code: 50013 }); const guild = fakeGuild({ fetchError: missingPermissions }); const sameGuild = fakeClient({ code: "AbC-12", guild: { id: GUILD_ID }, temporary: null }); const result = await checkInvitation(guild, sameGuild, "AbC-12"); assert.equal(result.link, "https://discord.gg/AbC-12"); assert.equal(result.warnings.length, 1); const otherGuild = fakeClient({ code: "AbC-12", guild: { id: "222222222222222222" }, temporary: null }); await assert.rejects(checkInvitation(guild, otherGuild, "AbC-12"), InvitationError); }); test("the invitation message shows the link and how to stay permanently", () => { const message = buildInvitationMessage("https://discord.gg/AbC-12"); assert.match(message, /```\nhttps:\/\/discord\.gg\/AbC-12\n```/); assert.match(message, /Hangaround/); });