返回 CodeWhale
lib.test.mjs
根目录 / integrations / feishu-bridge / test / lib.test.mjs
1 import test from "node:test";
2 import assert from "node:assert/strict";
3
4 import {
5 activeTurnBlock,
6 commandAction,
7 isAllowed,
8 pairingRefusalText,
9 parseApprovalDecisionArgs,
10 parseBool,
11 parseEnvText,
12 parseCommand,
13 parseList,
14 parseTextContent,
15 preservedChatStateFields,
16 splitMessage,
17 stripGroupPrefix,
18 helpText,
19 validateBridgeConfig
20 } from "../src/lib.mjs";
21
22 test("parseList trims empty values", () => {
23 assert.deepEqual(parseList(" oc_1, ou_2 ,, "), ["oc_1", "ou_2"]);
24 });
25
26 test("parseBool accepts common truthy values", () => {
27 assert.equal(parseBool("yes"), true);
28 assert.equal(parseBool("0", true), false);
29 assert.equal(parseBool(undefined, true), true);
30 });
31
32 test("parseTextContent reads Feishu JSON text content", () => {
33 assert.equal(parseTextContent(JSON.stringify({ text: "hello" })), "hello");
34 });
35
36 test("parseEnvText handles comments, export, and quoted values", () => {
37 assert.deepEqual(
38 parseEnvText(`
39 # ignored
40 export FEISHU_DOMAIN="lark"
41 DEEPSEEK_WORKSPACE='/opt/whalebro'
42 `),
43 {
44 FEISHU_DOMAIN: "lark",
45 DEEPSEEK_WORKSPACE: "/opt/whalebro"
46 }
47 );
48 });
49
50 test("stripGroupPrefix requires prefix in group chats", () => {
51 assert.deepEqual(
52 stripGroupPrefix("/ds inspect this", {
53 chatType: "group",
54 requirePrefix: true,
55 prefix: "/ds"
56 }),
57 { accepted: true, text: "inspect this" }
58 );
59 assert.equal(
60 stripGroupPrefix("inspect this", {
61 chatType: "group",
62 requirePrefix: true,
63 prefix: "/ds"
64 }).accepted,
65 false
66 );
67 });
68
69 test("stripGroupPrefix accepts DM text without group prefix", () => {
70 assert.deepEqual(
71 stripGroupPrefix("inspect this", {
72 chatType: "p2p",
73 requirePrefix: true,
74 prefix: "/ds"
75 }),
76 { accepted: true, text: "inspect this" }
77 );
78 });
79
80 test("parseCommand distinguishes prompts and slash commands", () => {
81 assert.deepEqual(parseCommand("hello"), { name: "prompt", args: "hello" });
82 assert.deepEqual(parseCommand("/allow abc remember"), {
83 name: "allow",
84 args: "abc remember"
85 });
86 });
87
88 test("commandAction maps bridge commands and falls back to prompts", () => {
89 assert.deepEqual(commandAction(parseCommand("/status")), { kind: "status" });
90 assert.deepEqual(commandAction(parseCommand("/resume thread-1")), {
91 kind: "resume",
92 threadId: "thread-1"
93 });
94 assert.deepEqual(commandAction(parseCommand("/model deepseek-v4-pro")), {
95 kind: "set_model",
96 modelName: "deepseek-v4-pro"
97 });
98 assert.deepEqual(commandAction(parseCommand("/unknown value")), {
99 kind: "prompt",
100 prompt: "/unknown value"
101 });
102 });
103
104 test("helpText documents per-chat model switching", () => {
105 assert.match(helpText(), /\/model <name\|default>/);
106 });
107
108 test("preservedChatStateFields carries model across state replacement", () => {
109 assert.deepEqual(
110 preservedChatStateFields({
111 threadId: "old-thread",
112 model: "deepseek-v4-flash",
113 replyToMessageId: "om_123",
114 activeTurnId: "turn-1"
115 }),
116 {
117 model: "deepseek-v4-flash",
118 replyToMessageId: "om_123"
119 }
120 );
121 assert.deepEqual(preservedChatStateFields({ model: null }), { model: null });
122 });
123
124 test("parseApprovalDecisionArgs extracts remember flag", () => {
125 assert.deepEqual(parseApprovalDecisionArgs("ap_123 remember"), {
126 approvalId: "ap_123",
127 remember: true
128 });
129 assert.deepEqual(parseApprovalDecisionArgs(""), { approvalId: "", remember: false });
130 });
131
132 test("isAllowed checks chat and user identifiers", () => {
133 assert.equal(
134 isAllowed({ chatId: "oc_x", openId: "ou_y" }, ["ou_y"], false),
135 true
136 );
137 assert.equal(isAllowed({ chatId: "oc_x" }, [], false), false);
138 assert.equal(isAllowed({ chatId: "oc_x" }, [], true), true);
139 });
140
141 test("pairingRefusalText includes allowlist identifiers", () => {
142 const body = pairingRefusalText({
143 chatId: "oc_chat",
144 openId: "ou_user",
145 unionId: "on_union",
146 userId: "u_user"
147 });
148 assert.match(body, /chat_id=oc_chat/);
149 assert.match(body, /open_id=ou_user/);
150 assert.match(body, /union_id=on_union/);
151 assert.match(body, /user_id=u_user/);
152 });
153
154 test("activeTurnBlock reports active queued or in-progress turn", () => {
155 assert.equal(activeTurnBlock({ turns: [{ id: "done", status: "completed" }] }), null);
156 assert.deepEqual(
157 activeTurnBlock({
158 turns: [
159 { id: "old", status: "completed" },
160 { id: "turn-2", status: "in_progress" }
161 ]
162 }),
163 {
164 turnId: "turn-2",
165 message: "Thread already has active turn turn-2. Wait for it to finish or send /interrupt."
166 }
167 );
168 });
169
170 test("splitMessage chunks long text", () => {
171 assert.deepEqual(splitMessage("abcdef", 2), ["ab", "cd", "ef"]);
172 });
173
174 test("splitMessage does not split surrogate pairs", () => {
175 assert.deepEqual(splitMessage("a🧪b", 2), ["a🧪", "b"]);
176 });
177
178 test("validateBridgeConfig accepts locked-down whalebro DM config", () => {
179 const result = validateBridgeConfig(
180 {
181 FEISHU_APP_ID: "cli_valid",
182 FEISHU_APP_SECRET: "secret",
183 FEISHU_DOMAIN: "lark",
184 DEEPSEEK_RUNTIME_URL: "http://127.0.0.1:7878",
185 DEEPSEEK_RUNTIME_TOKEN: "token-a",
186 DEEPSEEK_WORKSPACE: "/opt/whalebro",
187 DEEPSEEK_CHAT_ALLOWLIST: "oc_allowed",
188 DEEPSEEK_ALLOW_UNLISTED: "false",
189 FEISHU_THREAD_MAP_PATH: "/var/lib/codewhale-feishu-bridge/thread-map.json",
190 FEISHU_ALLOW_GROUPS: "false",
191 FEISHU_REQUIRE_PREFIX_IN_GROUP: "true"
192 },
193 {
194 workspaceRoot: "/opt/whalebro",
195 runtimeEnv: {
196 DEEPSEEK_RUNTIME_TOKEN: "token-a",
197 DEEPSEEK_API_KEY: "sk-valid",
198 DEEPSEEK_RUNTIME_PORT: "7878"
199 }
200 }
201 );
202 assert.equal(result.ok, true);
203 assert.equal(result.errors.length, 0);
204 });
205
206 test("validateBridgeConfig rejects unsafe group pairing and token mismatch", () => {
207 const result = validateBridgeConfig(
208 {
209 FEISHU_APP_ID: "cli_valid",
210 FEISHU_APP_SECRET: "secret",
211 FEISHU_DOMAIN: "feishu",
212 DEEPSEEK_RUNTIME_URL: "http://127.0.0.1:7878",
213 DEEPSEEK_RUNTIME_TOKEN: "bridge-token",
214 DEEPSEEK_WORKSPACE: "/opt/whalebro",
215 DEEPSEEK_ALLOW_UNLISTED: "true",
216 FEISHU_THREAD_MAP_PATH: "/var/lib/codewhale-feishu-bridge/thread-map.json",
217 FEISHU_ALLOW_GROUPS: "true",
218 FEISHU_REQUIRE_PREFIX_IN_GROUP: "false"
219 },
220 {
221 workspaceRoot: "/opt/whalebro",
222 runtimeEnv: {
223 DEEPSEEK_RUNTIME_TOKEN: "runtime-token",
224 DEEPSEEK_API_KEY: "replace-with-deepseek-platform-key"
225 }
226 }
227 );
228 assert.equal(result.ok, false);
229 assert.match(
230 result.errors.map((item) => item.code).join(","),
231 /open_group_control/
232 );
233 assert.match(result.errors.map((item) => item.code).join(","), /token_mismatch/);
234 assert.match(result.warnings.map((item) => item.code).join(","), /group_without_prefix/);
235 });
236
236 lines Plain Text