@kodelyth/feishu 2026.5.42 → 2026.6.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 (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +17 -4
  3. package/api.ts +0 -32
  4. package/channel-entry.ts +0 -20
  5. package/channel-plugin-api.ts +0 -1
  6. package/contract-api.ts +0 -16
  7. package/index.ts +0 -82
  8. package/runtime-api.ts +0 -52
  9. package/secret-contract-api.ts +0 -5
  10. package/security-contract-api.ts +0 -1
  11. package/session-key-api.ts +0 -1
  12. package/setup-api.ts +0 -3
  13. package/setup-entry.test.ts +0 -19
  14. package/setup-entry.ts +0 -13
  15. package/src/accounts.test.ts +0 -480
  16. package/src/accounts.ts +0 -333
  17. package/src/agent-config.ts +0 -21
  18. package/src/app-registration.ts +0 -331
  19. package/src/approval-auth.test.ts +0 -24
  20. package/src/approval-auth.ts +0 -25
  21. package/src/async.test.ts +0 -35
  22. package/src/async.ts +0 -104
  23. package/src/audio-preflight.runtime.ts +0 -9
  24. package/src/bitable.test.ts +0 -136
  25. package/src/bitable.ts +0 -762
  26. package/src/bot-content.ts +0 -485
  27. package/src/bot-group-name.test.ts +0 -116
  28. package/src/bot-runtime-api.ts +0 -12
  29. package/src/bot-sender-name.ts +0 -125
  30. package/src/bot.broadcast.test.ts +0 -523
  31. package/src/bot.card-action.test.ts +0 -552
  32. package/src/bot.checkBotMentioned.test.ts +0 -265
  33. package/src/bot.helpers.test.ts +0 -135
  34. package/src/bot.stripBotMention.test.ts +0 -126
  35. package/src/bot.test.ts +0 -3671
  36. package/src/bot.ts +0 -1703
  37. package/src/card-action.ts +0 -447
  38. package/src/card-interaction.test.ts +0 -131
  39. package/src/card-interaction.ts +0 -159
  40. package/src/card-test-helpers.ts +0 -54
  41. package/src/card-ux-approval.ts +0 -65
  42. package/src/card-ux-launcher.test.ts +0 -106
  43. package/src/card-ux-launcher.ts +0 -121
  44. package/src/card-ux-shared.ts +0 -33
  45. package/src/channel-runtime-api.ts +0 -16
  46. package/src/channel.runtime.ts +0 -47
  47. package/src/channel.test.ts +0 -1151
  48. package/src/channel.ts +0 -1423
  49. package/src/chat-schema.ts +0 -25
  50. package/src/chat.test.ts +0 -240
  51. package/src/chat.ts +0 -188
  52. package/src/client-timeout.ts +0 -42
  53. package/src/client.test.ts +0 -447
  54. package/src/client.ts +0 -262
  55. package/src/comment-dispatcher-runtime-api.ts +0 -6
  56. package/src/comment-dispatcher.test.ts +0 -185
  57. package/src/comment-dispatcher.ts +0 -107
  58. package/src/comment-handler-runtime-api.ts +0 -3
  59. package/src/comment-handler.test.ts +0 -592
  60. package/src/comment-handler.ts +0 -303
  61. package/src/comment-reaction.test.ts +0 -138
  62. package/src/comment-reaction.ts +0 -259
  63. package/src/comment-shared.test.ts +0 -183
  64. package/src/comment-shared.ts +0 -406
  65. package/src/comment-target.ts +0 -44
  66. package/src/config-schema.test.ts +0 -326
  67. package/src/config-schema.ts +0 -335
  68. package/src/conversation-id.test.ts +0 -18
  69. package/src/conversation-id.ts +0 -199
  70. package/src/dedup-runtime-api.ts +0 -1
  71. package/src/dedup.ts +0 -141
  72. package/src/dedupe-key.ts +0 -72
  73. package/src/directory.static.ts +0 -61
  74. package/src/directory.test.ts +0 -141
  75. package/src/directory.ts +0 -124
  76. package/src/doc-schema.ts +0 -182
  77. package/src/docx-batch-insert.test.ts +0 -116
  78. package/src/docx-batch-insert.ts +0 -223
  79. package/src/docx-color-text.ts +0 -154
  80. package/src/docx-table-ops.test.ts +0 -53
  81. package/src/docx-table-ops.ts +0 -316
  82. package/src/docx-types.ts +0 -38
  83. package/src/docx.account-selection.test.ts +0 -95
  84. package/src/docx.test.ts +0 -701
  85. package/src/docx.ts +0 -1596
  86. package/src/drive-schema.ts +0 -92
  87. package/src/drive.test.ts +0 -1237
  88. package/src/drive.ts +0 -829
  89. package/src/dynamic-agent.test.ts +0 -155
  90. package/src/dynamic-agent.ts +0 -143
  91. package/src/event-types.ts +0 -45
  92. package/src/external-keys.test.ts +0 -20
  93. package/src/external-keys.ts +0 -19
  94. package/src/lifecycle.test-support.ts +0 -220
  95. package/src/media.test.ts +0 -955
  96. package/src/media.ts +0 -1105
  97. package/src/mention-target.types.ts +0 -5
  98. package/src/mention.ts +0 -114
  99. package/src/message-action-contract.ts +0 -13
  100. package/src/monitor-state-runtime-api.ts +0 -7
  101. package/src/monitor-transport-runtime-api.ts +0 -10
  102. package/src/monitor.account.ts +0 -492
  103. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +0 -219
  104. package/src/monitor.bot-identity.ts +0 -86
  105. package/src/monitor.bot-menu-handler.ts +0 -165
  106. package/src/monitor.bot-menu.lifecycle.test-support.ts +0 -224
  107. package/src/monitor.bot-menu.test.ts +0 -188
  108. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +0 -264
  109. package/src/monitor.card-action.lifecycle.test-support.ts +0 -421
  110. package/src/monitor.cleanup.test.ts +0 -383
  111. package/src/monitor.comment-notice-handler.ts +0 -105
  112. package/src/monitor.comment.test.ts +0 -967
  113. package/src/monitor.comment.ts +0 -1386
  114. package/src/monitor.lifecycle.test.ts +0 -4
  115. package/src/monitor.message-handler.ts +0 -350
  116. package/src/monitor.reaction.lifecycle.test-support.ts +0 -68
  117. package/src/monitor.reaction.test.ts +0 -739
  118. package/src/monitor.startup.test.ts +0 -213
  119. package/src/monitor.startup.ts +0 -74
  120. package/src/monitor.state.defaults.test.ts +0 -46
  121. package/src/monitor.state.ts +0 -170
  122. package/src/monitor.synthetic-error.ts +0 -18
  123. package/src/monitor.test-mocks.ts +0 -46
  124. package/src/monitor.transport.ts +0 -451
  125. package/src/monitor.ts +0 -100
  126. package/src/monitor.webhook-e2e.test.ts +0 -279
  127. package/src/monitor.webhook-security.test.ts +0 -389
  128. package/src/monitor.webhook.test-helpers.ts +0 -116
  129. package/src/outbound-runtime-api.ts +0 -1
  130. package/src/outbound.test.ts +0 -1118
  131. package/src/outbound.ts +0 -785
  132. package/src/perm-schema.ts +0 -52
  133. package/src/perm.ts +0 -170
  134. package/src/pins.ts +0 -108
  135. package/src/policy.test.ts +0 -223
  136. package/src/policy.ts +0 -318
  137. package/src/post.test.ts +0 -105
  138. package/src/post.ts +0 -275
  139. package/src/probe.test.ts +0 -283
  140. package/src/probe.ts +0 -166
  141. package/src/processing-claims.ts +0 -59
  142. package/src/qr-terminal.ts +0 -1
  143. package/src/reactions.ts +0 -123
  144. package/src/reasoning-preview.test.ts +0 -113
  145. package/src/reasoning-preview.ts +0 -28
  146. package/src/reply-dispatcher-runtime-api.ts +0 -7
  147. package/src/reply-dispatcher.test.ts +0 -1513
  148. package/src/reply-dispatcher.ts +0 -748
  149. package/src/runtime.ts +0 -9
  150. package/src/secret-contract.ts +0 -145
  151. package/src/secret-input.ts +0 -1
  152. package/src/security-audit-shared.ts +0 -69
  153. package/src/security-audit.test.ts +0 -59
  154. package/src/security-audit.ts +0 -1
  155. package/src/send-result.ts +0 -80
  156. package/src/send-target.test.ts +0 -86
  157. package/src/send-target.ts +0 -35
  158. package/src/send.reply-fallback.test.ts +0 -417
  159. package/src/send.test.ts +0 -621
  160. package/src/send.ts +0 -861
  161. package/src/sequential-key.test.ts +0 -72
  162. package/src/sequential-key.ts +0 -25
  163. package/src/sequential-queue.test.ts +0 -165
  164. package/src/sequential-queue.ts +0 -86
  165. package/src/session-conversation.ts +0 -42
  166. package/src/session-route.ts +0 -48
  167. package/src/setup-core.ts +0 -51
  168. package/src/setup-surface.test.ts +0 -484
  169. package/src/setup-surface.ts +0 -618
  170. package/src/streaming-card.test.ts +0 -397
  171. package/src/streaming-card.ts +0 -571
  172. package/src/subagent-hooks.test.ts +0 -627
  173. package/src/subagent-hooks.ts +0 -413
  174. package/src/targets.ts +0 -97
  175. package/src/test-support/lifecycle-test-support.ts +0 -454
  176. package/src/thread-bindings.test.ts +0 -180
  177. package/src/thread-bindings.ts +0 -331
  178. package/src/tool-account-routing.test.ts +0 -250
  179. package/src/tool-account.test.ts +0 -44
  180. package/src/tool-account.ts +0 -93
  181. package/src/tool-factory-test-harness.ts +0 -79
  182. package/src/tool-result.test.ts +0 -32
  183. package/src/tool-result.ts +0 -16
  184. package/src/tools-config.test.ts +0 -21
  185. package/src/tools-config.ts +0 -22
  186. package/src/types.ts +0 -106
  187. package/src/typing.test.ts +0 -144
  188. package/src/typing.ts +0 -214
  189. package/src/wiki-schema.ts +0 -69
  190. package/src/wiki.ts +0 -270
  191. package/subagent-hooks-api.ts +0 -31
  192. package/tsconfig.json +0 -16
@@ -1,1118 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { verifyChannelMessageAdapterCapabilityProofs } from "klaw/plugin-sdk/channel-message";
5
- import type { MessagePresentation } from "klaw/plugin-sdk/interactive-runtime";
6
- import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
7
- import type { ClawdbotConfig } from "../runtime-api.js";
8
-
9
- const sendMediaFeishuMock = vi.hoisted(() => vi.fn());
10
- const sendMessageFeishuMock = vi.hoisted(() => vi.fn());
11
- const sendCardFeishuMock = vi.hoisted(() => vi.fn());
12
- const sendMarkdownCardFeishuMock = vi.hoisted(() => vi.fn());
13
- const sendStructuredCardFeishuMock = vi.hoisted(() => vi.fn());
14
- const deliverCommentThreadTextMock = vi.hoisted(() => vi.fn());
15
- const cleanupAmbientCommentTypingReactionMock = vi.hoisted(() => vi.fn(async () => false));
16
- const shouldSuppressFeishuTextForVoiceMediaMock = vi.hoisted(
17
- () => (params: { mediaUrl?: string; audioAsVoice?: boolean }) =>
18
- params.audioAsVoice === true || /\.(?:ogg|opus)(?:[?#]|$)/i.test(params.mediaUrl ?? ""),
19
- );
20
-
21
- vi.mock("./media.js", () => ({
22
- sendMediaFeishu: sendMediaFeishuMock,
23
- shouldSuppressFeishuTextForVoiceMedia: shouldSuppressFeishuTextForVoiceMediaMock,
24
- }));
25
-
26
- vi.mock("./send.js", () => ({
27
- editMessageFeishu: vi.fn(),
28
- getMessageFeishu: vi.fn(),
29
- sendCardFeishu: sendCardFeishuMock,
30
- sendMessageFeishu: sendMessageFeishuMock,
31
- sendMarkdownCardFeishu: sendMarkdownCardFeishuMock,
32
- sendStructuredCardFeishu: sendStructuredCardFeishuMock,
33
- resolveFeishuCardTemplate: (template?: string) =>
34
- new Set([
35
- "blue",
36
- "green",
37
- "red",
38
- "orange",
39
- "purple",
40
- "indigo",
41
- "wathet",
42
- "turquoise",
43
- "yellow",
44
- "grey",
45
- "carmine",
46
- "violet",
47
- "lime",
48
- ]).has(template ?? "")
49
- ? template
50
- : undefined,
51
- }));
52
-
53
- vi.mock("./runtime.js", () => ({
54
- getFeishuRuntime: () => ({
55
- channel: {
56
- text: {
57
- chunkMarkdownText: (text: string) => [text],
58
- },
59
- },
60
- }),
61
- }));
62
-
63
- vi.mock("./client.js", () => ({
64
- createFeishuClient: vi.fn(() => ({ request: vi.fn() })),
65
- }));
66
-
67
- vi.mock("./drive.js", () => ({
68
- deliverCommentThreadText: deliverCommentThreadTextMock,
69
- }));
70
-
71
- vi.mock("./comment-reaction.js", () => ({
72
- cleanupAmbientCommentTypingReaction: cleanupAmbientCommentTypingReactionMock,
73
- }));
74
-
75
- import { feishuPlugin } from "./channel.js";
76
- import { feishuOutbound } from "./outbound.js";
77
- import { createFeishuSendReceipt } from "./send-result.js";
78
-
79
- async function raceWithNextMacrotask<T>(promise: Promise<T>): Promise<T | "pending"> {
80
- return await Promise.race([
81
- promise,
82
- new Promise<"pending">((resolve) => {
83
- setImmediate(() => resolve("pending"));
84
- }),
85
- ]);
86
- }
87
-
88
- type FeishuSendText = NonNullable<typeof feishuOutbound.sendText>;
89
- type FeishuMessageAdapter = NonNullable<typeof feishuPlugin.message>;
90
- type FeishuMessageSender = NonNullable<FeishuMessageAdapter["send"]>;
91
-
92
- function requireFeishuSendText(): FeishuSendText {
93
- const sendText = feishuOutbound.sendText;
94
- if (!sendText) {
95
- throw new Error("Expected Feishu outbound sendText");
96
- }
97
- return sendText;
98
- }
99
-
100
- function requireFeishuMessageAdapter(): FeishuMessageAdapter {
101
- const adapter = feishuPlugin.message;
102
- if (!adapter) {
103
- throw new Error("Expected Feishu message adapter");
104
- }
105
- return adapter;
106
- }
107
-
108
- function requireFeishuTextSender(
109
- adapter: FeishuMessageAdapter,
110
- ): NonNullable<FeishuMessageSender["text"]> {
111
- const text = adapter.send?.text;
112
- if (!text) {
113
- throw new Error("Expected Feishu message adapter text sender");
114
- }
115
- return text;
116
- }
117
-
118
- function requireFeishuMediaSender(
119
- adapter: FeishuMessageAdapter,
120
- ): NonNullable<FeishuMessageSender["media"]> {
121
- const media = adapter.send?.media;
122
- if (!media) {
123
- throw new Error("Expected Feishu message adapter media sender");
124
- }
125
- return media;
126
- }
127
-
128
- const sendText = requireFeishuSendText();
129
- const emptyConfig: ClawdbotConfig = {};
130
- const cardRenderConfig: ClawdbotConfig = {
131
- channels: {
132
- feishu: {
133
- renderMode: "card",
134
- },
135
- },
136
- };
137
-
138
- afterAll(() => {
139
- vi.doUnmock("./media.js");
140
- vi.doUnmock("./send.js");
141
- vi.doUnmock("./runtime.js");
142
- vi.doUnmock("./client.js");
143
- vi.doUnmock("./drive.js");
144
- vi.doUnmock("./comment-reaction.js");
145
- vi.resetModules();
146
- });
147
-
148
- function resetOutboundMocks() {
149
- vi.clearAllMocks();
150
- sendMessageFeishuMock.mockResolvedValue({ messageId: "text_msg" });
151
- sendCardFeishuMock.mockResolvedValue({ messageId: "native_card_msg" });
152
- sendMarkdownCardFeishuMock.mockResolvedValue({ messageId: "card_msg" });
153
- sendStructuredCardFeishuMock.mockResolvedValue({ messageId: "card_msg" });
154
- sendMediaFeishuMock.mockResolvedValue({ messageId: "media_msg" });
155
- deliverCommentThreadTextMock.mockResolvedValue({
156
- delivery_mode: "reply_comment",
157
- reply_id: "reply_msg",
158
- });
159
- cleanupAmbientCommentTypingReactionMock.mockResolvedValue(false);
160
- }
161
-
162
- function sendMessageCall(index = 0): Record<string, any> | undefined {
163
- const calls = sendMessageFeishuMock.mock.calls as unknown as Array<[Record<string, any>]>;
164
- return calls[index]?.[0];
165
- }
166
-
167
- function sendMediaCall(index = 0): Record<string, any> | undefined {
168
- const calls = sendMediaFeishuMock.mock.calls as unknown as Array<[Record<string, any>]>;
169
- return calls[index]?.[0];
170
- }
171
-
172
- function sendCardCall(index = 0): Record<string, any> | undefined {
173
- const calls = sendCardFeishuMock.mock.calls as unknown as Array<[Record<string, any>]>;
174
- return calls[index]?.[0];
175
- }
176
-
177
- function sendStructuredCardCall(index = 0): Record<string, any> | undefined {
178
- const calls = sendStructuredCardFeishuMock.mock.calls as unknown as Array<[Record<string, any>]>;
179
- return calls[index]?.[0];
180
- }
181
-
182
- function sendMarkdownCardCall(index = 0): Record<string, any> | undefined {
183
- const calls = sendMarkdownCardFeishuMock.mock.calls as unknown as Array<[Record<string, any>]>;
184
- return calls[index]?.[0];
185
- }
186
-
187
- function commentThreadParams(index = 0): Record<string, any> | undefined {
188
- const calls = deliverCommentThreadTextMock.mock.calls as unknown as Array<
189
- [unknown, Record<string, any>]
190
- >;
191
- return calls[index]?.[1];
192
- }
193
-
194
- function cleanupReactionCall(index = 0): Record<string, any> | undefined {
195
- const calls = cleanupAmbientCommentTypingReactionMock.mock.calls as unknown as Array<
196
- [Record<string, any>]
197
- >;
198
- return calls[index]?.[0];
199
- }
200
-
201
- function expectFeishuResult(result: unknown, messageId: string) {
202
- const typedResult = result as { channel?: string; messageId?: string } | undefined;
203
- expect(typedResult?.channel).toBe("feishu");
204
- expect(typedResult?.messageId).toBe(messageId);
205
- }
206
-
207
- describe("feishuOutbound.sendText local-image auto-convert", () => {
208
- beforeEach(() => {
209
- resetOutboundMocks();
210
- });
211
-
212
- it("declares message adapter durable text and media with receipt proofs", async () => {
213
- sendMessageFeishuMock.mockResolvedValue({
214
- messageId: "feishu-text-1",
215
- chatId: "chat-1",
216
- receipt: createFeishuSendReceipt({
217
- messageId: "feishu-text-1",
218
- chatId: "chat-1",
219
- kind: "text",
220
- }),
221
- });
222
- sendMediaFeishuMock.mockResolvedValue({
223
- messageId: "feishu-media-1",
224
- chatId: "chat-1",
225
- receipt: createFeishuSendReceipt({
226
- messageId: "feishu-media-1",
227
- chatId: "chat-1",
228
- kind: "media",
229
- }),
230
- });
231
- const adapter = requireFeishuMessageAdapter();
232
- const adapterSendText = requireFeishuTextSender(adapter);
233
- const adapterSendMedia = requireFeishuMediaSender(adapter);
234
-
235
- const proofs = await verifyChannelMessageAdapterCapabilityProofs({
236
- adapterName: "feishu",
237
- adapter,
238
- proofs: {
239
- text: async () => {
240
- const result = await adapterSendText({
241
- cfg: emptyConfig,
242
- to: "chat:chat-1",
243
- text: "hello",
244
- accountId: "default",
245
- });
246
- expect(sendMessageCall()?.to).toBe("chat:chat-1");
247
- expect(sendMessageCall()?.text).toBe("hello");
248
- expect(sendMessageCall()?.accountId).toBe("default");
249
- expect(result.receipt.platformMessageIds).toEqual(["feishu-text-1"]);
250
- },
251
- media: async () => {
252
- const result = await adapterSendMedia({
253
- cfg: emptyConfig,
254
- to: "chat:chat-1",
255
- text: "",
256
- mediaUrl: "https://example.com/image.png",
257
- accountId: "default",
258
- });
259
- expect(sendMediaCall()?.to).toBe("chat:chat-1");
260
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/image.png");
261
- expect(sendMediaCall()?.accountId).toBe("default");
262
- expect(result.receipt.platformMessageIds).toEqual(["feishu-media-1"]);
263
- },
264
- },
265
- });
266
- expect(proofs.some((proof) => proof.capability === "text" && proof.status === "verified")).toBe(
267
- true,
268
- );
269
- expect(
270
- proofs.some((proof) => proof.capability === "media" && proof.status === "verified"),
271
- ).toBe(true);
272
- });
273
-
274
- it("chunks outbound text without requiring Feishu runtime initialization", () => {
275
- const chunker = feishuOutbound.chunker;
276
- if (!chunker) {
277
- throw new Error("feishuOutbound.chunker missing");
278
- }
279
-
280
- expect(chunker("hello world", 5)).toEqual(["hello", "world"]);
281
- });
282
-
283
- async function createTmpImage(ext = ".png"): Promise<{ dir: string; file: string }> {
284
- const dir = await fs.mkdtemp(path.join(os.tmpdir(), "klaw-feishu-outbound-"));
285
- const file = path.join(dir, `sample${ext}`);
286
- await fs.writeFile(file, "image-data");
287
- return { dir, file };
288
- }
289
-
290
- it("sends an absolute existing local image path as media", async () => {
291
- const { dir, file } = await createTmpImage();
292
- try {
293
- const result = await sendText({
294
- cfg: emptyConfig,
295
- to: "chat_1",
296
- text: file,
297
- accountId: "main",
298
- mediaLocalRoots: [dir],
299
- });
300
-
301
- expect(sendMediaCall()?.to).toBe("chat_1");
302
- expect(sendMediaCall()?.mediaUrl).toBe(file);
303
- expect(sendMediaCall()?.accountId).toBe("main");
304
- expect(sendMediaCall()?.mediaLocalRoots).toEqual([dir]);
305
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
306
- expectFeishuResult(result, "media_msg");
307
- } finally {
308
- await fs.rm(dir, { recursive: true, force: true });
309
- }
310
- });
311
-
312
- it("keeps non-path text on the text-send path", async () => {
313
- await sendText({
314
- cfg: emptyConfig,
315
- to: "chat_1",
316
- text: "please upload /tmp/example.png",
317
- accountId: "main",
318
- });
319
-
320
- expect(sendMediaFeishuMock).not.toHaveBeenCalled();
321
- expect(sendMessageCall()?.to).toBe("chat_1");
322
- expect(sendMessageCall()?.text).toBe("please upload /tmp/example.png");
323
- expect(sendMessageCall()?.accountId).toBe("main");
324
- });
325
-
326
- it("falls back to plain text if local-image media send fails", async () => {
327
- const { dir, file } = await createTmpImage();
328
- sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
329
- try {
330
- await sendText({
331
- cfg: emptyConfig,
332
- to: "chat_1",
333
- text: file,
334
- accountId: "main",
335
- });
336
-
337
- expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
338
- expect(sendMessageCall()?.to).toBe("chat_1");
339
- expect(sendMessageCall()?.text).toBe(file);
340
- expect(sendMessageCall()?.accountId).toBe("main");
341
- } finally {
342
- await fs.rm(dir, { recursive: true, force: true });
343
- }
344
- });
345
-
346
- it("uses markdown cards when renderMode=card", async () => {
347
- const result = await sendText({
348
- cfg: cardRenderConfig,
349
- to: "chat_1",
350
- text: "| a | b |\n| - | - |",
351
- accountId: "main",
352
- });
353
-
354
- expect(sendStructuredCardCall()?.to).toBe("chat_1");
355
- expect(sendStructuredCardCall()?.text).toBe("| a | b |\n| - | - |");
356
- expect(sendStructuredCardCall()?.accountId).toBe("main");
357
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
358
- expectFeishuResult(result, "card_msg");
359
- });
360
-
361
- it("forwards replyToId as replyToMessageId on sendText", async () => {
362
- await sendText({
363
- cfg: emptyConfig,
364
- to: "chat_1",
365
- text: "hello",
366
- replyToId: "om_reply_1",
367
- accountId: "main",
368
- });
369
-
370
- expect(sendMessageCall()?.to).toBe("chat_1");
371
- expect(sendMessageCall()?.text).toBe("hello");
372
- expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_1");
373
- expect(sendMessageCall()?.accountId).toBe("main");
374
- });
375
-
376
- it("falls back to threadId when replyToId is empty on sendText", async () => {
377
- await sendText({
378
- cfg: emptyConfig,
379
- to: "chat_1",
380
- text: "hello",
381
- replyToId: " ",
382
- threadId: "om_thread_2",
383
- accountId: "main",
384
- });
385
-
386
- expect(sendMessageCall()?.to).toBe("chat_1");
387
- expect(sendMessageCall()?.text).toBe("hello");
388
- expect(sendMessageCall()?.replyToMessageId).toBe("om_thread_2");
389
- expect(sendMessageCall()?.replyInThread).toBe(true);
390
- expect(sendMessageCall()?.accountId).toBe("main");
391
- });
392
- });
393
-
394
- describe("feishuOutbound.sendPayload native cards", () => {
395
- beforeEach(() => {
396
- resetOutboundMocks();
397
- });
398
-
399
- async function createTmpImage(ext = ".png"): Promise<{ dir: string; file: string }> {
400
- const dir = await fs.mkdtemp(path.join(os.tmpdir(), "klaw-feishu-payload-"));
401
- const file = path.join(dir, `sample${ext}`);
402
- await fs.writeFile(file, "image-data");
403
- return { dir, file };
404
- }
405
-
406
- it("renders presentation-only payloads into Feishu channelData cards for core delivery", async () => {
407
- const presentation: MessagePresentation = {
408
- title: "Approval",
409
- tone: "success",
410
- blocks: [
411
- { type: "text", text: "Approve the request?" },
412
- {
413
- type: "buttons",
414
- buttons: [
415
- { label: "Approve", value: "/approve req_1 allow-once", style: "success" as const },
416
- ],
417
- },
418
- ],
419
- };
420
- const payload = { presentation };
421
- const rendered = await feishuOutbound.renderPresentation?.({
422
- payload,
423
- presentation,
424
- ctx: {
425
- cfg: emptyConfig,
426
- to: "chat_1",
427
- text: "",
428
- accountId: "main",
429
- payload,
430
- },
431
- });
432
-
433
- if (!rendered) {
434
- throw new Error("expected Feishu presentation renderer to return a payload");
435
- }
436
- expect(rendered.text).toBe("Approval\n\nApprove the request?\n\n- Approve");
437
- const renderedChannelData = rendered.channelData as
438
- | { feishu?: { card?: Record<string, any> } }
439
- | undefined;
440
- const renderedCard = renderedChannelData?.feishu?.card;
441
- expect(renderedCard?.schema).toBe("2.0");
442
- expect(renderedCard?.header).toEqual({
443
- title: { tag: "plain_text", content: "Approval" },
444
- template: "green",
445
- });
446
- expect(renderedCard?.body?.elements?.[0]).toEqual({
447
- tag: "markdown",
448
- content: "Approve the request?",
449
- });
450
- expect(
451
- renderedCard?.body?.elements?.some((element: { tag?: string }) => element.tag === "action"),
452
- ).toBe(true);
453
- const { presentation: _presentation, ...coreRenderedPayload } = rendered;
454
- const result = await feishuOutbound.sendPayload?.({
455
- cfg: emptyConfig,
456
- to: "chat_1",
457
- text: coreRenderedPayload.text ?? "",
458
- accountId: "main",
459
- payload: coreRenderedPayload,
460
- });
461
-
462
- expect(sendCardCall()?.to).toBe("chat_1");
463
- expect(sendCardCall()?.card?.header).toEqual({
464
- title: { tag: "plain_text", content: "Approval" },
465
- template: "green",
466
- });
467
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
468
- expectFeishuResult(result, "native_card_msg");
469
- });
470
-
471
- it("sends interactive button payloads as native Feishu cards", async () => {
472
- const result = await feishuOutbound.sendPayload?.({
473
- cfg: emptyConfig,
474
- to: "chat_1",
475
- text: "Choose an action",
476
- accountId: "main",
477
- payload: {
478
- text: "Choose an action",
479
- interactive: {
480
- blocks: [
481
- { type: "text", text: "Approve the request?" },
482
- {
483
- type: "buttons",
484
- buttons: [
485
- { label: "Approve", value: "/approve req_1 allow-once", style: "success" },
486
- { label: "Deny", value: "/approve req_1 deny", style: "danger" },
487
- ],
488
- },
489
- ],
490
- },
491
- },
492
- });
493
-
494
- expect(sendCardCall()?.cfg).toBe(emptyConfig);
495
- expect(sendCardCall()?.to).toBe("chat_1");
496
- expect(sendCardCall()?.accountId).toBe("main");
497
- const card = sendCardCall()?.card;
498
- expect(card.schema).toBe("2.0");
499
- expect(card.body.elements[0]).toEqual({ tag: "markdown", content: "Choose an action" });
500
- expect(card.body.elements[1]).toEqual({
501
- tag: "markdown",
502
- content: "Approve the request?",
503
- });
504
- const actionElement = card.body.elements.find(
505
- (element: { tag?: string }) => element.tag === "action",
506
- );
507
- expect(actionElement?.actions[0]?.text).toEqual({ tag: "plain_text", content: "Approve" });
508
- expect(actionElement?.actions[0]?.type).toBe("primary");
509
- expect(actionElement?.actions[0]?.value?.oc).toBe("ocf1");
510
- expect(actionElement?.actions[0]?.value?.k).toBe("quick");
511
- expect(actionElement?.actions[0]?.value?.q).toBe("/approve req_1 allow-once");
512
- expect(actionElement?.actions[1]?.text).toEqual({ tag: "plain_text", content: "Deny" });
513
- expect(actionElement?.actions[1]?.type).toBe("danger");
514
- expect(actionElement?.actions[1]?.value?.oc).toBe("ocf1");
515
- expect(actionElement?.actions[1]?.value?.k).toBe("quick");
516
- expect(actionElement?.actions[1]?.value?.q).toBe("/approve req_1 deny");
517
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
518
- expectFeishuResult(result, "native_card_msg");
519
- });
520
-
521
- it("escapes generated markdown card text and drops unsafe button URLs", async () => {
522
- await feishuOutbound.sendPayload?.({
523
- cfg: emptyConfig,
524
- to: "chat_1",
525
- text: 'Choose <at id="ou_1">',
526
- accountId: "main",
527
- payload: {
528
- text: 'Choose <at id="ou_1">',
529
- presentation: {
530
- blocks: [
531
- { type: "context", text: '</font><at id="ou_2">Injected</at>' },
532
- {
533
- type: "buttons",
534
- buttons: [
535
- { label: "Open", url: "https://example.com/path" },
536
- { label: "Bad", url: "javascript:alert(1)" },
537
- ],
538
- },
539
- ],
540
- },
541
- },
542
- });
543
-
544
- const card = sendCardCall()?.card;
545
- expect(card.body.elements[0]).toEqual({
546
- tag: "markdown",
547
- content: 'Choose &lt;at id="ou_1"&gt;',
548
- });
549
- expect(card.body.elements[1]).toEqual({
550
- tag: "markdown",
551
- content: "<font color='grey'>&lt;/font&gt;&lt;at id=\"ou_2\"&gt;Injected&lt;/at&gt;</font>",
552
- });
553
- const actionElement = card.body.elements.find(
554
- (element: { tag?: string }) => element.tag === "action",
555
- );
556
- expect(actionElement?.actions[0]?.text).toEqual({ tag: "plain_text", content: "Open" });
557
- expect(actionElement?.actions[0]?.url).toBe("https://example.com/path");
558
- expect(JSON.stringify(card)).not.toContain("javascript:");
559
- });
560
-
561
- it("normalizes caller-supplied native Feishu cards before sending", async () => {
562
- await feishuOutbound.sendPayload?.({
563
- cfg: emptyConfig,
564
- to: "chat_1",
565
- text: "fallback",
566
- accountId: "main",
567
- payload: {
568
- text: "fallback",
569
- channelData: {
570
- feishu: {
571
- card: {
572
- schema: "2.0",
573
- header: {
574
- title: { tag: "plain_text", content: "Unsafe card" },
575
- template: "not-a-template",
576
- },
577
- body: {
578
- elements: [
579
- { tag: "img", img_key: "image-secret" },
580
- { tag: "markdown", content: '<at id="ou_1">ping</at>' },
581
- {
582
- tag: "action",
583
- actions: [
584
- {
585
- tag: "button",
586
- text: { tag: "plain_text", content: "Bad link" },
587
- url: "file:///etc/passwd",
588
- },
589
- {
590
- tag: "button",
591
- text: { tag: "plain_text", content: "Good link" },
592
- url: "https://example.com",
593
- },
594
- ],
595
- },
596
- ],
597
- },
598
- },
599
- },
600
- },
601
- },
602
- });
603
-
604
- const card = sendCardCall()?.card;
605
- expect(card.header.template).toBe("blue");
606
- expect(card.body.elements).toEqual([
607
- { tag: "markdown", content: '&lt;at id="ou_1"&gt;ping&lt;/at&gt;' },
608
- {
609
- tag: "action",
610
- actions: [
611
- {
612
- tag: "button",
613
- text: { tag: "plain_text", content: "Good link" },
614
- type: "default",
615
- url: "https://example.com",
616
- },
617
- ],
618
- },
619
- ]);
620
- expect(JSON.stringify(card)).not.toContain("file://");
621
- expect(JSON.stringify(card)).not.toContain("image-secret");
622
- });
623
-
624
- it("sends payload media before final native cards", async () => {
625
- const result = await feishuOutbound.sendPayload?.({
626
- cfg: emptyConfig,
627
- to: "chat_1",
628
- text: "See attached",
629
- accountId: "main",
630
- mediaLocalRoots: ["/tmp"],
631
- payload: {
632
- text: "See attached",
633
- mediaUrl: "/tmp/image.png",
634
- interactive: {
635
- blocks: [{ type: "buttons", buttons: [{ label: "Open", url: "https://example.com" }] }],
636
- },
637
- },
638
- });
639
-
640
- expect(sendMediaCall()?.to).toBe("chat_1");
641
- expect(sendMediaCall()?.mediaUrl).toBe("/tmp/image.png");
642
- expect(sendMediaCall()?.mediaLocalRoots).toEqual(["/tmp"]);
643
- expect(sendMediaCall()?.accountId).toBe("main");
644
- expect(sendCardCall()?.to).toBe("chat_1");
645
- expect(sendCardCall()?.accountId).toBe("main");
646
- expectFeishuResult(result, "native_card_msg");
647
- });
648
-
649
- it("keeps text/media fallback behavior for non-card payloads, including local image text", async () => {
650
- const { dir, file } = await createTmpImage();
651
- try {
652
- const result = await feishuOutbound.sendPayload?.({
653
- cfg: emptyConfig,
654
- to: "chat_1",
655
- text: file,
656
- accountId: "main",
657
- mediaLocalRoots: [dir],
658
- payload: { text: file },
659
- });
660
-
661
- expect(sendCardFeishuMock).not.toHaveBeenCalled();
662
- expect(sendMediaCall()?.to).toBe("chat_1");
663
- expect(sendMediaCall()?.mediaUrl).toBe(file);
664
- expect(sendMediaCall()?.mediaLocalRoots).toEqual([dir]);
665
- expect(sendMediaCall()?.accountId).toBe("main");
666
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
667
- expectFeishuResult(result, "media_msg");
668
- } finally {
669
- await fs.rm(dir, { recursive: true, force: true });
670
- }
671
- });
672
-
673
- it("falls back to comment-thread text instead of sending native cards to document comments", async () => {
674
- const result = await feishuOutbound.sendPayload?.({
675
- cfg: emptyConfig,
676
- to: "comment:docx:doxcn123:7623358762119646411",
677
- text: "Review this",
678
- accountId: "main",
679
- payload: {
680
- text: "Review this",
681
- interactive: {
682
- blocks: [{ type: "buttons", buttons: [{ label: "Approve", value: "/approve req_1" }] }],
683
- },
684
- },
685
- });
686
-
687
- expect(sendCardFeishuMock).not.toHaveBeenCalled();
688
- expect(commentThreadParams()?.content).toBe("Review this\n\n- Approve");
689
- expectFeishuResult(result, "reply_msg");
690
- });
691
- });
692
-
693
- describe("feishuOutbound comment-thread routing", () => {
694
- beforeEach(() => {
695
- resetOutboundMocks();
696
- });
697
-
698
- it("routes comment-thread text through deliverCommentThreadText", async () => {
699
- const result = await sendText({
700
- cfg: emptyConfig,
701
- to: "comment:docx:doxcn123:7623358762119646411",
702
- text: "handled in thread",
703
- accountId: "main",
704
- });
705
-
706
- expect(commentThreadParams()?.file_token).toBe("doxcn123");
707
- expect(commentThreadParams()?.file_type).toBe("docx");
708
- expect(commentThreadParams()?.comment_id).toBe("7623358762119646411");
709
- expect(commentThreadParams()?.content).toBe("handled in thread");
710
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
711
- expectFeishuResult(result, "reply_msg");
712
- });
713
-
714
- it("routes comment-thread code-block replies through deliverCommentThreadText instead of IM cards", async () => {
715
- const result = await sendText({
716
- cfg: emptyConfig,
717
- to: "comment:docx:doxcn123:7623358762119646411",
718
- text: "```ts\nconst x = 1\n```",
719
- accountId: "main",
720
- });
721
-
722
- expect(commentThreadParams()?.file_token).toBe("doxcn123");
723
- expect(commentThreadParams()?.file_type).toBe("docx");
724
- expect(commentThreadParams()?.comment_id).toBe("7623358762119646411");
725
- expect(commentThreadParams()?.content).toBe("```ts\nconst x = 1\n```");
726
- expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
727
- expect(sendMarkdownCardFeishuMock).not.toHaveBeenCalled();
728
- expectFeishuResult(result, "reply_msg");
729
- });
730
-
731
- it("routes comment-thread replies through deliverCommentThreadText even when renderMode=card", async () => {
732
- const result = await sendText({
733
- cfg: cardRenderConfig,
734
- to: "comment:docx:doxcn123:7623358762119646411",
735
- text: "handled in thread",
736
- accountId: "main",
737
- });
738
-
739
- expect(commentThreadParams()?.file_token).toBe("doxcn123");
740
- expect(commentThreadParams()?.file_type).toBe("docx");
741
- expect(commentThreadParams()?.comment_id).toBe("7623358762119646411");
742
- expect(commentThreadParams()?.content).toBe("handled in thread");
743
- expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
744
- expect(sendMarkdownCardFeishuMock).not.toHaveBeenCalled();
745
- expectFeishuResult(result, "reply_msg");
746
- });
747
-
748
- it("falls back to a text-only comment reply for media payloads", async () => {
749
- const result = await feishuOutbound.sendMedia?.({
750
- cfg: emptyConfig,
751
- to: "comment:docx:doxcn123:7623358762119646411",
752
- text: "see attachment",
753
- mediaUrl: "https://example.com/file.png",
754
- accountId: "main",
755
- });
756
-
757
- expect(commentThreadParams()?.content).toBe("see attachment\n\nhttps://example.com/file.png");
758
- expect(sendMediaFeishuMock).not.toHaveBeenCalled();
759
- expectFeishuResult(result, "reply_msg");
760
- });
761
-
762
- it("preserves comment-thread routing when deliverCommentThreadText falls back to add_comment", async () => {
763
- deliverCommentThreadTextMock.mockResolvedValueOnce({
764
- delivery_mode: "add_comment",
765
- comment_id: "comment_msg",
766
- reply_id: "reply_from_add_comment",
767
- });
768
-
769
- const result = await sendText({
770
- cfg: emptyConfig,
771
- to: "comment:docx:doxcn123:7623358762119646411",
772
- text: "whole-comment follow-up",
773
- accountId: "main",
774
- });
775
-
776
- expect(commentThreadParams()?.file_token).toBe("doxcn123");
777
- expect(commentThreadParams()?.file_type).toBe("docx");
778
- expect(commentThreadParams()?.comment_id).toBe("7623358762119646411");
779
- expect(commentThreadParams()?.content).toBe("whole-comment follow-up");
780
- expectFeishuResult(result, "reply_from_add_comment");
781
- });
782
-
783
- it("does not wait for ambient comment typing cleanup before sending comment-thread replies", async () => {
784
- let resolveCleanup: ((value: boolean) => void) | undefined;
785
- cleanupAmbientCommentTypingReactionMock.mockImplementationOnce(
786
- () =>
787
- new Promise<boolean>((resolve) => {
788
- resolveCleanup = resolve;
789
- }),
790
- );
791
-
792
- const sendPromise = sendText({
793
- cfg: emptyConfig,
794
- to: "comment:docx:doxcn123:7623358762119646411",
795
- text: "handled in thread",
796
- replyToId: "reply_ambient_1",
797
- accountId: "main",
798
- });
799
-
800
- const status = await raceWithNextMacrotask(sendPromise.then(() => "done"));
801
-
802
- expect(status).toBe("done");
803
- expect(deliverCommentThreadTextMock).toHaveBeenCalled();
804
- const cleanupCall = cleanupReactionCall();
805
- if (!cleanupCall?.client) {
806
- throw new Error("Expected cleanup reaction client");
807
- }
808
- expect(cleanupCall.deliveryContext).toEqual({
809
- channel: "feishu",
810
- to: "comment:docx:doxcn123:7623358762119646411",
811
- threadId: "reply_ambient_1",
812
- });
813
-
814
- resolveCleanup?.(false);
815
- await sendPromise;
816
- });
817
- });
818
-
819
- describe("feishuOutbound.sendText replyToId forwarding", () => {
820
- beforeEach(() => {
821
- resetOutboundMocks();
822
- });
823
-
824
- it("forwards replyToId as replyToMessageId to sendMessageFeishu", async () => {
825
- await sendText({
826
- cfg: emptyConfig,
827
- to: "chat_1",
828
- text: "hello",
829
- replyToId: "om_reply_target",
830
- accountId: "main",
831
- });
832
-
833
- expect(sendMessageCall()?.to).toBe("chat_1");
834
- expect(sendMessageCall()?.text).toBe("hello");
835
- expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_target");
836
- expect(sendMessageCall()?.accountId).toBe("main");
837
- });
838
-
839
- it("forwards replyToId to sendStructuredCardFeishu when renderMode=card", async () => {
840
- await sendText({
841
- cfg: cardRenderConfig,
842
- to: "chat_1",
843
- text: "```code```",
844
- replyToId: "om_reply_target",
845
- accountId: "main",
846
- });
847
-
848
- expect(sendStructuredCardCall()?.replyToMessageId).toBe("om_reply_target");
849
- });
850
-
851
- it("does not pass replyToMessageId when replyToId is absent", async () => {
852
- await sendText({
853
- cfg: emptyConfig,
854
- to: "chat_1",
855
- text: "hello",
856
- accountId: "main",
857
- });
858
-
859
- expect(sendMessageCall()?.to).toBe("chat_1");
860
- expect(sendMessageCall()?.text).toBe("hello");
861
- expect(sendMessageCall()?.accountId).toBe("main");
862
- expect(sendMessageCall()?.replyToMessageId).toBeUndefined();
863
- });
864
-
865
- it("propagates threadId as replyInThread=true to sendMessageFeishu", async () => {
866
- await sendText({
867
- cfg: emptyConfig,
868
- to: "chat_1",
869
- text: "topic reply",
870
- threadId: "om_topic_root",
871
- accountId: "main",
872
- });
873
-
874
- expect(sendMessageCall()?.replyToMessageId).toBe("om_topic_root");
875
- expect(sendMessageCall()?.replyInThread).toBe(true);
876
- });
877
-
878
- it("propagates threadId as replyInThread=true to sendStructuredCardFeishu when renderMode=card", async () => {
879
- await sendText({
880
- cfg: cardRenderConfig,
881
- to: "chat_1",
882
- text: "```code```",
883
- threadId: "om_topic_root",
884
- accountId: "main",
885
- });
886
-
887
- expect(sendStructuredCardCall()?.replyToMessageId).toBe("om_topic_root");
888
- expect(sendStructuredCardCall()?.replyInThread).toBe(true);
889
- });
890
-
891
- it("prefers replyToId over threadId for plain text (inline reply, no auto-thread)", async () => {
892
- await sendText({
893
- cfg: emptyConfig,
894
- to: "chat_1",
895
- text: "inline reply",
896
- replyToId: "om_inline",
897
- threadId: "om_topic_root",
898
- accountId: "main",
899
- });
900
-
901
- expect(sendMessageCall()?.replyToMessageId).toBe("om_inline");
902
- expect(sendMessageCall()?.replyInThread).toBe(false);
903
- });
904
- });
905
-
906
- describe("feishuOutbound.sendMedia replyToId forwarding", () => {
907
- beforeEach(() => {
908
- resetOutboundMocks();
909
- });
910
-
911
- it("forwards replyToId to sendMediaFeishu", async () => {
912
- await feishuOutbound.sendMedia?.({
913
- cfg: emptyConfig,
914
- to: "chat_1",
915
- text: "",
916
- mediaUrl: "https://example.com/image.png",
917
- replyToId: "om_reply_target",
918
- accountId: "main",
919
- });
920
-
921
- expect(sendMediaCall()?.replyToMessageId).toBe("om_reply_target");
922
- expect(sendMediaCall()?.replyInThread).toBe(false);
923
- });
924
-
925
- it("forwards threadId as replyInThread=true to sendMediaFeishu", async () => {
926
- await feishuOutbound.sendMedia?.({
927
- cfg: emptyConfig,
928
- to: "chat_1",
929
- text: "",
930
- mediaUrl: "https://example.com/image.png",
931
- threadId: "om_topic_root",
932
- accountId: "main",
933
- });
934
-
935
- expect(sendMediaCall()?.replyToMessageId).toBe("om_topic_root");
936
- expect(sendMediaCall()?.replyInThread).toBe(true);
937
- });
938
-
939
- it("prefers replyToId over threadId (inline reply) when both are set", async () => {
940
- await feishuOutbound.sendMedia?.({
941
- cfg: emptyConfig,
942
- to: "chat_1",
943
- text: "",
944
- mediaUrl: "https://example.com/image.png",
945
- replyToId: "om_inline",
946
- threadId: "om_topic_root",
947
- accountId: "main",
948
- });
949
-
950
- expect(sendMediaCall()?.replyToMessageId).toBe("om_inline");
951
- expect(sendMediaCall()?.replyInThread).toBe(false);
952
- });
953
-
954
- it("treats whitespace-only replyToId as absent for replyInThread (falls back to threadId)", async () => {
955
- await feishuOutbound.sendMedia?.({
956
- cfg: emptyConfig,
957
- to: "chat_1",
958
- text: "",
959
- mediaUrl: "https://example.com/image.png",
960
- replyToId: " ",
961
- threadId: "om_topic_root",
962
- accountId: "main",
963
- });
964
-
965
- expect(sendMediaCall()?.replyToMessageId).toBe("om_topic_root");
966
- expect(sendMediaCall()?.replyInThread).toBe(true);
967
- });
968
-
969
- it("forwards audioAsVoice to sendMediaFeishu", async () => {
970
- await feishuOutbound.sendMedia?.({
971
- cfg: emptyConfig,
972
- to: "chat_1",
973
- text: "",
974
- mediaUrl: "https://example.com/reply.mp3",
975
- audioAsVoice: true,
976
- accountId: "main",
977
- });
978
-
979
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/reply.mp3");
980
- expect(sendMediaCall()?.audioAsVoice).toBe(true);
981
- });
982
-
983
- it("suppresses duplicate text when sending voice media", async () => {
984
- await feishuOutbound.sendMedia?.({
985
- cfg: emptyConfig,
986
- to: "chat_1",
987
- text: "spoken reply",
988
- mediaUrl: "https://example.com/reply.mp3",
989
- audioAsVoice: true,
990
- accountId: "main",
991
- });
992
-
993
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
994
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/reply.mp3");
995
- expect(sendMediaCall()?.audioAsVoice).toBe(true);
996
- });
997
-
998
- it("sends skipped voice text when voice media degrades to a file attachment", async () => {
999
- sendMediaFeishuMock.mockResolvedValueOnce({
1000
- messageId: "file_msg",
1001
- voiceIntentDegradedToFile: true,
1002
- });
1003
-
1004
- await feishuOutbound.sendMedia?.({
1005
- cfg: emptyConfig,
1006
- to: "chat_1",
1007
- text: "spoken reply",
1008
- mediaUrl: "https://example.com/reply.mp3",
1009
- audioAsVoice: true,
1010
- accountId: "main",
1011
- });
1012
-
1013
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/reply.mp3");
1014
- expect(sendMediaCall()?.audioAsVoice).toBe(true);
1015
- expect(sendMessageFeishuMock).toHaveBeenCalledTimes(1);
1016
- expect(sendMessageCall()?.text).toBe("spoken reply");
1017
- });
1018
-
1019
- it("suppresses duplicate text for native voice media without audioAsVoice", async () => {
1020
- await feishuOutbound.sendMedia?.({
1021
- cfg: emptyConfig,
1022
- to: "chat_1",
1023
- text: "spoken reply",
1024
- mediaUrl: "https://example.com/reply.ogg?download=1",
1025
- accountId: "main",
1026
- });
1027
-
1028
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1029
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/reply.ogg?download=1");
1030
- });
1031
-
1032
- it("keeps captions for regular audio file attachments", async () => {
1033
- await feishuOutbound.sendMedia?.({
1034
- cfg: emptyConfig,
1035
- to: "chat_1",
1036
- text: "caption text",
1037
- mediaUrl: "https://example.com/song.mp3",
1038
- accountId: "main",
1039
- });
1040
-
1041
- expect(sendMessageCall()?.text).toBe("caption text");
1042
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/song.mp3");
1043
- });
1044
-
1045
- it("keeps skipped voice text in the upload failure fallback", async () => {
1046
- sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
1047
-
1048
- await feishuOutbound.sendMedia?.({
1049
- cfg: emptyConfig,
1050
- to: "chat_1",
1051
- text: "spoken reply",
1052
- mediaUrl: "https://example.com/reply.mp3",
1053
- audioAsVoice: true,
1054
- accountId: "main",
1055
- });
1056
-
1057
- expect(sendMessageFeishuMock).toHaveBeenCalledTimes(1);
1058
- expect(sendMessageCall()?.text).toBe("spoken reply\n\nšŸ“Ž https://example.com/reply.mp3");
1059
- });
1060
-
1061
- it("forwards replyToId to text caption send", async () => {
1062
- await feishuOutbound.sendMedia?.({
1063
- cfg: emptyConfig,
1064
- to: "chat_1",
1065
- text: "caption text",
1066
- mediaUrl: "https://example.com/image.png",
1067
- replyToId: "om_reply_target",
1068
- accountId: "main",
1069
- });
1070
-
1071
- expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_target");
1072
- });
1073
- });
1074
-
1075
- describe("feishuOutbound.sendMedia renderMode", () => {
1076
- beforeEach(() => {
1077
- resetOutboundMocks();
1078
- });
1079
-
1080
- it("uses markdown cards for captions when renderMode=card", async () => {
1081
- const result = await feishuOutbound.sendMedia?.({
1082
- cfg: cardRenderConfig,
1083
- to: "chat_1",
1084
- text: "| a | b |\n| - | - |",
1085
- mediaUrl: "https://example.com/image.png",
1086
- accountId: "main",
1087
- });
1088
-
1089
- expect(sendMarkdownCardCall()?.to).toBe("chat_1");
1090
- expect(sendMarkdownCardCall()?.text).toBe("| a | b |\n| - | - |");
1091
- expect(sendMarkdownCardCall()?.accountId).toBe("main");
1092
- expect(sendMediaCall()?.to).toBe("chat_1");
1093
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/image.png");
1094
- expect(sendMediaCall()?.accountId).toBe("main");
1095
- expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1096
- expectFeishuResult(result, "media_msg");
1097
- });
1098
-
1099
- it("uses threadId fallback as replyToMessageId on sendMedia", async () => {
1100
- await feishuOutbound.sendMedia?.({
1101
- cfg: emptyConfig,
1102
- to: "chat_1",
1103
- text: "caption",
1104
- mediaUrl: "https://example.com/image.png",
1105
- threadId: "om_thread_1",
1106
- accountId: "main",
1107
- });
1108
-
1109
- expect(sendMediaCall()?.to).toBe("chat_1");
1110
- expect(sendMediaCall()?.mediaUrl).toBe("https://example.com/image.png");
1111
- expect(sendMediaCall()?.replyToMessageId).toBe("om_thread_1");
1112
- expect(sendMediaCall()?.accountId).toBe("main");
1113
- expect(sendMessageCall()?.to).toBe("chat_1");
1114
- expect(sendMessageCall()?.text).toBe("caption");
1115
- expect(sendMessageCall()?.replyToMessageId).toBe("om_thread_1");
1116
- expect(sendMessageCall()?.accountId).toBe("main");
1117
- });
1118
- });