|
1 | | -import { createRuntimeEnv } from "openclaw/plugin-sdk/plugin-test-runtime"; |
2 | 1 | // Slack tests cover channel plugin behavior. |
| 2 | +import { createMessageReceiptFromOutboundResults } from "openclaw/plugin-sdk/channel-outbound"; |
| 3 | +import { createRuntimeEnv } from "openclaw/plugin-sdk/plugin-test-runtime"; |
3 | 4 | import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime"; |
4 | 5 | import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures"; |
5 | 6 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
6 | 7 | import { slackPlugin } from "./channel.js"; |
7 | 8 | import { registerSlackInstallationState } from "./installation-identity-state.js"; |
8 | 9 | import { slackOutbound } from "./outbound-adapter.js"; |
9 | 10 | import * as probeModule from "./probe.js"; |
| 11 | +import { SLACK_QUESTION_FINALIZATION_BLOCKS } from "./reply-action-ids.js"; |
10 | 12 | import type { OpenClawConfig } from "./runtime-api.js"; |
11 | 13 | import { setSlackRuntime } from "./runtime.js"; |
12 | 14 |
|
@@ -1605,7 +1607,11 @@ describe("slackPlugin outbound", () => { |
1605 | 1607 | }, |
1606 | 1608 | }, |
1607 | 1609 | ]); |
1608 | | - expect(result).toEqual({ channel: "slack", messageId: "m-final" }); |
| 1610 | + expect(result).toMatchObject({ |
| 1611 | + channel: "slack", |
| 1612 | + messageId: "m-final", |
| 1613 | + receipt: { platformMessageIds: ["m-media-1", "m-media-2", "m-final"] }, |
| 1614 | + }); |
1609 | 1615 | }); |
1610 | 1616 |
|
1611 | 1617 | it("renders shared interactive payloads into Slack Block Kit via plugin outbound", async () => { |
@@ -1671,6 +1677,118 @@ describe("slackPlugin outbound", () => { |
1671 | 1677 | expectRecordFields(options[1], "production option", { value: "production" }); |
1672 | 1678 | expect(result).toEqual({ channel: "slack", messageId: "m-interactive" }); |
1673 | 1679 | }); |
| 1680 | + |
| 1681 | + it.each([ |
| 1682 | + { surface: "interactive", type: "text" }, |
| 1683 | + { surface: "presentation", type: "text" }, |
| 1684 | + { surface: "presentation", type: "context" }, |
| 1685 | + ] as const)( |
| 1686 | + "delivers oversized $surface $type in order across the real Slack outbound adapter", |
| 1687 | + async ({ surface, type }) => { |
| 1688 | + const sendSlack = vi |
| 1689 | + .fn() |
| 1690 | + .mockResolvedValueOnce({ messageId: "m-chunk-1" }) |
| 1691 | + .mockResolvedValueOnce({ messageId: "m-chunk-2" }); |
| 1692 | + const text = "x".repeat(3_000 * 50 + 1); |
| 1693 | + const buttons = { |
| 1694 | + type: "buttons" as const, |
| 1695 | + buttons: [{ label: "Continue", value: "continue" }], |
| 1696 | + }; |
| 1697 | + const presentationTextBlock = |
| 1698 | + type === "context" ? { type: "context" as const, text } : { type: "text" as const, text }; |
| 1699 | + const payload = |
| 1700 | + surface === "interactive" |
| 1701 | + ? { text: "", interactive: { blocks: [{ type: "text" as const, text }, buttons] } } |
| 1702 | + : { text: "", presentation: { blocks: [presentationTextBlock, buttons] } }; |
| 1703 | + |
| 1704 | + const result = await requireSlackSendPayload()({ |
| 1705 | + cfg, |
| 1706 | + to: "channel:C123", |
| 1707 | + text: "", |
| 1708 | + payload, |
| 1709 | + accountId: "default", |
| 1710 | + deps: { sendSlack }, |
| 1711 | + }); |
| 1712 | + const batches = sendSlack.mock.calls.map((_call, index) => |
| 1713 | + requireArray(requireMockCallArg(sendSlack, index, 2).blocks, "Slack blocks"), |
| 1714 | + ); |
| 1715 | + const delivered = batches.flat().flatMap((entry) => { |
| 1716 | + const block = requireRecord(entry, "Slack block"); |
| 1717 | + const textObject = |
| 1718 | + block.type === "context" |
| 1719 | + ? requireArray(block.elements, "context elements")[0] |
| 1720 | + : block.type === "section" |
| 1721 | + ? block.text |
| 1722 | + : undefined; |
| 1723 | + return textObject ? [String(requireRecord(textObject, "Slack text").text)] : []; |
| 1724 | + }); |
| 1725 | + |
| 1726 | + expect(batches.map((blocks) => blocks.length)).toEqual([50, 2]); |
| 1727 | + expect(delivered.join("")).toBe(text); |
| 1728 | + expect(batches[1]?.[1]).toMatchObject({ type: "actions" }); |
| 1729 | + expect(result).toMatchObject({ |
| 1730 | + channel: "slack", |
| 1731 | + messageId: "m-chunk-2", |
| 1732 | + receipt: { platformMessageIds: ["m-chunk-1", "m-chunk-2"] }, |
| 1733 | + }); |
| 1734 | + }, |
| 1735 | + ); |
| 1736 | + |
| 1737 | + it("retains media and every reply receipt without losing an earlier question card", async () => { |
| 1738 | + const questionId = "ask_0123456789abcdef0123456789abcdef"; |
| 1739 | + const questionMeta = { |
| 1740 | + slackQuestionActionIds: ["openclaw:question_button:1:1"], |
| 1741 | + [SLACK_QUESTION_FINALIZATION_BLOCKS]: [{ type: "divider" as const }], |
| 1742 | + }; |
| 1743 | + const createResult = (messageId: string, kind: "media" | "card") => ({ |
| 1744 | + messageId, |
| 1745 | + channelId: "C123", |
| 1746 | + receipt: createMessageReceiptFromOutboundResults({ |
| 1747 | + results: [{ channel: "slack", messageId }], |
| 1748 | + kind, |
| 1749 | + }), |
| 1750 | + }); |
| 1751 | + const sendSlack = vi |
| 1752 | + .fn() |
| 1753 | + .mockResolvedValueOnce(createResult("m-upload", "media")) |
| 1754 | + .mockResolvedValueOnce({ ...createResult("m-question", "card"), meta: questionMeta }) |
| 1755 | + .mockResolvedValueOnce(createResult("m-final", "card")); |
| 1756 | + |
| 1757 | + const result = await requireSlackSendPayload()({ |
| 1758 | + cfg, |
| 1759 | + to: "channel:C123", |
| 1760 | + text: "", |
| 1761 | + accountId: "default", |
| 1762 | + deps: { sendSlack }, |
| 1763 | + payload: { |
| 1764 | + text: "", |
| 1765 | + mediaUrls: ["https://example.com/context.png"], |
| 1766 | + channelData: { |
| 1767 | + slack: { blocks: Array.from({ length: 48 }, () => ({ type: "divider" as const })) }, |
| 1768 | + }, |
| 1769 | + presentation: { |
| 1770 | + blocks: [ |
| 1771 | + { |
| 1772 | + type: "buttons", |
| 1773 | + buttons: [ |
| 1774 | + { |
| 1775 | + label: "Answer", |
| 1776 | + action: { type: "question", questionId, optionValue: "one" }, |
| 1777 | + }, |
| 1778 | + ], |
| 1779 | + }, |
| 1780 | + { type: "text", text: "x".repeat(3_001) }, |
| 1781 | + ], |
| 1782 | + }, |
| 1783 | + }, |
| 1784 | + }); |
| 1785 | + |
| 1786 | + expect(sendSlack).toHaveBeenCalledTimes(3); |
| 1787 | + expect(result.messageId).toBe("m-final"); |
| 1788 | + expect(result.receipt?.platformMessageIds).toEqual(["m-upload", "m-question", "m-final"]); |
| 1789 | + expect(result.receipt?.parts.map((part) => part.index)).toEqual([0, 1, 2]); |
| 1790 | + expect(result.meta).toEqual({ ...questionMeta, slackQuestionMessageId: "m-question" }); |
| 1791 | + }); |
1674 | 1792 | }); |
1675 | 1793 |
|
1676 | 1794 | describe("slackPlugin directory", () => { |
|
0 commit comments