palico-bot/test/invitation.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

89 lines
3.4 KiB
JavaScript

/**
* @file Vérifie la lecture des liens d'invitation, le refus des invitations permanentes ou inconnues
* et les avertissements joints au message.
*/
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/);
});