1
0
Fork 0
polaris-task-force/tests/int/bot-button-routing.int.spec.ts
Z8MB1E f478f38938 fix(bot): route poll buttons to their interaction handler
The InteractionCreate dispatcher in index.ts forwarded only customIds starting
with `ptf-att:`, so poll buttons (`ptf-poll:`) were never acknowledged: Discord
answered every press with "This interaction failed" and the message looked
broken while the same vote worked on the web. The post-op rating button had the
same defect and had never worked.

The prefix list now lives with the handler (UNIT_BUTTON_PREFIXES /
isUnitButtonCustomId) and the dispatcher reads it, so a prefix the handler
answers to cannot be filtered out above it. A `ptf-` button nothing claims
replies with a toast and logs a warning instead of failing silently.

Regression test pins the predicate to the customIds buildPollRows actually
generates, so the two halves cannot drift apart again.
2026-12-02 10:02:39 -05:00

90 lines
2.9 KiB
TypeScript

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);
});
});