|
2 | 2 | import type { App } from "@slack/bolt"; |
3 | 3 | import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import { createInboundSlackTestContext } from "../message-handler/prepare.test-helpers.js"; |
| 5 | +import { createSlackSystemEventRouteResolver } from "../system-event-session.js"; |
5 | 6 | import { |
6 | 7 | createSlackSystemEventTestHarness, |
7 | 8 | type SlackSystemEventTestOverrides, |
@@ -732,6 +733,155 @@ describe("registerSlackMessageEvents", () => { |
732 | 733 | }); |
733 | 734 | }); |
734 | 735 |
|
| 736 | + it.each([ |
| 737 | + { channelType: "channel", subtype: "message_changed", threadSession: true }, |
| 738 | + { channelType: "channel", subtype: "message_deleted", threadSession: true }, |
| 739 | + { channelType: "group", subtype: "message_changed", threadSession: true }, |
| 740 | + { channelType: "group", subtype: "message_deleted", threadSession: true }, |
| 741 | + { channelType: "mpim", subtype: "message_changed", threadSession: true }, |
| 742 | + { channelType: "mpim", subtype: "message_deleted", threadSession: true }, |
| 743 | + { channelType: "im", subtype: "message_changed", threadSession: false }, |
| 744 | + { channelType: "im", subtype: "message_deleted", threadSession: false }, |
| 745 | + { channelType: "channel", subtype: "message_changed", root: true, threadSession: false }, |
| 746 | + { channelType: "channel", subtype: "message_deleted", root: true, threadSession: false }, |
| 747 | + { |
| 748 | + channelType: "channel", |
| 749 | + subtype: "message_changed", |
| 750 | + previousOnly: true, |
| 751 | + threadSession: true, |
| 752 | + }, |
| 753 | + { |
| 754 | + channelType: "channel", |
| 755 | + subtype: "message_changed", |
| 756 | + root: true, |
| 757 | + parentUserId: "U_PARENT", |
| 758 | + threadSession: true, |
| 759 | + }, |
| 760 | + { |
| 761 | + channelType: "channel", |
| 762 | + subtype: "message_deleted", |
| 763 | + root: true, |
| 764 | + parentUserId: "U_PARENT", |
| 765 | + threadSession: true, |
| 766 | + }, |
| 767 | + { channelType: "mpim", subtype: "message_changed", denied: true, threadSession: false }, |
| 768 | + { channelType: "mpim", subtype: "message_deleted", denied: true, threadSession: false }, |
| 769 | + { |
| 770 | + channelType: "channel", |
| 771 | + subtype: "message_changed", |
| 772 | + enterprise: true, |
| 773 | + threadSession: true, |
| 774 | + }, |
| 775 | + { |
| 776 | + channelType: "channel", |
| 777 | + subtype: "message_deleted", |
| 778 | + enterprise: true, |
| 779 | + threadSession: true, |
| 780 | + }, |
| 781 | + ] as const)( |
| 782 | + "routes $channelType $subtype events to their actual conversation", |
| 783 | + async ({ channelType, subtype, threadSession, ...scenario }) => { |
| 784 | + const actualSystemEvents = await vi.importActual< |
| 785 | + typeof import("openclaw/plugin-sdk/system-event-runtime") |
| 786 | + >("openclaw/plugin-sdk/system-event-runtime"); |
| 787 | + actualSystemEvents.resetSystemEventsForTest(); |
| 788 | + messageQueueMock.mockImplementation( |
| 789 | + (text: string, { sessionKey, ...options }: { sessionKey: string }) => |
| 790 | + actualSystemEvents.enqueueRoutedSystemEvent( |
| 791 | + text, |
| 792 | + { agentId: "main", sessionKey }, |
| 793 | + options, |
| 794 | + ), |
| 795 | + ); |
| 796 | + |
| 797 | + const senderId = "U123"; |
| 798 | + const denied = "denied" in scenario; |
| 799 | + const enterprise = "enterprise" in scenario; |
| 800 | + const harness = createSlackSystemEventTestHarness({ |
| 801 | + channelType, |
| 802 | + ...(channelType === "mpim" ? { allowFrom: denied ? ["U_OTHER"] : [senderId] } : {}), |
| 803 | + }); |
| 804 | + harness.ctx.cfg = { channels: { slack: { enabled: true } } }; |
| 805 | + harness.ctx.accountId = "default"; |
| 806 | + harness.ctx.channelsConfigKeys = []; |
| 807 | + if (enterprise) { |
| 808 | + harness.ctx.installationIdentity = { |
| 809 | + kind: "enterprise", |
| 810 | + apiAppId: "A_TEST", |
| 811 | + enterpriseId: "E_TEST", |
| 812 | + }; |
| 813 | + } |
| 814 | + harness.ctx.resolveSlackSystemEventRoute = createSlackSystemEventRouteResolver({ |
| 815 | + cfg: harness.ctx.cfg, |
| 816 | + accountId: harness.ctx.accountId, |
| 817 | + getTeamId: () => harness.ctx.teamId, |
| 818 | + mainKey: "agent:main:main", |
| 819 | + threadInheritParent: false, |
| 820 | + recallSlackChannelType: () => channelType, |
| 821 | + }); |
| 822 | + registerSlackMessageEvents({ ctx: harness.ctx, handleSlackMessage: async () => {} }); |
| 823 | + |
| 824 | + const threadTs = "1712345678.123456"; |
| 825 | + const channelId = channelType === "im" ? "D123" : channelType === "mpim" ? "G123" : "C123"; |
| 826 | + const nestedMessage = { |
| 827 | + ts: "root" in scenario ? threadTs : "1712345678.654321", |
| 828 | + thread_ts: threadTs, |
| 829 | + user: senderId, |
| 830 | + ...("parentUserId" in scenario ? { parent_user_id: scenario.parentUserId } : {}), |
| 831 | + }; |
| 832 | + const event = { |
| 833 | + type: "message", |
| 834 | + subtype, |
| 835 | + channel: channelId, |
| 836 | + channel_type: channelType, |
| 837 | + previous_message: nestedMessage, |
| 838 | + event_ts: "1712345679.000000", |
| 839 | + ...(subtype === "message_changed" |
| 840 | + ? { |
| 841 | + message: |
| 842 | + "previousOnly" in scenario |
| 843 | + ? { ts: nestedMessage.ts, user: senderId } |
| 844 | + : nestedMessage, |
| 845 | + } |
| 846 | + : { deleted_ts: nestedMessage.ts }), |
| 847 | + }; |
| 848 | + const listenerClient = {} as App["client"]; |
| 849 | + await requireMessageHandler(harness.getHandler("message") as MessageHandler | null)({ |
| 850 | + event, |
| 851 | + body: { api_app_id: "A_TEST", event_id: `Ev-${channelType}-${subtype}` }, |
| 852 | + ...(enterprise |
| 853 | + ? { |
| 854 | + context: { isEnterpriseInstall: true, enterpriseId: "E_TEST", teamId: "T_GRID" }, |
| 855 | + client: listenerClient, |
| 856 | + } |
| 857 | + : {}), |
| 858 | + }); |
| 859 | + |
| 860 | + const eventScope = enterprise ? { teamId: "T_GRID", client: listenerClient } : undefined; |
| 861 | + const parentSessionKey = harness.ctx.resolveSlackSystemEventRoute({ |
| 862 | + channelId, |
| 863 | + channelType, |
| 864 | + senderId, |
| 865 | + eventScope, |
| 866 | + }).sessionKey; |
| 867 | + const threadSessionKey = harness.ctx.resolveSlackSystemEventRoute({ |
| 868 | + channelId, |
| 869 | + channelType, |
| 870 | + senderId, |
| 871 | + threadTs, |
| 872 | + eventScope, |
| 873 | + }).sessionKey; |
| 874 | + |
| 875 | + expect(actualSystemEvents.peekSystemEventEntries(parentSessionKey)).toHaveLength( |
| 876 | + denied || threadSession ? 0 : 1, |
| 877 | + ); |
| 878 | + expect(actualSystemEvents.peekSystemEventEntries(threadSessionKey)).toHaveLength( |
| 879 | + threadSession ? 1 : 0, |
| 880 | + ); |
| 881 | + actualSystemEvents.resetSystemEventsForTest(); |
| 882 | + }, |
| 883 | + ); |
| 884 | + |
735 | 885 | it("keeps bot edit and delete events on a remembered C-prefix mpDM session", async () => { |
736 | 886 | const handlers: Record<string, MessageHandler> = {}; |
737 | 887 | const conversationsInfo = vi.fn().mockRejectedValue(new Error("missing_scope")); |
|
0 commit comments