@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,188 +0,0 @@
1
- import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { ClawdbotConfig } from "../runtime-api.js";
3
- import { expectFirstSentCardUsesFillWidthOnly } from "./card-test-helpers.js";
4
- import { createFeishuBotMenuHandler } from "./monitor.bot-menu-handler.js";
5
-
6
- const handleFeishuMessageMock = vi.hoisted(() => vi.fn(async (_params?: unknown) => {}));
7
- const parseFeishuMessageEventMock = vi.hoisted(() => vi.fn());
8
- const sendCardFeishuMock = vi.hoisted(() =>
9
- vi.fn(async (_params?: unknown) => ({ messageId: "m1", chatId: "c1" })),
10
- );
11
- const getMessageFeishuMock = vi.hoisted(() => vi.fn());
12
-
13
- const originalStateDir = process.env.KLAW_STATE_DIR;
14
-
15
- vi.mock("./bot.js", () => {
16
- return {
17
- handleFeishuMessage: handleFeishuMessageMock,
18
- parseFeishuMessageEvent: parseFeishuMessageEventMock,
19
- };
20
- });
21
-
22
- vi.mock("./send.js", () => {
23
- return {
24
- sendCardFeishu: sendCardFeishuMock,
25
- getMessageFeishu: getMessageFeishuMock,
26
- };
27
- });
28
-
29
- function createBotMenuEvent(params: { eventKey: string; timestamp: string }) {
30
- return {
31
- event_key: params.eventKey,
32
- timestamp: params.timestamp,
33
- operator: {
34
- operator_id: {
35
- open_id: "ou_user1",
36
- user_id: "user_1",
37
- union_id: "union_1",
38
- },
39
- },
40
- };
41
- }
42
-
43
- async function registerHandlers() {
44
- return createFeishuBotMenuHandler({
45
- cfg: {} as ClawdbotConfig,
46
- accountId: "default",
47
- runtime: {
48
- log: vi.fn(),
49
- error: vi.fn(),
50
- exit: vi.fn(),
51
- },
52
- chatHistories: new Map(),
53
- fireAndForget: true,
54
- getBotOpenId: () => "ou_bot",
55
- getBotName: () => "Bot",
56
- });
57
- }
58
-
59
- function firstMockArg(mock: { mock: { calls: Array<readonly unknown[]> } }, label: string) {
60
- const call = mock.mock.calls[0];
61
- if (!call) {
62
- throw new Error(`expected ${label} call`);
63
- }
64
- return call[0];
65
- }
66
-
67
- describe("Feishu bot menu handler", () => {
68
- afterAll(() => {
69
- vi.doUnmock("./bot.js");
70
- vi.doUnmock("./send.js");
71
- vi.resetModules();
72
- });
73
-
74
- beforeEach(() => {
75
- vi.clearAllMocks();
76
- process.env.KLAW_STATE_DIR = `/tmp/klaw-feishu-bot-menu-test-${Date.now()}-${Math.random().toString(36).slice(2)}`;
77
- });
78
-
79
- afterEach(() => {
80
- if (originalStateDir === undefined) {
81
- delete process.env.KLAW_STATE_DIR;
82
- return;
83
- }
84
- process.env.KLAW_STATE_DIR = originalStateDir;
85
- });
86
-
87
- it("opens the quick-action launcher card at the webhook/event layer", async () => {
88
- const onBotMenu = await registerHandlers();
89
-
90
- await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000000" }));
91
-
92
- expect(sendCardFeishuMock).toHaveBeenCalledTimes(1);
93
- const sendArgs = firstMockArg(sendCardFeishuMock, "Feishu card send") as
94
- | {
95
- accountId?: string;
96
- card?: {
97
- config?: { width_mode?: string };
98
- header?: { title?: { content?: string } };
99
- };
100
- to?: string;
101
- }
102
- | undefined;
103
- expect(sendArgs?.to).toBe("user:ou_user1");
104
- expect(sendArgs?.accountId).toBe("default");
105
- expect(sendArgs?.card?.config?.width_mode).toBe("fill");
106
- expect(sendArgs?.card?.header?.title?.content).toBe("Quick actions");
107
- expect(handleFeishuMessageMock).not.toHaveBeenCalled();
108
- });
109
-
110
- it("does not block bot-menu handling on quick-action launcher send", async () => {
111
- const onBotMenu = await registerHandlers();
112
- let resolveSend: (() => void) | undefined;
113
- sendCardFeishuMock.mockImplementationOnce(
114
- () =>
115
- new Promise((resolve) => {
116
- resolveSend = () => resolve({ messageId: "m1", chatId: "c1" });
117
- }),
118
- );
119
-
120
- const pending = onBotMenu(
121
- createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000001" }),
122
- );
123
- let settled = false;
124
- void pending.finally(() => {
125
- settled = true;
126
- });
127
-
128
- await vi.waitFor(() => {
129
- expect(settled).toBe(true);
130
- });
131
-
132
- resolveSend?.();
133
- await pending;
134
- });
135
-
136
- it("falls back to the legacy /menu synthetic message path for unrelated bot menu keys", async () => {
137
- const onBotMenu = await registerHandlers();
138
-
139
- await onBotMenu(createBotMenuEvent({ eventKey: "custom-key", timestamp: "1700000000002" }));
140
-
141
- expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
142
- const handleArgs = firstMockArg(handleFeishuMessageMock, "Feishu synthetic message") as
143
- | { event?: { message?: { content?: string } } }
144
- | undefined;
145
- expect(handleArgs?.event?.message?.content).toBe('{"text":"/menu custom-key"}');
146
- expect(sendCardFeishuMock).not.toHaveBeenCalled();
147
- });
148
-
149
- it("falls back to the legacy /menu path when launcher rendering fails", async () => {
150
- const onBotMenu = await registerHandlers();
151
- sendCardFeishuMock.mockRejectedValueOnce(new Error("boom"));
152
-
153
- await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000003" }));
154
-
155
- await vi.waitFor(() => {
156
- expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
157
- });
158
- const handleArgs = firstMockArg(handleFeishuMessageMock, "Feishu fallback message") as
159
- | { event?: { message?: { content?: string } } }
160
- | undefined;
161
- expect(handleArgs?.event?.message?.content).toBe('{"text":"/menu quick-actions"}');
162
- expectFirstSentCardUsesFillWidthOnly(sendCardFeishuMock);
163
- });
164
-
165
- it("reopens replay for explicit retryable fallback failures", async () => {
166
- const onBotMenu = await registerHandlers();
167
- sendCardFeishuMock
168
- .mockImplementationOnce(async () => {
169
- throw new Error("boom");
170
- })
171
- .mockImplementationOnce(async () => {
172
- throw new Error("boom");
173
- });
174
- handleFeishuMessageMock
175
- .mockRejectedValueOnce(
176
- Object.assign(new Error("retry me"), {
177
- name: "FeishuRetryableSyntheticEventError",
178
- }),
179
- )
180
- .mockResolvedValueOnce(undefined);
181
-
182
- await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000004" }));
183
- await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000004" }));
184
-
185
- expect(sendCardFeishuMock).toHaveBeenCalledTimes(2);
186
- expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
187
- });
188
- });
@@ -1,264 +0,0 @@
1
- import "./lifecycle.test-support.js";
2
- import { createNonExitingRuntimeEnv } from "klaw/plugin-sdk/plugin-test-runtime";
3
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
- import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
5
- import { FeishuConfigSchema } from "./config-schema.js";
6
- import {
7
- getFeishuLifecycleTestMocks,
8
- resetFeishuLifecycleTestMocks,
9
- } from "./lifecycle.test-support.js";
10
- import {
11
- createFeishuTextMessageEvent,
12
- createFeishuLifecycleReplyDispatcher,
13
- installFeishuLifecycleReplyRuntime,
14
- mockFeishuReplyOnceDispatch,
15
- restoreFeishuLifecycleStateDir,
16
- runFeishuLifecycleSequence,
17
- setFeishuLifecycleStateDir,
18
- setupFeishuLifecycleHandler,
19
- } from "./test-support/lifecycle-test-support.js";
20
- import type { FeishuConfig, ResolvedFeishuAccount } from "./types.js";
21
-
22
- const {
23
- createEventDispatcherMock,
24
- createFeishuReplyDispatcherMock,
25
- dispatchReplyFromConfigMock,
26
- finalizeInboundContextMock,
27
- resolveAgentRouteMock,
28
- resolveBoundConversationMock,
29
- withReplyDispatcherMock,
30
- } = getFeishuLifecycleTestMocks();
31
-
32
- let handlersByAccount = new Map<string, Record<string, (data: unknown) => Promise<void>>>();
33
- let runtimesByAccount = new Map<string, RuntimeEnv>();
34
- const originalStateDir = process.env.KLAW_STATE_DIR;
35
-
36
- function createLifecycleConfig(): ClawdbotConfig {
37
- return {
38
- broadcast: {
39
- oc_broadcast_group: ["susan", "main"],
40
- },
41
- agents: {
42
- list: [{ id: "main" }, { id: "susan" }],
43
- },
44
- channels: {
45
- feishu: {
46
- enabled: true,
47
- groupPolicy: "open",
48
- requireMention: false,
49
- resolveSenderNames: false,
50
- accounts: {
51
- "account-A": {
52
- enabled: true,
53
- appId: "cli_a",
54
- appSecret: "secret_a", // pragma: allowlist secret
55
- connectionMode: "websocket",
56
- groupPolicy: "open",
57
- requireMention: false,
58
- resolveSenderNames: false,
59
- groups: {
60
- oc_broadcast_group: {
61
- requireMention: false,
62
- },
63
- },
64
- },
65
- "account-B": {
66
- enabled: true,
67
- appId: "cli_b",
68
- appSecret: "secret_b", // pragma: allowlist secret
69
- connectionMode: "websocket",
70
- groupPolicy: "open",
71
- requireMention: false,
72
- resolveSenderNames: false,
73
- groups: {
74
- oc_broadcast_group: {
75
- requireMention: false,
76
- },
77
- },
78
- },
79
- },
80
- },
81
- },
82
- messages: {
83
- inbound: {
84
- debounceMs: 0,
85
- byChannel: {
86
- feishu: 0,
87
- },
88
- },
89
- },
90
- } as ClawdbotConfig;
91
- }
92
-
93
- function createLifecycleAccount(accountId: "account-A" | "account-B"): ResolvedFeishuAccount {
94
- const config: FeishuConfig = FeishuConfigSchema.parse({
95
- enabled: true,
96
- connectionMode: "websocket",
97
- groupPolicy: "open",
98
- requireMention: false,
99
- resolveSenderNames: false,
100
- groups: {
101
- oc_broadcast_group: {
102
- requireMention: false,
103
- },
104
- },
105
- });
106
- return {
107
- accountId,
108
- selectionSource: "explicit",
109
- enabled: true,
110
- configured: true,
111
- appId: accountId === "account-A" ? "cli_a" : "cli_b",
112
- appSecret: accountId === "account-A" ? "secret_a" : "secret_b", // pragma: allowlist secret
113
- domain: "feishu",
114
- config,
115
- };
116
- }
117
-
118
- async function setupLifecycleMonitor(accountId: "account-A" | "account-B") {
119
- const runtime = createNonExitingRuntimeEnv();
120
- runtimesByAccount.set(accountId, runtime);
121
- return setupFeishuLifecycleHandler({
122
- createEventDispatcherMock,
123
- onRegister: (registered) => {
124
- handlersByAccount.set(accountId, registered);
125
- },
126
- runtime,
127
- cfg: createLifecycleConfig(),
128
- account: createLifecycleAccount(accountId),
129
- handlerKey: "im.message.receive_v1",
130
- missingHandlerMessage: `missing im.message.receive_v1 handler for ${accountId}`,
131
- once: true,
132
- });
133
- }
134
-
135
- describe("Feishu broadcast reply-once lifecycle", () => {
136
- beforeEach(() => {
137
- vi.useRealTimers();
138
- resetFeishuLifecycleTestMocks();
139
- handlersByAccount = new Map();
140
- runtimesByAccount = new Map();
141
- setFeishuLifecycleStateDir("klaw-feishu-broadcast");
142
-
143
- createFeishuReplyDispatcherMock.mockReturnValue(createFeishuLifecycleReplyDispatcher());
144
-
145
- resolveBoundConversationMock.mockReturnValue(null);
146
- resolveAgentRouteMock.mockReturnValue({
147
- agentId: "main",
148
- channel: "feishu",
149
- accountId: "account-A",
150
- sessionKey: "agent:main:feishu:group:oc_broadcast_group",
151
- mainSessionKey: "agent:main:main",
152
- matchedBy: "default",
153
- });
154
-
155
- mockFeishuReplyOnceDispatch({
156
- dispatchReplyFromConfigMock,
157
- replyText: "broadcast reply once",
158
- shouldSendFinalReply: (ctx) =>
159
- typeof (ctx as { SessionKey?: string } | undefined)?.SessionKey === "string" &&
160
- (ctx as { SessionKey: string }).SessionKey.includes("agent:main:"),
161
- });
162
-
163
- withReplyDispatcherMock.mockImplementation(async ({ run }) => await run());
164
-
165
- installFeishuLifecycleReplyRuntime({
166
- resolveAgentRouteMock,
167
- finalizeInboundContextMock,
168
- dispatchReplyFromConfigMock,
169
- withReplyDispatcherMock,
170
- storePath: "/tmp/feishu-broadcast-sessions.json",
171
- });
172
- });
173
-
174
- afterEach(() => {
175
- vi.useRealTimers();
176
- restoreFeishuLifecycleStateDir(originalStateDir);
177
- });
178
-
179
- it("uses one active reply path when the same broadcast event reaches two accounts", async () => {
180
- const onMessageA = await setupLifecycleMonitor("account-A");
181
- const onMessageB = await setupLifecycleMonitor("account-B");
182
- const event = createFeishuTextMessageEvent({
183
- messageId: "om_broadcast_once",
184
- chatId: "oc_broadcast_group",
185
- text: "hello broadcast",
186
- });
187
-
188
- await runFeishuLifecycleSequence(
189
- [() => onMessageA(event), () => onMessageB(event)],
190
- [
191
- () => {
192
- expect(dispatchReplyFromConfigMock.mock.calls.length).toBeGreaterThan(0);
193
- },
194
- () => {
195
- expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
196
- expect(createFeishuReplyDispatcherMock).toHaveBeenCalledTimes(1);
197
- },
198
- ],
199
- );
200
-
201
- expect(runtimesByAccount.get("account-A")?.error).not.toHaveBeenCalled();
202
- expect(runtimesByAccount.get("account-B")?.error).not.toHaveBeenCalled();
203
-
204
- expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
205
- expect(createFeishuReplyDispatcherMock).toHaveBeenCalledTimes(1);
206
- expect(createFeishuReplyDispatcherMock).toHaveBeenCalledWith(
207
- expect.objectContaining({
208
- accountId: "account-a",
209
- chatId: "oc_broadcast_group",
210
- replyToMessageId: "om_broadcast_once",
211
- }),
212
- );
213
-
214
- const sessionKeys = finalizeInboundContextMock.mock.calls.map(
215
- (call) => (call[0] as { SessionKey?: string }).SessionKey,
216
- );
217
- expect(sessionKeys).toContain("agent:main:feishu:group:oc_broadcast_group");
218
- expect(sessionKeys).toContain("agent:susan:feishu:group:oc_broadcast_group");
219
-
220
- const activeDispatcher = createFeishuReplyDispatcherMock.mock.results[0]?.value.dispatcher as {
221
- sendFinalReply: ReturnType<typeof vi.fn>;
222
- };
223
- expect(activeDispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
224
- });
225
-
226
- it("does not duplicate delivery after a post-send failure on the first account", async () => {
227
- const onMessageA = await setupLifecycleMonitor("account-A");
228
- const onMessageB = await setupLifecycleMonitor("account-B");
229
- const event = createFeishuTextMessageEvent({
230
- messageId: "om_broadcast_retry",
231
- chatId: "oc_broadcast_group",
232
- text: "hello broadcast",
233
- });
234
-
235
- dispatchReplyFromConfigMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
236
- if (typeof ctx?.SessionKey === "string" && ctx.SessionKey.includes("agent:susan:")) {
237
- return { queuedFinal: false, counts: { final: 0 } };
238
- }
239
- await dispatcher.sendFinalReply({ text: "broadcast reply once" });
240
- throw new Error("post-send failure");
241
- });
242
-
243
- await runFeishuLifecycleSequence(
244
- [() => onMessageA(event), () => onMessageB(event)],
245
- [
246
- () => {
247
- expect(dispatchReplyFromConfigMock.mock.calls.length).toBeGreaterThan(0);
248
- },
249
- () => {
250
- expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
251
- },
252
- ],
253
- );
254
-
255
- expect(runtimesByAccount.get("account-A")?.error).not.toHaveBeenCalled();
256
- expect(runtimesByAccount.get("account-B")?.error).not.toHaveBeenCalled();
257
- expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
258
-
259
- const activeDispatcher = createFeishuReplyDispatcherMock.mock.results[0]?.value.dispatcher as {
260
- sendFinalReply: ReturnType<typeof vi.fn>;
261
- };
262
- expect(activeDispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
263
- });
264
- });