import { beforeAll, describe, expect, it } from "vitest"; // The bot modules validate their environment at import time. process.env.DISCORD_TOKEN ||= "routing-test-token"; process.env.DISCORD_GUILD_ID ||= "1"; process.env.APP_URL ||= "https://polaris.test"; import type { Poll } from "@/payload-types"; type InteractionCreate = typeof import("@/bot/events/interactionCreate"); type PollEmbeds = typeof import("@/bot/services/pollEmbeds"); let routing: InteractionCreate; let embeds: PollEmbeds; beforeAll(async () => { routing = await import("@/bot/events/interactionCreate"); embeds = await import("@/bot/services/pollEmbeds"); }); const poll = (): Poll => ({ id: 9, title: "Question", options: [ { key: "agree", label: "Agree", emoji: null, description: null, id: "a" }, { key: "decline", label: "Decline", emoji: null, description: null, id: "b" }, ], status: "open", closesAt: new Date(Date.now() + 3_600_000).toISOString(), multiSelect: false, anonymity: "public", defaultWeight: 1, weightTiers: [], tieGraceHours: 24, sourceKey: "1:node", openedAt: new Date().toISOString(), initiatedBy: 1, eligibleRoles: [], updatedAt: "", createdAt: "", }) as unknown as Poll; /** * The bot dispatcher only forwards buttons whose customId it recognises, and an * unrecognised button is never acknowledged: Discord then tells the member * "This interaction failed". Poll buttons shipped without being added to that * routing list, so they did exactly that in production while the web page kept * working. These cases pin the two halves together. */ describe("Bot button routing", () => { it("routes every prefix the button handler answers to", () => { for (const customId of ["ptf-att:1:yes", "ptf-poll:1:agree", "ptf-rate:112:rate"]) { expect(routing.isUnitButtonCustomId(customId)).toBe(true); } expect(routing.UNIT_BUTTON_PREFIXES).toEqual([ "ptf-att:", "ptf-poll:", "ptf-rate:", ]); }); it("routes the exact customIds the poll message builds", () => { const rows = embeds.buildPollRows(poll()).map((row) => row.toJSON()); const customIds = rows.flatMap((row) => row.components.map((component) => String((component as { custom_id?: string }).custom_id)), ); expect(customIds).toEqual(["ptf-poll:9:agree", "ptf-poll:9:decline"]); for (const customId of customIds) { expect(routing.isUnitButtonCustomId(customId)).toBe(true); } }); it("leaves the other handlers' buttons to their own branches", () => { for (const customId of [ "ptf-xfer:4:accept", "ptf-announce:compose", "ptf-changelog-edit:abc", "ptf-rp-edit", ]) { expect(routing.isUnitButtonCustomId(customId)).toBe(false); } }); it("ignores anything without a ptf prefix", () => { expect(routing.isUnitButtonCustomId("something-else")).toBe(false); expect(routing.isUnitButtonCustomId("")).toBe(false); }); });