Skip to content

Commit 663a9d2

Browse files
authored
fix(discord): recover question feedback after failed acknowledgements (#128293)
1 parent bca206d commit 663a9d2

2 files changed

Lines changed: 116 additions & 15 deletions

File tree

‎extensions/discord/src/monitor/questions.test.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// Discord question component feedback tests.
2+
import { InteractionResponseType, MessageFlags } from "discord-api-types/v10";
23
import { describe, expect, it, vi } from "vitest";
3-
import type { ButtonInteraction } from "../internal/discord.js";
4+
import {
5+
createInteraction as createDiscordInteraction,
6+
type ButtonInteraction,
7+
} from "../internal/discord.js";
8+
import {
9+
attachRestMock,
10+
createInternalComponentInteractionPayload,
11+
createInternalTestClient,
12+
} from "../internal/test-builders.test-support.js";
413
import { createDiscordQuestionButton } from "./questions.js";
514

615
type InteractionHarness = {
@@ -91,4 +100,99 @@ describe("Discord question button", () => {
91100
ephemeral: true,
92101
});
93102
});
103+
104+
it.each(
105+
[
106+
{
107+
name: "submitted answer",
108+
result: {
109+
status: "answered" as const,
110+
questionId: "target",
111+
optionValue: "Production",
112+
},
113+
expectedText: "Answer submitted.",
114+
},
115+
{
116+
name: "already answered question",
117+
result: { status: "already-terminal" as const, reason: "already-terminal" as const },
118+
expectedText: "This question was already answered.",
119+
},
120+
{
121+
name: "question resolution failure",
122+
error: new Error("question service unavailable"),
123+
expectedText: "Could not submit this answer.",
124+
},
125+
].flatMap((outcome) => [
126+
{ ...outcome, acknowledged: false },
127+
{ ...outcome, acknowledged: true },
128+
]),
129+
)("delivers $name feedback when acknowledgement is $acknowledged", async (testCase) => {
130+
const client = createInternalTestClient();
131+
const post = vi.fn(async () => undefined);
132+
if (!testCase.acknowledged) {
133+
post.mockRejectedValueOnce(new Error("temporary connection reset"));
134+
}
135+
attachRestMock(client, { post });
136+
const interaction = createDiscordInteraction(
137+
client,
138+
createInternalComponentInteractionPayload({
139+
id: "interaction-1",
140+
token: "interaction-token",
141+
user: {
142+
id: "user-1",
143+
username: "Alice",
144+
discriminator: "0",
145+
avatar: null,
146+
global_name: null,
147+
},
148+
}),
149+
) as ButtonInteraction;
150+
const resolveQuestion = vi.fn(async () => {
151+
if (testCase.error) {
152+
throw testCase.error;
153+
}
154+
return testCase.result;
155+
});
156+
const button = createDiscordQuestionButton({
157+
cfg: {} as never,
158+
accountId: "default",
159+
authorizeQuestion: vi.fn(async () => true),
160+
resolveQuestion: resolveQuestion as never,
161+
});
162+
163+
await button.run(interaction, {
164+
id: "ask_0123456789abcdef0123456789abcdef",
165+
i: "1",
166+
});
167+
168+
expect(resolveQuestion).toHaveBeenCalledOnce();
169+
expect(post).toHaveBeenNthCalledWith(
170+
1,
171+
"/interactions/interaction-1/interaction-token/callback",
172+
{
173+
body: { type: InteractionResponseType.DeferredMessageUpdate },
174+
},
175+
);
176+
if (testCase.acknowledged) {
177+
expect(post).toHaveBeenNthCalledWith(
178+
2,
179+
"/webhooks/app1/interaction-token",
180+
{ body: { content: testCase.expectedText, flags: MessageFlags.Ephemeral } },
181+
undefined,
182+
);
183+
expect(interaction.responseState).toBe("deferred-update");
184+
} else {
185+
expect(post).toHaveBeenNthCalledWith(
186+
2,
187+
"/interactions/interaction-1/interaction-token/callback",
188+
{
189+
body: {
190+
type: InteractionResponseType.ChannelMessageWithSource,
191+
data: { content: testCase.expectedText, flags: MessageFlags.Ephemeral },
192+
},
193+
},
194+
);
195+
expect(interaction.responseState).toBe("replied");
196+
}
197+
});
94198
});

‎extensions/discord/src/monitor/questions.ts‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,28 @@ class QuestionButton extends Button {
4141
try {
4242
await interaction.acknowledge();
4343
} catch {}
44-
let result: Awaited<ReturnType<QuestionResolver>>;
44+
let content: string;
4545
try {
46-
result = await this.ctx.resolveQuestion({
46+
const result = await this.ctx.resolveQuestion({
4747
cfg: this.ctx.cfg,
4848
questionId: callback.questionId,
4949
optionIndex: callback.optionIndex,
5050
senderId: interaction.userId,
5151
clientDisplayName: `Discord question (${this.ctx.accountId})`,
5252
});
53+
content =
54+
result.status === "answered" ? "Answer submitted." : "This question was already answered.";
5355
} catch {
54-
try {
55-
await interaction.followUp({ content: "Could not submit this answer.", ephemeral: true });
56-
} catch {}
57-
return;
56+
content = "Could not submit this answer.";
5857
}
5958
try {
60-
await interaction.followUp({
61-
content:
62-
result.status === "answered"
63-
? "Answer submitted."
64-
: "This question was already answered.",
65-
ephemeral: true,
66-
});
59+
const feedback = { content, ephemeral: true };
60+
// A rejected acknowledgement leaves the initial callback available, not the webhook.
61+
await (interaction.responseState === "unacknowledged"
62+
? interaction.reply(feedback)
63+
: interaction.followUp(feedback));
6764
} catch {
68-
// Gateway state already committed; receipt delivery is best-effort.
65+
// Gateway state may already be committed; receipt delivery is best-effort.
6966
}
7067
}
7168
}

0 commit comments

Comments
 (0)