|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { deliverOutboundPayloads } from "../../../src/infra/outbound/deliver.js"; |
| 3 | +import { |
| 4 | + initializeGlobalHookRunner, |
| 5 | + resetGlobalHookRunner, |
| 6 | +} from "../../../src/plugins/hook-runner-global.js"; |
| 7 | +import { addTestHook } from "../../../src/plugins/hooks.test-helpers.js"; |
| 8 | +import { createEmptyPluginRegistry } from "../../../src/plugins/registry.js"; |
| 9 | +import { |
| 10 | + releasePinnedPluginChannelRegistry, |
| 11 | + setActivePluginRegistry, |
| 12 | +} from "../../../src/plugins/runtime.js"; |
| 13 | +import type { PluginHookRegistration } from "../../../src/plugins/types.js"; |
| 14 | +import { |
| 15 | + createOutboundTestPlugin, |
| 16 | + createTestRegistry, |
| 17 | +} from "../../../src/test-utils/channel-plugins.js"; |
| 18 | +import { slackOutbound } from "./outbound-adapter.js"; |
| 19 | +import type { OpenClawConfig } from "./runtime-api.js"; |
| 20 | + |
| 21 | +const sendMessageSlackMock = vi.hoisted(() => vi.fn()); |
| 22 | + |
| 23 | +vi.mock("./send.runtime.js", () => ({ |
| 24 | + sendMessageSlack: sendMessageSlackMock, |
| 25 | +})); |
| 26 | + |
| 27 | +const cfg: OpenClawConfig = { |
| 28 | + channels: { |
| 29 | + slack: { |
| 30 | + botToken: "xoxb-test", |
| 31 | + appToken: "xapp-test", |
| 32 | + accounts: { |
| 33 | + default: { |
| 34 | + botToken: "xoxb-default", |
| 35 | + appToken: "xapp-default", |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +describe("slack outbound shared hook wiring", () => { |
| 43 | + beforeEach(() => { |
| 44 | + sendMessageSlackMock.mockReset(); |
| 45 | + sendMessageSlackMock.mockResolvedValue({ messageId: "m1", channelId: "C123" }); |
| 46 | + setActivePluginRegistry( |
| 47 | + createTestRegistry([ |
| 48 | + { |
| 49 | + pluginId: "slack", |
| 50 | + plugin: createOutboundTestPlugin({ id: "slack", outbound: slackOutbound }), |
| 51 | + source: "test", |
| 52 | + }, |
| 53 | + ]), |
| 54 | + ); |
| 55 | + resetGlobalHookRunner(); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + resetGlobalHookRunner(); |
| 60 | + releasePinnedPluginChannelRegistry(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("fires message_sending once with shared routing fields", async () => { |
| 64 | + const hookRegistry = createEmptyPluginRegistry(); |
| 65 | + const handler = vi.fn().mockResolvedValue(undefined); |
| 66 | + addTestHook({ |
| 67 | + registry: hookRegistry, |
| 68 | + pluginId: "thread-ownership", |
| 69 | + hookName: "message_sending", |
| 70 | + handler: handler as PluginHookRegistration["handler"], |
| 71 | + }); |
| 72 | + initializeGlobalHookRunner(hookRegistry); |
| 73 | + |
| 74 | + await deliverOutboundPayloads({ |
| 75 | + cfg, |
| 76 | + channel: "slack", |
| 77 | + to: "C123", |
| 78 | + payloads: [{ text: "hello" }], |
| 79 | + accountId: "default", |
| 80 | + replyToId: "1712000000.000001", |
| 81 | + }); |
| 82 | + |
| 83 | + expect(handler).toHaveBeenCalledTimes(1); |
| 84 | + expect(handler).toHaveBeenCalledWith( |
| 85 | + expect.objectContaining({ |
| 86 | + to: "C123", |
| 87 | + content: "hello", |
| 88 | + replyToId: "1712000000.000001", |
| 89 | + }), |
| 90 | + expect.objectContaining({ |
| 91 | + channelId: "slack", |
| 92 | + accountId: "default", |
| 93 | + conversationId: "C123", |
| 94 | + }), |
| 95 | + ); |
| 96 | + expect(sendMessageSlackMock).toHaveBeenCalledTimes(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it("respects cancel from the shared hook without a second adapter pass", async () => { |
| 100 | + const hookRegistry = createEmptyPluginRegistry(); |
| 101 | + const handler = vi.fn().mockResolvedValue({ cancel: true }); |
| 102 | + addTestHook({ |
| 103 | + registry: hookRegistry, |
| 104 | + pluginId: "thread-ownership", |
| 105 | + hookName: "message_sending", |
| 106 | + handler: handler as PluginHookRegistration["handler"], |
| 107 | + }); |
| 108 | + initializeGlobalHookRunner(hookRegistry); |
| 109 | + |
| 110 | + const result = await deliverOutboundPayloads({ |
| 111 | + cfg, |
| 112 | + channel: "slack", |
| 113 | + to: "C123", |
| 114 | + payloads: [{ text: "hello" }], |
| 115 | + accountId: "default", |
| 116 | + replyToId: "1712000000.000001", |
| 117 | + }); |
| 118 | + |
| 119 | + expect(handler).toHaveBeenCalledTimes(1); |
| 120 | + expect(sendMessageSlackMock).not.toHaveBeenCalled(); |
| 121 | + expect(result).toEqual([]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments