@actagent/feishu 2026.6.2

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 (207) hide show
  1. package/README.md +11 -0
  2. package/actagent.plugin.json +224 -0
  3. package/api.ts +33 -0
  4. package/channel-entry.ts +21 -0
  5. package/channel-plugin-api.ts +2 -0
  6. package/contract-api.ts +17 -0
  7. package/index.ts +83 -0
  8. package/legacy-state-migrations-api.ts +2 -0
  9. package/npm-shrinkwrap.json +539 -0
  10. package/package.json +64 -0
  11. package/runtime-api.ts +58 -0
  12. package/runtime-setter-api.ts +3 -0
  13. package/secret-contract-api.ts +6 -0
  14. package/security-contract-api.ts +2 -0
  15. package/session-key-api.ts +2 -0
  16. package/setup-api.ts +4 -0
  17. package/setup-entry.test.ts +33 -0
  18. package/setup-entry.ts +25 -0
  19. package/skills/feishu-doc/SKILL.md +211 -0
  20. package/skills/feishu-doc/references/block-types.md +103 -0
  21. package/skills/feishu-drive/SKILL.md +97 -0
  22. package/skills/feishu-perm/SKILL.md +119 -0
  23. package/skills/feishu-wiki/SKILL.md +113 -0
  24. package/src/accounts.test.ts +481 -0
  25. package/src/accounts.ts +380 -0
  26. package/src/agent-config.ts +22 -0
  27. package/src/app-registration.test.ts +62 -0
  28. package/src/app-registration.ts +355 -0
  29. package/src/approval-auth.test.ts +25 -0
  30. package/src/approval-auth.ts +26 -0
  31. package/src/async.test.ts +68 -0
  32. package/src/async.ts +109 -0
  33. package/src/audio-preflight.runtime.ts +10 -0
  34. package/src/bitable.test.ts +174 -0
  35. package/src/bitable.ts +781 -0
  36. package/src/bot-content.ts +488 -0
  37. package/src/bot-group-name.test.ts +148 -0
  38. package/src/bot-runtime-api.ts +13 -0
  39. package/src/bot-sender-name.test.ts +68 -0
  40. package/src/bot-sender-name.ts +137 -0
  41. package/src/bot.broadcast.test.ts +643 -0
  42. package/src/bot.card-action.test.ts +647 -0
  43. package/src/bot.checkBotMentioned.test.ts +266 -0
  44. package/src/bot.helpers.test.ts +136 -0
  45. package/src/bot.stripBotMention.test.ts +127 -0
  46. package/src/bot.test.ts +3817 -0
  47. package/src/bot.ts +1788 -0
  48. package/src/card-action.ts +515 -0
  49. package/src/card-interaction.test.ts +132 -0
  50. package/src/card-interaction.ts +160 -0
  51. package/src/card-test-helpers.ts +55 -0
  52. package/src/card-ux-approval.ts +66 -0
  53. package/src/card-ux-launcher.test.ts +126 -0
  54. package/src/card-ux-launcher.ts +136 -0
  55. package/src/card-ux-shared.ts +34 -0
  56. package/src/channel-runtime-api.ts +17 -0
  57. package/src/channel.runtime.ts +48 -0
  58. package/src/channel.test.ts +1337 -0
  59. package/src/channel.ts +1401 -0
  60. package/src/chat-schema.ts +30 -0
  61. package/src/chat.test.ts +295 -0
  62. package/src/chat.ts +198 -0
  63. package/src/client-timeout.ts +44 -0
  64. package/src/client.test.ts +463 -0
  65. package/src/client.ts +263 -0
  66. package/src/comment-dispatcher-runtime-api.ts +7 -0
  67. package/src/comment-dispatcher.test.ts +186 -0
  68. package/src/comment-dispatcher.ts +108 -0
  69. package/src/comment-handler-runtime-api.ts +4 -0
  70. package/src/comment-handler.test.ts +588 -0
  71. package/src/comment-handler.ts +304 -0
  72. package/src/comment-reaction.test.ts +139 -0
  73. package/src/comment-reaction.ts +260 -0
  74. package/src/comment-shared.test.ts +184 -0
  75. package/src/comment-shared.ts +405 -0
  76. package/src/comment-target.ts +45 -0
  77. package/src/config-schema.test.ts +327 -0
  78. package/src/config-schema.ts +338 -0
  79. package/src/conversation-id.test.ts +19 -0
  80. package/src/conversation-id.ts +199 -0
  81. package/src/dedup-migrations.test.ts +90 -0
  82. package/src/dedup-migrations.ts +103 -0
  83. package/src/dedup.test.ts +95 -0
  84. package/src/dedup.ts +304 -0
  85. package/src/dedupe-key.ts +68 -0
  86. package/src/directory.static.ts +62 -0
  87. package/src/directory.test.ts +142 -0
  88. package/src/directory.ts +125 -0
  89. package/src/doc-schema.ts +183 -0
  90. package/src/doctor.test.ts +382 -0
  91. package/src/doctor.ts +876 -0
  92. package/src/docx-batch-insert.test.ts +117 -0
  93. package/src/docx-batch-insert.ts +223 -0
  94. package/src/docx-color-text.ts +154 -0
  95. package/src/docx-table-ops.test.ts +54 -0
  96. package/src/docx-table-ops.ts +316 -0
  97. package/src/docx-types.ts +39 -0
  98. package/src/docx.account-selection.test.ts +96 -0
  99. package/src/docx.test.ts +706 -0
  100. package/src/docx.ts +1598 -0
  101. package/src/drive-schema.ts +93 -0
  102. package/src/drive.test.ts +1240 -0
  103. package/src/drive.ts +830 -0
  104. package/src/dynamic-agent.test.ts +156 -0
  105. package/src/dynamic-agent.ts +144 -0
  106. package/src/event-types.ts +46 -0
  107. package/src/external-keys.test.ts +21 -0
  108. package/src/external-keys.ts +20 -0
  109. package/src/lifecycle.test-support.ts +223 -0
  110. package/src/media.test.ts +956 -0
  111. package/src/media.ts +1106 -0
  112. package/src/mention-target.types.ts +6 -0
  113. package/src/mention.ts +115 -0
  114. package/src/message-action-contract.ts +14 -0
  115. package/src/monitor-state-runtime-api.ts +8 -0
  116. package/src/monitor-transport-runtime-api.ts +11 -0
  117. package/src/monitor.account.ts +501 -0
  118. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +215 -0
  119. package/src/monitor.bot-identity.ts +87 -0
  120. package/src/monitor.bot-menu-handler.ts +164 -0
  121. package/src/monitor.bot-menu.lifecycle.test-support.ts +221 -0
  122. package/src/monitor.bot-menu.test.ts +200 -0
  123. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +265 -0
  124. package/src/monitor.card-action.lifecycle.test-support.ts +418 -0
  125. package/src/monitor.cleanup.test.ts +384 -0
  126. package/src/monitor.comment-notice-handler.ts +106 -0
  127. package/src/monitor.comment.test.ts +968 -0
  128. package/src/monitor.comment.ts +1386 -0
  129. package/src/monitor.lifecycle.test.ts +5 -0
  130. package/src/monitor.message-handler.ts +346 -0
  131. package/src/monitor.reaction.test.ts +770 -0
  132. package/src/monitor.startup.test.ts +232 -0
  133. package/src/monitor.startup.ts +76 -0
  134. package/src/monitor.state.defaults.test.ts +47 -0
  135. package/src/monitor.state.ts +171 -0
  136. package/src/monitor.synthetic-error.ts +19 -0
  137. package/src/monitor.test-mocks.ts +47 -0
  138. package/src/monitor.transport.ts +451 -0
  139. package/src/monitor.ts +104 -0
  140. package/src/monitor.webhook-e2e.test.ts +284 -0
  141. package/src/monitor.webhook-security.test.ts +394 -0
  142. package/src/monitor.webhook.test-helpers.ts +138 -0
  143. package/src/outbound-runtime-api.ts +2 -0
  144. package/src/outbound.test.ts +1255 -0
  145. package/src/outbound.ts +742 -0
  146. package/src/perm-schema.ts +53 -0
  147. package/src/perm.ts +171 -0
  148. package/src/pins.ts +109 -0
  149. package/src/policy.test.ts +224 -0
  150. package/src/policy.ts +322 -0
  151. package/src/post.test.ts +106 -0
  152. package/src/post.ts +276 -0
  153. package/src/presentation-card.ts +204 -0
  154. package/src/probe.test.ts +310 -0
  155. package/src/probe.ts +181 -0
  156. package/src/processing-claims.ts +60 -0
  157. package/src/qr-terminal.ts +2 -0
  158. package/src/reactions.ts +124 -0
  159. package/src/reasoning-preview.test.ts +114 -0
  160. package/src/reasoning-preview.ts +29 -0
  161. package/src/reply-dispatcher-runtime-api.ts +8 -0
  162. package/src/reply-dispatcher.test.ts +2009 -0
  163. package/src/reply-dispatcher.ts +865 -0
  164. package/src/runtime.ts +10 -0
  165. package/src/secret-contract.ts +146 -0
  166. package/src/secret-input.ts +2 -0
  167. package/src/security-audit-shared.ts +70 -0
  168. package/src/security-audit.test.ts +60 -0
  169. package/src/security-audit.ts +2 -0
  170. package/src/send-result.ts +81 -0
  171. package/src/send-target.test.ts +87 -0
  172. package/src/send-target.ts +36 -0
  173. package/src/send.reply-fallback.test.ts +418 -0
  174. package/src/send.test.ts +661 -0
  175. package/src/send.ts +860 -0
  176. package/src/sequential-key.test.ts +73 -0
  177. package/src/sequential-key.ts +29 -0
  178. package/src/sequential-queue.test.ts +184 -0
  179. package/src/sequential-queue.ts +90 -0
  180. package/src/session-conversation.ts +42 -0
  181. package/src/session-route.ts +49 -0
  182. package/src/setup-core.ts +52 -0
  183. package/src/setup-surface.test.ts +485 -0
  184. package/src/setup-surface.ts +620 -0
  185. package/src/streaming-card.test.ts +549 -0
  186. package/src/streaming-card.ts +611 -0
  187. package/src/subagent-hooks.test.ts +632 -0
  188. package/src/subagent-hooks.ts +414 -0
  189. package/src/targets.ts +98 -0
  190. package/src/test-support/lifecycle-test-support.ts +459 -0
  191. package/src/thread-bindings.test.ts +181 -0
  192. package/src/thread-bindings.ts +332 -0
  193. package/src/tool-account-routing.test.ts +419 -0
  194. package/src/tool-account.test.ts +45 -0
  195. package/src/tool-account.ts +98 -0
  196. package/src/tool-factory-test-harness.ts +83 -0
  197. package/src/tool-result.test.ts +33 -0
  198. package/src/tool-result.ts +17 -0
  199. package/src/tools-config.test.ts +52 -0
  200. package/src/tools-config.ts +29 -0
  201. package/src/types.ts +111 -0
  202. package/src/typing.test.ts +145 -0
  203. package/src/typing.ts +215 -0
  204. package/src/wiki-schema.ts +70 -0
  205. package/src/wiki.ts +271 -0
  206. package/subagent-hooks-api.ts +22 -0
  207. package/tsconfig.json +16 -0
@@ -0,0 +1,418 @@
1
+ // Feishu plugin module implements monitor.card action.lifecycle support behavior.
2
+ import { createRuntimeEnv } from "actagent/plugin-sdk/plugin-test-runtime";
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
+ import "./lifecycle.test-support.js";
5
+ import { resetProcessedFeishuCardActionTokensForTests } from "./card-action.js";
6
+ import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
7
+ import {
8
+ getFeishuLifecycleTestMocks,
9
+ resetFeishuLifecycleTestMocks,
10
+ } from "./lifecycle.test-support.js";
11
+ import {
12
+ createFeishuLifecycleConfig,
13
+ createFeishuLifecycleReplyDispatcher,
14
+ createResolvedFeishuLifecycleAccount,
15
+ expectFeishuReplyDispatcherSentFinalReplyOnce,
16
+ expectFeishuReplyPipelineDedupedAcrossReplay,
17
+ expectFeishuReplyPipelineDedupedAfterPostSendFailure,
18
+ installFeishuLifecycleReplyRuntime,
19
+ mockFeishuReplyOnceDispatch,
20
+ restoreFeishuLifecycleStateDir,
21
+ setFeishuLifecycleStateDir,
22
+ setupFeishuLifecycleHandler,
23
+ } from "./test-support/lifecycle-test-support.js";
24
+
25
+ const {
26
+ createEventDispatcherMock,
27
+ createFeishuReplyDispatcherMock,
28
+ dispatchReplyFromConfigMock,
29
+ finalizeInboundContextMock,
30
+ resolveAgentRouteMock,
31
+ resolveBoundConversationMock,
32
+ sendCardFeishuMock,
33
+ sendMessageFeishuMock,
34
+ touchBindingMock,
35
+ withReplyDispatcherMock,
36
+ } = getFeishuLifecycleTestMocks();
37
+ let lastRuntime = createRuntimeEnv();
38
+ const originalStateDir = process.env.ACTAGENT_STATE_DIR;
39
+ const lifecycleConfig = createFeishuLifecycleConfig({
40
+ accountId: "acct-card",
41
+ appId: "cli_test",
42
+ appSecret: "secret_test",
43
+ channelConfig: {
44
+ dmPolicy: "open",
45
+ allowFrom: ["ou_user1"],
46
+ },
47
+ accountConfig: {
48
+ dmPolicy: "open",
49
+ allowFrom: ["ou_user1"],
50
+ },
51
+ });
52
+
53
+ const lifecycleAccount = createResolvedFeishuLifecycleAccount({
54
+ accountId: "acct-card",
55
+ appId: "cli_test",
56
+ appSecret: "secret_test",
57
+ config: {
58
+ dmPolicy: "open",
59
+ allowFrom: ["ou_user1"],
60
+ },
61
+ });
62
+
63
+ function createCardActionEvent(params: {
64
+ token: string;
65
+ action: string;
66
+ command: string;
67
+ chatId?: string;
68
+ chatType?: "group" | "p2p";
69
+ }) {
70
+ const openId = "ou_user1";
71
+ const chatId = params.chatId ?? "p2p:ou_user1";
72
+ const chatType = params.chatType ?? "p2p";
73
+ return {
74
+ operator: {
75
+ open_id: openId,
76
+ user_id: "user_1",
77
+ union_id: "union_1",
78
+ },
79
+ token: params.token,
80
+ action: {
81
+ tag: "button",
82
+ value: createFeishuCardInteractionEnvelope({
83
+ k: "quick",
84
+ a: params.action,
85
+ q: params.command,
86
+ c: {
87
+ u: openId,
88
+ h: chatId,
89
+ t: chatType,
90
+ e: Date.now() + 60_000,
91
+ },
92
+ }),
93
+ },
94
+ context: {
95
+ open_id: openId,
96
+ user_id: "user_1",
97
+ chat_id: chatId,
98
+ },
99
+ };
100
+ }
101
+
102
+ async function setupLifecycleMonitor() {
103
+ lastRuntime = createRuntimeEnv();
104
+ return setupFeishuLifecycleHandler({
105
+ createEventDispatcherMock,
106
+ onRegister: () => {},
107
+ runtime: lastRuntime,
108
+ cfg: lifecycleConfig,
109
+ account: lifecycleAccount,
110
+ handlerKey: "card.action.trigger",
111
+ missingHandlerMessage: "missing card.action.trigger handler",
112
+ });
113
+ }
114
+
115
+ function latestReplyDispatcherParams() {
116
+ const call = createFeishuReplyDispatcherMock.mock.calls.at(-1);
117
+ if (!call) {
118
+ throw new Error("expected Feishu reply dispatcher call");
119
+ }
120
+ return call[0] as {
121
+ accountId?: string;
122
+ chatId?: string;
123
+ replyToMessageId?: string;
124
+ };
125
+ }
126
+
127
+ function latestFinalizedContext() {
128
+ const call = finalizeInboundContextMock.mock.calls.at(-1);
129
+ if (!call) {
130
+ throw new Error("expected finalized inbound context call");
131
+ }
132
+ return call[0] as {
133
+ AccountId?: string;
134
+ SessionKey?: string;
135
+ MessageSid?: string;
136
+ };
137
+ }
138
+
139
+ describe("Feishu card-action lifecycle", () => {
140
+ beforeEach(() => {
141
+ vi.useRealTimers();
142
+ resetFeishuLifecycleTestMocks();
143
+ lastRuntime = createRuntimeEnv();
144
+ resetProcessedFeishuCardActionTokensForTests();
145
+ setFeishuLifecycleStateDir("actagent-feishu-card-action");
146
+
147
+ createFeishuReplyDispatcherMock.mockReturnValue(createFeishuLifecycleReplyDispatcher());
148
+
149
+ resolveBoundConversationMock.mockImplementation(() => ({
150
+ bindingId: "binding-card",
151
+ targetSessionKey: "agent:bound-agent:feishu:direct:ou_user1",
152
+ }));
153
+
154
+ resolveAgentRouteMock.mockReturnValue({
155
+ agentId: "main",
156
+ channel: "feishu",
157
+ accountId: "acct-card",
158
+ sessionKey: "agent:main:feishu:direct:ou_user1",
159
+ mainSessionKey: "agent:main:main",
160
+ matchedBy: "default",
161
+ });
162
+
163
+ mockFeishuReplyOnceDispatch({
164
+ dispatchReplyFromConfigMock,
165
+ replyText: "card action reply once",
166
+ });
167
+
168
+ withReplyDispatcherMock.mockImplementation(async ({ run }) => await run());
169
+
170
+ installFeishuLifecycleReplyRuntime({
171
+ resolveAgentRouteMock,
172
+ finalizeInboundContextMock,
173
+ dispatchReplyFromConfigMock,
174
+ withReplyDispatcherMock,
175
+ storePath: "/tmp/feishu-card-action-sessions.json",
176
+ });
177
+ });
178
+
179
+ afterEach(() => {
180
+ vi.useRealTimers();
181
+ resetProcessedFeishuCardActionTokensForTests();
182
+ restoreFeishuLifecycleStateDir(originalStateDir);
183
+ });
184
+
185
+ it("routes one reply across duplicate callback delivery", async () => {
186
+ const onCardAction = await setupLifecycleMonitor();
187
+ const event = createCardActionEvent({
188
+ token: "tok-card-once",
189
+ action: "feishu.quick_actions.help",
190
+ command: "/help",
191
+ });
192
+
193
+ await expectFeishuReplyPipelineDedupedAcrossReplay({
194
+ handler: onCardAction,
195
+ event,
196
+ dispatchReplyFromConfigMock,
197
+ createFeishuReplyDispatcherMock,
198
+ });
199
+
200
+ expect(lastRuntime?.error).not.toHaveBeenCalled();
201
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
202
+ expect(createFeishuReplyDispatcherMock).toHaveBeenCalledTimes(1);
203
+ const dispatcherParams = latestReplyDispatcherParams();
204
+ expect(dispatcherParams.accountId).toBe("acct-card");
205
+ expect(dispatcherParams.chatId).toBe("p2p:ou_user1");
206
+ expect(dispatcherParams.replyToMessageId).toBeUndefined();
207
+ const finalized = latestFinalizedContext();
208
+ expect(finalized.AccountId).toBe("acct-card");
209
+ expect(finalized.SessionKey).toBe("agent:bound-agent:feishu:direct:ou_user1");
210
+ expect(finalized.MessageSid).toBe("card-action-tok-card-once");
211
+ expect(touchBindingMock).toHaveBeenCalledWith("binding-card");
212
+
213
+ expectFeishuReplyDispatcherSentFinalReplyOnce({ createFeishuReplyDispatcherMock });
214
+ expect(sendMessageFeishuMock).not.toHaveBeenCalled();
215
+ expect(sendCardFeishuMock).not.toHaveBeenCalled();
216
+ });
217
+
218
+ it("routes v2 callbacks that report open_chat_id instead of chat_id", async () => {
219
+ const onCardAction = await setupLifecycleMonitor();
220
+ const chatId = "oc_group_v2";
221
+
222
+ await onCardAction({
223
+ operator: {
224
+ open_id: "ou_user1",
225
+ },
226
+ token: "tok-card-v2-context",
227
+ action: {
228
+ tag: "button",
229
+ value: createFeishuCardInteractionEnvelope({
230
+ k: "quick",
231
+ a: "feishu.quick_actions.help",
232
+ q: "/help",
233
+ c: {
234
+ u: "ou_user1",
235
+ h: chatId,
236
+ t: "group",
237
+ e: Date.now() + 60_000,
238
+ },
239
+ }),
240
+ },
241
+ context: {
242
+ open_message_id: "om_card_v2",
243
+ open_chat_id: chatId,
244
+ },
245
+ });
246
+
247
+ expect(lastRuntime?.error).not.toHaveBeenCalled();
248
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
249
+ const dispatcherParams = latestReplyDispatcherParams();
250
+ expect(dispatcherParams.accountId).toBe("acct-card");
251
+ expect(dispatcherParams.chatId).toBe(chatId);
252
+ expect(dispatcherParams.replyToMessageId).toBe("om_card_v2");
253
+ expect(latestFinalizedContext().MessageSid).toBe("card-action-tok-card-v2-context");
254
+ });
255
+
256
+ it("routes v2 callbacks with nested operator identity", async () => {
257
+ const onCardAction = await setupLifecycleMonitor();
258
+ const chatId = "p2p:ou_user1";
259
+
260
+ await onCardAction({
261
+ operator: {
262
+ user_id: {
263
+ open_id: "ou_user1",
264
+ user_id: "user_1",
265
+ union_id: "union_1",
266
+ },
267
+ },
268
+ token: "tok-card-v2-nested-operator",
269
+ action: {
270
+ tag: "button",
271
+ value: createFeishuCardInteractionEnvelope({
272
+ k: "quick",
273
+ a: "feishu.quick_actions.help",
274
+ q: "/help",
275
+ c: {
276
+ u: "ou_user1",
277
+ h: chatId,
278
+ t: "p2p",
279
+ e: Date.now() + 60_000,
280
+ },
281
+ }),
282
+ },
283
+ context: {
284
+ open_message_id: "om_card_v2_nested",
285
+ open_chat_id: chatId,
286
+ },
287
+ });
288
+
289
+ expect(lastRuntime?.error).not.toHaveBeenCalled();
290
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
291
+ expect(createFeishuReplyDispatcherMock).toHaveBeenCalledWith(
292
+ expect.objectContaining({
293
+ accountId: "acct-card",
294
+ chatId,
295
+ replyToMessageId: "om_card_v2_nested",
296
+ }),
297
+ );
298
+ expect(finalizeInboundContextMock).toHaveBeenCalledWith(
299
+ expect.objectContaining({
300
+ AccountId: "acct-card",
301
+ SessionKey: "agent:bound-agent:feishu:direct:ou_user1",
302
+ MessageSid: "card-action-tok-card-v2-nested-operator",
303
+ }),
304
+ undefined,
305
+ );
306
+ });
307
+
308
+ it("routes SDK-style card callbacks without context as direct callbacks", async () => {
309
+ const onCardAction = await setupLifecycleMonitor();
310
+
311
+ await onCardAction({
312
+ open_id: "ou_user1",
313
+ user_id: "user_1",
314
+ tenant_key: "tenant_1",
315
+ open_message_id: "om_sdk_card",
316
+ token: "tok-card-sdk-flat",
317
+ action: {
318
+ tag: "button",
319
+ value: {
320
+ command: "/help",
321
+ },
322
+ },
323
+ });
324
+
325
+ expect(lastRuntime?.error).not.toHaveBeenCalled();
326
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
327
+ const dispatcherParams = latestReplyDispatcherParams();
328
+ expect(dispatcherParams.accountId).toBe("acct-card");
329
+ expect(dispatcherParams.chatId).toBe("ou_user1");
330
+ expect(dispatcherParams.replyToMessageId).toBe("om_sdk_card");
331
+ expect(latestFinalizedContext().MessageSid).toBe("card-action-tok-card-sdk-flat");
332
+ });
333
+
334
+ it("plain-sends card action replies when Feishu provides no real message id", async () => {
335
+ const onCardAction = await setupLifecycleMonitor();
336
+
337
+ await onCardAction({
338
+ open_id: "ou_user1",
339
+ token: "tok-card-no-reply-target",
340
+ action: {
341
+ tag: "button",
342
+ value: {
343
+ command: "/help",
344
+ },
345
+ },
346
+ });
347
+
348
+ expect(lastRuntime?.error).not.toHaveBeenCalled();
349
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
350
+ const dispatcherParams = latestReplyDispatcherParams();
351
+ expect(dispatcherParams.accountId).toBe("acct-card");
352
+ expect(dispatcherParams.chatId).toBe("ou_user1");
353
+ expect(dispatcherParams.replyToMessageId).toBeUndefined();
354
+ expect(latestFinalizedContext().MessageSid).toBe("card-action-tok-card-no-reply-target");
355
+ });
356
+
357
+ it("does not duplicate delivery when retrying after a post-send failure", async () => {
358
+ const onCardAction = await setupLifecycleMonitor();
359
+ const event = createCardActionEvent({
360
+ token: "tok-card-retry",
361
+ action: "feishu.quick_actions.help",
362
+ command: "/help",
363
+ });
364
+
365
+ dispatchReplyFromConfigMock.mockImplementationOnce(async ({ dispatcher }) => {
366
+ await dispatcher.sendFinalReply({ text: "card action reply once" });
367
+ throw new Error("post-send failure");
368
+ });
369
+
370
+ await expectFeishuReplyPipelineDedupedAfterPostSendFailure({
371
+ handler: onCardAction,
372
+ event,
373
+ dispatchReplyFromConfigMock,
374
+ runtimeErrorMock: lastRuntime?.error as ReturnType<typeof vi.fn>,
375
+ });
376
+
377
+ expect(lastRuntime?.error).toHaveBeenCalledTimes(1);
378
+ expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(1);
379
+ expectFeishuReplyDispatcherSentFinalReplyOnce({ createFeishuReplyDispatcherMock });
380
+ });
381
+
382
+ it("drops malformed card-action events with empty tokens before handler dispatch", async () => {
383
+ const onCardAction = await setupLifecycleMonitor();
384
+
385
+ await onCardAction({
386
+ operator: {
387
+ open_id: "ou_user1",
388
+ user_id: "user_1",
389
+ union_id: "union_1",
390
+ },
391
+ token: "",
392
+ action: {
393
+ tag: "button",
394
+ value: createFeishuCardInteractionEnvelope({
395
+ k: "quick",
396
+ a: "feishu.quick_actions.help",
397
+ q: "/help",
398
+ c: {
399
+ u: "ou_user1",
400
+ h: "p2p:ou_user1",
401
+ t: "p2p",
402
+ e: Date.now() + 60_000,
403
+ },
404
+ }),
405
+ },
406
+ context: {
407
+ open_id: "ou_user1",
408
+ user_id: "user_1",
409
+ chat_id: "p2p:ou_user1",
410
+ },
411
+ });
412
+
413
+ expect(lastRuntime?.error).toHaveBeenCalledWith(
414
+ "feishu[acct-card]: ignoring malformed card action payload",
415
+ );
416
+ expect(dispatchReplyFromConfigMock).not.toHaveBeenCalled();
417
+ });
418
+ });