@openclaw/feishu 2026.3.13 → 2026.5.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/api.ts +31 -0
  2. package/channel-entry.ts +20 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/contract-api.ts +16 -0
  5. package/index.ts +70 -53
  6. package/openclaw.plugin.json +1827 -4
  7. package/package.json +32 -7
  8. package/runtime-api.ts +55 -0
  9. package/secret-contract-api.ts +5 -0
  10. package/security-contract-api.ts +1 -0
  11. package/session-key-api.ts +1 -0
  12. package/setup-api.ts +3 -0
  13. package/setup-entry.test.ts +14 -0
  14. package/setup-entry.ts +13 -0
  15. package/src/accounts.test.ts +95 -7
  16. package/src/accounts.ts +199 -117
  17. package/src/app-registration.ts +331 -0
  18. package/src/approval-auth.test.ts +24 -0
  19. package/src/approval-auth.ts +25 -0
  20. package/src/async.test.ts +35 -0
  21. package/src/async.ts +43 -1
  22. package/src/audio-preflight.runtime.ts +9 -0
  23. package/src/bitable.test.ts +131 -0
  24. package/src/bitable.ts +59 -22
  25. package/src/bot-content.ts +474 -0
  26. package/src/bot-group-name.test.ts +108 -0
  27. package/src/bot-runtime-api.ts +12 -0
  28. package/src/bot-sender-name.ts +125 -0
  29. package/src/bot.broadcast.test.ts +463 -0
  30. package/src/bot.card-action.test.ts +519 -5
  31. package/src/bot.checkBotMentioned.test.ts +92 -20
  32. package/src/bot.helpers.test.ts +118 -0
  33. package/src/bot.stripBotMention.test.ts +13 -21
  34. package/src/bot.test.ts +1334 -401
  35. package/src/bot.ts +778 -775
  36. package/src/card-action.ts +408 -40
  37. package/src/card-interaction.test.ts +129 -0
  38. package/src/card-interaction.ts +159 -0
  39. package/src/card-test-helpers.ts +47 -0
  40. package/src/card-ux-approval.ts +65 -0
  41. package/src/card-ux-launcher.test.ts +99 -0
  42. package/src/card-ux-launcher.ts +121 -0
  43. package/src/card-ux-shared.ts +33 -0
  44. package/src/channel-runtime-api.ts +16 -0
  45. package/src/channel.runtime.ts +47 -0
  46. package/src/channel.test.ts +914 -3
  47. package/src/channel.ts +1253 -309
  48. package/src/chat-schema.ts +5 -4
  49. package/src/chat.test.ts +135 -28
  50. package/src/chat.ts +68 -10
  51. package/src/client.test.ts +212 -103
  52. package/src/client.ts +115 -21
  53. package/src/comment-dispatcher-runtime-api.ts +6 -0
  54. package/src/comment-dispatcher.test.ts +169 -0
  55. package/src/comment-dispatcher.ts +107 -0
  56. package/src/comment-handler-runtime-api.ts +3 -0
  57. package/src/comment-handler.test.ts +486 -0
  58. package/src/comment-handler.ts +309 -0
  59. package/src/comment-reaction.test.ts +166 -0
  60. package/src/comment-reaction.ts +259 -0
  61. package/src/comment-shared.test.ts +182 -0
  62. package/src/comment-shared.ts +406 -0
  63. package/src/comment-target.ts +44 -0
  64. package/src/config-schema.test.ts +63 -1
  65. package/src/config-schema.ts +31 -4
  66. package/src/conversation-id.test.ts +18 -0
  67. package/src/conversation-id.ts +199 -0
  68. package/src/dedup-runtime-api.ts +1 -0
  69. package/src/dedup.ts +33 -95
  70. package/src/directory.static.ts +61 -0
  71. package/src/directory.test.ts +116 -20
  72. package/src/directory.ts +60 -92
  73. package/src/doc-schema.ts +1 -1
  74. package/src/docx-batch-insert.test.ts +39 -38
  75. package/src/docx-batch-insert.ts +55 -19
  76. package/src/docx-color-text.ts +9 -4
  77. package/src/docx-table-ops.test.ts +53 -0
  78. package/src/docx-table-ops.ts +52 -34
  79. package/src/docx-types.ts +38 -0
  80. package/src/docx.account-selection.test.ts +12 -3
  81. package/src/docx.test.ts +314 -74
  82. package/src/docx.ts +278 -122
  83. package/src/drive-schema.ts +47 -1
  84. package/src/drive.test.ts +1219 -0
  85. package/src/drive.ts +614 -13
  86. package/src/dynamic-agent.ts +10 -4
  87. package/src/event-types.ts +45 -0
  88. package/src/external-keys.ts +1 -1
  89. package/src/lifecycle.test-support.ts +220 -0
  90. package/src/media.test.ts +403 -26
  91. package/src/media.ts +509 -132
  92. package/src/mention-target.types.ts +5 -0
  93. package/src/mention.ts +32 -51
  94. package/src/message-action-contract.ts +13 -0
  95. package/src/monitor-state-runtime-api.ts +7 -0
  96. package/src/monitor-transport-runtime-api.ts +7 -0
  97. package/src/monitor.account.ts +218 -312
  98. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +219 -0
  99. package/src/monitor.bot-identity.ts +86 -0
  100. package/src/monitor.bot-menu-handler.ts +165 -0
  101. package/src/monitor.bot-menu.lifecycle.test-support.ts +224 -0
  102. package/src/monitor.bot-menu.test.ts +178 -0
  103. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
  104. package/src/monitor.card-action.lifecycle.test-support.ts +373 -0
  105. package/src/monitor.cleanup.test.ts +376 -0
  106. package/src/monitor.comment-notice-handler.ts +105 -0
  107. package/src/monitor.comment.test.ts +937 -0
  108. package/src/monitor.comment.ts +1386 -0
  109. package/src/monitor.lifecycle.test.ts +4 -0
  110. package/src/monitor.message-handler.ts +339 -0
  111. package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
  112. package/src/monitor.reaction.test.ts +108 -48
  113. package/src/monitor.startup.test.ts +11 -9
  114. package/src/monitor.startup.ts +26 -16
  115. package/src/monitor.state.ts +20 -5
  116. package/src/monitor.synthetic-error.ts +18 -0
  117. package/src/monitor.test-mocks.ts +2 -2
  118. package/src/monitor.transport.ts +220 -60
  119. package/src/monitor.ts +15 -10
  120. package/src/monitor.webhook-e2e.test.ts +65 -7
  121. package/src/monitor.webhook-security.test.ts +122 -0
  122. package/src/monitor.webhook.test-helpers.ts +44 -26
  123. package/src/outbound-runtime-api.ts +1 -0
  124. package/src/outbound.test.ts +616 -37
  125. package/src/outbound.ts +623 -81
  126. package/src/perm-schema.ts +1 -1
  127. package/src/perm.ts +1 -7
  128. package/src/pins.ts +108 -0
  129. package/src/policy.test.ts +297 -117
  130. package/src/policy.ts +142 -29
  131. package/src/post.ts +7 -6
  132. package/src/probe.test.ts +14 -9
  133. package/src/probe.ts +26 -16
  134. package/src/processing-claims.ts +59 -0
  135. package/src/qr-terminal.ts +1 -0
  136. package/src/reactions.ts +4 -34
  137. package/src/reasoning-preview.test.ts +59 -0
  138. package/src/reasoning-preview.ts +20 -0
  139. package/src/reply-dispatcher-runtime-api.ts +7 -0
  140. package/src/reply-dispatcher.test.ts +660 -29
  141. package/src/reply-dispatcher.ts +407 -154
  142. package/src/runtime.ts +6 -3
  143. package/src/secret-contract.ts +145 -0
  144. package/src/secret-input.ts +1 -13
  145. package/src/security-audit-shared.ts +69 -0
  146. package/src/security-audit.test.ts +61 -0
  147. package/src/security-audit.ts +1 -0
  148. package/src/send-result.ts +1 -1
  149. package/src/send-target.test.ts +9 -3
  150. package/src/send-target.ts +10 -4
  151. package/src/send.reply-fallback.test.ts +105 -2
  152. package/src/send.test.ts +386 -4
  153. package/src/send.ts +414 -95
  154. package/src/sequential-key.test.ts +72 -0
  155. package/src/sequential-key.ts +28 -0
  156. package/src/sequential-queue.test.ts +92 -0
  157. package/src/sequential-queue.ts +16 -0
  158. package/src/session-conversation.ts +42 -0
  159. package/src/session-route.ts +48 -0
  160. package/src/setup-core.ts +51 -0
  161. package/src/{onboarding.test.ts → setup-surface.test.ts} +52 -21
  162. package/src/setup-surface.ts +581 -0
  163. package/src/streaming-card.test.ts +138 -2
  164. package/src/streaming-card.ts +134 -18
  165. package/src/subagent-hooks.test.ts +603 -0
  166. package/src/subagent-hooks.ts +397 -0
  167. package/src/targets.ts +3 -13
  168. package/src/test-support/lifecycle-test-support.ts +453 -0
  169. package/src/thread-bindings.test.ts +143 -0
  170. package/src/thread-bindings.ts +330 -0
  171. package/src/tool-account-routing.test.ts +66 -8
  172. package/src/tool-account.test.ts +44 -0
  173. package/src/tool-account.ts +40 -17
  174. package/src/tool-factory-test-harness.ts +11 -8
  175. package/src/tool-result.ts +3 -1
  176. package/src/tools-config.ts +1 -1
  177. package/src/types.ts +16 -15
  178. package/src/typing.ts +10 -6
  179. package/src/wiki-schema.ts +1 -1
  180. package/src/wiki.ts +1 -7
  181. package/subagent-hooks-api.ts +31 -0
  182. package/tsconfig.json +16 -0
  183. package/src/feishu-command-handler.ts +0 -59
  184. package/src/onboarding.status.test.ts +0 -25
  185. package/src/onboarding.ts +0 -489
  186. package/src/send-message.ts +0 -71
  187. package/src/targets.test.ts +0 -70
@@ -1,13 +1,63 @@
1
- import type { OpenClawConfig } from "openclaw/plugin-sdk/feishu";
2
- import { describe, expect, it, vi } from "vitest";
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import type { OpenClawConfig } from "../runtime-api.js";
3
+ import { feishuPlugin } from "./channel.js";
4
+ import { looksLikeFeishuId, normalizeFeishuTarget, resolveReceiveIdType } from "./targets.js";
3
5
 
4
6
  const probeFeishuMock = vi.hoisted(() => vi.fn());
7
+ const createFeishuClientMock = vi.hoisted(() => vi.fn());
8
+ const addReactionFeishuMock = vi.hoisted(() => vi.fn());
9
+ const listReactionsFeishuMock = vi.hoisted(() => vi.fn());
10
+ const removeReactionFeishuMock = vi.hoisted(() => vi.fn());
11
+ const sendCardFeishuMock = vi.hoisted(() => vi.fn());
12
+ const sendMessageFeishuMock = vi.hoisted(() => vi.fn());
13
+ const getMessageFeishuMock = vi.hoisted(() => vi.fn());
14
+ const editMessageFeishuMock = vi.hoisted(() => vi.fn());
15
+ const createPinFeishuMock = vi.hoisted(() => vi.fn());
16
+ const listPinsFeishuMock = vi.hoisted(() => vi.fn());
17
+ const removePinFeishuMock = vi.hoisted(() => vi.fn());
18
+ const getChatInfoMock = vi.hoisted(() => vi.fn());
19
+ const getChatMembersMock = vi.hoisted(() => vi.fn());
20
+ const getFeishuMemberInfoMock = vi.hoisted(() => vi.fn());
21
+ const listFeishuDirectoryPeersLiveMock = vi.hoisted(() => vi.fn());
22
+ const listFeishuDirectoryGroupsLiveMock = vi.hoisted(() => vi.fn());
23
+ const feishuOutboundSendMediaMock = vi.hoisted(() => vi.fn());
5
24
 
6
25
  vi.mock("./probe.js", () => ({
7
26
  probeFeishu: probeFeishuMock,
8
27
  }));
9
28
 
10
- import { feishuPlugin } from "./channel.js";
29
+ vi.mock("./client.js", () => ({
30
+ createFeishuClient: createFeishuClientMock,
31
+ }));
32
+
33
+ vi.mock("./channel.runtime.js", () => ({
34
+ feishuChannelRuntime: {
35
+ addReactionFeishu: addReactionFeishuMock,
36
+ createPinFeishu: createPinFeishuMock,
37
+ editMessageFeishu: editMessageFeishuMock,
38
+ getChatInfo: getChatInfoMock,
39
+ getChatMembers: getChatMembersMock,
40
+ getFeishuMemberInfo: getFeishuMemberInfoMock,
41
+ getMessageFeishu: getMessageFeishuMock,
42
+ listFeishuDirectoryGroupsLive: listFeishuDirectoryGroupsLiveMock,
43
+ listFeishuDirectoryPeersLive: listFeishuDirectoryPeersLiveMock,
44
+ listPinsFeishu: listPinsFeishuMock,
45
+ listReactionsFeishu: listReactionsFeishuMock,
46
+ probeFeishu: probeFeishuMock,
47
+ removePinFeishu: removePinFeishuMock,
48
+ removeReactionFeishu: removeReactionFeishuMock,
49
+ sendCardFeishu: sendCardFeishuMock,
50
+ sendMessageFeishu: sendMessageFeishuMock,
51
+ feishuOutbound: {
52
+ sendText: vi.fn(),
53
+ sendMedia: feishuOutboundSendMediaMock,
54
+ },
55
+ },
56
+ }));
57
+
58
+ function getDescribedActions(cfg: OpenClawConfig, accountId?: string): string[] {
59
+ return [...(feishuPlugin.actions?.describeMessageTool?.({ cfg, accountId })?.actions ?? [])];
60
+ }
11
61
 
12
62
  describe("feishuPlugin.status.probeAccount", () => {
13
63
  it("uses current account credentials for multi-account config", async () => {
@@ -46,3 +96,864 @@ describe("feishuPlugin.status.probeAccount", () => {
46
96
  expect(result).toMatchObject({ ok: true, appId: "cli_main" });
47
97
  });
48
98
  });
99
+
100
+ describe("feishuPlugin.pairing.notifyApproval", () => {
101
+ beforeEach(() => {
102
+ sendMessageFeishuMock.mockReset();
103
+ sendMessageFeishuMock.mockResolvedValue({ messageId: "pairing-msg", chatId: "ou_user" });
104
+ });
105
+
106
+ it("preserves accountId when sending pairing approvals", async () => {
107
+ const cfg = {
108
+ channels: {
109
+ feishu: {
110
+ accounts: {
111
+ work: {
112
+ appId: "cli_work",
113
+ appSecret: "secret_work",
114
+ enabled: true,
115
+ },
116
+ },
117
+ },
118
+ },
119
+ } as OpenClawConfig;
120
+
121
+ await feishuPlugin.pairing?.notifyApproval?.({
122
+ cfg,
123
+ id: "ou_user",
124
+ accountId: "work",
125
+ });
126
+
127
+ expect(sendMessageFeishuMock).toHaveBeenCalledWith(
128
+ expect.objectContaining({
129
+ cfg,
130
+ to: "ou_user",
131
+ accountId: "work",
132
+ }),
133
+ );
134
+ });
135
+ });
136
+
137
+ describe("feishuPlugin messaging", () => {
138
+ it("owns sender/topic session inheritance candidates", () => {
139
+ expect(
140
+ feishuPlugin.messaging?.resolveSessionConversation?.({
141
+ kind: "group",
142
+ rawId: "oc_group_chat:topic:om_topic_root:sender:ou_topic_user",
143
+ }),
144
+ ).toEqual({
145
+ id: "oc_group_chat:topic:om_topic_root:sender:ou_topic_user",
146
+ baseConversationId: "oc_group_chat",
147
+ parentConversationCandidates: ["oc_group_chat:topic:om_topic_root", "oc_group_chat"],
148
+ });
149
+ expect(
150
+ feishuPlugin.messaging?.resolveSessionConversation?.({
151
+ kind: "group",
152
+ rawId: "oc_group_chat:topic:om_topic_root",
153
+ }),
154
+ ).toEqual({
155
+ id: "oc_group_chat:topic:om_topic_root",
156
+ baseConversationId: "oc_group_chat",
157
+ parentConversationCandidates: ["oc_group_chat"],
158
+ });
159
+ expect(
160
+ feishuPlugin.messaging?.resolveSessionConversation?.({
161
+ kind: "group",
162
+ rawId: "oc_group_chat:Topic:om_topic_root:Sender:ou_topic_user",
163
+ }),
164
+ ).toEqual({
165
+ id: "oc_group_chat:topic:om_topic_root:sender:ou_topic_user",
166
+ baseConversationId: "oc_group_chat",
167
+ parentConversationCandidates: ["oc_group_chat:topic:om_topic_root", "oc_group_chat"],
168
+ });
169
+ });
170
+ });
171
+
172
+ describe("feishuPlugin actions", () => {
173
+ const cfg = {
174
+ channels: {
175
+ feishu: {
176
+ enabled: true,
177
+ appId: "cli_main",
178
+ appSecret: "secret_main",
179
+ actions: {
180
+ reactions: true,
181
+ },
182
+ },
183
+ },
184
+ } as OpenClawConfig;
185
+
186
+ beforeEach(() => {
187
+ vi.clearAllMocks();
188
+ createFeishuClientMock.mockReturnValue({ tag: "client" });
189
+ });
190
+
191
+ it("advertises the expanded Feishu action surface", () => {
192
+ expect(getDescribedActions(cfg)).toEqual([
193
+ "send",
194
+ "read",
195
+ "edit",
196
+ "thread-reply",
197
+ "pin",
198
+ "list-pins",
199
+ "unpin",
200
+ "member-info",
201
+ "channel-info",
202
+ "channel-list",
203
+ "react",
204
+ "reactions",
205
+ ]);
206
+ });
207
+
208
+ it("does not advertise reactions when disabled via actions config", () => {
209
+ const disabledCfg = {
210
+ channels: {
211
+ feishu: {
212
+ enabled: true,
213
+ appId: "cli_main",
214
+ appSecret: "secret_main",
215
+ actions: {
216
+ reactions: false,
217
+ },
218
+ },
219
+ },
220
+ } as OpenClawConfig;
221
+
222
+ expect(getDescribedActions(disabledCfg)).toEqual([
223
+ "send",
224
+ "read",
225
+ "edit",
226
+ "thread-reply",
227
+ "pin",
228
+ "list-pins",
229
+ "unpin",
230
+ "member-info",
231
+ "channel-info",
232
+ "channel-list",
233
+ ]);
234
+ });
235
+
236
+ it("honors the selected Feishu account during discovery", () => {
237
+ const cfg = {
238
+ channels: {
239
+ feishu: {
240
+ enabled: true,
241
+ actions: { reactions: false },
242
+ accounts: {
243
+ default: {
244
+ enabled: true,
245
+ appId: "cli_main",
246
+ appSecret: "secret_main",
247
+ actions: { reactions: false },
248
+ },
249
+ work: {
250
+ enabled: true,
251
+ appId: "cli_work",
252
+ appSecret: "secret_work",
253
+ actions: { reactions: true },
254
+ },
255
+ },
256
+ },
257
+ },
258
+ } as OpenClawConfig;
259
+
260
+ expect(getDescribedActions(cfg, "default")).toEqual([
261
+ "send",
262
+ "read",
263
+ "edit",
264
+ "thread-reply",
265
+ "pin",
266
+ "list-pins",
267
+ "unpin",
268
+ "member-info",
269
+ "channel-info",
270
+ "channel-list",
271
+ ]);
272
+ expect(getDescribedActions(cfg, "work")).toEqual([
273
+ "send",
274
+ "read",
275
+ "edit",
276
+ "thread-reply",
277
+ "pin",
278
+ "list-pins",
279
+ "unpin",
280
+ "member-info",
281
+ "channel-info",
282
+ "channel-list",
283
+ "react",
284
+ "reactions",
285
+ ]);
286
+ });
287
+
288
+ it("sends text messages", async () => {
289
+ sendMessageFeishuMock.mockResolvedValueOnce({ messageId: "om_sent", chatId: "oc_group_1" });
290
+
291
+ const result = await feishuPlugin.actions?.handleAction?.({
292
+ action: "send",
293
+ params: { to: "chat:oc_group_1", message: "hello" },
294
+ cfg,
295
+ accountId: undefined,
296
+ toolContext: {},
297
+ } as never);
298
+
299
+ expect(sendMessageFeishuMock).toHaveBeenCalledWith({
300
+ cfg,
301
+ to: "chat:oc_group_1",
302
+ text: "hello",
303
+ accountId: undefined,
304
+ replyToMessageId: undefined,
305
+ replyInThread: false,
306
+ });
307
+ expect(result?.details).toMatchObject({ ok: true, messageId: "om_sent", chatId: "oc_group_1" });
308
+ });
309
+
310
+ it("renders presentation messages as cards", async () => {
311
+ sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
312
+
313
+ const result = await feishuPlugin.actions?.handleAction?.({
314
+ action: "send",
315
+ params: {
316
+ to: "chat:oc_group_1",
317
+ presentation: {
318
+ title: "Status",
319
+ blocks: [{ type: "text", text: "Build completed" }],
320
+ },
321
+ },
322
+ cfg,
323
+ accountId: undefined,
324
+ toolContext: {},
325
+ } as never);
326
+
327
+ expect(sendCardFeishuMock).toHaveBeenCalledWith({
328
+ cfg,
329
+ to: "chat:oc_group_1",
330
+ card: expect.objectContaining({
331
+ schema: "2.0",
332
+ header: {
333
+ title: { tag: "plain_text", content: "Status" },
334
+ template: "blue",
335
+ },
336
+ body: {
337
+ elements: [
338
+ {
339
+ tag: "markdown",
340
+ content: "Build completed",
341
+ },
342
+ ],
343
+ },
344
+ }),
345
+ accountId: undefined,
346
+ replyToMessageId: undefined,
347
+ replyInThread: false,
348
+ });
349
+ expect(result?.details).toMatchObject({ ok: true, messageId: "om_card", chatId: "oc_group_1" });
350
+ });
351
+
352
+ it("renders presentation button labels into the card fallback", async () => {
353
+ sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
354
+
355
+ await feishuPlugin.actions?.handleAction?.({
356
+ action: "send",
357
+ params: {
358
+ to: "chat:oc_group_1",
359
+ presentation: {
360
+ blocks: [
361
+ {
362
+ type: "buttons",
363
+ buttons: [{ label: "Run help", value: "feishu.quick_actions.help" }],
364
+ },
365
+ ],
366
+ },
367
+ },
368
+ cfg,
369
+ accountId: undefined,
370
+ toolContext: {},
371
+ } as never);
372
+
373
+ expect(sendCardFeishuMock).toHaveBeenCalledWith(
374
+ expect.objectContaining({
375
+ card: expect.objectContaining({
376
+ body: {
377
+ elements: [
378
+ {
379
+ tag: "markdown",
380
+ content: "- Run help",
381
+ },
382
+ ],
383
+ },
384
+ }),
385
+ }),
386
+ );
387
+ });
388
+
389
+ it("renders presentation select labels into the card fallback", async () => {
390
+ sendCardFeishuMock.mockResolvedValueOnce({ messageId: "om_card", chatId: "oc_group_1" });
391
+
392
+ await feishuPlugin.actions?.handleAction?.({
393
+ action: "send",
394
+ params: {
395
+ to: "chat:oc_group_1",
396
+ presentation: {
397
+ blocks: [
398
+ {
399
+ type: "select",
400
+ placeholder: "Pick one",
401
+ options: [{ label: "Option A", value: "a" }],
402
+ },
403
+ ],
404
+ },
405
+ },
406
+ cfg,
407
+ accountId: undefined,
408
+ toolContext: {},
409
+ } as never);
410
+
411
+ expect(sendCardFeishuMock).toHaveBeenCalledWith(
412
+ expect.objectContaining({
413
+ card: expect.objectContaining({
414
+ body: {
415
+ elements: [
416
+ {
417
+ tag: "markdown",
418
+ content: "Pick one:\n- Option A",
419
+ },
420
+ ],
421
+ },
422
+ }),
423
+ }),
424
+ );
425
+ });
426
+
427
+ it("sends media through the outbound adapter", async () => {
428
+ feishuOutboundSendMediaMock.mockResolvedValueOnce({
429
+ channel: "feishu",
430
+ messageId: "om_media",
431
+ details: { messageId: "om_media", chatId: "oc_group_1" },
432
+ });
433
+
434
+ const result = await feishuPlugin.actions?.handleAction?.({
435
+ action: "send",
436
+ params: {
437
+ to: "chat:oc_group_1",
438
+ message: "test",
439
+ media: "/tmp/image.png",
440
+ },
441
+ cfg,
442
+ accountId: undefined,
443
+ toolContext: {},
444
+ mediaLocalRoots: ["/tmp"],
445
+ } as never);
446
+
447
+ expect(feishuOutboundSendMediaMock).toHaveBeenCalledWith({
448
+ cfg,
449
+ to: "chat:oc_group_1",
450
+ text: "test",
451
+ mediaUrl: "/tmp/image.png",
452
+ accountId: undefined,
453
+ mediaLocalRoots: ["/tmp"],
454
+ replyToId: undefined,
455
+ });
456
+ expect(result?.details).toMatchObject({ messageId: "om_media" });
457
+ });
458
+
459
+ it("passes asVoice through media sends", async () => {
460
+ feishuOutboundSendMediaMock.mockResolvedValueOnce({
461
+ channel: "feishu",
462
+ messageId: "om_voice",
463
+ details: { messageId: "om_voice", chatId: "oc_group_1" },
464
+ });
465
+
466
+ await feishuPlugin.actions?.handleAction?.({
467
+ action: "send",
468
+ params: {
469
+ to: "chat:oc_group_1",
470
+ media: "https://example.com/reply.mp3",
471
+ asVoice: true,
472
+ },
473
+ cfg,
474
+ accountId: undefined,
475
+ toolContext: {},
476
+ mediaLocalRoots: [],
477
+ } as never);
478
+
479
+ expect(feishuOutboundSendMediaMock).toHaveBeenCalledWith(
480
+ expect.objectContaining({
481
+ mediaUrl: "https://example.com/reply.mp3",
482
+ audioAsVoice: true,
483
+ }),
484
+ );
485
+ });
486
+
487
+ it("reads messages", async () => {
488
+ getMessageFeishuMock.mockResolvedValueOnce({
489
+ messageId: "om_1",
490
+ content: "hello",
491
+ contentType: "text",
492
+ });
493
+
494
+ const result = await feishuPlugin.actions?.handleAction?.({
495
+ action: "read",
496
+ params: { messageId: "om_1" },
497
+ cfg,
498
+ accountId: undefined,
499
+ } as never);
500
+
501
+ expect(getMessageFeishuMock).toHaveBeenCalledWith({
502
+ cfg,
503
+ messageId: "om_1",
504
+ accountId: undefined,
505
+ });
506
+ expect(result?.details).toMatchObject({
507
+ ok: true,
508
+ message: expect.objectContaining({ messageId: "om_1", content: "hello" }),
509
+ });
510
+ });
511
+
512
+ it("returns an error result when message reads fail", async () => {
513
+ getMessageFeishuMock.mockResolvedValueOnce(null);
514
+
515
+ const result = await feishuPlugin.actions?.handleAction?.({
516
+ action: "read",
517
+ params: { messageId: "om_missing" },
518
+ cfg,
519
+ accountId: undefined,
520
+ } as never);
521
+
522
+ expect((result as { isError?: boolean } | undefined)?.isError).toBe(true);
523
+ expect(result?.details).toEqual({
524
+ error: "Feishu read failed or message not found: om_missing",
525
+ });
526
+ });
527
+
528
+ it("edits messages", async () => {
529
+ editMessageFeishuMock.mockResolvedValueOnce({ messageId: "om_2", contentType: "post" });
530
+
531
+ const result = await feishuPlugin.actions?.handleAction?.({
532
+ action: "edit",
533
+ params: { messageId: "om_2", text: "updated" },
534
+ cfg,
535
+ accountId: undefined,
536
+ } as never);
537
+
538
+ expect(editMessageFeishuMock).toHaveBeenCalledWith({
539
+ cfg,
540
+ messageId: "om_2",
541
+ text: "updated",
542
+ card: undefined,
543
+ accountId: undefined,
544
+ });
545
+ expect(result?.details).toMatchObject({ ok: true, messageId: "om_2", contentType: "post" });
546
+ });
547
+
548
+ it("sends explicit thread replies with reply_in_thread semantics", async () => {
549
+ sendMessageFeishuMock.mockResolvedValueOnce({ messageId: "om_reply", chatId: "oc_group_1" });
550
+
551
+ const result = await feishuPlugin.actions?.handleAction?.({
552
+ action: "thread-reply",
553
+ params: { to: "chat:oc_group_1", messageId: "om_parent", text: "reply body" },
554
+ cfg,
555
+ accountId: undefined,
556
+ toolContext: {},
557
+ } as never);
558
+
559
+ expect(sendMessageFeishuMock).toHaveBeenCalledWith({
560
+ cfg,
561
+ to: "chat:oc_group_1",
562
+ text: "reply body",
563
+ accountId: undefined,
564
+ replyToMessageId: "om_parent",
565
+ replyInThread: true,
566
+ });
567
+ expect(result?.details).toMatchObject({
568
+ ok: true,
569
+ action: "thread-reply",
570
+ messageId: "om_reply",
571
+ });
572
+ });
573
+
574
+ it("creates pins", async () => {
575
+ createPinFeishuMock.mockResolvedValueOnce({ messageId: "om_pin", chatId: "oc_group_1" });
576
+
577
+ const result = await feishuPlugin.actions?.handleAction?.({
578
+ action: "pin",
579
+ params: { messageId: "om_pin" },
580
+ cfg,
581
+ accountId: undefined,
582
+ } as never);
583
+
584
+ expect(createPinFeishuMock).toHaveBeenCalledWith({
585
+ cfg,
586
+ messageId: "om_pin",
587
+ accountId: undefined,
588
+ });
589
+ expect(result?.details).toMatchObject({
590
+ ok: true,
591
+ pin: expect.objectContaining({ messageId: "om_pin" }),
592
+ });
593
+ });
594
+
595
+ it("lists pins", async () => {
596
+ listPinsFeishuMock.mockResolvedValueOnce({
597
+ chatId: "oc_group_1",
598
+ pins: [{ messageId: "om_pin" }],
599
+ hasMore: false,
600
+ pageToken: undefined,
601
+ });
602
+
603
+ const result = await feishuPlugin.actions?.handleAction?.({
604
+ action: "list-pins",
605
+ params: { chatId: "oc_group_1" },
606
+ cfg,
607
+ accountId: undefined,
608
+ toolContext: {},
609
+ } as never);
610
+
611
+ expect(listPinsFeishuMock).toHaveBeenCalledWith({
612
+ cfg,
613
+ chatId: "oc_group_1",
614
+ startTime: undefined,
615
+ endTime: undefined,
616
+ pageSize: undefined,
617
+ pageToken: undefined,
618
+ accountId: undefined,
619
+ });
620
+ expect(result?.details).toMatchObject({
621
+ ok: true,
622
+ pins: [expect.objectContaining({ messageId: "om_pin" })],
623
+ });
624
+ });
625
+
626
+ it("removes pins", async () => {
627
+ const result = await feishuPlugin.actions?.handleAction?.({
628
+ action: "unpin",
629
+ params: { messageId: "om_pin" },
630
+ cfg,
631
+ accountId: undefined,
632
+ } as never);
633
+
634
+ expect(removePinFeishuMock).toHaveBeenCalledWith({
635
+ cfg,
636
+ messageId: "om_pin",
637
+ accountId: undefined,
638
+ });
639
+ expect(result?.details).toMatchObject({ ok: true, messageId: "om_pin" });
640
+ });
641
+
642
+ it("fetches channel info", async () => {
643
+ getChatInfoMock.mockResolvedValueOnce({ chat_id: "oc_group_1", name: "Eng" });
644
+
645
+ const result = await feishuPlugin.actions?.handleAction?.({
646
+ action: "channel-info",
647
+ params: { chatId: "oc_group_1" },
648
+ cfg,
649
+ accountId: undefined,
650
+ toolContext: {},
651
+ } as never);
652
+
653
+ expect(createFeishuClientMock).toHaveBeenCalled();
654
+ expect(getChatInfoMock).toHaveBeenCalledWith({ tag: "client" }, "oc_group_1");
655
+ expect(result?.details).toMatchObject({
656
+ ok: true,
657
+ channel: expect.objectContaining({ chat_id: "oc_group_1", name: "Eng" }),
658
+ });
659
+ });
660
+
661
+ it("fetches member lists from a chat", async () => {
662
+ getChatMembersMock.mockResolvedValueOnce({
663
+ chat_id: "oc_group_1",
664
+ members: [{ member_id: "ou_1", name: "Alice" }],
665
+ has_more: false,
666
+ });
667
+
668
+ const result = await feishuPlugin.actions?.handleAction?.({
669
+ action: "member-info",
670
+ params: { chatId: "oc_group_1" },
671
+ cfg,
672
+ accountId: undefined,
673
+ toolContext: {},
674
+ } as never);
675
+
676
+ expect(getChatMembersMock).toHaveBeenCalledWith(
677
+ { tag: "client" },
678
+ "oc_group_1",
679
+ undefined,
680
+ undefined,
681
+ "open_id",
682
+ );
683
+ expect(result?.details).toMatchObject({
684
+ ok: true,
685
+ members: [expect.objectContaining({ member_id: "ou_1", name: "Alice" })],
686
+ });
687
+ });
688
+
689
+ it("fetches individual member info", async () => {
690
+ getFeishuMemberInfoMock.mockResolvedValueOnce({ member_id: "ou_1", name: "Alice" });
691
+
692
+ const result = await feishuPlugin.actions?.handleAction?.({
693
+ action: "member-info",
694
+ params: { memberId: "ou_1" },
695
+ cfg,
696
+ accountId: undefined,
697
+ toolContext: {},
698
+ } as never);
699
+
700
+ expect(getFeishuMemberInfoMock).toHaveBeenCalledWith({ tag: "client" }, "ou_1", "open_id");
701
+ expect(result?.details).toMatchObject({
702
+ ok: true,
703
+ member: expect.objectContaining({ member_id: "ou_1", name: "Alice" }),
704
+ });
705
+ });
706
+
707
+ it("infers user_id lookups from the userId alias", async () => {
708
+ getFeishuMemberInfoMock.mockResolvedValueOnce({ member_id: "u_1", name: "Alice" });
709
+
710
+ await feishuPlugin.actions?.handleAction?.({
711
+ action: "member-info",
712
+ params: { userId: "u_1" },
713
+ cfg,
714
+ accountId: undefined,
715
+ toolContext: {},
716
+ } as never);
717
+
718
+ expect(getFeishuMemberInfoMock).toHaveBeenCalledWith({ tag: "client" }, "u_1", "user_id");
719
+ });
720
+
721
+ it("honors explicit open_id over alias heuristics", async () => {
722
+ getFeishuMemberInfoMock.mockResolvedValueOnce({ member_id: "u_1", name: "Alice" });
723
+
724
+ await feishuPlugin.actions?.handleAction?.({
725
+ action: "member-info",
726
+ params: { userId: "u_1", memberIdType: "open_id" },
727
+ cfg,
728
+ accountId: undefined,
729
+ toolContext: {},
730
+ } as never);
731
+
732
+ expect(getFeishuMemberInfoMock).toHaveBeenCalledWith({ tag: "client" }, "u_1", "open_id");
733
+ });
734
+
735
+ it("lists directory-backed peers and groups", async () => {
736
+ listFeishuDirectoryGroupsLiveMock.mockResolvedValueOnce([{ kind: "group", id: "oc_group_1" }]);
737
+ listFeishuDirectoryPeersLiveMock.mockResolvedValueOnce([{ kind: "user", id: "ou_1" }]);
738
+
739
+ const result = await feishuPlugin.actions?.handleAction?.({
740
+ action: "channel-list",
741
+ params: { query: "eng", limit: 5 },
742
+ cfg,
743
+ accountId: undefined,
744
+ } as never);
745
+
746
+ expect(listFeishuDirectoryGroupsLiveMock).toHaveBeenCalledWith({
747
+ cfg,
748
+ query: "eng",
749
+ limit: 5,
750
+ fallbackToStatic: false,
751
+ accountId: undefined,
752
+ });
753
+ expect(listFeishuDirectoryPeersLiveMock).toHaveBeenCalledWith({
754
+ cfg,
755
+ query: "eng",
756
+ limit: 5,
757
+ fallbackToStatic: false,
758
+ accountId: undefined,
759
+ });
760
+ expect(result?.details).toMatchObject({
761
+ ok: true,
762
+ groups: [expect.objectContaining({ id: "oc_group_1" })],
763
+ peers: [expect.objectContaining({ id: "ou_1" })],
764
+ });
765
+ });
766
+
767
+ it("fails channel-list when live discovery fails", async () => {
768
+ listFeishuDirectoryGroupsLiveMock.mockRejectedValueOnce(new Error("token expired"));
769
+
770
+ await expect(
771
+ feishuPlugin.actions?.handleAction?.({
772
+ action: "channel-list",
773
+ params: { query: "eng", limit: 5, scope: "groups" },
774
+ cfg,
775
+ accountId: undefined,
776
+ } as never),
777
+ ).rejects.toThrow("token expired");
778
+ });
779
+
780
+ it("requires clearAll=true before removing all bot reactions", async () => {
781
+ await expect(
782
+ feishuPlugin.actions?.handleAction?.({
783
+ action: "react",
784
+ params: { messageId: "om_msg1" },
785
+ cfg,
786
+ accountId: undefined,
787
+ } as never),
788
+ ).rejects.toThrow(
789
+ "Emoji is required to add a Feishu reaction. Set clearAll=true to remove all bot reactions.",
790
+ );
791
+ });
792
+
793
+ it("allows explicit clearAll=true when removing all bot reactions", async () => {
794
+ listReactionsFeishuMock.mockResolvedValueOnce([
795
+ { reactionId: "r1", operatorType: "app" },
796
+ { reactionId: "r2", operatorType: "app" },
797
+ ]);
798
+
799
+ const result = await feishuPlugin.actions?.handleAction?.({
800
+ action: "react",
801
+ params: { messageId: "om_msg1", clearAll: true },
802
+ cfg,
803
+ accountId: undefined,
804
+ } as never);
805
+
806
+ expect(listReactionsFeishuMock).toHaveBeenCalledWith({
807
+ cfg,
808
+ messageId: "om_msg1",
809
+ accountId: undefined,
810
+ });
811
+ expect(removeReactionFeishuMock).toHaveBeenCalledTimes(2);
812
+ expect(result?.details).toMatchObject({ ok: true, removed: 2 });
813
+ });
814
+
815
+ it("fails for missing params on supported actions", async () => {
816
+ await expect(
817
+ feishuPlugin.actions?.handleAction?.({
818
+ action: "thread-reply",
819
+ params: { to: "chat:oc_group_1", message: "reply body" },
820
+ cfg,
821
+ accountId: undefined,
822
+ } as never),
823
+ ).rejects.toThrow("Feishu thread-reply requires messageId.");
824
+ });
825
+
826
+ it("sends media-only messages without requiring card", async () => {
827
+ feishuOutboundSendMediaMock.mockResolvedValueOnce({
828
+ channel: "feishu",
829
+ messageId: "om_media_only",
830
+ details: { messageId: "om_media_only", chatId: "oc_group_1" },
831
+ });
832
+
833
+ const result = await feishuPlugin.actions?.handleAction?.({
834
+ action: "send",
835
+ params: {
836
+ to: "chat:oc_group_1",
837
+ media: "https://example.com/image.png",
838
+ },
839
+ cfg,
840
+ accountId: undefined,
841
+ toolContext: {},
842
+ mediaLocalRoots: [],
843
+ } as never);
844
+
845
+ expect(feishuOutboundSendMediaMock).toHaveBeenCalledWith(
846
+ expect.objectContaining({
847
+ to: "chat:oc_group_1",
848
+ mediaUrl: "https://example.com/image.png",
849
+ }),
850
+ );
851
+ expect(result?.details).toMatchObject({ messageId: "om_media_only" });
852
+ });
853
+
854
+ it("fails for unsupported action names", async () => {
855
+ await expect(
856
+ feishuPlugin.actions?.handleAction?.({
857
+ action: "search",
858
+ params: {},
859
+ cfg,
860
+ accountId: undefined,
861
+ } as never),
862
+ ).rejects.toThrow('Unsupported Feishu action: "search"');
863
+ });
864
+ });
865
+
866
+ describe("resolveReceiveIdType", () => {
867
+ it("resolves chat IDs by oc_ prefix", () => {
868
+ expect(resolveReceiveIdType("oc_123")).toBe("chat_id");
869
+ });
870
+
871
+ it("resolves open IDs by ou_ prefix", () => {
872
+ expect(resolveReceiveIdType("ou_123")).toBe("open_id");
873
+ });
874
+
875
+ it("defaults unprefixed IDs to user_id", () => {
876
+ expect(resolveReceiveIdType("u_123")).toBe("user_id");
877
+ });
878
+
879
+ it("treats explicit group targets as chat_id", () => {
880
+ expect(resolveReceiveIdType("group:oc_123")).toBe("chat_id");
881
+ });
882
+
883
+ it("treats explicit channel targets as chat_id", () => {
884
+ expect(resolveReceiveIdType("channel:oc_123")).toBe("chat_id");
885
+ });
886
+
887
+ it("treats dm-prefixed open IDs as open_id", () => {
888
+ expect(resolveReceiveIdType("dm:ou_123")).toBe("open_id");
889
+ });
890
+ });
891
+
892
+ describe("normalizeFeishuTarget", () => {
893
+ it("strips provider and user prefixes", () => {
894
+ expect(normalizeFeishuTarget("feishu:user:ou_123")).toBe("ou_123");
895
+ expect(normalizeFeishuTarget("lark:user:ou_123")).toBe("ou_123");
896
+ });
897
+
898
+ it("strips provider and chat prefixes", () => {
899
+ expect(normalizeFeishuTarget("feishu:chat:oc_123")).toBe("oc_123");
900
+ });
901
+
902
+ it("normalizes group/channel prefixes to chat ids", () => {
903
+ expect(normalizeFeishuTarget("group:oc_123")).toBe("oc_123");
904
+ expect(normalizeFeishuTarget("feishu:group:oc_123")).toBe("oc_123");
905
+ expect(normalizeFeishuTarget("channel:oc_456")).toBe("oc_456");
906
+ expect(normalizeFeishuTarget("lark:channel:oc_456")).toBe("oc_456");
907
+ });
908
+
909
+ it("accepts provider-prefixed raw ids", () => {
910
+ expect(normalizeFeishuTarget("feishu:ou_123")).toBe("ou_123");
911
+ });
912
+
913
+ it("strips provider and dm prefixes", () => {
914
+ expect(normalizeFeishuTarget("lark:dm:ou_123")).toBe("ou_123");
915
+ });
916
+ });
917
+
918
+ describe("feishuPlugin.messaging.resolveDeliveryTarget", () => {
919
+ it("routes direct conversations to user targets", () => {
920
+ expect(
921
+ feishuPlugin.messaging?.resolveDeliveryTarget?.({
922
+ conversationId: "ou_123",
923
+ }),
924
+ ).toEqual({ to: "user:ou_123" });
925
+ });
926
+
927
+ it("routes group conversations to chat targets", () => {
928
+ expect(
929
+ feishuPlugin.messaging?.resolveDeliveryTarget?.({
930
+ conversationId: "oc_123",
931
+ }),
932
+ ).toEqual({ to: "chat:oc_123" });
933
+ });
934
+
935
+ it("routes topic conversations to parent chat plus thread id", () => {
936
+ expect(
937
+ feishuPlugin.messaging?.resolveDeliveryTarget?.({
938
+ conversationId: "oc_123:topic:omt_456",
939
+ parentConversationId: "oc_123",
940
+ }),
941
+ ).toEqual({ to: "chat:oc_123", threadId: "omt_456" });
942
+ });
943
+ });
944
+
945
+ describe("looksLikeFeishuId", () => {
946
+ it("accepts provider-prefixed user targets", () => {
947
+ expect(looksLikeFeishuId("feishu:user:ou_123")).toBe(true);
948
+ });
949
+
950
+ it("accepts provider-prefixed chat targets", () => {
951
+ expect(looksLikeFeishuId("lark:chat:oc_123")).toBe(true);
952
+ });
953
+
954
+ it("accepts group/channel targets", () => {
955
+ expect(looksLikeFeishuId("feishu:group:oc_123")).toBe(true);
956
+ expect(looksLikeFeishuId("group:oc_123")).toBe(true);
957
+ expect(looksLikeFeishuId("channel:oc_456")).toBe(true);
958
+ });
959
+ });