200 lines
7.1 KiB
TypeScript
200 lines
7.1 KiB
TypeScript
import fs from "fs";
|
|
import path from "path";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
/**
|
|
* Slash command builders validate Discord's constraints (name format,
|
|
* description <= 100 chars, option counts) at construction time, so a single
|
|
* over-long description crashes the whole bot process at import before it can
|
|
* log in - taking every command down, not just the offending one. This test
|
|
* imports the command registry and serializes every builder so CI catches
|
|
* that class of failure instead of the prod crash loop doing it.
|
|
*/
|
|
describe("Bot slash command builders", () => {
|
|
// config.ts fail-fasts on missing env at import; supply dummies (no
|
|
// network happens here, toJSON is pure).
|
|
process.env.DISCORD_TOKEN ||= "regression-test-token";
|
|
process.env.DISCORD_GUILD_ID ||= "123456789012345678";
|
|
|
|
it("import and serialize within Discord's builder constraints", async () => {
|
|
const { commands } = await import("@/bot/commands");
|
|
expect(commands.length).toBeGreaterThan(0);
|
|
|
|
for (const command of commands) {
|
|
const json = command.builder.toJSON();
|
|
expect(json.name).toBeTruthy();
|
|
expect((json.description ?? "").length).toBeLessThanOrEqual(100);
|
|
for (const option of json.options ?? []) {
|
|
expect(
|
|
(option.description ?? "").length,
|
|
`option "${option.name}" on /${json.name}`,
|
|
).toBeLessThanOrEqual(100);
|
|
}
|
|
expect(() => command.builder.toJSON()).not.toThrow();
|
|
}
|
|
});
|
|
|
|
it("registers every BotCommand exported by a file in commands/", async () => {
|
|
const dir = path.join(process.cwd(), "src", "bot", "commands");
|
|
const { commands } = await import("@/bot/commands");
|
|
const registered = new Set(commands.map((c) => c.builder.name));
|
|
|
|
const files = fs.readdirSync(dir).filter((file) => file.endsWith(".ts") && file !== "index.ts");
|
|
expect(files.length).toBeGreaterThanOrEqual(7);
|
|
|
|
for (const file of files) {
|
|
const mod = (await import(path.join(dir, file))) as Record<string, unknown>;
|
|
for (const value of Object.values(mod)) {
|
|
const candidate = value as { builder?: { name?: string }; execute?: unknown };
|
|
if (
|
|
candidate &&
|
|
typeof candidate === "object" &&
|
|
candidate.builder?.name &&
|
|
candidate.execute
|
|
) {
|
|
expect(
|
|
registered.has(candidate.builder.name),
|
|
`/${candidate.builder.name} (${file}) is not in the commands registry`,
|
|
).toBe(true);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("Bot autocomplete wiring", () => {
|
|
process.env.DISCORD_TOKEN ||= "regression-test-token";
|
|
process.env.DISCORD_GUILD_ID ||= "123456789012345678";
|
|
|
|
it("/changelog enables autocomplete on range and ships a handler", async () => {
|
|
const { changelogCommand } = await import("@/bot/commands/changelog");
|
|
const json = changelogCommand.builder.toJSON();
|
|
const range = (json.options ?? []).find((option) => option.name === "range") as
|
|
{ autocomplete?: boolean } | undefined;
|
|
expect(range?.autocomplete).toBe(true);
|
|
expect(typeof changelogCommand.autocomplete).toBe("function");
|
|
});
|
|
|
|
it("answers with versions when the releases channel is unavailable", async () => {
|
|
const { changelogCommand } = await import("@/bot/commands/changelog");
|
|
const responded: { value: string }[][] = [];
|
|
const interaction = {
|
|
options: { getFocused: () => ({ name: "range", value: "" }) },
|
|
client: { user: { id: "123456789012345678" } },
|
|
guild: null,
|
|
respond: async (choices: { value: string }[]) => {
|
|
responded.push(choices);
|
|
},
|
|
};
|
|
|
|
await changelogCommand.autocomplete!(
|
|
interaction as never,
|
|
{
|
|
logger: { error: () => undefined },
|
|
} as never,
|
|
);
|
|
|
|
expect(responded).toHaveLength(1);
|
|
expect(responded[0].length).toBeGreaterThan(0);
|
|
// No watermark without a channel, so `pending` is not offered.
|
|
expect(responded[0].some((choice) => choice.value === "pending")).toBe(false);
|
|
});
|
|
|
|
it("filters the choices by what has been typed", async () => {
|
|
const { changelogCommand } = await import("@/bot/commands/changelog");
|
|
const responded: { value: string }[][] = [];
|
|
const interaction = {
|
|
options: { getFocused: () => ({ name: "range", value: "0.21" }) },
|
|
client: { user: { id: "123456789012345678" } },
|
|
guild: null,
|
|
respond: async (choices: { value: string }[]) => {
|
|
responded.push(choices);
|
|
},
|
|
};
|
|
|
|
await changelogCommand.autocomplete!(
|
|
interaction as never,
|
|
{
|
|
logger: { error: () => undefined },
|
|
} as never,
|
|
);
|
|
|
|
expect(responded[0].length).toBeGreaterThan(0);
|
|
expect(responded[0].every((choice) => choice.value.startsWith("v0.21"))).toBe(true);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* A slash command has three seconds to acknowledge an interaction. Deferring
|
|
* after a member fetch and a permission check misses that window often enough
|
|
* that Discord drops the token: the command then fails with "This application
|
|
* did not respond" and any later reply throws DiscordAPIError 10062,
|
|
* "Unknown interaction". That unhandled rejection used to kill the bot process
|
|
* (see the 2026-11-03 /rollcall crash).
|
|
*/
|
|
describe("Bot interaction acknowledgement", () => {
|
|
process.env.DISCORD_TOKEN ||= "regression-test-token";
|
|
process.env.DISCORD_GUILD_ID ||= "123456789012345678";
|
|
|
|
const fakePayload = {
|
|
logger: { error: () => undefined, info: () => undefined, warn: () => undefined },
|
|
};
|
|
|
|
it("/rollcall defers before the member fetch and permission check", async () => {
|
|
const { rollCallCommand } = await import("@/bot/commands/rollcall");
|
|
const calls: string[] = [];
|
|
const interaction = {
|
|
inGuild: () => true,
|
|
guild: {
|
|
members: {
|
|
fetch: async () => {
|
|
calls.push("members.fetch");
|
|
throw new Error("Discord REST call timed out");
|
|
},
|
|
},
|
|
},
|
|
user: { id: "123456789012345678" },
|
|
client: {},
|
|
deferReply: async () => {
|
|
calls.push("deferReply");
|
|
},
|
|
editReply: async () => {
|
|
calls.push("editReply");
|
|
},
|
|
reply: async () => {
|
|
calls.push("reply");
|
|
},
|
|
options: { getString: () => null },
|
|
};
|
|
|
|
await expect(
|
|
rollCallCommand.execute(interaction as never, fakePayload as never),
|
|
).rejects.toThrow("Discord REST call timed out");
|
|
|
|
expect(calls[0]).toBe("deferReply");
|
|
expect(calls).not.toContain("reply");
|
|
});
|
|
|
|
it("safeEphemeralReply never rethrows a stale-interaction failure", async () => {
|
|
const { safeEphemeralReply } = await import("@/bot/lib/interactions");
|
|
const logged: string[] = [];
|
|
const interaction = {
|
|
commandName: "rollcall",
|
|
isChatInputCommand: () => true,
|
|
reply: async () => {
|
|
throw new Error("Unknown interaction");
|
|
},
|
|
};
|
|
|
|
await expect(
|
|
safeEphemeralReply(
|
|
interaction as never,
|
|
{
|
|
logger: { error: (line: string) => logged.push(line) },
|
|
} as never,
|
|
"Something went wrong while running this command.",
|
|
),
|
|
).resolves.toBeUndefined();
|
|
expect(logged.join("\n")).toContain("Unknown interaction");
|
|
});
|
|
});
|