|
1 | 1 | // Discord question component feedback tests. |
| 2 | +import { InteractionResponseType, MessageFlags } from "discord-api-types/v10"; |
2 | 3 | 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"; |
4 | 13 | import { createDiscordQuestionButton } from "./questions.js"; |
5 | 14 |
|
6 | 15 | type InteractionHarness = { |
@@ -91,4 +100,99 @@ describe("Discord question button", () => { |
91 | 100 | ephemeral: true, |
92 | 101 | }); |
93 | 102 | }); |
| 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 | + }); |
94 | 198 | }); |
0 commit comments