Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

AX-4947: route every outbound Slack call through the channel transport (#3527)

Philipp Comans09fed04bbd15
diff --git a/.changeset/slack-single-api-transport.md b/.changeset/slack-single-api-transport.md
new file mode 100644
index 000000000..338ff50df
--- /dev/null
+++ b/.changeset/slack-single-api-transport.md
@@ -0,0 +1,7 @@
+---
+"eve": patch
+---
+
+The Slack channel's `views.open` and answered-card `chat.update` now go through the channel's own Slack API transport. Slack sees the same calls, and a `views.open` that fails after Slack responds is still logged and acknowledged.
+
+A failed `views.open` is now logged as `Slack views.open failed` with the error attached, where it was `Slack views.open returned non-2xx` with a `status` field. Alerts or log queries keyed on either need updating.
diff --git a/packages/eve/src/public/channels/slack/api-errors.ts b/packages/eve/src/public/channels/slack/api-errors.ts
new file mode 100644
index 000000000..aa50df7a0
--- /dev/null
+++ b/packages/eve/src/public/channels/slack/api-errors.ts
@@ -0,0 +1,64 @@
+/**
+ * Records whether a Slack Web API call got a response.
+ *
+ * Slack retries an interaction whose acknowledgement is not a 2xx, so a
+ * handler answering a click has to swallow a call Slack answered badly
+ * and rethrow a call that never arrived. Those two are indistinguishable
+ * once thrown: the transport consumes the response body before it reads
+ * the status, so a body cut off partway raises the same `TypeError` as a
+ * connection that was never made. Nothing on the error says which it
+ * was, so every Slack call eve makes goes out through here and the
+ * answer is recorded while the call runs.
+ *
+ * Separate from `api.ts` because that file sits on the 700-line cap
+ * `internal/structural-tests/file-length.test.ts` enforces.
+ */
+
+import {
+ callSlackApi as callSlackApiPrimitive,
+ type SlackApiOptions,
+ type SlackApiResponse,
+} from "#compiled/@chat-adapter/slack/api.js";
+
+/** Errors raised after Slack's response arrived. */
+const slackResponseErrors = new WeakSet<object>();
+
+/**
+ * POSTs to a Slack Web API method and records the response arriving.
+ *
+ * The transport is handed a `fetch` that observes the response, so
+ * everything thrown from that moment on — a non-2xx, a body that is not
+ * the JSON envelope, a body cut off mid-read — is marked for
+ * {@link isSlackResponseError}. A failure before then stays unmarked and
+ * reaches the caller, which is what fails the interaction request.
+ */
+export async function callSlackApiTrackingResponse(
+ operation: string,
+ body: Record<string, unknown>,
+ options: SlackApiOptions,
+): Promise<SlackApiResponse> {
+ let responded = false;
+ const observe: typeof globalThis.fetch = async (input, init) => {
+ const response = await (options.fetch ?? globalThis.fetch)(input, init);
+ responded = true;
+ return response;
+ };
+ try {
+ return await callSlackApiPrimitive(operation, body, { ...options, fetch: observe });
+ } catch (error) {
+ if (responded && typeof error === "object" && error !== null) {
+ slackResponseErrors.add(error);
+ }
+ throw error;
+ }
+}
+
+/**
+ * True when Slack answered the call, false when the call never got a
+ * response. A caller that must acknowledge Slack swallows the first and
+ * rethrows the second; see {@link callSlackApiTrackingResponse} for why
+ * the error itself cannot be asked.
+ */
+export function isSlackResponseError(error: unknown): boolean {
+ return typeof error === "object" && error !== null && slackResponseErrors.has(error);
+}
diff --git a/packages/eve/src/public/channels/slack/api.ts b/packages/eve/src/public/channels/slack/api.ts
index 0755d9019..93d9af3d7 100644
--- a/packages/eve/src/public/channels/slack/api.ts
+++ b/packages/eve/src/public/channels/slack/api.ts
@@ -16,7 +16,6 @@
*/
import {
- callSlackApi as callSlackApiPrimitive,
fetchSlackThreadReplies,
postSlackEphemeral,
postSlackMessage,
@@ -30,6 +29,7 @@ import {
import { isCardElement, type CardElement, type FileUpload } from "#compiled/chat/index.js";
import { createLogger, logError } from "#internal/logging.js";
+import { callSlackApiTrackingResponse } from "#public/channels/slack/api-errors.js";
import { cardToBlocks, cardToFallbackText } from "#public/channels/slack/blocks.js";
import { resolveSlackInboundMrkdwn } from "#public/channels/slack/inbound-content.js";
import { truncateTypingStatus } from "#public/channels/slack/limits.js";
@@ -92,7 +92,7 @@ export async function callSlackApi(input: {
readonly operation: string;
readonly body: unknown;
}): Promise<SlackApiResponse> {
- return callSlackApiPrimitive(
+ return callSlackApiTrackingResponse(
input.operation,
normalizeSlackApiBody(input.body),
createSlackApiOptions(input.botToken, input.context),
diff --git a/packages/eve/src/public/channels/slack/interaction-cards.ts b/packages/eve/src/public/channels/slack/interaction-cards.ts
index d051b9dbd..d34b46b90 100644
--- a/packages/eve/src/public/channels/slack/interaction-cards.ts
+++ b/packages/eve/src/public/channels/slack/interaction-cards.ts
@@ -1,4 +1,4 @@
-import { resolveSlackBotToken } from "#public/channels/slack/api.js";
+import { callSlackApi } from "#public/channels/slack/api.js";
import { buildAnsweredBlocks, isHitlAction } from "#public/channels/slack/hitl.js";
import {
SLACK_CARD_SUBTEXT_MAX_LENGTH,
@@ -183,21 +183,15 @@ async function updateAnsweredCard(input: {
readonly installationTeamId?: string;
readonly messageTs: string;
}): Promise<void> {
- const token = await resolveSlackBotToken(input.deps.config.credentials?.botToken, {
- teamId: input.installationTeamId,
- });
- const response = await fetch("https://slack.com/api/chat.update", {
- method: "POST",
- headers: {
- authorization: `Bearer ${token}`,
- "content-type": "application/json; charset=utf-8",
- },
- body: JSON.stringify({
+ await callSlackApi({
+ botToken: input.deps.config.credentials?.botToken,
+ context: { teamId: input.installationTeamId },
+ operation: "chat.update",
+ body: {
channel: input.channelId,
ts: input.messageTs,
blocks: input.blocks,
text: `Answered: ${input.answerLabel}`,
- }),
+ },
});
- if (!response.ok) throw new Error(`Slack chat.update returned HTTP ${response.status}`);
}
diff --git a/packages/eve/src/public/channels/slack/interactions.ts b/packages/eve/src/public/channels/slack/interactions.ts
index e54741638..0d3d62d23 100644
--- a/packages/eve/src/public/channels/slack/interactions.ts
+++ b/packages/eve/src/public/channels/slack/interactions.ts
@@ -11,9 +11,11 @@ import {
} from "#compiled/@chat-adapter/slack/webhook.js";
import { createLogger } from "#internal/logging.js";
+import { isSlackResponseError } from "#public/channels/slack/api-errors.js";
import {
buildSlackBinding,
buildSlackWorkspaceHandle,
+ callSlackApi,
resolveSlackBotToken,
slackContinuationToken,
} from "#public/channels/slack/api.js";
@@ -181,10 +183,6 @@ function extractActionLabel(action: Record<string, unknown>): string | undefined
return undefined;
}
-function findPromptBlock(blocks: readonly unknown[]): unknown {
- return findPromptBlocks(blocks)[0];
-}
-
function findPromptBlocks(blocks: readonly unknown[]): unknown[] {
const promptBlocks: unknown[] = [];
for (const block of blocks) {
@@ -203,7 +201,7 @@ function findPromptBlocks(blocks: readonly unknown[]): unknown[] {
}
function readPromptTextFromBlocks(blocks: readonly unknown[]): string | undefined {
- const prompt = findPromptBlock(blocks) as { text?: unknown } | undefined;
+ const prompt = findPromptBlocks(blocks)[0] as { text?: unknown } | undefined;
const text = readSlackTextObject(prompt?.text);
return text.length > 0 ? text : undefined;
}
@@ -570,16 +568,19 @@ async function openFreeformModal(input: {
teamId: input.interaction.installationTeamId,
});
- const response = await fetch("https://slack.com/api/views.open", {
- method: "POST",
- headers: {
- authorization: `Bearer ${token}`,
- "content-type": "application/json; charset=utf-8",
- },
- body: JSON.stringify({ trigger_id: triggerId, view }),
- });
- if (!response.ok) {
- log.error("Slack views.open returned non-2xx", { status: response.status });
+ // Slack retries a click whose acknowledgement is not a 2xx, so a modal
+ // that fails to open is logged and the click still acknowledged; a call
+ // that never reached Slack is rethrown and fails the request. The token
+ // above resolves outside the try so a throwing resolver joins that case.
+ try {
+ await callSlackApi({
+ botToken: token,
+ operation: "views.open",
+ body: { trigger_id: triggerId, view },
+ });
+ } catch (error) {
+ if (!isSlackResponseError(error)) throw error;
+ log.error("Slack views.open failed", { error });
}
}
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 408e1e1ef..04d8c6448 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -1,6 +1,6 @@
import { createHmac } from "node:crypto";
-import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterAll, afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from "vitest";
import { buildAdapterContext } from "#channel/adapter-context.js";
import { callAdapterEventHandler, type ChannelAdapter } from "#channel/adapter.js";
@@ -9,6 +9,7 @@ import type { ChannelFrom, ChannelSource } from "#channel/channel-operations.js"
import { isHttpRouteDefinition } from "#channel/routes.js";
import { ContextContainer, contextStorage } from "#context/container.js";
import { SessionKey } from "#context/keys.js";
+import { setLogRecordSubscriber, type LogRecord } from "#internal/logging.js";
import {
mockChannelContext,
type ObservedChannelDelivery,
@@ -92,6 +93,64 @@ function parseSlackRequestBody(init: RequestInit | undefined): Record<string, un
return decodeSlackApiBody(init.body, contentType) as Record<string, unknown>;
}
+// Selects the captured calls to one Slack Web API method. The transport
+// hands `fetch` a `URL`, which never equals a string, so a URL assertion
+// has to stringify before comparing.
+function slackCalls(
+ fetchMock: ReturnType<typeof vi.fn>,
+ operation: string,
+): Array<[unknown, RequestInit | undefined]> {
+ return fetchMock.mock.calls.filter(
+ ([url]) => String(url) === `https://slack.com/api/${operation}`,
+ ) as Array<[unknown, RequestInit | undefined]>;
+}
+
+// Names the Slack Web API method of every captured call, in order. An
+// assertion on this list says which calls happened, so it still fails
+// when a call moves to a URL the filter in `slackCalls` would miss; a URL
+// this cannot classify throws rather than dropping out of the list.
+function slackOperations(fetchMock: ReturnType<typeof vi.fn>): string[] {
+ return fetchMock.mock.calls.map(([url]) => {
+ const operation = /^https:\/\/slack\.com\/api\/([\w.]+)$/.exec(String(url))?.[1];
+ if (operation === undefined) throw new Error(`Unexpected Slack request: ${String(url)}`);
+ return operation;
+ });
+}
+
+// Captures structured log records for the rest of the current test. The
+// subscriber slot is process-wide, so the helper registers its own
+// teardown rather than leaving that to each caller.
+function captureLogRecords(): { records: LogRecord[] } {
+ const records: LogRecord[] = [];
+ setLogRecordSubscriber((record) => records.push(record));
+ onTestFinished(() => setLogRecordSubscriber(undefined));
+ return { records };
+}
+
+// A response from a gateway in front of Slack: a received non-2xx whose
+// body is an HTML error page rather than a Slack JSON envelope.
+function htmlGatewayError(): Response {
+ return new Response("<html><body>502 Bad Gateway</body></html>", {
+ headers: { "content-type": "text/html" },
+ status: 502,
+ });
+}
+
+// A response whose headers arrived but whose body was cut off mid-read:
+// reading it rejects with the `TypeError` an interrupted connection
+// raises, after the status line is already known.
+function interruptedBody(status: number): Response {
+ return new Response(
+ new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode('{"ok":'));
+ controller.error(new TypeError("terminated"));
+ },
+ }),
+ { status },
+ );
+}
+
function useSuccessfulSlackFileUpload(
fetchMock: ReturnType<typeof vi.fn>,
postTs = "1700000001.000001",
@@ -972,14 +1031,12 @@ describe("slackChannel() default event handlers", () => {
ctx,
);
- const postCalls = fetchMock.mock.calls.filter(
- ([url]) => String(url) === "https://slack.com/api/chat.postMessage",
- );
+ const postCalls = slackCalls(fetchMock, "chat.postMessage");
expect(postCalls).toHaveLength(2);
- const details = parseSlackRequestBody(postCalls[0]?.[1] as RequestInit);
+ const details = parseSlackRequestBody(postCalls[0]?.[1]);
expect(JSON.stringify(details)).toContain("cursor cloud task");
- const controls = parseSlackRequestBody(postCalls[1]?.[1] as RequestInit) as {
+ const controls = parseSlackRequestBody(postCalls[1]?.[1]) as {
blocks: Array<{
actions?: Array<{
action_id: string;
@@ -3171,6 +3228,55 @@ describe("slackChannel() HITL interaction pipeline", () => {
});
}
+ function buildFreeformSubmissionRequest(): Request {
+ return buildSignedInteractionRequest({
+ type: "view_submission",
+ team: { id: "T_ACTOR" },
+ user: { id: "U_SUBMITTER", username: "grace", name: "grace", team_id: "T_ACTOR" },
+ view: {
+ app_installed_team_id: "T_INSTALLATION",
+ callback_id: HITL_FREEFORM_MODAL_CALLBACK_ID,
+ private_metadata: JSON.stringify({
+ channelId: "C_ORIGINAL",
+ continuationToken: "C_ORIGINAL:1700000000.000001",
+ messageChannelId: "D_REVIEW",
+ messageTs: "1700000000.000010",
+ requestId: "call_abc123",
+ threadTs: "1700000000.000001",
+ }),
+ state: {
+ values: {
+ [HITL_FREEFORM_MODAL_BLOCK_ID]: {
+ [HITL_FREEFORM_MODAL_ACTION_ID]: { value: "approved with context" },
+ },
+ },
+ },
+ },
+ });
+ }
+
+ function buildFreeformButtonRequest(): Request {
+ return buildSignedInteractionRequest({
+ type: "block_actions",
+ trigger_id: "trigger-123",
+ team: { id: "T_INSTALLATION" },
+ user: { id: "U01", username: "ada", team_id: "T_ACTOR" },
+ channel: { id: "C01" },
+ message: {
+ ts: "1700000000.000010",
+ thread_ts: "1700000000.000001",
+ blocks: [{ type: "section", text: { type: "mrkdwn", text: "Explain" } }],
+ },
+ actions: [
+ {
+ action_id: `${HITL_FREEFORM_ACTION_PREFIX}route:C_ORIGINAL:1700000000.000001:call_abc123`,
+ text: { type: "plain_text", text: "Type your answer" },
+ value: "call_abc123",
+ },
+ ],
+ });
+ }
+
afterAll(() => {
if (ORIGINAL_SIGNING_SECRET === undefined) {
delete process.env.SLACK_SIGNING_SECRET;
@@ -3470,35 +3576,12 @@ describe("slackChannel() HITL interaction pipeline", () => {
const botToken = vi.fn((_context: { readonly teamId?: string }) => "xoxb-test");
const channel = slackChannel({ credentials: { botToken } });
- await firePost(
- channel,
- buildSignedInteractionRequest({
- type: "block_actions",
- trigger_id: "trigger-123",
- team: { id: "T_INSTALLATION" },
- user: { id: "U01", username: "ada", team_id: "T_ACTOR" },
- channel: { id: "C01" },
- message: {
- ts: "1700000000.000010",
- thread_ts: "1700000000.000001",
- blocks: [{ type: "section", text: { type: "mrkdwn", text: "Explain" } }],
- },
- actions: [
- {
- action_id: `${HITL_FREEFORM_ACTION_PREFIX}route:C_ORIGINAL:1700000000.000001:call_abc123`,
- text: { type: "plain_text", text: "Type your answer" },
- value: "call_abc123",
- },
- ],
- }),
- );
+ await firePost(channel, buildFreeformButtonRequest());
expect(botToken).toHaveBeenCalledWith({ teamId: "T_INSTALLATION" });
- const openCall = fetchMock.mock.calls.find(
- ([url]) => String(url) === "https://slack.com/api/views.open",
- );
+ const openCall = slackCalls(fetchMock, "views.open")[0];
expect(openCall).toBeDefined();
- const body = JSON.parse(String((openCall![1] as RequestInit).body)) as {
+ const body = parseSlackRequestBody(openCall![1]) as {
view: { private_metadata: string };
};
expect(JSON.parse(body.view.private_metadata)).toMatchObject({
@@ -3511,6 +3594,115 @@ describe("slackChannel() HITL interaction pipeline", () => {
});
});
+ it("fails the interaction when views.open never reaches Slack", async () => {
+ fetchMock.mockImplementation(() => Promise.reject(new Error("ECONNRESET")));
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+
+ // A call that never reached Slack is not a response, so it escapes the
+ // handler and the route answers a 500. Narrowing the catch to what a
+ // received response raises is what keeps it escaping.
+ await expect(firePost(channel, buildFreeformButtonRequest())).rejects.toThrow("ECONNRESET");
+ });
+
+ it("fails the interaction when the bot token cannot be resolved", async () => {
+ const channel = slackChannel({
+ credentials: {
+ botToken: () => {
+ throw new Error("no installation for T_INSTALLATION");
+ },
+ },
+ });
+
+ // Token resolution sits outside the try, so an unknown installation
+ // workspace escapes the handler and nothing is sent.
+ await expect(firePost(channel, buildFreeformButtonRequest())).rejects.toThrow(
+ "no installation for T_INSTALLATION",
+ );
+ expect(slackOperations(fetchMock)).toEqual([]);
+ });
+
+ it("acks the interaction when views.open fails with a non-2xx", async () => {
+ fetchMock.mockImplementation(() =>
+ Promise.resolve(
+ new Response(JSON.stringify({ ok: false, error: "ratelimited" }), { status: 429 }),
+ ),
+ );
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const logs = captureLogRecords();
+
+ const { response } = await firePost(channel, buildFreeformButtonRequest());
+
+ // Slack retries any ack that is not a 2xx, so the click must be
+ // acknowledged even though the modal never opened.
+ expect(response.status).toBe(200);
+ expect(logs.records).toContainEqual(
+ expect.objectContaining({ level: "error", message: "Slack views.open failed" }),
+ );
+ });
+
+ it("acks the interaction when views.open fails with a non-JSON error page", async () => {
+ fetchMock.mockImplementation(() => Promise.resolve(htmlGatewayError()));
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const logs = captureLogRecords();
+
+ const { response } = await firePost(channel, buildFreeformButtonRequest());
+
+ // Slack retries any ack that is not a 2xx, so the click must be
+ // acknowledged even though the modal never opened.
+ expect(response.status).toBe(200);
+ expect(logs.records).toContainEqual(
+ expect.objectContaining({ level: "error", message: "Slack views.open failed" }),
+ );
+ });
+
+ it("acks the interaction when a 200 to views.open is cut off mid-body", async () => {
+ fetchMock.mockImplementation(() => Promise.resolve(interruptedBody(200)));
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const logs = captureLogRecords();
+
+ const { response } = await firePost(channel, buildFreeformButtonRequest());
+
+ // The response arrived, so the click is acknowledged even though the
+ // body never did. Classifying by error type missed this: reading an
+ // interrupted body raises a `TypeError`, not a Slack error.
+ expect(response.status).toBe(200);
+ expect(logs.records).toContainEqual(
+ expect.objectContaining({ level: "error", message: "Slack views.open failed" }),
+ );
+ });
+
+ it("acks the interaction when a non-2xx to views.open is cut off mid-body", async () => {
+ fetchMock.mockImplementation(() => Promise.resolve(interruptedBody(502)));
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const logs = captureLogRecords();
+
+ const { response } = await firePost(channel, buildFreeformButtonRequest());
+
+ // The transport reads the body before it reads the status, so an
+ // interrupted body hides the non-2xx behind the same `TypeError`.
+ expect(response.status).toBe(200);
+ expect(logs.records).toContainEqual(
+ expect.objectContaining({ level: "error", message: "Slack views.open failed" }),
+ );
+ });
+
+ it("acks the interaction when views.open answers ok:false", async () => {
+ fetchMock.mockImplementation(() =>
+ Promise.resolve(
+ new Response(JSON.stringify({ ok: false, error: "expired_trigger_id" }), { status: 200 }),
+ ),
+ );
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+
+ const { response } = await firePost(channel, buildFreeformButtonRequest());
+
+ // Slack's usual failure for views.open is a 200 carrying ok:false.
+ // The channel does not inspect `ok` here, so the modal silently
+ // never opens and the click is still acknowledged.
+ expect(response.status).toBe(200);
+ expect(slackCalls(fetchMock, "views.open")).toHaveLength(1);
+ });
+
it("authorizes HITL button answers before resuming with the returned auth", async () => {
const customAuth = {
attributes: { role: "deployer" },
@@ -3599,9 +3791,9 @@ describe("slackChannel() HITL interaction pipeline", () => {
ctx,
);
- const ephemeralBodies = fetchMock.mock.calls
- .filter(([url]) => String(url) === "https://slack.com/api/chat.postEphemeral")
- .map(([, init]) => parseSlackRequestBody(init as RequestInit));
+ const ephemeralBodies = slackCalls(fetchMock, "chat.postEphemeral").map(([, init]) =>
+ parseSlackRequestBody(init),
+ );
expect(ephemeralBodies).toEqual([
expect.objectContaining({
channel: "C01",
@@ -3614,10 +3806,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
user: "U_APPROVER",
}),
]);
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual(["chat.postEphemeral", "chat.postEphemeral"]);
});
it("keeps HITL pending when the input-response hook rejects or throws", async () => {
@@ -3639,10 +3828,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
expect(onInputResponse).toHaveBeenCalledTimes(1);
expect(send).not.toHaveBeenCalled();
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual([]);
}
});
@@ -3665,10 +3851,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
await firePost(channel, buildHitlButtonRequest(), { send });
expect(send).toHaveBeenCalledTimes(1);
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual([]);
});
it("waits for approval settlement before updating a tool-approval card", async () => {
@@ -3775,10 +3958,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
inputResponses: [{ optionId: "approve", requestId: "approval_451" }],
});
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual([]);
});
it("covers the observed e0 batched escalation approval run", async () => {
@@ -3830,11 +4010,9 @@ describe("slackChannel() HITL interaction pipeline", () => {
ctx,
);
- const postCalls = fetchMock.mock.calls.filter(
- ([url]) => String(url) === "https://slack.com/api/chat.postMessage",
- );
+ const postCalls = slackCalls(fetchMock, "chat.postMessage");
expect(postCalls).toHaveLength(2);
- const postedDetails = parseSlackRequestBody(postCalls[0]?.[1] as RequestInit) as {
+ const postedDetails = parseSlackRequestBody(postCalls[0]?.[1]) as {
blocks: Array<{
child_blocks?: Array<{ text?: { text?: string } }>;
title?: { text?: string };
@@ -3848,7 +4026,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
);
expect(postedDetails.blocks[1]?.child_blocks?.[0]?.text?.text).toContain('"issueNumber": 508');
- const postedControls = parseSlackRequestBody(postCalls[1]?.[1] as RequestInit) as {
+ const postedControls = parseSlackRequestBody(postCalls[1]?.[1]) as {
blocks: Array<{
actions?: Array<{ action_id?: string; text?: { text?: string }; value?: string }>;
type?: string;
@@ -3899,48 +4077,14 @@ describe("slackChannel() HITL interaction pipeline", () => {
inputResponses: [{ optionId: "cancel", requestId: "approval_451" }],
});
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual(["chat.postMessage", "chat.postMessage"]);
});
it("resumes freeform modal answers with the submitting Slack user auth", async () => {
const botToken = vi.fn((_context: { readonly teamId?: string }) => "xoxb-test");
const channel = slackChannel({ credentials: { botToken } });
- const { send } = await firePost(
- channel,
- buildSignedInteractionRequest({
- type: "view_submission",
- team: { id: "T_ACTOR" },
- user: {
- id: "U_SUBMITTER",
- username: "grace",
- name: "grace",
- team_id: "T_ACTOR",
- },
- view: {
- app_installed_team_id: "T_INSTALLATION",
- callback_id: HITL_FREEFORM_MODAL_CALLBACK_ID,
- private_metadata: JSON.stringify({
- channelId: "C_ORIGINAL",
- continuationToken: "C_ORIGINAL:1700000000.000001",
- messageChannelId: "D_REVIEW",
- messageTs: "1700000000.000010",
- requestId: "call_abc123",
- threadTs: "1700000000.000001",
- }),
- state: {
- values: {
- [HITL_FREEFORM_MODAL_BLOCK_ID]: {
- [HITL_FREEFORM_MODAL_ACTION_ID]: { value: "approved with context" },
- },
- },
- },
- },
- }),
- );
+ const { send } = await firePost(channel, buildFreeformSubmissionRequest());
expect(send).toHaveBeenCalledTimes(1);
const [continuationToken, input] = send.mock.calls[0]!;
@@ -3963,10 +4107,48 @@ describe("slackChannel() HITL interaction pipeline", () => {
inputResponses: [{ requestId: "call_abc123", text: "approved with context" }],
});
expect(botToken).toHaveBeenCalledWith({ teamId: "T_INSTALLATION" });
- expect(fetchMock).toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.objectContaining({ body: expect.stringContaining('"channel":"D_REVIEW"') }),
+ const updateCall = slackCalls(fetchMock, "chat.update")[0];
+ expect(updateCall).toBeDefined();
+ expect(parseSlackRequestBody(updateCall![1])).toMatchObject({
+ channel: "D_REVIEW",
+ });
+ });
+
+ it("keeps a delivered freeform answer when chat.update fails with a non-JSON error page", async () => {
+ fetchMock.mockImplementation(() => Promise.resolve(htmlGatewayError()));
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const logs = captureLogRecords();
+
+ const { response, send } = await firePost(channel, buildFreeformSubmissionRequest());
+
+ // The answer already reached the session; only the card decoration
+ // failed, so the submission stands and the failure is logged.
+ expect(response.status).toBe(200);
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(logs.records).toContainEqual(
+ expect.objectContaining({
+ level: "error",
+ message: "freeform answered-card update failed",
+ }),
+ );
+ });
+
+ it("keeps a delivered freeform answer when chat.update answers ok:false", async () => {
+ fetchMock.mockImplementation(() =>
+ Promise.resolve(
+ new Response(JSON.stringify({ ok: false, error: "message_not_found" }), { status: 200 }),
+ ),
);
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+
+ const { response, send } = await firePost(channel, buildFreeformSubmissionRequest());
+
+ // Slack's usual failure for chat.update is a 200 carrying ok:false.
+ // The channel does not inspect `ok` here, so the card is left
+ // undecorated without a word and the submission still stands.
+ expect(response.status).toBe(200);
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(slackCalls(fetchMock, "chat.update")).toHaveLength(1);
});
it("authorizes freeform modal answers before resuming", async () => {
@@ -4013,10 +4195,7 @@ describe("slackChannel() HITL interaction pipeline", () => {
}),
);
expect(send).not.toHaveBeenCalled();
- expect(fetchMock).not.toHaveBeenCalledWith(
- "https://slack.com/api/chat.update",
- expect.anything(),
- );
+ expect(slackOperations(fetchMock)).toEqual([]);
});
});