Skip to content

Commit 383d5ac

Browse files
committed
fix(test): split feishu bot helpers
1 parent 2db10fb commit 383d5ac

File tree

2 files changed

+94
-86
lines changed

2 files changed

+94
-86
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { ClawdbotConfig } from "../runtime-api.js";
3+
import {
4+
buildBroadcastSessionKey,
5+
buildFeishuAgentBody,
6+
resolveBroadcastAgents,
7+
toMessageResourceType,
8+
} from "./bot.js";
9+
10+
describe("buildFeishuAgentBody", () => {
11+
it("builds message id, speaker, quoted content, mentions, and permission notice in order", () => {
12+
const body = buildFeishuAgentBody({
13+
ctx: {
14+
content: "hello world",
15+
senderName: "Sender Name",
16+
senderOpenId: "ou-sender",
17+
messageId: "msg-42",
18+
mentionTargets: [{ openId: "ou-target", name: "Target User", key: "@_user_1" }],
19+
},
20+
quotedContent: "previous message",
21+
permissionErrorForAgent: {
22+
code: 99991672,
23+
message: "permission denied",
24+
grantUrl: "https://open.feishu.cn/app/cli_test",
25+
},
26+
});
27+
28+
expect(body).toBe(
29+
'[message_id: msg-42]\nSender Name: [Replying to: "previous message"]\n\nhello world\n\n[System: Your reply will automatically @mention: Target User. Do not write @xxx yourself.]\n\n[System: The bot encountered a Feishu API permission error. Please inform the user about this issue and provide the permission grant URL for the admin to authorize. Permission grant URL: https://open.feishu.cn/app/cli_test]',
30+
);
31+
});
32+
});
33+
34+
describe("toMessageResourceType", () => {
35+
it("maps image to image", () => {
36+
expect(toMessageResourceType("image")).toBe("image");
37+
});
38+
39+
it("maps audio to file", () => {
40+
expect(toMessageResourceType("audio")).toBe("file");
41+
});
42+
43+
it("maps video/file/sticker to file", () => {
44+
expect(toMessageResourceType("video")).toBe("file");
45+
expect(toMessageResourceType("file")).toBe("file");
46+
expect(toMessageResourceType("sticker")).toBe("file");
47+
});
48+
});
49+
50+
describe("resolveBroadcastAgents", () => {
51+
it("returns agent list when broadcast config has the peerId", () => {
52+
const cfg = { broadcast: { oc_group123: ["susan", "main"] } } as unknown as ClawdbotConfig;
53+
expect(resolveBroadcastAgents(cfg, "oc_group123")).toEqual(["susan", "main"]);
54+
});
55+
56+
it("returns null when no broadcast config", () => {
57+
const cfg = {} as ClawdbotConfig;
58+
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
59+
});
60+
61+
it("returns null when peerId not in broadcast", () => {
62+
const cfg = { broadcast: { oc_other: ["susan"] } } as unknown as ClawdbotConfig;
63+
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
64+
});
65+
66+
it("returns null when agent list is empty", () => {
67+
const cfg = { broadcast: { oc_group123: [] } } as unknown as ClawdbotConfig;
68+
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
69+
});
70+
});
71+
72+
describe("buildBroadcastSessionKey", () => {
73+
it("replaces agent ID prefix in session key", () => {
74+
expect(buildBroadcastSessionKey("agent:main:feishu:group:oc_group123", "main", "susan")).toBe(
75+
"agent:susan:feishu:group:oc_group123",
76+
);
77+
});
78+
79+
it("handles compound peer IDs", () => {
80+
expect(
81+
buildBroadcastSessionKey(
82+
"agent:main:feishu:group:oc_group123:sender:ou_user1",
83+
"main",
84+
"susan",
85+
),
86+
).toBe("agent:susan:feishu:group:oc_group123:sender:ou_user1");
87+
});
88+
89+
it("returns base key unchanged when prefix does not match", () => {
90+
expect(buildBroadcastSessionKey("custom:key:format", "main", "susan")).toBe(
91+
"custom:key:format",
92+
);
93+
});
94+
});

extensions/feishu/src/bot.test.ts

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,6 @@ async function dispatchMessage(params: { cfg: ClawdbotConfig; event: FeishuMessa
110110
return runtime;
111111
}
112112

113-
describe("buildFeishuAgentBody", () => {
114-
it("builds message id, speaker, quoted content, mentions, and permission notice in order", () => {
115-
const body = buildFeishuAgentBody({
116-
ctx: {
117-
content: "hello world",
118-
senderName: "Sender Name",
119-
senderOpenId: "ou-sender",
120-
messageId: "msg-42",
121-
mentionTargets: [{ openId: "ou-target", name: "Target User", key: "@_user_1" }],
122-
},
123-
quotedContent: "previous message",
124-
permissionErrorForAgent: {
125-
code: 99991672,
126-
message: "permission denied",
127-
grantUrl: "https://open.feishu.cn/app/cli_test",
128-
},
129-
});
130-
131-
expect(body).toBe(
132-
'[message_id: msg-42]\nSender Name: [Replying to: "previous message"]\n\nhello world\n\n[System: Your reply will automatically @mention: Target User. Do not write @xxx yourself.]\n\n[System: The bot encountered a Feishu API permission error. Please inform the user about this issue and provide the permission grant URL for the admin to authorize. Permission grant URL: https://open.feishu.cn/app/cli_test]',
133-
);
134-
});
135-
});
136-
137113
describe("handleFeishuMessage ACP routing", () => {
138114
beforeEach(() => {
139115
vi.clearAllMocks();
@@ -2344,68 +2320,6 @@ describe("handleFeishuMessage command authorization", () => {
23442320
});
23452321
});
23462322

2347-
describe("toMessageResourceType", () => {
2348-
it("maps image to image", () => {
2349-
expect(toMessageResourceType("image")).toBe("image");
2350-
});
2351-
2352-
it("maps audio to file", () => {
2353-
expect(toMessageResourceType("audio")).toBe("file");
2354-
});
2355-
2356-
it("maps video/file/sticker to file", () => {
2357-
expect(toMessageResourceType("video")).toBe("file");
2358-
expect(toMessageResourceType("file")).toBe("file");
2359-
expect(toMessageResourceType("sticker")).toBe("file");
2360-
});
2361-
});
2362-
2363-
describe("resolveBroadcastAgents", () => {
2364-
it("returns agent list when broadcast config has the peerId", () => {
2365-
const cfg = { broadcast: { oc_group123: ["susan", "main"] } } as unknown as ClawdbotConfig;
2366-
expect(resolveBroadcastAgents(cfg, "oc_group123")).toEqual(["susan", "main"]);
2367-
});
2368-
2369-
it("returns null when no broadcast config", () => {
2370-
const cfg = {} as ClawdbotConfig;
2371-
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
2372-
});
2373-
2374-
it("returns null when peerId not in broadcast", () => {
2375-
const cfg = { broadcast: { oc_other: ["susan"] } } as unknown as ClawdbotConfig;
2376-
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
2377-
});
2378-
2379-
it("returns null when agent list is empty", () => {
2380-
const cfg = { broadcast: { oc_group123: [] } } as unknown as ClawdbotConfig;
2381-
expect(resolveBroadcastAgents(cfg, "oc_group123")).toBeNull();
2382-
});
2383-
});
2384-
2385-
describe("buildBroadcastSessionKey", () => {
2386-
it("replaces agent ID prefix in session key", () => {
2387-
expect(buildBroadcastSessionKey("agent:main:feishu:group:oc_group123", "main", "susan")).toBe(
2388-
"agent:susan:feishu:group:oc_group123",
2389-
);
2390-
});
2391-
2392-
it("handles compound peer IDs", () => {
2393-
expect(
2394-
buildBroadcastSessionKey(
2395-
"agent:main:feishu:group:oc_group123:sender:ou_user1",
2396-
"main",
2397-
"susan",
2398-
),
2399-
).toBe("agent:susan:feishu:group:oc_group123:sender:ou_user1");
2400-
});
2401-
2402-
it("returns base key unchanged when prefix does not match", () => {
2403-
expect(buildBroadcastSessionKey("custom:key:format", "main", "susan")).toBe(
2404-
"custom:key:format",
2405-
);
2406-
});
2407-
});
2408-
24092323
describe("broadcast dispatch", () => {
24102324
const mockFinalizeInboundContext = vi.fn((ctx: unknown) => ctx);
24112325
const mockDispatchReplyFromConfig = vi

0 commit comments

Comments
 (0)