49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { getGuildMemberIdentity, updateGuildNickname } from "../src/index";
|
|
|
|
describe("Discord guild identity", () => {
|
|
it("reads the member username, global name, nickname, and immutable ID", async () => {
|
|
const request = vi.fn<typeof fetch>().mockResolvedValue(new Response(JSON.stringify({
|
|
nick: "Sam (Notch)",
|
|
user: { id: "987654321098765432", username: "samcraft", global_name: "Sam" },
|
|
}), { status: 200, headers: { "content-type": "application/json" } }));
|
|
|
|
await expect(getGuildMemberIdentity({
|
|
guildId: "123456789012345678",
|
|
discordUserId: "987654321098765432",
|
|
botToken: "secret",
|
|
}, request)).resolves.toEqual({
|
|
id: "987654321098765432",
|
|
username: "samcraft",
|
|
globalName: "Sam",
|
|
nickname: "Sam (Notch)",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Discord nickname updates", () => {
|
|
it("updates a member in the configured guild using bot authentication", async () => {
|
|
const request = vi.fn<typeof fetch>().mockResolvedValue(new Response(null, { status: 204 }));
|
|
|
|
await updateGuildNickname(
|
|
{ guildId: "123456789012345678", discordUserId: "987654321098765432", nickname: "Sam (Notch)", botToken: "secret" },
|
|
request,
|
|
);
|
|
|
|
expect(request).toHaveBeenCalledWith(
|
|
"https://discord.com/api/v10/guilds/123456789012345678/members/987654321098765432",
|
|
expect.objectContaining({ method: "PATCH", body: JSON.stringify({ nick: "Sam (Notch)" }) }),
|
|
);
|
|
});
|
|
|
|
it("reports Discord permission failures without pretending the nickname changed", async () => {
|
|
const request = vi.fn<typeof fetch>().mockResolvedValue(new Response("Missing Permissions", { status: 403 }));
|
|
await expect(
|
|
updateGuildNickname(
|
|
{ guildId: "123456789012345678", discordUserId: "987654321098765432", nickname: "Sam (Notch)", botToken: "secret" },
|
|
request,
|
|
),
|
|
).rejects.toThrow("Discord nickname update failed (403)");
|
|
});
|
|
});
|